diff options
author | Thong Kuah <tkuah@gitlab.com> | 2019-06-28 04:48:57 +0000 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2019-06-28 04:48:57 +0000 |
commit | ae68c7ea142b12fe179a4027b17d31ac6fb2649c (patch) | |
tree | e558ec7fc798f354884e02343beb473e07c96732 | |
parent | 732ee60693c5986f9041b05665588306eefd69d9 (diff) | |
parent | 96b116d0998df5b026b168e686add915fdf2e719 (diff) | |
download | gitlab-ce-ae68c7ea142b12fe179a4027b17d31ac6fb2649c.tar.gz |
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
-rw-r--r-- | changelogs/unreleased/support-jsonb-default-value.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/database/migration_helpers.rb | 3 | ||||
-rw-r--r-- | spec/lib/gitlab/database/migration_helpers_spec.rb | 18 |
3 files changed, 25 insertions, 1 deletions
diff --git a/changelogs/unreleased/support-jsonb-default-value.yml b/changelogs/unreleased/support-jsonb-default-value.yml new file mode 100644 index 00000000000..d46156276f9 --- /dev/null +++ b/changelogs/unreleased/support-jsonb-default-value.yml @@ -0,0 +1,5 @@ +--- +title: Support jsonb default in add_column_with_default migration helper +merge_request: 29871 +author: +type: other diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb index 0b12e862ded..e2cbf91f281 100644 --- a/lib/gitlab/database/migration_helpers.rb +++ b/lib/gitlab/database/migration_helpers.rb @@ -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 diff --git a/spec/lib/gitlab/database/migration_helpers_spec.rb b/spec/lib/gitlab/database/migration_helpers_spec.rb index 3cf3d032bf4..7409572288c 100644 --- a/spec/lib/gitlab/database/migration_helpers_spec.rb +++ b/spec/lib/gitlab/database/migration_helpers_spec.rb @@ -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 |