diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2010-07-26 14:21:30 +0200 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2010-09-03 22:59:09 +0200 |
commit | 43a2aef3165793d9265aea1257063a60766f2226 (patch) | |
tree | 15ddf97940fdd7cf94c5db7a0ffb59dad713b5af | |
parent | 75f8ac6ea71e4b2337f870b91ac05df98f33a8d2 (diff) | |
download | rails-43a2aef3165793d9265aea1257063a60766f2226.tar.gz rails-43a2aef3165793d9265aea1257063a60766f2226.tar.bz2 rails-43a2aef3165793d9265aea1257063a60766f2226.zip |
rake db:copy_migrations task, which copies railties migrations into application's db/migrate directory
-rw-r--r-- | activerecord/lib/active_record/railties/databases.rake | 22 | ||||
-rw-r--r-- | railties/test/railties/shared_tests.rb | 49 |
2 files changed, 71 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index f8a5114870..aedda26ba5 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -5,6 +5,28 @@ namespace :db do ActiveRecord::Migrator.migrations_path = Rails.application.config.paths.db.migrate.to_a.first end + desc "Copies missing migrations from Railties (e.g. plugins, engines). You can specify Railties to use with FROM=railtie1,railtie2" + 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.config.respond_to?(:paths) && railtie.config.paths.db + railties[railtie.railtie_name] = railtie.config.paths.db.migrate.to_a.first + 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 diff --git a/railties/test/railties/shared_tests.rb b/railties/test/railties/shared_tests.rb index ce7c55c11c..6aae17c237 100644 --- a/railties/test/railties/shared_tests.rb +++ b/railties/test/railties/shared_tests.rb @@ -10,6 +10,55 @@ module RailtiesTest @app ||= Rails.application end + def test_copying_migrations + @plugin.write "db/migrate/1_create_users.rb", <<-RUBY + class CreateUsers < ActiveRecord::Migration + end + RUBY + + @plugin.write "db/migrate/2_add_last_name_to_users.rb", <<-RUBY + class AddLastNameToUsers < ActiveRecord::Migration + end + RUBY + + app_file "db/migrate/1_create_sessions.rb", <<-RUBY + class CreateSessions < ActiveRecord::Migration + end + RUBY + + yaffle = plugin "acts_as_yaffle", "::LEVEL = config.log_level" do |plugin| + plugin.write "lib/acts_as_yaffle.rb", "class ActsAsYaffle; end" + end + + yaffle.write "db/migrate/1_create_yaffles.rb", <<-RUBY + class CreateYaffles < ActiveRecord::Migration + end + RUBY + + add_to_config "ActiveRecord::Base.timestamped_migrations = false" + + Dir.chdir(app_path) do + output = `rake db:copy_migrations FROM=bukkits` + + 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_equal 3, Dir["#{app_path}/db/migrate/*.rb"].length + + output = `rake db:copy_migrations` + + assert File.exists?("#{app_path}/db/migrate/4_create_yaffles.acts_as_yaffle.rb") + assert_match /4_create_yaffles/, output + + migrations_count = Dir["#{app_path}/db/migrate/*.rb"].length + output = `rake db:copy_migrations` + + assert_equal migrations_count, Dir["#{app_path}/db/migrate/*.rb"].length + assert_match /No migrations were copied/, output + end + end + def test_puts_its_lib_directory_on_load_path boot_rails require "another" |