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

Cache migrated versions list in Migrator and use it to fetch the latest migrated version name [#845 state:resolved]

Also optimized Migrator#current_version class method to fetch
only the latest version number and not all of them.

With this change no matter how many migrations there are the
schema_migrations table is only SELECTed from once.

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Tarmo Tänav 2008-08-26 04:55:57 +03:00 committed by Jeremy Kemper
parent 4bcd64c9e9
commit 3d2ac918b9

View file

@ -407,10 +407,9 @@ module ActiveRecord
end end
def current_version def current_version
version = Base.connection.select_values( Base.connection.select_value(
"SELECT version FROM #{schema_migrations_table_name}" "SELECT MAX(CAST(version AS integer)) FROM #{schema_migrations_table_name}"
).map(&:to_i).max rescue nil ).to_i rescue 0
version || 0
end end
def proper_table_name(name) def proper_table_name(name)
@ -426,7 +425,7 @@ module ActiveRecord
end end
def current_version def current_version
self.class.current_version migrated.last || 0
end end
def current_migration def current_migration
@ -518,16 +517,19 @@ module ActiveRecord
def migrated def migrated
sm_table = self.class.schema_migrations_table_name sm_table = self.class.schema_migrations_table_name
Base.connection.select_values("SELECT version FROM #{sm_table}").map(&:to_i).sort @migrated_versions ||= Base.connection.select_values("SELECT version FROM #{sm_table}").map(&:to_i).sort
end end
private private
def record_version_state_after_migrating(version) def record_version_state_after_migrating(version)
sm_table = self.class.schema_migrations_table_name sm_table = self.class.schema_migrations_table_name
@migrated_versions ||= []
if down? if down?
@migrated_versions.delete(version.to_i)
Base.connection.update("DELETE FROM #{sm_table} WHERE version = '#{version}'") Base.connection.update("DELETE FROM #{sm_table} WHERE version = '#{version}'")
else else
@migrated_versions.push(version.to_i).sort!
Base.connection.insert("INSERT INTO #{sm_table} (version) VALUES ('#{version}')") Base.connection.insert("INSERT INTO #{sm_table} (version) VALUES ('#{version}')")
end end
end end