<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -5,8 +5,7 @@ class PluginMigrationGenerator &lt; Rails::Generator::Base
   def initialize(runtime_args, runtime_options={})
     super
     @options = {:assigns =&gt; {}}
-    
-    ensure_plugin_schema_table_exists
+    ensure_schema_table_exists    
     get_plugins_to_migrate(runtime_args)
     
     if @plugins_to_migrate.empty?
@@ -25,10 +24,9 @@ class PluginMigrationGenerator &lt; Rails::Generator::Base
   end
   
   protected
-  
-    # Create the plugin schema table if it doesn't already exist. See
-    # Engines::RailsExtensions::Migrations#initialize_schema_migrations_table_with_engine_additions
-    def ensure_plugin_schema_table_exists
+
+    # Create the schema table if it doesn't already exist.
+    def ensure_schema_table_exists
       ActiveRecord::Base.connection.initialize_schema_migrations_table
     end
 
@@ -70,10 +68,11 @@ class PluginMigrationGenerator &lt; Rails::Generator::Base
     end
 
     # Construct a unique migration name based on the plugins involved and the
-    # versions they should reach after this migration is run.
+    # versions they should reach after this migration is run. The name constructed
+    # needs to be lowercase
     def build_migration_name
       @plugins_to_migrate.map do |plugin| 
         &quot;#{plugin.name}_to_version_#{@new_versions[plugin.name]}&quot; 
-      end.join(&quot;_and_&quot;)
+      end.join(&quot;_and_&quot;).downcase
     end  
 end
\ No newline at end of file</diff>
      <filename>generators/plugin_migration/plugin_migration_generator.rb</filename>
    </modified>
    <modified>
      <diff>@@ -111,11 +111,15 @@ module Engines
     # Returns the version number of the latest migration for this plugin. Returns
     # nil if this plugin has no migrations.
     def latest_migration
