From f3bff9f7d3a5fb95ebac03623eb3381f73145b1c Mon Sep 17 00:00:00 2001 From: Katharina Przybill <30441792+kathap@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:57:15 +0200 Subject: [PATCH] add test for migration --- ...straint_quota_definitions_name_key_spec.rb | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 spec/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb 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 new file mode 100644 index 00000000000..07fd0e276d8 --- /dev/null +++ b/spec/migrations/20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb @@ -0,0 +1,81 @@ +require 'spec_helper' +require 'migrations/helpers/migration_shared_context' + +RSpec.describe 'migration to add or remove unique constraint on name column in quota_definitions table', isolation: :truncation, type: :migration do + include_context 'migration' do + let(:migration_filename) { '20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb' } + end + describe 'up migration' do + context '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 + end + + context 'unique constraint on name column does not exist' do + it 'does not fail' 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 + 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) + 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 + 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 + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) + end + end + end + end +end