|
| 1 | +require 'spec_helper' |
| 2 | +require 'migrations/helpers/migration_shared_context' |
| 3 | + |
| 4 | +RSpec.describe 'migration to allow multiple service bindings', isolation: :truncation, type: :migration do |
| 5 | + include_context 'migration' do |
| 6 | + let(:migration_filename) { '20250731071027_allow_multiple_service_bindings.rb' } |
| 7 | + end |
| 8 | + |
| 9 | + describe 'service_bindings table' do |
| 10 | + context 'up migration' do |
| 11 | + it 'is in the correct state before migration' do |
| 12 | + expect(db.indexes(:service_bindings)).to include(:unique_service_binding_service_instance_guid_app_guid) |
| 13 | + expect(db.indexes(:service_bindings)).to include(:unique_service_binding_app_guid_name) |
| 14 | + expect(db.indexes(:service_bindings)).not_to include(:service_bindings_app_guid_service_instance_guid_index) |
| 15 | + expect(db.indexes(:service_bindings)).not_to include(:service_bindings_app_guid_name_index) |
| 16 | + end |
| 17 | + |
| 18 | + it 'migrates successfully' do |
| 19 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 20 | + expect(db.indexes(:service_bindings)).not_to include(:unique_service_binding_app_guid_name) |
| 21 | + expect(db.indexes(:service_bindings)).not_to include(:unique_service_binding_service_instance_guid_app_guid) |
| 22 | + expect(db.indexes(:service_bindings)).to include(:service_bindings_app_guid_service_instance_guid_index) |
| 23 | + expect(db.indexes(:service_bindings)).to include(:service_bindings_app_guid_name_index) |
| 24 | + end |
| 25 | + |
| 26 | + it 'does not fail if indexes/constraints are already in desired state' do |
| 27 | + db.alter_table :service_bindings do |
| 28 | + drop_constraint :unique_service_binding_service_instance_guid_app_guid |
| 29 | + drop_constraint :unique_service_binding_app_guid_name |
| 30 | + end |
| 31 | + if db.database_type == :postgres |
| 32 | + db.add_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, if_not_exists: true, concurrently: true |
| 33 | + db.add_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, if_not_exists: true, concurrently: true |
| 34 | + else |
| 35 | + db.add_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, if_not_exists: true |
| 36 | + db.add_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, if_not_exists: true |
| 37 | + end |
| 38 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'down migration' do |
| 43 | + it 'rolls back successfully' do |
| 44 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 45 | + expect(db.indexes(:service_bindings)).to include(:unique_service_binding_service_instance_guid_app_guid) |
| 46 | + expect(db.indexes(:service_bindings)).to include(:unique_service_binding_app_guid_name) |
| 47 | + expect(db.indexes(:service_bindings)).not_to include(:service_bindings_app_guid_service_instance_guid_index) |
| 48 | + expect(db.indexes(:service_bindings)).not_to include(:service_bindings_app_guid_name_index) |
| 49 | + end |
| 50 | + |
| 51 | + it 'does not fail if indexes/constraints are already in desired state' do |
| 52 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 53 | + db.alter_table :service_bindings do |
| 54 | + add_unique_constraint %i[service_instance_guid app_guid], name: :unique_service_binding_service_instance_guid_app_guid |
| 55 | + add_unique_constraint %i[app_guid name], name: :unique_service_binding_app_guid_name |
| 56 | + end |
| 57 | + if db.database_type == :postgres |
| 58 | + db.drop_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, if_exists: true, concurrently: true |
| 59 | + db.drop_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, if_exists: true, concurrently: true |
| 60 | + else |
| 61 | + db.drop_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, if_exists: true |
| 62 | + db.drop_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, if_exists: true |
| 63 | + end |
| 64 | + |
| 65 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments