1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #39712 from arthurschreiber/arthur/use-subquery-for-information-schema

Use a subquery when filtering `information_schema.tables` by `table_name`.
This commit is contained in:
Eileen M. Uchitelle 2020-06-25 09:59:07 -04:00 committed by GitHub
commit 0f6a4f5895
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -203,10 +203,14 @@ module ActiveRecord
def data_source_sql(name = nil, type: nil)
scope = quoted_scope(name, type: type)
sql = +"SELECT table_name FROM information_schema.tables"
sql << " WHERE table_schema = #{scope[:schema]}"
sql << " AND table_name = #{scope[:name]}" if scope[:name]
sql << " AND table_type = #{scope[:type]}" if scope[:type]
sql = +"SELECT table_name FROM (SELECT * FROM information_schema.tables "
sql << " WHERE table_schema = #{scope[:schema]}) _subquery"
if scope[:type] || scope[:name]
conditions = []
conditions << "_subquery.table_type = #{scope[:type]}" if scope[:type]
conditions << "_subquery.table_name = #{scope[:name]}" if scope[:name]
sql << " WHERE #{conditions.join(" AND ")}"
end
sql
end