-
Notifications
You must be signed in to change notification settings - Fork 368
Replacing validate uniqueness with db constraint on RevisionSidecar model #4950
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
Open
serdarozerr
wants to merge
3
commits into
cloudfoundry:main
Choose a base branch
from
sap-contributions:feature/replace-validate-uniqueness-with-db-constrain-on-revision-sidecar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions
59
db/migrations/20260320141005_add_unique_constraint_to_revision_sidecars.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,59 @@ | ||
| Sequel.migration do | ||
| no_transaction # required for concurrently option on postgres | ||
|
|
||
| up do | ||
| transaction do | ||
| # remove duplicate entries if they exist | ||
| duplicates = self[:revision_sidecars]. | ||
| select(:revision_guid, :name). | ||
| group(:revision_guid, :name). | ||
| having { count(1) > 1 } | ||
|
|
||
| duplicates.each do |dup| | ||
| ids_to_remove = self[:revision_sidecars]. | ||
| where(revision_guid: dup[:revision_guid], name: dup[:name]). | ||
| select(:id). | ||
| order(:id). | ||
| offset(1). | ||
| map(:id) | ||
| self[:revision_sidecars].where(id: ids_to_remove).delete | ||
| end | ||
| end | ||
|
|
||
| if database_type == :postgres | ||
| VCAP::Migration.with_concurrent_timeout(self) do | ||
| add_index :revision_sidecars, %i[revision_guid name], | ||
| name: :revision_sidecars_revision_guid_name_index, | ||
| unique: true, | ||
| concurrently: true, | ||
| if_not_exists: true | ||
| end | ||
| else | ||
| alter_table(:revision_sidecars) do | ||
| # rubocop:disable Sequel/ConcurrentIndex -- MySQL does not support concurrent index operations | ||
| unless @db.indexes(:revision_sidecars).key?(:revision_sidecars_revision_guid_name_index) | ||
| add_index %i[revision_guid name], unique: true, | ||
| name: :revision_sidecars_revision_guid_name_index | ||
| end | ||
| # rubocop:enable Sequel/ConcurrentIndex | ||
| end | ||
| end | ||
| end | ||
|
|
||
| down do | ||
| if database_type == :postgres | ||
| VCAP::Migration.with_concurrent_timeout(self) do | ||
| drop_index :revision_sidecars, nil, | ||
| name: :revision_sidecars_revision_guid_name_index, | ||
| concurrently: true, | ||
| if_exists: true | ||
| end | ||
| else | ||
| alter_table(:revision_sidecars) do | ||
| # rubocop:disable Sequel/ConcurrentIndex -- MySQL does not support concurrent index operations | ||
| drop_index %i[revision_guid name], name: :revision_sidecars_revision_guid_name_index if @db.indexes(:revision_sidecars).key?(:revision_sidecars_revision_guid_name_index) | ||
| # rubocop:enable Sequel/ConcurrentIndex | ||
| end | ||
| end | ||
| end | ||
| end |
47 changes: 47 additions & 0 deletions
47
spec/migrations/20260320141005_add_unique_constraint_to_revision_sidecars_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,47 @@ | ||
| require 'spec_helper' | ||
| require 'migrations/helpers/migration_shared_context' | ||
| RSpec.describe 'add unique constraint to revision sidecar types', isolation: :truncation, type: :migration do | ||
| include_context 'migration' do | ||
| let(:migration_filename) { '20260320141005_add_unique_constraint_to_revision_sidecars.rb' } | ||
| end | ||
|
|
||
| let!(:app) { VCAP::CloudController::AppModel.make } | ||
| let!(:revision) { VCAP::CloudController::RevisionModel.make(:app) } | ||
|
|
||
| it 'remove dublicates, add constraint and revert migration' do | ||
| # ========================================================================================= | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't use this kind of comments in other places (or we should not use them) as it makes reading harder from my point of view. A single line comment is fine. |
||
| # SETUP: Create duplicate entries to test the de-duplication logic. | ||
| # ========================================================================================= | ||
| db[:revision_sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', revision_guid: revision.guid) | ||
| db[:revision_sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', revision_guid: revision.guid) | ||
| expect(db[:revision_sidecars].where(name: 'app', revision_guid: revision.guid).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[:revision_sidecars].where(name: 'app', revision_guid: revision.guid).count).to eq(1) | ||
| expect(db.indexes(:revision_sidecars)).to include(:revision_sidecars_revision_guid_name_index) | ||
| expect { db[:revision_sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', revision_guid: revision.guid) }.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(:revision_sidecars)).not_to include(:revision_sidecars_revision_guid_name_index) | ||
| expect { db[:revision_sidecars].insert(guid: SecureRandom.uuid, name: 'app', command: 'command', revision_guid: revision.guid) }.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
Oops, something went wrong.
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.
typo: duplicates