From e8334f2ebb240a16eadd98e1ddb369101898f214 Mon Sep 17 00:00:00 2001 From: winebarrel Date: Sat, 24 Sep 2022 14:52:31 +0900 Subject: [PATCH] Support check const merge mode --- lib/ridgepole/diff.rb | 9 +++-- .../migrate_add_check_constraint_spec.rb | 36 +++++++++++++++++ .../migrate_change_check_constraint_spec.rb | 39 ++++++++++++++++++- .../migrate_drop_check_constraint_spec.rb | 35 ++++++++++++++++- .../migrate_add_check_constraint_spec.rb | 30 ++++++++++++++ .../migrate_change_check_constraint_spec.rb | 31 +++++++++++++++ .../migrate_drop_check_constraint_spec.rb | 27 +++++++++++++ 7 files changed, 201 insertions(+), 6 deletions(-) diff --git a/lib/ridgepole/diff.rb b/lib/ridgepole/diff.rb index bd94d864..783b872f 100644 --- a/lib/ridgepole/diff.rb +++ b/lib/ridgepole/diff.rb @@ -473,7 +473,6 @@ def scan_foreign_keys_change(from, to, table_delta, options) table_delta[:foreign_keys] = foreign_keys_delta unless foreign_keys_delta.empty? end - # NOTE: @options[:merge] not supported def scan_check_constraints_change(from, to, table_delta) from = (from || {}).dup to = (to || {}).dup @@ -496,9 +495,11 @@ def scan_check_constraints_change(from, to, table_delta) end end - from.each do |name, from_attrs| - check_constraints_delta[:delete] ||= {} - check_constraints_delta[:delete][name] = from_attrs + unless @options[:merge] + from.each do |name, from_attrs| + check_constraints_delta[:delete] ||= {} + check_constraints_delta[:delete][name] = from_attrs + end end table_delta[:check_constraints] = check_constraints_delta unless check_constraints_delta.empty? diff --git a/spec/mysql/migrate/migrate_add_check_constraint_spec.rb b/spec/mysql/migrate/migrate_add_check_constraint_spec.rb index bce3740e..27987469 100644 --- a/spec/mysql/migrate/migrate_add_check_constraint_spec.rb +++ b/spec/mysql/migrate/migrate_add_check_constraint_spec.rb @@ -36,4 +36,40 @@ expect(subject.dump).to match_ruby expected_dsl } end + + context 'when add check constraint' do + let(:actual_dsl) do + erbh(<<-ERB) + create_table "salaries", id: false, force: :cascade do |t| + t.integer "emp_no", null: false + t.integer "salary", null: false + t.date "from_date", null: false + t.date "to_date", null: false + end + ERB + end + + let(:expected_dsl) do + erbh(<<-ERB) + create_table "salaries", id: false, force: :cascade do |t| + t.integer "emp_no", null: false + t.integer "salary", null: false + t.date "from_date", null: false + t.date "to_date", null: false + t.check_constraint "`salary` > 100", name: "salary_check" + end + ERB + end + + before { subject.diff(actual_dsl).migrate } + subject { client(merge: true) } + + it { + delta = subject.diff(expected_dsl) + expect(delta.differ?).to be_truthy + expect(subject.dump).to match_ruby actual_dsl + delta.migrate + expect(subject.dump).to match_ruby expected_dsl + } + end end diff --git a/spec/mysql/migrate/migrate_change_check_constraint_spec.rb b/spec/mysql/migrate/migrate_change_check_constraint_spec.rb index 6c22407c..77a7f955 100644 --- a/spec/mysql/migrate/migrate_change_check_constraint_spec.rb +++ b/spec/mysql/migrate/migrate_change_check_constraint_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe 'Ridgepole::Client#diff -> migrate', condition: [['>= 6.1', :mysql80]] do - context 'when add check constraint' do + context 'when change check constraint' do let(:actual_dsl) do erbh(<<-ERB) create_table "salaries", id: false, force: :cascade do |t| @@ -37,4 +37,41 @@ expect(subject.dump).to match_ruby expected_dsl } end + + context 'when change check constraint (merge: true)' do + let(:actual_dsl) do + erbh(<<-ERB) + create_table "salaries", id: false, force: :cascade do |t| + t.integer "emp_no", null: false + t.integer "salary", null: false + t.date "from_date", null: false + t.date "to_date", null: false + t.check_constraint "`salary` > 100", name: "salary_check" + end + ERB + end + + let(:expected_dsl) do + erbh(<<-ERB) + create_table "salaries", id: false, force: :cascade do |t| + t.integer "emp_no", null: false + t.integer "salary", null: false + t.date "from_date", null: false + t.date "to_date", null: false + t.check_constraint "`salary` > 200", name: "salary_check" + end + ERB + end + + before { subject.diff(actual_dsl).migrate } + subject { client(marge: true) } + + it { + delta = subject.diff(expected_dsl) + expect(delta.differ?).to be_truthy + expect(subject.dump).to match_ruby actual_dsl + delta.migrate + expect(subject.dump).to match_ruby expected_dsl + } + end end diff --git a/spec/mysql/migrate/migrate_drop_check_constraint_spec.rb b/spec/mysql/migrate/migrate_drop_check_constraint_spec.rb index 9a66b5f8..1a232d0a 100644 --- a/spec/mysql/migrate/migrate_drop_check_constraint_spec.rb +++ b/spec/mysql/migrate/migrate_drop_check_constraint_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe 'Ridgepole::Client#diff -> migrate', condition: [['>= 6.1', :mysql80]] do - context 'when add check constraint' do + context 'when drop check constraint' do let(:actual_dsl) do erbh(<<-ERB) create_table "salaries", id: false, force: :cascade do |t| @@ -36,4 +36,37 @@ expect(subject.dump).to match_ruby expected_dsl } end + + context 'when drop check constraint (merge: true)' do + let(:actual_dsl) do + erbh(<<-ERB) + create_table "salaries", id: false, force: :cascade do |t| + t.integer "emp_no", null: false + t.integer "salary", null: false + t.date "from_date", null: false + t.date "to_date", null: false + t.check_constraint "`salary` > 100", name: "salary_check" + end + ERB + end + + let(:expected_dsl) do + erbh(<<-ERB) + create_table "salaries", id: false, force: :cascade do |t| + t.integer "emp_no", null: false + t.integer "salary", null: false + t.date "from_date", null: false + t.date "to_date", null: false + end + ERB + end + + before { subject.diff(actual_dsl).migrate } + subject { client(merge: true) } + + it { + delta = subject.diff(expected_dsl) + expect(delta.differ?).to be_falsy + } + end end diff --git a/spec/postgresql/migrate/migrate_add_check_constraint_spec.rb b/spec/postgresql/migrate/migrate_add_check_constraint_spec.rb index d9f7af18..f5bb4fa2 100644 --- a/spec/postgresql/migrate/migrate_add_check_constraint_spec.rb +++ b/spec/postgresql/migrate/migrate_add_check_constraint_spec.rb @@ -30,4 +30,34 @@ expect(subject.dump).to match_ruby expected_dsl } end + + context 'when add check constraint (merge: true)' do + let(:actual_dsl) do + erbh(<<-ERB) + create_table "clubs", force: :cascade do |t| + t.bigint "value", null: false + end + ERB + end + + let(:expected_dsl) do + erbh(<<-ERB) + create_table "clubs", force: :cascade do |t| + t.bigint "value", null: false + t.check_constraint "value > 100", name: "value_check" + end + ERB + end + + before { subject.diff(actual_dsl).migrate } + subject { client(merge: true) } + + it { + delta = subject.diff(expected_dsl) + expect(delta.differ?).to be_truthy + expect(subject.dump).to match_ruby actual_dsl + delta.migrate + expect(subject.dump).to match_ruby expected_dsl + } + end end diff --git a/spec/postgresql/migrate/migrate_change_check_constraint_spec.rb b/spec/postgresql/migrate/migrate_change_check_constraint_spec.rb index abface92..84d2cb7a 100644 --- a/spec/postgresql/migrate/migrate_change_check_constraint_spec.rb +++ b/spec/postgresql/migrate/migrate_change_check_constraint_spec.rb @@ -31,4 +31,35 @@ expect(subject.dump).to match_ruby expected_dsl } end + + context 'when change check constraint (merge: true)' do + let(:actual_dsl) do + erbh(<<-ERB) + create_table "clubs", force: :cascade do |t| + t.bigint "value", null: false + t.check_constraint "value > 0", name: "value_check" + end + ERB + end + + let(:expected_dsl) do + erbh(<<-ERB) + create_table "clubs", force: :cascade do |t| + t.bigint "value", null: false + t.check_constraint "value > 100", name: "value_check" + end + ERB + end + + before { subject.diff(actual_dsl).migrate } + subject { client(merge: true) } + + it { + delta = subject.diff(expected_dsl) + expect(delta.differ?).to be_truthy + expect(subject.dump).to match_ruby actual_dsl + delta.migrate + expect(subject.dump).to match_ruby expected_dsl + } + end end diff --git a/spec/postgresql/migrate/migrate_drop_check_constraint_spec.rb b/spec/postgresql/migrate/migrate_drop_check_constraint_spec.rb index 2e761970..b7720f0c 100644 --- a/spec/postgresql/migrate/migrate_drop_check_constraint_spec.rb +++ b/spec/postgresql/migrate/migrate_drop_check_constraint_spec.rb @@ -30,4 +30,31 @@ expect(subject.dump).to match_ruby expected_dsl } end + + context 'when drop check constraint (merge: true)' do + let(:actual_dsl) do + erbh(<<-ERB) + create_table "clubs", force: :cascade do |t| + t.bigint "value", null: false + t.check_constraint "value > 100", name: "value_check" + end + ERB + end + + let(:expected_dsl) do + erbh(<<-ERB) + create_table "clubs", force: :cascade do |t| + t.bigint "value", null: false + end + ERB + end + + before { subject.diff(actual_dsl).migrate } + subject { client(merge: true) } + + it { + delta = subject.diff(expected_dsl) + expect(delta.differ?).to be_falsy + } + end end