提交 9fedd013 编写于 作者: R Ryuta Kamizono

Ensure associations doesn't table name collide with string joins

Currently we have no test for alias tracking with string joins. I've add
test case for that to catch a future regression.
上级 f4d1aa53
......@@ -19,14 +19,16 @@ def self.create(connection, initial_table, joins)
end
def self.initial_count_for(connection, name, table_joins)
# quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase
quoted_name = connection.quote_table_name(name).downcase
quoted_name = nil
counts = table_joins.map do |join|
if join.is_a?(Arel::Nodes::StringJoin)
# quoted_name should be case ignored as some database adapters (Oracle) return quoted name in uppercase
quoted_name ||= connection.quote_table_name(name)
# Table names + table aliases
join.left.downcase.scan(
/join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
join.left.scan(
/JOIN(?:\s+\w+)?\s+(?:\S+\s+)?(?:#{quoted_name}|#{name})\sON/i
).size
elsif join.respond_to? :left
join.left.table_name == name ? 1 : 0
......
......@@ -27,6 +27,11 @@ def test_construct_finder_sql_does_not_table_name_collide_on_duplicate_associati
end
end
def test_construct_finder_sql_does_not_table_name_collide_with_string_joins
sql = Person.joins(:agents).joins("JOIN people agents_people ON agents_people.primary_contact_id = people.id").to_sql
assert_match(/agents_people_2/i, sql)
end
def test_construct_finder_sql_ignores_empty_joins_hash
sql = Author.joins({}).to_sql
assert_no_match(/JOIN/i, sql)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册