Skip to content

Commit

Permalink
Ensure convert_to_sql matcher returns unprepared statements (no "?"…
Browse files Browse the repository at this point in the history
… placeholder)
  • Loading branch information
Samuel Lebeau committed Dec 11, 2014
1 parent 94e84e7 commit 6b3214b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion spec/matchers/convert_to_sql.rb
Expand Up @@ -19,7 +19,22 @@
end

def convert_to_sql(actual)
normalize_sql(actual.to_sql)
normalize_sql to_unprepared_sql(actual)
end

# In ActiveRecord 4.0 and 4.1, when the adapter supports prepared statements, calling `to_sql` on
# a `has_many` association collection proxy returns a statement with "?" instead of the actual foreign key:
#
# SELECT "messages".* FROM "messages" WHERE "messages"."author_id" = ?
#
# Here we ensure we always get the unprepared statement.
def to_unprepared_sql(actual)
connection = actual.respond_to?(:connection) && actual.connection
if connection && connection.respond_to?(:unprepared_statement)
connection.unprepared_statement { actual.to_sql }
else
actual.to_sql
end
end

# Arel < 6 seems to inserts an additionnal (harmless) space before the WHERE clause.
Expand Down

0 comments on commit 6b3214b

Please sign in to comment.