aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Lee <rails@lazymalevolence.com>2019-06-07 11:13:18 -0700
committerMark Lee <rails@lazymalevolence.com>2019-06-10 18:01:32 -0700
commitcb8b57d07e132a6528bce47c93ee392c6cc64c10 (patch)
treef63d5817d9284de073071cb16aa1cd362e7b928b
parent0542e0608f86d9e4089861f4e72c578bc983f89f (diff)
downloadrails-cb8b57d07e132a6528bce47c93ee392c6cc64c10.tar.gz
rails-cb8b57d07e132a6528bce47c93ee392c6cc64c10.tar.bz2
rails-cb8b57d07e132a6528bce47c93ee392c6cc64c10.zip
Convert the db:abort_if_pending_migrations task to be multi-DB aware
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/railties/databases.rake26
-rw-r--r--railties/test/application/rake/multi_dbs_test.rb26
3 files changed, 55 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 09fdc66788..a4def5b270 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Add support for multiple databases to `rails db:abort_if_pending_migrations`.
+
+ *Mark Lee*
+
* Fix sqlite3 collation parsing when using decimal columns.
*Martin R. Schuster*
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake
index befcbc8984..d17acc408c 100644
--- a/activerecord/lib/active_record/railties/databases.rake
+++ b/activerecord/lib/active_record/railties/databases.rake
@@ -250,7 +250,11 @@ db_namespace = namespace :db do
# desc "Raises an error if there are pending migrations"
task abort_if_pending_migrations: :load_config do
- pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
+ pending_migrations = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).flat_map do |db_config|
+ ActiveRecord::Base.establish_connection(db_config.config)
+
+ ActiveRecord::Base.connection.migration_context.open.pending_migrations
+ end
if pending_migrations.any?
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
@@ -261,6 +265,26 @@ db_namespace = namespace :db do
end
end
+ namespace :abort_if_pending_migrations do
+ ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
+ # desc "Raises an error if there are pending migrations for #{spec_name} database"
+ task spec_name => :load_config do
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
+ ActiveRecord::Base.establish_connection(db_config.config)
+
+ pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
+
+ if pending_migrations.any?
+ puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
+ pending_migrations.each do |pending_migration|
+ puts " %4d %s" % [pending_migration.version, pending_migration.name]
+ end
+ abort %{Run `rails db:migrate:#{spec_name}` to update your database then try again.}
+ end
+ end
+ end
+ end
+
desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
diff --git a/railties/test/application/rake/multi_dbs_test.rb b/railties/test/application/rake/multi_dbs_test.rb
index 8c41b252da..2606e64424 100644
--- a/railties/test/application/rake/multi_dbs_test.rb
+++ b/railties/test/application/rake/multi_dbs_test.rb
@@ -299,6 +299,32 @@ module ApplicationTests
db_migrate_and_schema_cache_dump_and_schema_cache_clear
end
+ test "db:abort_if_pending_migrations works on all databases" do
+ require "#{app_path}/config/environment"
+
+ app_file "db/animals_migrate/02_two_migration.rb", <<-MIGRATION
+ class TwoMigration < ActiveRecord::Migration::Current
+ end
+ MIGRATION
+
+ output = rails("db:abort_if_pending_migrations", allow_failure: true)
+ assert_match(/You have 1 pending migration/, output)
+ end
+
+ test "db:abort_if_pending_migrations:namespace works" do
+ require "#{app_path}/config/environment"
+
+ app_file "db/animals_migrate/02_two_migration.rb", <<-MIGRATION
+ class TwoMigration < ActiveRecord::Migration::Current
+ end
+ MIGRATION
+
+ output = rails("db:abort_if_pending_migrations:primary")
+ assert_no_match(/You have \d+ pending migration/, output)
+ output = rails("db:abort_if_pending_migrations:animals", allow_failure: true)
+ assert_match(/You have 1 pending migration/, output)
+ end
+
test "db:prepare works on all databases" do
require "#{app_path}/config/environment"
db_prepare