<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -407,10 +407,9 @@ module ActiveRecord
       end
 
       def current_version
-        version = Base.connection.select_values(
-          &quot;SELECT version FROM #{schema_migrations_table_name}&quot;
-        ).map(&amp;:to_i).max rescue nil
-        version || 0
+        Base.connection.select_value(
+          &quot;SELECT MAX(CAST(version AS integer)) FROM #{schema_migrations_table_name}&quot;
+        ).to_i rescue 0
       end
 
       def proper_table_name(name)
@@ -426,7 +425,7 @@ module ActiveRecord
     end
 
     def current_version
-      self.class.current_version
+      migrated.last || 0
     end
     
     def current_migration
@@ -518,16 +517,19 @@ module ActiveRecord
 
     def migrated
       sm_table = self.class.schema_migrations_table_name
-      Base.connection.select_values(&quot;SELECT version FROM #{sm_table}&quot;).map(&amp;:to_i).sort
+      @migrated_versions ||= Base.connection.select_values(&quot;SELECT version FROM #{sm_table}&quot;).map(&amp;: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(&quot;DELETE FROM #{sm_table} WHERE version = '#{version}'&quot;)
         else
+          @migrated_versions.push(version.to_i).sort!
           Base.connection.insert(&quot;INSERT INTO #{sm_table} (version) VALUES ('#{version}')&quot;)
         end
       end</diff>
      <filename>activerecord/lib/active_record/migration.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>4bcd64c9e93af2f13dc7309cd76bb764e4d7d23d</id>
    </parent>
  </parents>
  <author>
    <name>Tarmo T&#228;nav</name>
    <email>tarmo@itech.ee</email>
  </author>
  <url>http://github.com/rails/rails/commit/3d2ac918b987ef0cff34f6a7fdd20926b7a9e5d9</url>
  <id>3d2ac918b987ef0cff34f6a7fdd20926b7a9e5d9</id>
  <committed-date>2008-08-25T22:03:47-07:00</committed-date>
  <authored-date>2008-08-25T18:55:57-07:00</authored-date>
  <message>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 &lt;jeremy@bitsweat.net&gt;</message>
  <tree>c59dd78994dd9892c0707947d4e02d6c79b2556d</tree>
  <committer>
    <name>Jeremy Kemper</name>
    <email>jeremy@bitsweat.net</email>
  </committer>
</commit>
