Support jsonb Default Value in add_column_with_default Migration Helper

This commit is contained in:
Jason Goodman 2019-06-28 04:48:57 +00:00 committed by Thong Kuah
parent 732ee60693
commit 96b116d099
3 changed files with 25 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
title: Support jsonb default in add_column_with_default migration helper
merge_request: 29871
author:
type: other

View File

@ -434,7 +434,8 @@ module Gitlab
end
begin
update_column_in_batches(table, column, default, &block)
default_after_type_cast = connection.type_cast(default, column_for(table, column))
update_column_in_batches(table, column, default_after_type_cast, &block)
change_column_null(table, column, false) unless allow_null
# We want to rescue _all_ exceptions here, even those that don't inherit

View File

@ -583,6 +583,24 @@ describe Gitlab::Database::MigrationHelpers do
model.add_column_with_default(:projects, :foo, :integer, default: 10, limit: 8)
end
end
it 'adds a column with an array default value for a jsonb type' do
create(:project)
allow(model).to receive(:transaction_open?).and_return(false)
allow(model).to receive(:transaction).and_yield
expect(model).to receive(:update_column_in_batches).with(:projects, :foo, '[{"foo":"json"}]').and_call_original
model.add_column_with_default(:projects, :foo, :jsonb, default: [{ foo: "json" }])
end
it 'adds a column with an object default value for a jsonb type' do
create(:project)
allow(model).to receive(:transaction_open?).and_return(false)
allow(model).to receive(:transaction).and_yield
expect(model).to receive(:update_column_in_batches).with(:projects, :foo, '{"foo":"json"}').and_call_original
model.add_column_with_default(:projects, :foo, :jsonb, default: { foo: "json" })
end
end
context 'inside a transaction' do