Skip to content

Commit

Permalink
Reraise errors when running background schema migrations inline
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Apr 8, 2024
1 parent ce71acd commit 38cee8c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
31 changes: 29 additions & 2 deletions test/background_schema_migrations/migration_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/background_schema_migrations/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38cee8c

Please sign in to comment.