From e8c08a7b31314373b50206c22f52a88819e25e86 Mon Sep 17 00:00:00 2001 From: Sven Krieger <37476281+svkrieger@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:14:38 +0200 Subject: [PATCH] Adjust migration to support mysql 5.7 --- ...e_constraint_quota_definitions_name_key.rb | 36 ++++---- ...straint_quota_definitions_name_key_spec.rb | 87 +++++++++++-------- 2 files changed, 71 insertions(+), 52 deletions(-) diff --git a/db/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key.rb b/db/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key.rb index 4bd8074c64e..e7f714303c9 100644 --- a/db/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key.rb +++ b/db/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key.rb @@ -1,10 +1,12 @@ Sequel.migration do up do - if self.class.name.match?(/mysql/i) - alter_table :quota_definitions do - drop_constraint :name, if_exists: true + if database_type == :mysql + if self.indexes(:quota_definitions).include?(:name) + alter_table :quota_definitions do + drop_index :name, name: :name + end end - elsif self.class.name.match?(/postgres/i) + elsif database_type == :postgres alter_table :quota_definitions do drop_constraint :quota_definitions_name_key, if_exists: true end @@ -12,19 +14,23 @@ end down do - if self.class.name.match?(/mysql/i) - # mysql 5 is not so smart as mysql 8, prevent Mysql2::Error: Duplicate key name 'name' - alter_table :quota_definitions do - # rubocop:disable Sequel/ConcurrentIndex - drop_index :name, name: :name if @db.indexes(:quota_definitions).include?(:name) - # rubocop:enable Sequel/ConcurrentIndex - end - alter_table :quota_definitions do - add_unique_constraint :name, name: :name, if_not_exists: true + if database_type == :mysql + unless self.indexes(:quota_definitions).include?(:name) + + # if self.server_version < 6000 + # sql_mode = self.fetch("SELECT @@sql_mode").first[:@@sql_mode] + # modes = sql_mode.split(',') + # modes.delete('STRICT_TRANS_TABLES') + # self.run("SET SESSION sql_mode='#{modes.join(',')}'") + # end + + alter_table :quota_definitions do + add_index :name, name: :name, unique: true + end end - elsif self.class.name.match?(/postgres/i) + elsif database_type == :postgres alter_table :quota_definitions do - add_unique_constraint :name, name: :quota_definitions_name_key, if_not_exists: true + add_index :name, name: :quota_definitions_name_key, if_not_exists: true end end end diff --git a/spec/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb b/spec/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb index 4b762ca1c09..4354d3f09bd 100644 --- a/spec/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb +++ b/spec/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb @@ -6,70 +6,83 @@ let(:migration_filename) { '20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb' } end describe 'up migration' do - context 'the unique constraint on name column exists' do - it 'removes the unique constraint' do - if db.database_type == :mysql - expect(db.indexes(:quota_definitions)).to include(:name) - elsif db.database_type == :postgres - expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) - end - expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error - if db.database_type == :mysql - expect(db.indexes(:quota_definitions)).not_to include(:name) - elsif db.database_type == :postgres - expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) - end + it 'removes the unique constraint' do + if db.database_type == :mysql + expect(db.indexes(:quota_definitions)).to include(:name) + elsif db.database_type == :postgres + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) end - context 'the unique constraint on name column does not exist' do - it 'does not throw an error' do - expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error - if db.database_type == :mysql - expect(db.indexes(:quota_definitions)).not_to include(:name) - elsif db.database_type == :postgres - expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) - end - expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error - if db.database_type == :mysql - expect(db.indexes(:quota_definitions)).not_to include(:name) - elsif db.database_type == :postgres - expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) - end - end + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error + + if db.database_type == :mysql + expect(db.indexes(:quota_definitions)).not_to include(:name) + elsif db.database_type == :postgres + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) end end - end - describe 'down migration' do context 'unique constraint on name column does not exist' do before do - Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) + db.drop_index :quota_definitions, :name, name: :name end - it 'adds the unique constraint' do + it 'does not throw an error' do if db.database_type == :mysql expect(db.indexes(:quota_definitions)).not_to include(:name) elsif db.database_type == :postgres expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) end - expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error + + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error + if db.database_type == :mysql - expect(db.indexes(:quota_definitions)).to include(:name) + expect(db.indexes(:quota_definitions)).not_to include(:name) elsif db.database_type == :postgres - expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) end end end + end + + describe 'down migration' do + before do + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) + end + + it 'adds the unique constraint' do + if db.database_type == :mysql + expect(db.indexes(:quota_definitions)).not_to include(:name) + elsif db.database_type == :postgres + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) + end + + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error + + if db.database_type == :mysql + expect(db.indexes(:quota_definitions)).to include(:name) + elsif db.database_type == :postgres + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) + end + end + + context 'unique constraint on name column already exists' do + before do + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) + db.alter_table :quota_definitions do + add_index :name, name: :name + end + end - context 'unique constraint on name column does exist' do it 'does not fail' do - expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error if db.database_type == :mysql expect(db.indexes(:quota_definitions)).to include(:name) elsif db.database_type == :postgres expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) end + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error + if db.database_type == :mysql expect(db.indexes(:quota_definitions)).to include(:name) elsif db.database_type == :postgres