|
| 1 | +require 'spec_helper' |
| 2 | +require 'migrations/helpers/migration_shared_context' |
| 3 | +RSpec.describe 'add unique constraint to sidecars', isolation: :truncation, type: :migration do |
| 4 | + include_context 'migration' do |
| 5 | + let(:migration_filename) { '20260323092954_add_unique_constraint_to_sidecars.rb' } |
| 6 | + end |
| 7 | + |
| 8 | + let!(:app) { VCAP::CloudController::AppModel.make } |
| 9 | + |
| 10 | + it 'remove dublicates, add constraint and revert migration' do |
| 11 | + # ========================================================================================= |
| 12 | + # SETUP: Create duplicate entries to test the de-duplication logic. |
| 13 | + # ========================================================================================= |
| 14 | + db[:sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', app_guid: app.guid) |
| 15 | + db[:sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', app_guid: app.guid) |
| 16 | + expect(db[:sidecars].where(name: 'app', app_guid: app.guid).count).to eq(2) |
| 17 | + |
| 18 | + # ========================================================================================= |
| 19 | + # UP MIGRATION: Run the migration to apply the unique constraints. |
| 20 | + # ======================================================================================== |
| 21 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) |
| 22 | + |
| 23 | + # ========================================================================================= |
| 24 | + # ASSERT UP MIGRATION: Verify that duplicates are removed and constraints are enforced. |
| 25 | + # ========================================================================================= |
| 26 | + expect(db[:sidecars].where(name: 'app', app_guid: app.guid).count).to eq(1) |
| 27 | + expect(db.indexes(:sidecars)).to include(:sidecars_app_guid_name_index) |
| 28 | + expect { db[:sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', app_guid: app.guid) }.to raise_error(Sequel::UniqueConstraintViolation) |
| 29 | + |
| 30 | + # ========================================================================================= |
| 31 | + # TEST IDEMPOTENCY: Running the migration again should not cause any errors. |
| 32 | + # ========================================================================================= |
| 33 | + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error |
| 34 | + |
| 35 | + # ========================================================================================= |
| 36 | + # DOWN MIGRATION: Roll back the migration to remove the constraints. |
| 37 | + # ========================================================================================= |
| 38 | + Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) |
| 39 | + |
| 40 | + # ========================================================================================= |
| 41 | + # ASSERT DOWN MIGRATION: Verify that constraints are removed and duplicates can be re-inserted. |
| 42 | + # ========================================================================================= |
| 43 | + expect(db.indexes(:sidecars)).not_to include(:sidecars_app_guid_name_index) |
| 44 | + expect { db[:sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', app_guid: app.guid) }.not_to raise_error |
| 45 | + end |
| 46 | +end |
0 commit comments