mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
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.
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user