From 8636f64defb4b6fd9f00c70ffc9dc7ffc017fe58 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 9 Oct 2010 12:15:36 +0200 Subject: [PATCH] Rename rake railties:copy_migrations to rake railties:install:migrations and fix it to work with new copying strategy --- .../lib/active_record/railties/databases.rake | 48 ++++++++++--------- railties/test/railties/shared_tests.rb | 27 +++++++---- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 4ef6c6f751..778bce697e 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -5,27 +5,6 @@ namespace :db do ActiveRecord::Migrator.migrations_path = Rails.application.paths["db/migrate"].first end - task :copy_migrations => :load_config do - to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map {|n| n.strip } - railties = {} - Rails.application.railties.all do |railtie| - next unless to_load == :all || to_load.include?(railtie.railtie_name) - - if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first) - railties[railtie.railtie_name] = path - end - end - - copied = ActiveRecord::Migration.copy(ActiveRecord::Migrator.migrations_path, railties) - - if copied.blank? - puts "No migrations were copied, project is up to date." - else - puts "The following migrations were copied:" - puts copied.map{ |path| File.basename(path) }.join("\n") - end - end - namespace :create do # desc 'Create all the local databases defined in config/database.yml' task :all => :load_config do @@ -501,8 +480,31 @@ namespace :db do end namespace :railties do - desc "Copies missing migrations from Railties (e.g. plugins, engines). You can specify Railties to use with FROM=railtie1,railtie2" - task :copy_migrations => 'db:copy_migrations' + namespace :install do + desc "Copies missing migrations from Railties (e.g. plugins, engines). You can specify Railties to use with FROM=railtie1,railtie2" + task :migrations => :"db:load_config" do + to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map {|n| n.strip } + railties = {} + Rails.application.railties.all do |railtie| + next unless to_load == :all || to_load.include?(railtie.railtie_name) + + if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first) + railties[railtie.railtie_name] = path + end + end + + on_skip = Proc.new do |name, migration| + $stderr.puts "WARNING: Migration #{migration.basename} from #{name} has been skipped. Migration with the same name already exists." + end + + on_copy = Proc.new do |name, migration, old_path| + puts "Copied migration #{migration.basename} from #{name}" + end + + ActiveRecord::Migration.copy( ActiveRecord::Migrator.migrations_path, railties, + :on_skip => on_skip, :on_copy => on_copy) + end + end end task 'test:prepare' => 'db:test:prepare' diff --git a/railties/test/railties/shared_tests.rb b/railties/test/railties/shared_tests.rb index 9b62f88fd7..7bd65158b4 100644 --- a/railties/test/railties/shared_tests.rb +++ b/railties/test/railties/shared_tests.rb @@ -21,6 +21,11 @@ module RailtiesTest end RUBY + @plugin.write "db/migrate/3_create_sessions.rb", <<-RUBY + class CreateSessions < ActiveRecord::Migration + end + RUBY + app_file "db/migrate/1_create_sessions.rb", <<-RUBY class CreateSessions < ActiveRecord::Migration end @@ -38,24 +43,26 @@ module RailtiesTest add_to_config "ActiveRecord::Base.timestamped_migrations = false" Dir.chdir(app_path) do - output = `rake railties:copy_migrations FROM=bukkits` + output = `rake railties:install:migrations FROM=bukkits 2>&1` - assert File.exists?("#{app_path}/db/migrate/2_create_users.bukkits.rb") - assert File.exists?("#{app_path}/db/migrate/3_add_last_name_to_users.bukkits.rb") - assert_match /2_create_users/, output - assert_match /3_add_last_name_to_users/, output + assert File.exists?("#{app_path}/db/migrate/2_create_users.rb") + assert File.exists?("#{app_path}/db/migrate/3_add_last_name_to_users.rb") + assert_match /Copied migration 2_create_users.rb from bukkits/, output + assert_match /Copied migration 3_add_last_name_to_users.rb from bukkits/, output + assert_match /WARNING: Migration 3_create_sessions.rb from bukkits has been skipped/, output assert_equal 3, Dir["#{app_path}/db/migrate/*.rb"].length - output = `rake railties:copy_migrations` + output = `rake railties:install:migrations 2>&1` - assert File.exists?("#{app_path}/db/migrate/4_create_yaffles.acts_as_yaffle.rb") - assert_match /4_create_yaffles/, output + assert File.exists?("#{app_path}/db/migrate/4_create_yaffles.rb") + assert_match /WARNING: Migration 3_create_sessions.rb from bukkits has been skipped/, output + assert_match /Copied migration 4_create_yaffles.rb from acts_as_yaffle/, output + assert_no_match /2_create_users/, output migrations_count = Dir["#{app_path}/db/migrate/*.rb"].length - output = `rake railties:copy_migrations` + output = `rake railties:install:migrations 2>&1` assert_equal migrations_count, Dir["#{app_path}/db/migrate/*.rb"].length - assert_match /No migrations were copied/, output end end