From 38cee8cb78a8ff122bbaae32b8e2f098e841e51e Mon Sep 17 00:00:00 2001 From: fatkodima Date: Tue, 9 Apr 2024 02:53:10 +0300 Subject: [PATCH] Reraise errors when running background schema migrations inline --- CHANGELOG.md | 2 ++ .../migration_runner.rb | 1 + .../migration_runner_test.rb | 31 +++++++++++++++++-- .../migration_test.rb | 4 ++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a50f78b..a22d6b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## master (unreleased) +- Reraise errors when running background schema migrations inline + ## 0.16.1 (2024-03-29) - Improve error message when background schema migration name is already taken diff --git a/lib/online_migrations/background_schema_migrations/migration_runner.rb b/lib/online_migrations/background_schema_migrations/migration_runner.rb index ef77425..faf6125 100644 --- a/lib/online_migrations/background_schema_migrations/migration_runner.rb +++ b/lib/online_migrations/background_schema_migrations/migration_runner.rb @@ -76,6 +76,7 @@ def do_run ) ::OnlineMigrations.config.background_schema_migrations.error_handler.call(e, migration) + raise if Utils.run_background_migrations_inline? end def should_throttle? diff --git a/test/background_schema_migrations/migration_runner_test.rb b/test/background_schema_migrations/migration_runner_test.rb index dfe2d5d..bacec1a 100644 --- a/test/background_schema_migrations/migration_runner_test.rb +++ b/test/background_schema_migrations/migration_runner_test.rb @@ -107,7 +107,9 @@ def test_adding_existing_index def test_run_saves_error_when_failed m = create_migration(definition: "SOME INVALID SQL") - run_migration(m) + assert_raises(ActiveRecord::StatementInvalid) do + run_migration(m) + end assert m.failed? assert m.finished_at @@ -126,13 +128,38 @@ def test_run_calls_error_handler_when_failed assert_equal m, errored_migration end - run_migration(m) + assert_raises(ActiveRecord::StatementInvalid) do + run_migration(m) + end assert_instance_of ActiveRecord::StatementInvalid, handled_error ensure OnlineMigrations.config.background_schema_migrations.error_handler = previous end + def test_run_reraises_error_when_running_background_migrations_inline + m = create_migration(definition: "SOME INVALID SQL") + + prev = OnlineMigrations.config.run_background_migrations_inline + OnlineMigrations.config.run_background_migrations_inline = -> { true } + + assert_raises(ActiveRecord::StatementInvalid) do + run_migration(m) + end + ensure + OnlineMigrations.config.run_background_migrations_inline = prev + end + + def test_run_do_not_reraise_error_when_running_background_migrations_in_background + m = create_migration(definition: "SOME INVALID SQL") + + OnlineMigrations.config.stub(:run_background_migrations_inline, nil) do + assert_nothing_raised do + run_migration(m) + end + end + end + def test_uses_custom_statement_timeout m = create_migration(statement_timeout: 42) assert_sql("SET statement_timeout TO 42000") do diff --git a/test/background_schema_migrations/migration_test.rb b/test/background_schema_migrations/migration_test.rb index 8004f6f..a48b6d9 100644 --- a/test/background_schema_migrations/migration_test.rb +++ b/test/background_schema_migrations/migration_test.rb @@ -154,7 +154,9 @@ def test_creates_child_migrations_for_sharded_migration def test_retry m = create_migration(definition: "SOME INVALID SQL") - m.max_attempts.times { run_migration(m) } + assert_raises(ActiveRecord::StatementInvalid) do + m.max_attempts.times { run_migration(m) } + end assert m.failed? m.retry