From 35a1744a45501fe79660ba11fbee35a7bf099bce Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 9 Dec 2011 20:53:55 +0100 Subject: [PATCH] Allow to run migrations with given scope, with SCOPE= Scope in migrations can be defined by adding suffix in filename, like: 01_a_migration.blog.rb. Such migration have blog scope. Scope is automatically added while copying migrations from engine, so if you want to revert all of the migrations from given engine, you can just run db:migrate with SCOPE, like: rake db:migrate SCOPE=blog --- activerecord/CHANGELOG.md | 12 ++++++++++ .../lib/active_record/railties/databases.rake | 4 +++- .../test/application/rake/migrations_test.rb | 24 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 888bc43ec9199..268799b4b3c19 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,17 @@ ## Rails 3.2.0 (unreleased) ## +* Added ability to run migrations only for given scope, which allows + to run migrations only from one engine (for example to revert changes + from engine that you want to remove). + + Example: + rake db:migrate SCOPE=blog + + *Piotr Sarnacki* + +* Migrations copied from engines are now scoped with engine's name, + for example 01_create_posts.blog.rb. *Piotr Sarnacki* + * Implements `AR::Base.silence_auto_explain`. This method allows the user to selectively disable automatic EXPLAINs within a block. *fxn* diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index abd71793fdb93..0aafb5184f960 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -149,7 +149,9 @@ db_namespace = namespace :db do desc "Migrate the database (options: VERSION=x, VERBOSE=false)." task :migrate => [:environment, :load_config] do ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true - ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) + ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration| + ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope) + end db_namespace['_dump'].invoke end diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb index 4ec6f1b67cd80..3d3e01cdc0c4c 100644 --- a/railties/test/application/rake/migrations_test.rb +++ b/railties/test/application/rake/migrations_test.rb @@ -12,6 +12,30 @@ def setup def teardown teardown_app end + + test 'running migrations with given scope' do + Dir.chdir(app_path) do + `rails generate model user username:string password:string` + end + app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION + class AMigration < ActiveRecord::Migration + end + MIGRATION + + output = Dir.chdir(app_path) { `rake db:migrate SCOPE=bukkits` } + assert_no_match(/create_table\(:users\)/, output) + assert_no_match(/CreateUsers/, output) + assert_no_match(/add_column\(:users, :email, :string\)/, output) + + assert_match(/AMigration: migrated/, output) + + output = Dir.chdir(app_path) { `rake db:migrate SCOPE=bukkits VERSION=0` } + assert_no_match(/drop_table\(:users\)/, output) + assert_no_match(/CreateUsers/, output) + assert_no_match(/remove_column\(:users, :email\)/, output) + + assert_match(/AMigration: reverted/, output) + end test 'model and migration generator with change syntax' do Dir.chdir(app_path) do