diff options
Diffstat (limited to 'vendor/plugins/engines/tasks/engines.rake')
-rw-r--r-- | vendor/plugins/engines/tasks/engines.rake | 80 |
1 files changed, 77 insertions, 3 deletions
diff --git a/vendor/plugins/engines/tasks/engines.rake b/vendor/plugins/engines/tasks/engines.rake index a88631f6e..0ffd7e6e7 100644 --- a/vendor/plugins/engines/tasks/engines.rake +++ b/vendor/plugins/engines/tasks/engines.rake @@ -43,6 +43,80 @@ namespace :db do end end + desc 'For engines coming from Rails version < 2.0 or for those previously updated to work with Sven Fuch\'s fork of engines, you need to upgrade the schema info table' + task :upgrade_plugin_migrations => :environment do + svens_fork_table_name = 'plugin_schema_migrations' + + # Check if app was previously using Sven's fork + if ActiveRecord::Base.connection.table_exists?(svens_fork_table_name) + old_sm_table = svens_fork_table_name + else + old_sm_table = ActiveRecord::Migrator.proper_table_name(Engines.schema_info_table) + end + + unless ActiveRecord::Base.connection.table_exists?(old_sm_table) + abort "Cannot find old migration table - assuming nothing needs to be done" + 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( + "SELECT version, plugin_name FROM #{old_sm_table}" + ).uniq + + def insert_new_version(plugin_name, version) + version_string = "#{version}-#{plugin_name}" + 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("SELECT * FROM #{new_sm_table} WHERE version = #{version_string.dump.gsub("\"", "'")}").size > 0 + + puts "Inserting new version #{version} for plugin #{plugin_name}.." + ActiveRecord::Base.connection.insert("INSERT INTO #{new_sm_table} (version) VALUES (#{version_string.dump.gsub("\"", "'")})") + end + + # We need to figure out if they already used "fix_plugin_migrations" + versions = {} + results.each do |r| + versions[r[1]] ||= [] + versions[r[1]] << r[0].to_i + end + + if versions.values.find{ |v| v.size > 1 } == nil + puts "Fixing migration info" + # 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 < "YYYYMMDDHHMMSS".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 "YYYYMMDDHHMMSS", we just copy it across... + # The case in which this occurs is very rare.. + insert_new_version(plugin_name, version) + end + end + else + puts "Moving migration info" + # 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 "Migration info successfully migrated - removing old schema info table" + ActiveRecord::Base.connection.drop_table(old_sm_table) + end + desc 'Migrate a specified plugin.' task({:plugin => :environment}, :name, :version) do |task, args| name = args[:name] || ENV['NAME'] @@ -53,7 +127,7 @@ namespace :db do else puts "Plugin #{name} does not exist." end - end + end end end @@ -174,5 +248,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["test:plugins"].prerequisites << "test:plugins:setup_plugin_fixtures" - end -end
\ No newline at end of file + end +end |