Merge pull request #501 from buehmann/fix/quote-table-stats-query

Properly quote table names in table_stats_query
This commit is contained in:
Mauro Otonelli 2018-02-09 19:21:55 -03:00 committed by GitHub
commit b393fa81fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 7 deletions

View File

@ -66,14 +66,16 @@ module DatabaseCleaner::ActiveRecord
if @cache_tables && !@table_stats_query.nil?
return @table_stats_query
else
@table_stats_query = connection.select_values(<<-SQL).join(' UNION ')
SELECT CONCAT('SELECT \"', table_name, '\" AS table_name, COUNT(*) AS exact_row_count FROM ', table_name)
FROM
INFORMATION_SCHEMA.TABLES
WHERE
table_schema = '#{db_name}'
AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
tables = connection.select_values(<<-SQL)
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '#{db_name}'
AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
SQL
queries = tables.map do |table|
"SELECT #{connection.quote(table)} AS table_name, COUNT(*) AS exact_row_count FROM #{connection.quote_table_name(table)}"
end
@table_stats_query = queries.join(' UNION ')
end
end