<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,64 +1,78 @@
-# Goldberg::Migrator is a plugin migration system based on
-# PluginAWeek's (http://pluginaweek.org) plugin_migrations.  It allows
-# plugins to have their own migration streams.  These can be made
-# available as rake tasks that work similarly to Rails' &quot;db:migrate&quot;
-# task, including respecting the VERSION environment variable.  See
-# Goldberg's &quot;goldberg:migrate&quot; task.
-# 
-# This code is included in the Goldberg project in order to remove the
-# dependency on the plugin_migrations gem, while retaining schema
-# compatibilty so that users can use Goldberg alongside other plugins
-# that utilise plugin_migrations.
+# Provide separate migrations for plugins.
+
+# Goldberg::Migrator inherits from ActiveRecord::Migrator, overriding
+# some methods to allow migration data for plugins to be stored in the
+# table &quot;plugin_schema_migrations&quot;.  This also requires modifying some
+# methods in ActiveRecord::ConnectionAdapters::SchemaStatements.
+
+# This code used to be based on PluginAWeek's plugin_migrations, but
+# has been re-written for the new &quot;schema_migrations&quot; system provided
+# by Rails 2.1.
 
 module Goldberg
   module SchemaStatements
     def self.included(base) #:nodoc:
       base.class_eval do
-        alias_method_chain :initialize_schema_migrations_table, :plugins
         alias_method_chain :dump_schema_information, :plugins
+        alias_method_chain :initialize_schema_migrations_table, :plugins
+        alias_method_chain :assume_migrated_upto_version, :plugins
       end
     end
     
