Permalink
Browse files

Cache migrated versions list in Migrator and use it to fetch the late…

…st 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>
  • Loading branch information...
1 parent 4bcd64c commit 3d2ac918b987ef0cff34f6a7fdd20926b7a9e5d9 @tarmo tarmo committed with jeremy Aug 26, 2008
Showing with 8 additions and 6 deletions.
  1. +8 −6 activerecord/lib/active_record/migration.rb
@@ -407,10 +407,9 @@ def schema_migrations_table_name
end
def current_version
- version = Base.connection.select_values(
- "SELECT version FROM #{schema_migrations_table_name}"
- ).map(&:to_i).max rescue nil
- version || 0
+ Base.connection.select_value(
+ "SELECT MAX(CAST(version AS integer)) FROM #{schema_migrations_table_name}"
+ ).to_i rescue 0
end
def proper_table_name(name)
@@ -426,7 +425,7 @@ def initialize(direction, migrations_path, target_version = nil)
end
def current_version
- self.class.current_version
+ migrated.last || 0
end
def current_migration
@@ -518,16 +517,19 @@ def pending_migrations
def migrated
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
private
def record_version_state_after_migrating(version)
sm_table = self.class.schema_migrations_table_name
+ @migrated_versions ||= []
if down?
+ @migrated_versions.delete(version.to_i)
Base.connection.update("DELETE FROM #{sm_table} WHERE version = '#{version}'")
else
+ @migrated_versions.push(version.to_i).sort!
Base.connection.insert("INSERT INTO #{sm_table} (version) VALUES ('#{version}')")
end
end

0 comments on commit 3d2ac91

Please sign in to comment.