-
Notifications
You must be signed in to change notification settings - Fork 368
Add unique constraint for sidecar process types #4632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
db/migrations/20251030100000_add_unique_constraint_to_sidecar_process_types.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| Sequel.migration do | ||
| up do | ||
| transaction do | ||
| # Clean up sidecar_process_types | ||
| duplicates = self[:sidecar_process_types]. | ||
| select(:sidecar_guid, :type). | ||
| group(:sidecar_guid, :type). | ||
| having { count(1) > 1 } | ||
|
|
||
| duplicates.each do |dup| | ||
| pks_to_remove = self[:sidecar_process_types]. | ||
| where(sidecar_guid: dup[:sidecar_guid], type: dup[:type]). | ||
| select(:id). | ||
| order(:id). | ||
| offset(1). | ||
| map(:id) | ||
|
|
||
| self[:sidecar_process_types].where(id: pks_to_remove).delete | ||
| end | ||
|
|
||
| alter_table(:sidecar_process_types) do | ||
| unless @db.indexes(:sidecar_process_types).key?(:sidecar_process_types_sidecar_guid_type_index) | ||
| add_unique_constraint %i[sidecar_guid type], | ||
| name: :sidecar_process_types_sidecar_guid_type_index | ||
| end | ||
| end | ||
| end | ||
|
|
||
| transaction do | ||
| # Clean up revision_sidecar_process_types | ||
| duplicates = self[:revision_sidecar_process_types]. | ||
| select(:revision_sidecar_guid, :type). | ||
| group(:revision_sidecar_guid, :type). | ||
| having { count(1) > 1 } | ||
|
|
||
| duplicates.each do |dup| | ||
| pks_to_remove = self[:revision_sidecar_process_types]. | ||
| where(revision_sidecar_guid: dup[:revision_sidecar_guid], type: dup[:type]). | ||
| select(:id). | ||
| order(:id). | ||
| offset(1). | ||
| map(:id) | ||
|
|
||
| self[:revision_sidecar_process_types].where(id: pks_to_remove).delete | ||
| end | ||
|
|
||
| alter_table(:revision_sidecar_process_types) do | ||
| unless @db.indexes(:revision_sidecar_process_types).key?(:revision_sidecar_process_types_revision_sidecar_guid_type_index) | ||
| add_unique_constraint %i[revision_sidecar_guid type], | ||
| name: :revision_sidecar_process_types_revision_sidecar_guid_type_index | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| down do | ||
| alter_table(:sidecar_process_types) do | ||
| drop_constraint(:sidecar_process_types_sidecar_guid_type_index, type: :unique) if @db.indexes(:sidecar_process_types).key?(:sidecar_process_types_sidecar_guid_type_index) | ||
| end | ||
|
|
||
| alter_table(:revision_sidecar_process_types) do | ||
| if @db.indexes(:revision_sidecar_process_types).key?(:revision_sidecar_process_types_revision_sidecar_guid_type_index) | ||
| drop_constraint(:revision_sidecar_process_types_revision_sidecar_guid_type_index, | ||
| type: :unique) | ||
| end | ||
| end | ||
| end | ||
| end |
61 changes: 61 additions & 0 deletions
61
spec/migrations/20251030100000_add_unique_constraint_to_sidecar_process_types_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| require 'spec_helper' | ||
| require 'migrations/helpers/migration_shared_context' | ||
|
|
||
| RSpec.describe 'add unique constraint to sidecar process types', isolation: :truncation, type: :migration do | ||
| include_context 'migration' do | ||
| let(:migration_filename) { '20251030100000_add_unique_constraint_to_sidecar_process_types.rb' } | ||
| end | ||
|
|
||
| let!(:app) { VCAP::CloudController::AppModel.make } | ||
| let!(:sidecar) { VCAP::CloudController::SidecarModel.make(app:) } | ||
| let!(:revision) { VCAP::CloudController::RevisionModel.make(app:) } | ||
| let!(:revision_sidecar) { VCAP::CloudController::RevisionSidecarModel.make(revision:) } | ||
|
|
||
| it 'removes duplicates, adds unique constraints, and is reversible' do | ||
| # ========================================================================================= | ||
| # SETUP: Create duplicate entries for both tables to test the de-duplication logic. | ||
| # ========================================================================================= | ||
| db[:sidecar_process_types].insert(sidecar_guid: sidecar.guid, type: 'web', app_guid: app.guid, guid: SecureRandom.uuid) | ||
| db[:sidecar_process_types].insert(sidecar_guid: sidecar.guid, type: 'web', app_guid: app.guid, guid: SecureRandom.uuid) | ||
| expect(db[:sidecar_process_types].where(sidecar_guid: sidecar.guid, type: 'web').count).to eq(2) | ||
|
|
||
| db[:revision_sidecar_process_types].insert(revision_sidecar_guid: revision_sidecar.guid, type: 'worker', guid: SecureRandom.uuid) | ||
| db[:revision_sidecar_process_types].insert(revision_sidecar_guid: revision_sidecar.guid, type: 'worker', guid: SecureRandom.uuid) | ||
| expect(db[:revision_sidecar_process_types].where(revision_sidecar_guid: revision_sidecar.guid, type: 'worker').count).to eq(2) | ||
|
|
||
| # ========================================================================================= | ||
| # UP MIGRATION: Run the migration to apply the unique constraints. | ||
| # ========================================================================================= | ||
| Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) | ||
|
|
||
| # ========================================================================================= | ||
| # ASSERT UP MIGRATION: Verify that duplicates are removed and constraints are enforced. | ||
| # ========================================================================================= | ||
| expect(db[:sidecar_process_types].where(sidecar_guid: sidecar.guid, type: 'web').count).to eq(1) | ||
| expect(db.indexes(:sidecar_process_types)).to include(:sidecar_process_types_sidecar_guid_type_index) | ||
| expect { db[:sidecar_process_types].insert(sidecar_guid: sidecar.guid, type: 'web', app_guid: app.guid, guid: SecureRandom.uuid) }.to raise_error(Sequel::UniqueConstraintViolation) | ||
|
|
||
| expect(db[:revision_sidecar_process_types].where(revision_sidecar_guid: revision_sidecar.guid, type: 'worker').count).to eq(1) | ||
| expect(db.indexes(:revision_sidecar_process_types)).to include(:revision_sidecar_process_types_revision_sidecar_guid_type_index) | ||
| expect { db[:revision_sidecar_process_types].insert(revision_sidecar_guid: revision_sidecar.guid, type: 'worker', guid: SecureRandom.uuid) }.to raise_error(Sequel::UniqueConstraintViolation) | ||
|
|
||
| # ========================================================================================= | ||
| # TEST IDEMPOTENCY: Running the migration again should not cause any errors. | ||
| # ========================================================================================= | ||
| expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error | ||
|
|
||
| # ========================================================================================= | ||
| # DOWN MIGRATION: Roll back the migration to remove the constraints. | ||
| # ========================================================================================= | ||
| Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) | ||
|
|
||
| # ========================================================================================= | ||
| # ASSERT DOWN MIGRATION: Verify that constraints are removed and duplicates can be re-inserted. | ||
| # ========================================================================================= | ||
| expect(db.indexes(:sidecar_process_types)).not_to include(:sidecar_process_types_sidecar_guid_type_index) | ||
| expect { db[:sidecar_process_types].insert(sidecar_guid: sidecar.guid, type: 'web', app_guid: app.guid, guid: SecureRandom.uuid) }.not_to raise_error | ||
|
|
||
| expect(db.indexes(:revision_sidecar_process_types)).not_to include(:revision_sidecar_process_types_revision_sidecar_guid_type_index) | ||
| expect { db[:revision_sidecar_process_types].insert(revision_sidecar_guid: revision_sidecar.guid, type: 'worker', guid: SecureRandom.uuid) }.not_to raise_error | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to print different error messages for "revision" sidecars? (I guess not...)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same error message as before (see deleted line 13). As the user might be unaware of revisions I'd rather keep it out