+    # Dumps the plugin schema info table as well as information about the
+    # current plugin migrations
+    def dump_schema_information_with_plugins
+      begin
+        sm_table = Goldberg::Migrator.schema_migrations_table_name
+        migrated = select_all(&quot;SELECT version FROM #{sm_table}&quot;)
+        migrated.map do |v|
+          &quot;INSERT INTO #{sm_table} (plugin_name, version) VALUES ('#{v['plugin_name']}', '#{v['version']}');&quot;
+        end.join(&quot;\n\n&quot;)
+      rescue ActiveRecord::StatementInvalid 
+        # No Schema Info
+        ''
+      end
+    end
+
     # Creates the plugin schema info table
     def initialize_schema_migrations_table_with_plugins
-      initialize_schema_migrations_table_without_plugins
+      sm_table = Goldberg::Migrator.schema_migrations_table_name
       
-      begin
-        execute &lt;&lt;-EOS
-CREATE TABLE #{Goldberg::Migrator.schema_info_table_name}
-(plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})
-EOS
-      rescue ActiveRecord::StatementInvalid
-        # Schema has already been initialised?
+      unless tables.detect { |t| t == sm_table }
+        create_table(sm_table, :id =&gt; false) do |schema_migrations_table|
+          schema_migrations_table.column :plugin_name, :string, :null =&gt; false
+          schema_migrations_table.column :version, :string, :null =&gt; false
+        end
+        add_index sm_table, [:plugin_name, :version], :unique =&gt; true,
+        :name =&gt; 'unique_schema_migrations'
+        
+        # Backwards-compatibility: if we find schema_info, assume we've
+        # migrated up to that point:
+        si_table = ActiveRecord::Base.table_name_prefix + 'plugin_schema_info' +
+          ActiveRecord::Base.table_name_suffix
+        if tables.detect { |t| t == si_table }
+          old_version = select_value(&quot;SELECT version FROM #{quote_table_name(si_table)} WHERE plugin_name=#{Goldberg::Migrator.plugin}&quot;).to_i
+          assume_migrated_upto_version(old_version)
+          drop_table(si_table)
+        end
       end
     end
     
-    # Dumps the plugin schema info table as well as information about the
-    # current plugin migrations
-    def dump_schema_information_with_plugins
-      schema_information = []
-      
-      dump = dump_schema_information_without_plugins
-      dump &amp;&amp; (schema_information &lt;&lt; dump)
+    def assume_migrated_upto_version_with_plugins(version)
+      sm_table = Goldberg::Migrator.schema_migrations_table_name
+      migration_path = &quot;#{RAILS_ROOT}/vendor/plugins/#{Goldberg::Migrator.plugin_name}/db/migrate&quot;
       
-      begin
-        plugins = ActiveRecord::Base.connection.select_all &lt;&lt;-EOS
-SELECT * FROM #{Goldberg::Migrator.schema_info_table_name}
-EOS
-        plugins.each do |plugin|
-          if (version = plugin['version'].to_i) &gt; 0
-            plugin_esc = ActiveRecord::Base.quote_value(plugin['plugin_name'])
-            schema_information &lt;&lt; %Q&lt;
-INSERT INTO #{Goldberg::Migrator.schema_info_table_name}
-(plugin_name, version) VALUES (#{plugin_esc}, #{version})&gt;
-          end
-        end
-      rescue ActiveRecord::StatementInvalid 
-        # No Schema Info
+      migrated = select_values(&quot;SELECT version FROM #{sm_table} WHERE plugin_name=#{Goldberg::Migrator.plugin}&quot;).map(&amp;:to_i)
+      versions = Dir[&quot;#{migration_path}[0-9]*_*.rb&quot;].map do |filename|
+        filename.split('/').last.split('_').first.to_i
       end
       
-      schema_information.join(&quot;;\n&quot;)
+      execute &quot;INSERT INTO #{sm_table} (plugin_name, version) VALUES (#{Goldberg::Migrator.plugin}, '#{version}')&quot; unless migrated.include?(version.to_i)
+      (versions - migrated).select { |v| v &lt; version.to_i }.each do |v|
+        execute &quot;INSERT INTO #{sm_table} (plugin_name, version) VALUES (#{Goldberg::Migrator.plugin}, '#{v}')&quot;
+      end
     end
+    
   end  # module SchemaStatements
 
 
@@ -77,20 +91,15 @@ INSERT INTO #{Goldberg::Migrator.schema_info_table_name}
         super(&quot;#{RAILS_ROOT}/vendor/plugins/#{plugin_name}/db/migrate&quot;, version)
       end
       
-      def schema_info_table_name
-        ActiveRecord::Base.table_name_prefix + 'plugin_schema_info' +
+      def schema_migrations_table_name
+        ActiveRecord::Base.table_name_prefix + 'plugin_schema_migrations' +
           ActiveRecord::Base.table_name_suffix
       end
       
       def current_version
         begin
-          if result = ActiveRecord::Base.connection.select_one(%Q&lt;
-SELECT version FROM #{schema_info_table_name} WHERE plugin_name=#{plugin}&gt;)
-            result['version'].to_i
-          else
-            # No such plugin migrated yet?
-            0
-          end
+          version = ActiveRecord::Base.connection.select_values(&quot;SELECT version FROM #{schema_migrations_table_name} WHERE plugin_name=#{Goldberg::Migrator.plugin}&quot;).map(&amp;:to_i).max
+        version || 0
         rescue ActiveRecord::StatementInvalid
           # No migration info table, so never migrated
           0
@@ -103,24 +112,20 @@ SELECT version FROM #{schema_info_table_name} WHERE plugin_name=#{plugin}&gt;)
       end
     end  # class &lt;&lt; self
       
-    # Sets the version of the current plugin
-    def set_schema_version(version)
-      version = down? ? version.to_i - 1 : version.to_i
+    def migrated
+      sm_table = self.class.schema_migrations_table_name
+      ActiveRecord::Base.connection.select_values(&quot;SELECT version FROM #{sm_table} WHERE plugin_name=#{self.class.plugin}&quot;).map(&amp;:to_i).sort
+    end
+    
+    private
+
+    def record_version_state_after_migrating(version)
+      sm_table = self.class.schema_migrations_table_name
       
-      if ActiveRecord::Base.connection.select_one &lt;&lt;-EOS
-SELECT version FROM #{self.class.schema_info_table_name}
-WHERE plugin_name = #{self.class.plugin}
-EOS
-        ActiveRecord::Base.connection.update &lt;&lt;-EOS
-UPDATE #{self.class.schema_info_table_name} SET version = #{version}
-WHERE plugin_name = #{self.class.plugin}
-EOS
+      if down?
+        ActiveRecord::Base.connection.update(&quot;DELETE FROM #{sm_table} WHERE version = '#{version}' AND plugin_name=#{self.class.plugin}&quot;)
       else
-        # We need to create the entry since it doesn't exist yet
-        ActiveRecord::Base.connection.execute &lt;&lt;-EOS
-INSERT INTO #{self.class.schema_info_table_name} (version, plugin_name)
-VALUES (#{version}, #{self.class.plugin})
-EOS
+        ActiveRecord::Base.connection.insert(&quot;INSERT INTO #{sm_table} (plugin_name, version) VALUES (#{self.class.plugin}, '#{version}')&quot;)
       end
     end
 </diff>
      <filename>lib/goldberg/migrator.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>89da6cfe786aa0e4f4d860a6d30bd32ae244eef5</id>
    </parent>
  </parents>
  <author>
    <name>Dave Nelson</name>
    <email>urbanus@240gl.org</email>
  </author>
  <url>http://github.com/urbanus/goldberg/commit/3040f30e9d861064a7d1bb06c98634d328058509</url>
  <id>3040f30e9d861064a7d1bb06c98634d328058509</id>
  <committed-date>2008-07-09T19:57:37-07:00</committed-date>
  <authored-date>2008-07-09T19:57:37-07:00</authored-date>
  <message>Modified Goldberg's plugin migrations for compatibility with
ActiveRecord 2.1.</message>
  <tree>fc2be9b78366b4f2cbca8dd716b231c9220ef2c5</tree>
  <committer>
    <name>Dave Nelson</name>
    <email>urbanus@240gl.org</email>
  </committer>
</commit>
