Browse files

rake db:copy_migrations task, which copies railties migrations into a…

…pplication's db/migrate directory
  • Loading branch information...
1 parent 75f8ac6 commit 43a2aef3165793d9265aea1257063a60766f2226 @drogus drogus committed Jul 26, 2010
Showing with 71 additions and 0 deletions.
  1. +22 −0 activerecord/lib/active_record/railties/databases.rake
  2. +49 −0 railties/test/railties/shared_tests.rb
View
22 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
View
49 railties/test/railties/shared_tests.rb
@@ -10,6 +10,55 @@ def app
@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"

0 comments on commit 43a2aef

Please sign in to comment.