Skip to content

Commit 7b9497a

Browse files
authored
Merge pull request rails#48134 from eileencodes/deprecate-check_pending
Deprecate `check_pending!` in favor of `check_all_pending!`
2 parents 19b1894 + 03379d1 commit 7b9497a

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Deprecate `check_pending!` in favor of `check_pending_migrations!`.
2+
3+
`check_pending!` will only check for pending migrations on the current database connection or the one passed in. This has been deprecated in favor of `check_pending_migrations!` which will find all pending migrations for the database configurations in a given environment.
4+
5+
*Eileen M. Uchitelle*
6+
17
* Make `increment_counter`/`decrement_counter` accept an amount argument
28

39
```ruby

activerecord/lib/active_record/migration.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,38 @@ def nearest_delegate # :nodoc:
643643
end
644644

645645
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
646+
#
647+
# This is deprecated in favor of +check_pending_migrations!+
646648
def check_pending!(connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection)
647-
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
649+
ActiveRecord.deprecator.warn(<<-MSG.squish)
650+
The `check_pending!` method is deprecated in favor of `check_pending_migrations!`. The
651+
new implementation will loop through all available database configurations and find
652+
pending migrations. The prior implementation did not permit this.
653+
MSG
654+
655+
pending_migrations = connection.migration_context.open.pending_migrations
656+
657+
if pending_migrations.any?
658+
raise ActiveRecord::PendingMigrationError.new(pending_migrations: pending_migrations)
659+
end
660+
end
661+
662+
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending
663+
# for all database configurations in an environment.
664+
def check_all_pending!
665+
pending_migrations = []
666+
667+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: env) do |connection|
668+
if pending = connection.migration_context.open.pending_migrations
669+
pending_migrations << pending
670+
end
671+
end
672+
673+
migrations = pending_migrations.flatten
674+
675+
if migrations.any?
676+
raise ActiveRecord::PendingMigrationError.new(pending_migrations: migrations)
677+
end
648678
end
649679

650680
def load_schema_if_pending!

activerecord/test/cases/migration/pending_migrations_test.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def test_errors_if_pending
4444
assert_pending_migrations("01_create_foo.rb")
4545
end
4646

47+
def test_errors_if_pending_with_deprecated_method
48+
create_migration "01", "create_foo"
49+
assert_deprecated_check_pending("01_create_foo.rb")
50+
end
51+
4752
def test_checks_if_supported
4853
run_migrations
4954
assert_no_pending_migrations
@@ -87,10 +92,29 @@ def test_with_stdlib_logger
8792
end
8893

8994
private
95+
def assert_deprecated_check_pending(*expected_migrations)
96+
2.times do
97+
assert_raises ActiveRecord::PendingMigrationError do
98+
assert_deprecated(ActiveRecord.deprecator) do
99+
ActiveRecord::Migration.check_pending!
100+
end
101+
end
102+
103+
error = assert_raises ActiveRecord::PendingMigrationError do
104+
CheckPending.new(proc { flunk }).call({})
105+
end
106+
107+
assert_includes error.message, "Migrations are pending."
108+
expected_migrations.each do |migration|
109+
assert_includes error.message, migration
110+
end
111+
end
112+
end
113+
90114
def assert_pending_migrations(*expected_migrations)
91115
2.times do
92116
assert_raises ActiveRecord::PendingMigrationError do
93-
ActiveRecord::Migration.check_pending!
117+
ActiveRecord::Migration.check_all_pending!
94118
end
95119

96120
error = assert_raises ActiveRecord::PendingMigrationError do
@@ -110,7 +134,7 @@ def assert_no_pending_migrations
110134

111135
2.times do
112136
assert_nothing_raised do
113-
ActiveRecord::Migration.check_pending!
137+
ActiveRecord::Migration.check_all_pending!
114138
end
115139

116140
app.expect :call, nil, [{}]

0 commit comments

Comments
 (0)