Skip to content

Commit

Permalink
Fix regression of clean up degenerate deployments (#3596)
Browse files Browse the repository at this point in the history
* If a foreign key constraint exists, first delete the child records, then delete the parent records.

Co-authored-by: Philipp Thun <[email protected]>
  • Loading branch information
WeiQuan0605 and philippthun authored Jan 15, 2024
1 parent 06ca46a commit 340aa3e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/unit_tests_backwards_compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ on:
new_cc_ref:
description: 'New Version of CC_NG that needs testing for backwards incompatible changes'
required: true
new_cc_repo:
description: 'New REPO of CC_NG that needs testing for backwards incompatible changes'
required: false
default: 'cloudfoundry/cloud_controller_ng'
pull_request:
branches: [ main ]
paths:
Expand Down Expand Up @@ -50,7 +54,12 @@ jobs:
ref: ${{ (
github.event_name == 'workflow_dispatch'
&& github.event.inputs.new_cc_ref
|| github.head_ref
|| github.event.pull_request.head.ref
)}}
repository: ${{ (
github.event_name == 'workflow_dispatch'
&& github.event.inputs.new_cc_repo
|| github.event.pull_request.head.repo.full_name
)}}
- name: Setup Environment
uses: ./.github/workflows/composite/setup
Expand Down Expand Up @@ -94,7 +103,12 @@ jobs:
ref: ${{ (
github.event_name == 'workflow_dispatch'
&& github.event.inputs.new_cc_ref
|| github.head_ref
|| github.event.pull_request.head.ref
)}}
repository: ${{ (
github.event_name == 'workflow_dispatch'
&& github.event.inputs.new_cc_repo
|| github.event.pull_request.head.repo.full_name
)}}
- name: Setup Environment
uses: ./.github/workflows/composite/setup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
Sequel.migration do
up do
degenerate_records = self[:deployments].where(status_reason: 'DEGENERATE')
degenerate_records.delete

if degenerate_records.count > 0
guids_dataset = degenerate_records.select(:guid)
self[:deployment_processes].where(deployment_guid: guids_dataset).delete
self[:deployment_labels].where(resource_guid: guids_dataset).delete
self[:deployment_annotations].where(resource_guid: guids_dataset).delete
degenerate_records.delete
end
end

down do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@

describe 'deployments table' do
it 'degenerate record is removed from deployments' do
db[:deployments].insert(
guid: 'bommel',
original_web_process_instance_count: 1,
status_reason: 'DEGENERATE'
)
db[:deployments].insert(guid: 'deployed_guid', original_web_process_instance_count: 1, status_reason: 'DEPLOYED')
db[:deployment_processes].insert(guid: 'deployed_process_guid', deployment_guid: 'deployed_guid')
db[:deployment_annotations].insert(guid: 'deployed_annotation_guid', resource_guid: 'deployed_guid')
db[:deployment_labels].insert(guid: 'deployed_label_guid', resource_guid: 'deployed_guid')

db[:deployments].insert(guid: 'degenerate_guid', original_web_process_instance_count: 1, status_reason: 'DEGENERATE')
db[:deployment_processes].insert(guid: 'degenerate_process_guid', deployment_guid: 'degenerate_guid')
db[:deployment_annotations].insert(guid: 'degenerate_annotation_guid', resource_guid: 'degenerate_guid')
db[:deployment_labels].insert(guid: 'degenerate_label_guid', resource_guid: 'degenerate_guid')

expect { db[:deployments].where(guid: 'degenerate_guid').delete }.to raise_error(Sequel::ForeignKeyConstraintViolation)

expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
deployment = db[:deployments].first(status_reason: 'DEGENERATE')
expect(deployment).to be_nil

expect(db[:deployments].where(status_reason: 'DEGENERATE').count).to eq(0)

expect(db[:deployments].first(guid: 'degenerate_guid')).to be_nil
expect(db[:deployment_processes].first(guid: 'degenerate_process_guid')).to be_nil
expect(db[:deployment_annotations].first(guid: 'degenerate_annotation_guid')).to be_nil
expect(db[:deployment_labels].first(guid: 'degenerate_label_guid')).to be_nil

expect(db[:deployments].first(guid: 'deployed_guid')).not_to be_nil
expect(db[:deployment_processes].first(guid: 'deployed_process_guid')).not_to be_nil
expect(db[:deployment_annotations].first(guid: 'deployed_annotation_guid')).not_to be_nil
expect(db[:deployment_labels].first(guid: 'deployed_label_guid')).not_to be_nil
end
end
end

0 comments on commit 340aa3e

Please sign in to comment.