Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate check_pending! in favor of check_all_pending! #48134

Merged
merged 1 commit into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Deprecate `check_pending!` in favor of `check_pending_migrations!`.

`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.

*Eileen M. Uchitelle*

* Make `increment_counter`/`decrement_counter` accept an amount argument

```ruby
Expand Down
32 changes: 31 additions & 1 deletion activerecord/lib/active_record/migration.rb
Expand Up @@ -643,8 +643,38 @@ def nearest_delegate # :nodoc:
end

# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
#
# This is deprecated in favor of +check_pending_migrations!+
def check_pending!(connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection)
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
ActiveRecord.deprecator.warn(<<-MSG.squish)
The `check_pending!` method is deprecated in favor of `check_pending_migrations!`. The
new implementation will loop through all available database configurations and find
pending migrations. The prior implementation did not permit this.
MSG

pending_migrations = connection.migration_context.open.pending_migrations

if pending_migrations.any?
raise ActiveRecord::PendingMigrationError.new(pending_migrations: pending_migrations)
end
end

# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending
# for all database configurations in an environment.
def check_all_pending!
pending_migrations = []

ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each(env: env) do |connection|
if pending = connection.migration_context.open.pending_migrations
pending_migrations << pending
end
end

migrations = pending_migrations.flatten

if migrations.any?
raise ActiveRecord::PendingMigrationError.new(pending_migrations: migrations)
end
end

def load_schema_if_pending!
Expand Down
28 changes: 26 additions & 2 deletions activerecord/test/cases/migration/pending_migrations_test.rb
Expand Up @@ -44,6 +44,11 @@ def test_errors_if_pending
assert_pending_migrations("01_create_foo.rb")
end

def test_errors_if_pending_with_deprecated_method
create_migration "01", "create_foo"
assert_deprecated_check_pending("01_create_foo.rb")
end

def test_checks_if_supported
run_migrations
assert_no_pending_migrations
Expand Down Expand Up @@ -87,10 +92,29 @@ def test_with_stdlib_logger
end

private
def assert_deprecated_check_pending(*expected_migrations)
2.times do
assert_raises ActiveRecord::PendingMigrationError do
assert_deprecated(ActiveRecord.deprecator) do
ActiveRecord::Migration.check_pending!
end
end

error = assert_raises ActiveRecord::PendingMigrationError do
CheckPending.new(proc { flunk }).call({})
end

assert_includes error.message, "Migrations are pending."
expected_migrations.each do |migration|
assert_includes error.message, migration
end
end
end

def assert_pending_migrations(*expected_migrations)
2.times do
assert_raises ActiveRecord::PendingMigrationError do
ActiveRecord::Migration.check_pending!
ActiveRecord::Migration.check_all_pending!
end

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

2.times do
assert_nothing_raised do
ActiveRecord::Migration.check_pending!
ActiveRecord::Migration.check_all_pending!
end

app.expect :call, nil, [{}]
Expand Down