diff options
author | Jade Rubick <jade@newrelic.com> | 2011-12-05 10:32:56 -0800 |
---|---|---|
committer | Jade Rubick <jade@newrelic.com> | 2011-12-05 11:20:10 -0800 |
commit | 73a331c2acfce971aa2dda8e72af5edc6867e344 (patch) | |
tree | dfa0b68f06f76da054fc2f410282308769b62c7c /activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | |
parent | f6cc4fd7a251490df717a85be3be335a70edaec5 (diff) | |
download | rails-73a331c2acfce971aa2dda8e72af5edc6867e344.tar.gz rails-73a331c2acfce971aa2dda8e72af5edc6867e344.tar.bz2 rails-73a331c2acfce971aa2dda8e72af5edc6867e344.zip |
Speed up table_exists? for databases with a large number of tables
At New Relic, we have hundreds of thousands of tables, and our migrations took 30 minutes without this similar patch. This cuts it down to a more reasonable amount of time.
The rescue false part is ugly, but necessary as far as I can tell. I don't know of a cross-database statement you can make that will work without trapping errors.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index ce4c5a1383..7ff3b755fd 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -23,7 +23,12 @@ module ActiveRecord # === Example # table_exists?(:developers) def table_exists?(table_name) - tables.include?(table_name.to_s) + begin + select_value("SELECT 1 FROM #{table_name.to_s} where 1=0") + true + rescue + false + end end # Returns an array of indexes for the given table. |