|
| 1 | +require 'spec_helper' |
| 2 | +require 'migrations/helpers/migration_shared_context' |
| 3 | + |
| 4 | +RSpec.describe 'migration to add or remove unique constraint on name column in quota_definitions table', isolation: :truncation, type: :migration do |
| 5 | + include_context 'migration' do |
| 6 | + let(:migration_filename) { '20240808118000_drop_unique_constraint_quota_definitions_name_key_spec.rb' } |
| 7 | + end |
| 8 | + describe 'up migration' do |
| 9 | + context 'mysql' do |
| 10 | + before do |
| 11 | + skip if db.database_type != :mysql |
| 12 | + end |
| 13 | + |
| 14 | + it 'removes the unique constraint' do |
| 15 | + expect(db.indexes(:quota_definitions)).to include(:name) |
| 16 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 17 | + expect(db.indexes(:quota_definitions)).not_to include(:name) |
| 18 | + end |
| 19 | + |
| 20 | + context 'unique constraint on name column does not exist' do |
| 21 | + before do |
| 22 | + db.drop_index :quota_definitions, :name, name: :name |
| 23 | + end |
| 24 | + |
| 25 | + it 'does not throw an error' do |
| 26 | + expect(db.indexes(:quota_definitions)).not_to include(:name) |
| 27 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 28 | + expect(db.indexes(:quota_definitions)).not_to include(:name) |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context 'postgres' do |
| 34 | + before do |
| 35 | + skip if db.database_type != :postgres |
| 36 | + end |
| 37 | + |
| 38 | + it 'removes the unique constraint' do |
| 39 | + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) |
| 40 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 41 | + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) |
| 42 | + end |
| 43 | + |
| 44 | + context 'unique constraint on name column does not exist' do |
| 45 | + before do |
| 46 | + db.alter_table :quota_definitions do |
| 47 | + drop_constraint :quota_definitions_name_key |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + it 'does not throw an error' do |
| 52 | + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) |
| 53 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 54 | + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + describe 'down migration' do |
| 61 | + before do |
| 62 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) |
| 63 | + end |
| 64 | + |
| 65 | + context 'mysql' do |
| 66 | + before do |
| 67 | + skip if db.database_type != :mysql |
| 68 | + end |
| 69 | + |
| 70 | + it 'adds the unique constraint' do |
| 71 | + expect(db.indexes(:quota_definitions)).not_to include(:name) |
| 72 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 73 | + expect(db.indexes(:quota_definitions)).to include(:name) |
| 74 | + end |
| 75 | + |
| 76 | + context 'unique constraint on name column already exists' do |
| 77 | + before do |
| 78 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) |
| 79 | + |
| 80 | + db.alter_table :quota_definitions do |
| 81 | + add_index :name, name: :name |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + it 'does not fail' do |
| 86 | + expect(db.indexes(:quota_definitions)).to include(:name) |
| 87 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 88 | + expect(db.indexes(:quota_definitions)).to include(:name) |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context 'postgres' do |
| 94 | + before do |
| 95 | + skip if db.database_type != :postgres |
| 96 | + end |
| 97 | + |
| 98 | + it 'adds the unique constraint' do |
| 99 | + expect(db.indexes(:quota_definitions)).not_to include(:quota_definitions_name_key) |
| 100 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 101 | + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) |
| 102 | + end |
| 103 | + |
| 104 | + context 'unique constraint on name column already exists' do |
| 105 | + before do |
| 106 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) |
| 107 | + |
| 108 | + db.alter_table :quota_definitions do |
| 109 | + add_unique_constraint :name, name: :quota_definitions_name_key |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + it 'does not fail' do |
| 114 | + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) |
| 115 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 116 | + expect(db.indexes(:quota_definitions)).to include(:quota_definitions_name_key) |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments