Skip to content

Commit 340aa3e

Browse files
Fix regression of clean up degenerate deployments (#3596)
* If a foreign key constraint exists, first delete the child records, then delete the parent records. Co-authored-by: Philipp Thun <[email protected]>
1 parent 06ca46a commit 340aa3e

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

.github/workflows/unit_tests_backwards_compatibility.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ on:
1212
new_cc_ref:
1313
description: 'New Version of CC_NG that needs testing for backwards incompatible changes'
1414
required: true
15+
new_cc_repo:
16+
description: 'New REPO of CC_NG that needs testing for backwards incompatible changes'
17+
required: false
18+
default: 'cloudfoundry/cloud_controller_ng'
1519
pull_request:
1620
branches: [ main ]
1721
paths:
@@ -50,7 +54,12 @@ jobs:
5054
ref: ${{ (
5155
github.event_name == 'workflow_dispatch'
5256
&& github.event.inputs.new_cc_ref
53-
|| github.head_ref
57+
|| github.event.pull_request.head.ref
58+
)}}
59+
repository: ${{ (
60+
github.event_name == 'workflow_dispatch'
61+
&& github.event.inputs.new_cc_repo
62+
|| github.event.pull_request.head.repo.full_name
5463
)}}
5564
- name: Setup Environment
5665
uses: ./.github/workflows/composite/setup
@@ -94,7 +103,12 @@ jobs:
94103
ref: ${{ (
95104
github.event_name == 'workflow_dispatch'
96105
&& github.event.inputs.new_cc_ref
97-
|| github.head_ref
106+
|| github.event.pull_request.head.ref
107+
)}}
108+
repository: ${{ (
109+
github.event_name == 'workflow_dispatch'
110+
&& github.event.inputs.new_cc_repo
111+
|| github.event.pull_request.head.repo.full_name
98112
)}}
99113
- name: Setup Environment
100114
uses: ./.github/workflows/composite/setup

db/migrations/20231205143526_remove_deployments_with_degenerate.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
Sequel.migration do
22
up do
33
degenerate_records = self[:deployments].where(status_reason: 'DEGENERATE')
4-
degenerate_records.delete
4+
5+
if degenerate_records.count > 0
6+
guids_dataset = degenerate_records.select(:guid)
7+
self[:deployment_processes].where(deployment_guid: guids_dataset).delete
8+
self[:deployment_labels].where(resource_guid: guids_dataset).delete
9+
self[:deployment_annotations].where(resource_guid: guids_dataset).delete
10+
degenerate_records.delete
11+
end
512
end
613

714
down do

spec/migrations/20231205143526_remove_deployments_with_degenerate_spec.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@
88

99
describe 'deployments table' do
1010
it 'degenerate record is removed from deployments' do
11-
db[:deployments].insert(
12-
guid: 'bommel',
13-
original_web_process_instance_count: 1,
14-
status_reason: 'DEGENERATE'
15-
)
11+
db[:deployments].insert(guid: 'deployed_guid', original_web_process_instance_count: 1, status_reason: 'DEPLOYED')
12+
db[:deployment_processes].insert(guid: 'deployed_process_guid', deployment_guid: 'deployed_guid')
13+
db[:deployment_annotations].insert(guid: 'deployed_annotation_guid', resource_guid: 'deployed_guid')
14+
db[:deployment_labels].insert(guid: 'deployed_label_guid', resource_guid: 'deployed_guid')
15+
16+
db[:deployments].insert(guid: 'degenerate_guid', original_web_process_instance_count: 1, status_reason: 'DEGENERATE')
17+
db[:deployment_processes].insert(guid: 'degenerate_process_guid', deployment_guid: 'degenerate_guid')
18+
db[:deployment_annotations].insert(guid: 'degenerate_annotation_guid', resource_guid: 'degenerate_guid')
19+
db[:deployment_labels].insert(guid: 'degenerate_label_guid', resource_guid: 'degenerate_guid')
20+
21+
expect { db[:deployments].where(guid: 'degenerate_guid').delete }.to raise_error(Sequel::ForeignKeyConstraintViolation)
1622

1723
expect { Sequel::Migrator.run(db, migration_to_test, allow_missing_migration_files: true) }.not_to raise_error
18-
deployment = db[:deployments].first(status_reason: 'DEGENERATE')
19-
expect(deployment).to be_nil
24+
25+
expect(db[:deployments].where(status_reason: 'DEGENERATE').count).to eq(0)
26+
27+
expect(db[:deployments].first(guid: 'degenerate_guid')).to be_nil
28+
expect(db[:deployment_processes].first(guid: 'degenerate_process_guid')).to be_nil
29+
expect(db[:deployment_annotations].first(guid: 'degenerate_annotation_guid')).to be_nil
30+
expect(db[:deployment_labels].first(guid: 'degenerate_label_guid')).to be_nil
31+
32+
expect(db[:deployments].first(guid: 'deployed_guid')).not_to be_nil
33+
expect(db[:deployment_processes].first(guid: 'deployed_process_guid')).not_to be_nil
34+
expect(db[:deployment_annotations].first(guid: 'deployed_annotation_guid')).not_to be_nil
35+
expect(db[:deployment_labels].first(guid: 'deployed_label_guid')).not_to be_nil
2036
end
2137
end
2238
end

0 commit comments

Comments
 (0)