Commit 3fc20c01 authored by Yorick Peterse's avatar Yorick Peterse

Fix adding defaults for concurrent column renames

By adding the default value _after_ adding the column we avoid updating
all rows in a table, saving a lot of time and unnecessary work in the
process.
parent 99f36c2c
...@@ -283,11 +283,15 @@ module Gitlab ...@@ -283,11 +283,15 @@ module Gitlab
add_column(table, new, new_type, add_column(table, new, new_type,
limit: old_col.limit, limit: old_col.limit,
default: old_col.default,
null: old_col.null, null: old_col.null,
precision: old_col.precision, precision: old_col.precision,
scale: old_col.scale) scale: old_col.scale)
# We set the default value _after_ adding the column so we don't end up
# updating any existing data with the default value. This isn't
# necessary since we copy over old values further down.
change_column_default(table, new, old_col.default) if old_col.default
trigger_name = rename_trigger_name(table, old, new) trigger_name = rename_trigger_name(table, old, new)
quoted_table = quote_table_name(table) quoted_table = quote_table_name(table)
quoted_old = quote_column_name(old) quoted_old = quote_column_name(old)
......
...@@ -382,11 +382,13 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -382,11 +382,13 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
expect(model).to receive(:add_column). expect(model).to receive(:add_column).
with(:users, :new, :integer, with(:users, :new, :integer,
limit: old_column.limit, limit: old_column.limit,
default: old_column.default,
null: old_column.null, null: old_column.null,
precision: old_column.precision, precision: old_column.precision,
scale: old_column.scale) scale: old_column.scale)
expect(model).to receive(:change_column_default).
with(:users, :new, old_column.default)
expect(model).to receive(:update_column_in_batches) expect(model).to receive(:update_column_in_batches)
expect(model).to receive(:copy_indexes).with(:users, :old, :new) expect(model).to receive(:copy_indexes).with(:users, :old, :new)
...@@ -406,11 +408,13 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -406,11 +408,13 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
expect(model).to receive(:add_column). expect(model).to receive(:add_column).
with(:users, :new, :integer, with(:users, :new, :integer,
limit: old_column.limit, limit: old_column.limit,
default: old_column.default,
null: old_column.null, null: old_column.null,
precision: old_column.precision, precision: old_column.precision,
scale: old_column.scale) scale: old_column.scale)
expect(model).to receive(:change_column_default).
with(:users, :new, old_column.default)
expect(model).to receive(:update_column_in_batches) expect(model).to receive(:update_column_in_batches)
expect(model).to receive(:copy_indexes).with(:users, :old, :new) expect(model).to receive(:copy_indexes).with(:users, :old, :new)
......
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