|
| 1 | +require 'spec_helper' |
| 2 | +require 'migrations/helpers/migration_shared_context' |
| 3 | + |
| 4 | +RSpec.describe 'route bindings unique index', isolation: :truncation, type: :migration do |
| 5 | + include_context 'migration' do |
| 6 | + let(:migration_filename) { '20251028135214_route_bindings_unique_index.rb' } |
| 7 | + end |
| 8 | + |
| 9 | + let(:space) { VCAP::CloudController::Space.make } |
| 10 | + let(:service_instance_1) { VCAP::CloudController::ServiceInstance.make(space:) } |
| 11 | + let(:service_instance_2) { VCAP::CloudController::ServiceInstance.make(space:) } |
| 12 | + let(:route_1) { VCAP::CloudController::Route.make(space:) } |
| 13 | + let(:route_2) { VCAP::CloudController::Route.make(space:) } |
| 14 | + |
| 15 | + describe 'route_bindings table' do |
| 16 | + context 'up migration' do |
| 17 | + it 'is in the correct state before migration' do |
| 18 | + expect(db.indexes(:route_bindings)).not_to include(:route_bindings_route_id_service_instance_id_index) |
| 19 | + end |
| 20 | + |
| 21 | + it 'removes duplicates and migrates successfully by adding unique index' do |
| 22 | + db[:route_bindings].insert(route_id: route_1.id, service_instance_id: service_instance_1.id, guid: SecureRandom.uuid) |
| 23 | + db[:route_bindings].insert(route_id: route_1.id, service_instance_id: service_instance_1.id, guid: SecureRandom.uuid) |
| 24 | + db[:route_bindings].insert(route_id: route_2.id, service_instance_id: service_instance_1.id, guid: SecureRandom.uuid) |
| 25 | + db[:route_bindings].insert(route_id: route_1.id, service_instance_id: service_instance_2.id, guid: SecureRandom.uuid) |
| 26 | + db[:route_bindings].insert(route_id: route_2.id, service_instance_id: service_instance_2.id, guid: SecureRandom.uuid) |
| 27 | + db[:route_bindings].insert(route_id: route_2.id, service_instance_id: service_instance_2.id, guid: SecureRandom.uuid) |
| 28 | + db[:route_bindings].insert(route_id: route_2.id, service_instance_id: service_instance_2.id, guid: SecureRandom.uuid) |
| 29 | + |
| 30 | + # Count duplicates before migration |
| 31 | + expect(db[:route_bindings].where(service_instance_id: service_instance_1.id, route_id: route_1.id).count).to eq(2) |
| 32 | + expect(db[:route_bindings].where(service_instance_id: service_instance_1.id, route_id: route_2.id).count).to eq(1) |
| 33 | + expect(db[:route_bindings].where(service_instance_id: service_instance_2.id, route_id: route_1.id).count).to eq(1) |
| 34 | + expect(db[:route_bindings].where(service_instance_id: service_instance_2.id, route_id: route_2.id).count).to eq(3) |
| 35 | + |
| 36 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 37 | + |
| 38 | + # Verify duplicates are removed after migration |
| 39 | + expect(db[:route_bindings].where(service_instance_id: service_instance_1.id, route_id: route_1.id).count).to eq(1) |
| 40 | + expect(db[:route_bindings].where(service_instance_id: service_instance_1.id, route_id: route_2.id).count).to eq(1) |
| 41 | + expect(db[:route_bindings].where(service_instance_id: service_instance_2.id, route_id: route_1.id).count).to eq(1) |
| 42 | + expect(db[:route_bindings].where(service_instance_id: service_instance_2.id, route_id: route_2.id).count).to eq(1) |
| 43 | + |
| 44 | + # Verify index is added |
| 45 | + expect(db.indexes(:route_bindings)).to include(:route_bindings_route_id_service_instance_id_index) |
| 46 | + end |
| 47 | + |
| 48 | + it 'does not fail if indexes/constraints are already in desired state' do |
| 49 | + db.alter_table(:route_bindings) { add_index %i[route_id service_instance_id], unique: true, name: :route_bindings_route_id_service_instance_id_index } |
| 50 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context 'down migration' do |
| 55 | + it 'rolls back successfully' do |
| 56 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 57 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error |
| 58 | + expect(db.indexes(:route_bindings)).not_to include(:route_bindings_route_id_service_instance_id_index) |
| 59 | + expect(db.indexes(:route_bindings)).to include(:route_id) if db.database_type == :mysql |
| 60 | + end |
| 61 | + |
| 62 | + it 'does not fail if indexes/constraints are already in desired state' do |
| 63 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 64 | + db.alter_table(:route_bindings) { drop_index %i[route_id service_instance_id], unique: true, name: :route_bindings_route_id_service_instance_id_index } |
| 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