Unverified Commit b33b7be5 authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Handle NULL migration errors in migration helpers

This ensures that whenever changing the NULL constraint of a column
fails we still drop the column.
parent ed1d62af5d4e
......@@ -126,6 +126,8 @@ def add_column_with_default(table, column, type, default:, allow_null: false)
begin
transaction do
update_column_in_batches(table, column, default)
change_column_null(table, column, false) unless allow_null
end
# We want to rescue _all_ exceptions here, even those that don't inherit
# from StandardError.
......@@ -134,8 +136,6 @@ def add_column_with_default(table, column, type, default:, allow_null: false)
raise error
end
change_column_null(table, column, false) unless allow_null
end
end
end
......
......@@ -120,6 +120,19 @@
model.add_column_with_default(:projects, :foo, :integer, default: 10)
end.to raise_error(RuntimeError)
end
it 'removes the added column whenever changing a column NULL constraint fails' do
expect(model).to receive(:change_column_null).
with(:projects, :foo, false).
and_raise(RuntimeError)
expect(model).to receive(:remove_column).
with(:projects, :foo)
expect do
model.add_column_with_default(:projects, :foo, :integer, default: 10)
end.to raise_error(RuntimeError)
end
end
context 'inside a transaction' do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment