The error_sql extension adds a DatabaseError#sql method that you can use to get the sql that caused the error to be raised.
begin DB.run "Invalid SQL" rescue => e puts e.sql # "Invalid SQL" end
On some databases, the error message contains part or all of the SQL used, but on other databases, none of the SQL used is displayed in the error message, so it can be difficult to track down what is causing the error without using a logger. This extension should hopefully make debugging easier on databases that have bad error messages.
This extension may not work correctly in the following cases:
log_connection_yield is not used when executing the query.
The underlying exception is frozen or reused.
The underlying exception doesn't correctly record instance variables set on it (seems to happen on JRuby when underlying exception objects are Java exceptions).
To load the extension into the database: