Commit ae68c7ea authored by Thong Kuah's avatar Thong Kuah

Merge branch 'support-jsonb-default-value' into 'master'

Support jsonb Default Value in add_column_with_default Migration Helper

See merge request gitlab-org/gitlab-ce!29871
parents 732ee606 96b116d0
---
title: Support jsonb default in add_column_with_default migration helper
merge_request: 29871
author:
type: other
......@@ -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
......
......@@ -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
......
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