+      migrations.last
+    end
+    
+    # Returns the version numbers of all migrations for this plugin.
+    def migrations
       migrations = Dir[migration_directory+&quot;/*.rb&quot;]
-      return nil if migrations.empty?
-      migrations.map { |p| File.basename(p) }.sort.last.match(/0*(\d+)\_/)[1].to_i
+      migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
     end
-  
+    
     # Migrate this plugin to the given version. See Engines::Plugin::Migrator for more
     # information.   
     def migrate(version = nil)</diff>
      <filename>lib/engines/plugin.rb</filename>
    </modified>
    <modified>
      <diff>@@ -12,63 +12,30 @@ class Engines::Plugin::Migrator &lt; ActiveRecord::Migrator
   # We need to be able to set the 'current' engine being migrated.
   cattr_accessor :current_plugin
 
-  # Runs the migrations from a plugin, up (or down) to the version given
-  def self.migrate_plugin(plugin, version)
-    self.current_plugin = plugin
-    # There seems to be a bug in Rails' own migrations, where migrating
-    # to the existing version causes all migrations to be run where that
-    # migration number doesn't exist (i.e. zero). We could fix this by
-    # removing the line if the version hits zero...?
-    return if current_version(plugin) == version
-    migrate(plugin.migration_directory, version)
-  end
-  
-  # Returns the name of the table used to store schema information about
-  # installed plugins.
-  #
-  # See Engines.schema_info_table for more details.
-  def self.schema_info_table_name
-    proper_table_name Engines.schema_info_table
-  end
-
-  # Returns the current version of the given plugin
-  def self.current_version(plugin=current_plugin)
-    result = ActiveRecord::Base.connection.select_one(&lt;&lt;-ESQL
-      SELECT version FROM #{schema_info_table_name} 
-      WHERE plugin_name = '#{plugin.name}'
-    ESQL
-    )
-    if result
-      result[&quot;version&quot;].to_i
-    else
-      # There probably isn't an entry for this engine in the migration info table.
-      # We need to create that entry, and set the version to 0
-      ActiveRecord::Base.connection.execute(&lt;&lt;-ESQL
-        INSERT INTO #{schema_info_table_name} (version, plugin_name) 
-        VALUES (0,'#{plugin.name}')
-      ESQL
-      )      
-      0
+  class &lt;&lt; self
+    # Runs the migrations from a plugin, up (or down) to the version given
+    def migrate_plugin(plugin, version)
+      self.current_plugin = plugin
+      return if current_version(plugin) == version
+      migrate(plugin.migration_directory, version)
+    end
+    
+    def current_version(plugin=current_plugin)
+      # Delete migrations that don't match .. to_i will work because the number comes first
+      ::ActiveRecord::Base.connection.select_values(
+        &quot;SELECT version FROM #{schema_migrations_table_name}&quot;
+      ).delete_if{ |v| v.match(/-#{plugin.name}/) == nil }.map(&amp;:to_i).max || 0
     end
   end
-  
-  def migrated(plugin=current_plugin)
-    current = ActiveRecord::Base.connection.select_value(&lt;&lt;-ESQL
-      SELECT version FROM #{self.class.schema_info_table_name}
-      WHERE plugin_name = '#{plugin.name}'
-    ESQL
-    ).to_i
-    current ? (1..current).to_a : []
+       
+  def migrated
+    sm_table = self.class.schema_migrations_table_name
+    ::ActiveRecord::Base.connection.select_values(
+      &quot;SELECT version FROM #{sm_table}&quot;
+    ).delete_if{ |v| v.match(/-#{current_plugin.name}/) == nil }.map(&amp;:to_i).sort
   end
   
-  # Sets the version of the plugin in Engines::Plugin::Migrator.current_plugin to
-  # the given version.
   def record_version_state_after_migrating(version)
-    ActiveRecord::Base.connection.update(&lt;&lt;-ESQL
-      UPDATE #{self.class.schema_info_table_name} 
-      SET version = #{down? ? version.to_i - 1 : version.to_i} 
-      WHERE plugin_name = '#{self.current_plugin.name}'
-    ESQL
-    )
+    super(version.to_s + &quot;-&quot; + current_plugin.name)
   end
 end</diff>
      <filename>lib/engines/plugin/migrator.rb</filename>
    </modified>
    <modified>
      <diff>@@ -32,8 +32,8 @@ require &quot;engines/plugin/migrator&quot;
 #         |- lib/
 #         |- db/
 #             |-migrate/
-#                 |- 001_do_something.rb
-#                 |- 002_and_something_else.rb
+#                 |- 20081105123419_add_some_new_feature.rb
+#                 |- 20081107144959_and_something_else.rb
 #                 |- ...
 # 
 # When you install a plugin which contains migrations, you are undertaking a
@@ -44,8 +44,8 @@ require &quot;engines/plugin/migrator&quot;
 #
 # == An example
 #
-# For example, our current application is at version 14 (according to the
-# +schema_info+ table), when we decide that we want to add a tagging plugin. The
+# For example, our current application is at version 20081106164503 (according to the
+# +schema_migrations+ table), when we decide that we want to add a tagging plugin. The
 # tagging plugin chosen includes migrations to create the tables it requires
 # (say, _tags_ and _taggings_, for instance), along with the models and helpers
 # one might expect.
@@ -57,14 +57,14 @@ require &quot;engines/plugin/migrator&quot;
 #
 #   $ script/generate plugin_migration
 #         exists  db/migrate
-#         create  db/migrate/015_migrate_tagging_plugin_to_version_3.rb
+#         create  db/migrate/20081108120415_my_plugin_to_version_20081107144959.rb
 #
-# This migration will take our application to version 15, and contains the following, 
-# typical migration code:
+# This migration will take our application to version 20081108120415, and contains the 
+# following, typical migration code:
 # 
-#   class MigrateTaggingPluginToVersion3 &lt; ActiveRecord::Migration
+#   class TaggingToVersion20081107144959 &lt; ActiveRecord::Migration
 #     def self.up
-#       Engines.plugins[:tagging].migrate(3)
+#       Engines.plugins[:tagging].migrate(20081107144959)
 #     end
 #     def self.down
 #       Engines.plugins[:tagging].migrate(0)
@@ -72,8 +72,8 @@ require &quot;engines/plugin/migrator&quot;
 #   end
 #
 # When we migrate our application up, using &lt;tt&gt;rake db:migrate&lt;/tt&gt; as normal,
-# the plugin will be migrated up to its latest version (3 in this example). If we
-# ever decide to migrate the application back to the state it was in at version 14,
+# the plugin will be migrated up to its latest version (20081108120415 in this example). If we
+# ever decide to migrate the application back to the state it was in at version 20081106164503,
 # the plugin migrations will be taken back down to version 0 (which, typically,
 # would remove all tables the plugin migrations define).
 #
@@ -88,21 +88,21 @@ require &quot;engines/plugin/migrator&quot;
 #
 #   $ script/generate plugin_migration
 #        exists db/migrate
-#        create db/migrate/023_migrate_tagging_plugin_to_version_5.rb
+#        create db/migrate/20081210131437_tagging_to_version_20081201172034.rb
 #
 # The contents of this migration are:
 #
-#   class MigrateTaggingPluginToVersion3 &lt; ActiveRecord::Migration
+#   class TaggingToVersion20081108120415 &lt; ActiveRecord::Migration
 #     def self.up
-#       Engines.plugins[:tagging].migrate(5)
+#       Engines.plugins[:tagging].migrate(20081201172034)
 #     end
 #     def self.down
-#       Engines.plugins[:tagging].migrate(3)
+#       Engines.plugins[:tagging].migrate(20081107144959)
 #     end
 #   end
 #
-# Notice that if we were to migrate down to revision 22 or lower, the tagging plugin
-# will be migrated back down to version 3 - the version we were previously at.
+# Notice that if we were to migrate down to revision 20081108120415 or lower, the tagging plugin
+# will be migrated back down to version 20081107144959 - the version we were previously at.
 #
 #
 # = Creating migrations in plugins
@@ -118,44 +118,16 @@ require &quot;engines/plugin/migrator&quot;
 #
 #   Engines.plugins[:whatever].migrate(version)
 #
-# ---
 #
-# The Engines::RailsExtensions::Migrations module defines extensions for Rails' 
-# migration systems. Specifically:
+# = Upgrading from previous versions of the engines plugin
 #
-# * Adding a hook to initialize_schema_migrations_table to create the plugin schema
-#   info table.
+# Thanks to the tireless work of the plugin developer community, we can now relying on the migration 
+# mechanism in Rails 2.1+ to do much of the plugin migration work for us. This also means that we
+# don't need a seperate schema_info table for plugins.
 #
-module Engines::RailsExtensions::Migrations
-  def self.included(base) # :nodoc:
-    base.class_eval { alias_method_chain :initialize_schema_migrations_table, :engine_additions }
-  end
-
-  # Create the schema tables, and ensure that the plugin schema table
-  # is also initialized. The plugin schema info table is defined by
-  # Engines::Plugin::Migrator.schema_info_table_name.
-  def initialize_schema_migrations_table_with_engine_additions
-    initialize_schema_migrations_table_without_engine_additions
-
-    # create the plugin schema stuff.    
-    begin
-      execute &lt;&lt;-ESQL
-        CREATE TABLE #{Engines::Plugin::Migrator.schema_info_table_name} 
-          (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})
-      ESQL
-    rescue ActiveRecord::StatementInvalid
-      # Schema has been initialized
-    end
-  end
-end
-
-module ::ActiveRecord #:nodoc:
-  module ConnectionAdapters #:nodoc:
-    module SchemaStatements #:nodoc:
-      include Engines::RailsExtensions::Migrations
-    end
-  end
-end
-
-# Set ActiveRecord to ignore the plugin schema table by default
-::ActiveRecord::SchemaDumper.ignore_tables &lt;&lt; Engines.schema_info_table
\ No newline at end of file
+# To update your application, run
+#
+#   rake db:migrate:upgrade_plugin_migrations
+#
+# This will ensure that migration information is carried over into the main schema_migrations table.
+# 
\ No newline at end of file</diff>
      <filename>lib/engines/rails_extensions/migrations.rb</filename>
    </modified>
    <modified>
      <diff>@@ -43,6 +43,77 @@ namespace :db do
       end
     end
 
+    desc 'For engines coming from Rails version &lt; 2.0, you need to upgrade the schema info table'
+    task :upgrade_plugin_migrations =&gt; :environment do
+      # This task will upgrade Rails &lt; 2.0, and also databases previously updated with
+      # db:migrate:fix_engines_migrations
+      
+      # Old table name
+      old_sm_table = ActiveRecord::Migrator.proper_table_name(Engines.schema_info_table)
+      
+      unless ActiveRecord::Base.connection.table_exists?(old_sm_table)
+        abort &quot;Cannot find old migration table - assuming nothing needs to be done&quot;
+      end
+      
+      # There are two forms of the engines schema info - pre-fix_plugin_migrations and post
+      # We need to figure this out before we continue.
+      
+      results = ActiveRecord::Base.connection.select_rows(
+        &quot;SELECT version, plugin_name FROM #{old_sm_table}&quot;
+      ).uniq
+      
+      def insert_new_version(plugin_name, version)
+        version_string = &quot;#{version}-#{plugin_name}&quot;
+        new_sm_table = ActiveRecord::Migrator.schema_migrations_table_name
+        
+        # Check if the row already exists for some reason - maybe run this task more than once.
+        return if ActiveRecord::Base.connection.select_rows(&quot;SELECT * FROM #{new_sm_table} WHERE version = #{version_string.dump}&quot;).size &gt; 0
+        
+        puts &quot;Inserting new version #{version} for plugin #{plugin_name}..&quot;
+        ActiveRecord::Base.connection.insert(&quot;INSERT INTO #{new_sm_table} (version) VALUES (#{version_string.dump})&quot;)
+      end
+      
+      # We need to figure out if they already used &quot;fix_plugin_migrations&quot;
+      versions = {}
+      results.each do |r|
+        versions[r[1]] ||= []
+        versions[r[1]] &lt;&lt; r[0].to_i
+      end
+      
+      if versions.values.find{ |v| v.size &gt; 1 } == nil
+        puts &quot;Fixing migration info&quot;
+        # We only have one listed migration per plugin - this is pre-fix_plugin_migrations,
+        # so we build all versions required. In this case, all migrations should 
+        versions.each do |plugin_name, version|
+          version = version[0] # There is only one version
+          
+          # We have to make an assumption that numeric migrations won't get this long..
+          # I'm not sure if there is a better assumption, it should work in all
+          # current cases.. (touch wood..)
+          if version.to_s.size &lt; &quot;YYYYMMDDHHMMSS&quot;.size
+            # Insert version records for each migration
+            (1..version).each do |v|
+             insert_new_version(plugin_name, v)
+            end
+          else
+            # If the plugin is new-format &quot;YYYYMMDDHHMMSS&quot;, we just copy it across... 
+            # The case in which this occurs is very rare..
+            insert_new_version(plugin_name, version)
+          end
+        end
+      else
+        puts &quot;Moving migration info&quot;
+        # We have multiple migrations listed per plugin - thus we can assume they have
+        # already applied fix_plugin_migrations - we just copy it across verbatim
+        versions.each do |plugin_name, version|
+          version.each { |v| insert_new_version(plugin_name, v) }
+        end
+      end
+      
+      puts &quot;Migration info successfully migrated - removing old schema info table&quot;
+      ActiveRecord::Base.connection.drop_table(old_sm_table)
+    end
+    
     desc 'Migrate a specified plugin.'
     task({:plugin =&gt; :environment}, :name, :version) do |task, args|
       name = args[:name] || ENV['NAME']
@@ -53,7 +124,7 @@ namespace :db do
       else
         puts &quot;Plugin #{name} does not exist.&quot;
       end
-    end    
+    end
   end
 end
 
@@ -174,5 +245,5 @@ Report any issues on http://dev.rails-engines.org. Thanks!
     
     # Patch the default plugin testing task to have setup_plugin_fixtures as a prerequisite
     Rake::Task[&quot;test:plugins&quot;].prerequisites &lt;&lt; &quot;test:plugins:setup_plugin_fixtures&quot;
-  end  
-end
\ No newline at end of file
+  end
+end</diff>
      <filename>tasks/engines.rake</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>6cf3ce7cdc422da84c842552ae4e5209718127e3</id>
    </parent>
    <parent>
      <id>42af62ae4ddd0de61ec78a4aa6fe408713a1d72c</id>
    </parent>
  </parents>
  <author>
    <name>James Adam</name>
    <email>james@lazyatom.com</email>
  </author>
  <url>http://github.com/lazyatom/engines/commit/c152ff169b941a233d69f0c5ef7625cc3cca20cd</url>
  <id>c152ff169b941a233d69f0c5ef7625cc3cca20cd</id>
  <committed-date>2008-11-09T08:49:27-08:00</committed-date>
  <authored-date>2008-11-09T08:49:27-08:00</authored-date>
  <message>Merge branch 'use_rails_own_migration_mechanism'</message>
  <tree>46688e8949882c886c74903ab179055c1735c7f1</tree>
  <committer>
    <name>James Adam</name>
    <email>james@lazyatom.com</email>
  </committer>
</commit>
