Skip to content

Commit 81d35e4

Browse files
authored
Group migration specs to reduce runtime (#4790)
1 parent 9621038 commit 81d35e4

22 files changed

+739
-1414
lines changed

spec/migrations/20190712210940_backfill_status_for_deployments_spec.rb

Lines changed: 106 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -12,169 +12,117 @@
1212

1313
let(:app) { VCAP::CloudController::AppModel.make }
1414

15-
context 'when a deployment has state DEPLOYED' do
16-
let!(:deployment_with_state_deployed) do
17-
VCAP::CloudController::DeploymentModel.create(
18-
guid: 'with-state-deployed',
19-
state: VCAP::CloudController::DeploymentModel::DEPLOYED_STATE,
20-
app: app,
21-
original_web_process_instance_count: 1
22-
)
23-
end
24-
25-
it 'sets status_value to FINALIZED without changing the state' do
26-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
27-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_deployed.guid).first
28-
29-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYED_STATE)
30-
expect(deployment.status_value).to eq(VCAP::CloudController::DeploymentModel::FINALIZED_STATUS_VALUE)
31-
expect(deployment.status_reason).to be_nil
32-
end
33-
end
15+
it 'backfills status_value based on deployment state' do
16+
# Create all deployment variations
17+
deployment_deployed = VCAP::CloudController::DeploymentModel.create(
18+
guid: 'with-state-deployed',
19+
state: VCAP::CloudController::DeploymentModel::DEPLOYED_STATE,
20+
app: app,
21+
original_web_process_instance_count: 1
22+
)
3423

35-
context 'when a deployment has state CANCELED' do
36-
let!(:deployment_with_state_canceled) do
37-
VCAP::CloudController::DeploymentModel.create(
38-
guid: 'with-state-canceled',
39-
state: VCAP::CloudController::DeploymentModel::CANCELED_STATE,
40-
app: app,
41-
original_web_process_instance_count: 1
42-
)
43-
end
44-
45-
it 'sets status_value to FINALIZED without changing the state' do
46-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
47-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_canceled.guid).first
48-
49-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::CANCELED_STATE)
50-
expect(deployment.status_value).to eq(VCAP::CloudController::DeploymentModel::FINALIZED_STATUS_VALUE)
51-
expect(deployment.status_reason).to be_nil
52-
end
53-
end
24+
deployment_canceled = VCAP::CloudController::DeploymentModel.create(
25+
guid: 'with-state-canceled',
26+
state: VCAP::CloudController::DeploymentModel::CANCELED_STATE,
27+
app: app,
28+
original_web_process_instance_count: 1
29+
)
5430

55-
context 'when a deployment has state FAILED' do
56-
let!(:deployment_with_state_failed) do
57-
VCAP::CloudController::DeploymentModel.create(
58-
guid: 'with-state-failed',
59-
state: 'FAILED',
60-
app: app,
61-
original_web_process_instance_count: 1
62-
)
63-
end
64-
65-
it 'sets status_value to FINALIZED without changing the state' do
66-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
67-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_failed.guid).first
68-
69-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYED_STATE)
70-
expect(deployment.status_value).to eq(VCAP::CloudController::DeploymentModel::FINALIZED_STATUS_VALUE)
71-
expect(deployment.status_reason).to be_nil
72-
end
73-
end
31+
deployment_failed = VCAP::CloudController::DeploymentModel.create(
32+
guid: 'with-state-failed',
33+
state: 'FAILED',
34+
app: app,
35+
original_web_process_instance_count: 1
36+
)
7437

75-
context 'when a deployment has state DEPLOYING' do
76-
let!(:deployment_with_state_deploying) do
77-
VCAP::CloudController::DeploymentModel.create(
78-
guid: 'with-state-deploying',
79-
state: VCAP::CloudController::DeploymentModel::DEPLOYING_STATE,
80-
app: app,
81-
original_web_process_instance_count: 1
82-
)
83-
end
84-
85-
it 'sets status_value to DEPLOYING without changing the state' do
86-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
87-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_deploying.guid).first
88-
89-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYING_STATE)
90-
expect(deployment.status_value).to eq('DEPLOYING')
91-
expect(deployment.status_reason).to be_nil
92-
end
93-
end
38+
deployment_deploying = VCAP::CloudController::DeploymentModel.create(
39+
guid: 'with-state-deploying',
40+
state: VCAP::CloudController::DeploymentModel::DEPLOYING_STATE,
41+
app: app,
42+
original_web_process_instance_count: 1
43+
)
9444

95-
context 'when a deployment has state CANCELING' do
96-
let!(:deployment_with_state_canceling) do
97-
VCAP::CloudController::DeploymentModel.create(
98-
guid: 'with-state-canceling',
99-
state: VCAP::CloudController::DeploymentModel::CANCELING_STATE,
100-
app: app,
101-
original_web_process_instance_count: 1
102-
)
103-
end
104-
105-
it 'sets status_value to DEPLOYING without changing the state' do
106-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
107-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_canceling.guid).first
108-
109-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::CANCELING_STATE)
110-
expect(deployment.status_value).to eq('DEPLOYING')
111-
expect(deployment.status_reason).to be_nil
112-
end
113-
end
45+
deployment_canceling = VCAP::CloudController::DeploymentModel.create(
46+
guid: 'with-state-canceling',
47+
state: VCAP::CloudController::DeploymentModel::CANCELING_STATE,
48+
app: app,
49+
original_web_process_instance_count: 1
50+
)
11451

115-
context 'when a deployment has state FAILING' do
116-
let!(:deployment_with_state_failing) do
117-
VCAP::CloudController::DeploymentModel.create(
118-
guid: 'with-state-failing',
119-
state: 'FAILING',
120-
app: app,
121-
original_web_process_instance_count: 1
122-
)
123-
end
124-
125-
it 'sets status_value to DEPLOYING without changing the state' do
126-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
127-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_failing.guid).first
128-
129-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYING_STATE)
130-
expect(deployment.status_value).to eq('DEPLOYING')
131-
expect(deployment.status_reason).to be_nil
132-
end
133-
end
52+
deployment_failing = VCAP::CloudController::DeploymentModel.create(
53+
guid: 'with-state-failing',
54+
state: 'FAILING',
55+
app: app,
56+
original_web_process_instance_count: 1
57+
)
58+
59+
deployment_with_existing_status = VCAP::CloudController::DeploymentModel.create(
60+
guid: 'with-existing-status',
61+
state: VCAP::CloudController::DeploymentModel::DEPLOYED_STATE,
62+
status_value: 'foo',
63+
status_reason: 'bar',
64+
app: app,
65+
original_web_process_instance_count: 1
66+
)
67+
68+
deployment_failing_with_reason = VCAP::CloudController::DeploymentModel.create(
69+
guid: 'failing-with-reason',
70+
state: 'FAILING',
71+
status_value: 'foo',
72+
status_reason: 'bar',
73+
app: app,
74+
original_web_process_instance_count: 1
75+
)
13476

135-
context 'when the deployment already has a status' do
136-
context 'when the deployment has state DEPLOYED' do
137-
let!(:deployment_with_state_deployed) do
138-
VCAP::CloudController::DeploymentModel.create(
139-
guid: 'with-state-deployed',
140-
state: VCAP::CloudController::DeploymentModel::DEPLOYED_STATE,
141-
status_value: 'foo',
142-
status_reason: 'bar',
143-
app: app,
144-
original_web_process_instance_count: 1
145-
)
146-
end
147-
148-
it 'does not reset the status value' do
149-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
150-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_deployed.guid).first
151-
152-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYED_STATE)
153-
expect(deployment.status_value).to eq('foo')
154-
expect(deployment.status_reason).to eq('bar')
155-
end
156-
end
157-
158-
context 'when the deployment has state FAILING' do
159-
let!(:deployment_with_state_failing) do
160-
VCAP::CloudController::DeploymentModel.create(
161-
guid: 'with-state-deployed',
162-
state: 'FAILING',
163-
status_value: 'foo',
164-
status_reason: 'bar',
165-
app: app,
166-
original_web_process_instance_count: 1
167-
)
168-
end
169-
170-
it 'does not reset the status reason' do
171-
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
172-
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_state_failing.guid).first
173-
174-
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYING_STATE)
175-
expect(deployment.status_value).to eq('DEPLOYING')
176-
expect(deployment.status_reason).to eq('bar')
177-
end
178-
end
77+
# Run migration once
78+
Sequel::Migrator.run(VCAP::CloudController::DeploymentModel.db, tmp_migrations_dir, table: :my_fake_table)
79+
80+
# Test: DEPLOYED state -> FINALIZED status
81+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_deployed.guid).first
82+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYED_STATE)
83+
expect(deployment.status_value).to eq(VCAP::CloudController::DeploymentModel::FINALIZED_STATUS_VALUE)
84+
expect(deployment.status_reason).to be_nil
85+
86+
# Test: CANCELED state -> FINALIZED status
87+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_canceled.guid).first
88+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::CANCELED_STATE)
89+
expect(deployment.status_value).to eq(VCAP::CloudController::DeploymentModel::FINALIZED_STATUS_VALUE)
90+
expect(deployment.status_reason).to be_nil
91+
92+
# Test: FAILED state -> DEPLOYED state + FINALIZED status
93+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_failed.guid).first
94+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYED_STATE)
95+
expect(deployment.status_value).to eq(VCAP::CloudController::DeploymentModel::FINALIZED_STATUS_VALUE)
96+
expect(deployment.status_reason).to be_nil
97+
98+
# Test: DEPLOYING state -> DEPLOYING status
99+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_deploying.guid).first
100+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYING_STATE)
101+
expect(deployment.status_value).to eq('DEPLOYING')
102+
expect(deployment.status_reason).to be_nil
103+
104+
# Test: CANCELING state -> DEPLOYING status
105+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_canceling.guid).first
106+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::CANCELING_STATE)
107+
expect(deployment.status_value).to eq('DEPLOYING')
108+
expect(deployment.status_reason).to be_nil
109+
110+
# Test: FAILING state -> DEPLOYING state + DEPLOYING status
111+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_failing.guid).first
112+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYING_STATE)
113+
expect(deployment.status_value).to eq('DEPLOYING')
114+
expect(deployment.status_reason).to be_nil
115+
116+
# Test: existing status_value is not reset
117+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_with_existing_status.guid).first
118+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYED_STATE)
119+
expect(deployment.status_value).to eq('foo')
120+
expect(deployment.status_reason).to eq('bar')
121+
122+
# Test: existing status_reason is preserved
123+
deployment = VCAP::CloudController::DeploymentModel.where(guid: deployment_failing_with_reason.guid).first
124+
expect(deployment.state).to eq(VCAP::CloudController::DeploymentModel::DEPLOYING_STATE)
125+
expect(deployment.status_value).to eq('DEPLOYING')
126+
expect(deployment.status_reason).to eq('bar')
179127
end
180128
end

spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,35 @@
77
end
88

99
describe 'annotation tables' do
10-
it 'converts all legacy key_prefixes to annotations with prefixes in the key_prefix column' do
11-
db[:isolation_segments].insert(name: 'bommel', guid: '123')
12-
db[:isolation_segment_annotations].insert(
13-
guid: 'bommel',
14-
created_at: Time.now - 60,
15-
updated_at: Time.now - 60,
16-
resource_guid: '123',
17-
key: 'mylegacyprefix/mykey',
18-
value: 'some_value'
19-
)
20-
a1 = db[:isolation_segment_annotations].first(resource_guid: '123')
21-
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
22-
b1 = db[:isolation_segment_annotations].first(resource_guid: '123')
23-
expect(b1[:guid]).to eq a1[:guid]
24-
expect(b1[:created_at]).to eq a1[:created_at]
25-
expect(b1[:updated_at]).not_to eq a1[:updated_at]
26-
expect(b1[:resource_guid]).to eq a1[:resource_guid]
27-
expect(b1[:key_prefix]).not_to eq a1[:key_prefix]
28-
expect(b1[:key]).not_to eq a1[:key]
29-
expect(b1[:key_prefix]).to eq 'mylegacyprefix'
30-
expect(b1[:key]).to eq 'mykey'
31-
end
10+
it 'converts legacy key_prefixes to prefixes in key_prefix column and leaves non-legacy values unchanged' do
11+
resource_guid = 'iso-seg-guid'
12+
db[:isolation_segments].insert(name: 'iso_seg', guid: resource_guid)
13+
db[:isolation_segment_annotations].insert(guid: 'anno-1-guid', resource_guid: resource_guid, key: 'mylegacyprefix/mykey', value: 'some_value')
14+
db[:isolation_segment_annotations].insert(guid: 'anno-2-guid', resource_guid: resource_guid, key_prefix: 'myprefix', key: 'mykey', value: 'some_value')
15+
db[:isolation_segment_annotations].insert(guid: 'anno-3-guid', resource_guid: resource_guid, key: 'yourkey', value: 'some_other_value')
16+
17+
anno1 = db[:isolation_segment_annotations].first(guid: 'anno-1-guid')
18+
anno2 = db[:isolation_segment_annotations].first(key: 'mykey')
19+
anno3 = db[:isolation_segment_annotations].first(key: 'yourkey')
3220

33-
it 'doesnt touch any values that have no legacy key_prefix in its key field' do
34-
db[:isolation_segments].insert(name: 'bommel', guid: '123')
35-
db[:isolation_segment_annotations].insert(guid: 'bommel', resource_guid: '123', key_prefix: 'myprefix', key: 'mykey', value: 'some_value')
36-
db[:isolation_segment_annotations].insert(guid: 'bommel2', resource_guid: '123', key: 'mykey2', value: 'some_value2')
37-
b1 = db[:isolation_segment_annotations].first(key: 'mykey')
38-
b2 = db[:isolation_segment_annotations].first(key: 'mykey2')
3921
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
40-
c1 = db[:isolation_segment_annotations].first(key: 'mykey')
41-
c2 = db[:isolation_segment_annotations].first(key: 'mykey2')
42-
expect(b1.values).to eq(c1.values)
43-
expect(b2.values).to eq(c2.values)
22+
23+
# Check legacy prefix was converted
24+
anno1_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-1-guid')
25+
expect(anno1_after_mig[:guid]).to eq anno1[:guid]
26+
expect(anno1_after_mig[:created_at]).to eq anno1[:created_at]
27+
expect(anno1_after_mig[:updated_at]).not_to eq anno1[:updated_at]
28+
expect(anno1_after_mig[:resource_guid]).to eq anno1[:resource_guid]
29+
expect(anno1_after_mig[:key_prefix]).not_to eq anno1[:key_prefix]
30+
expect(anno1_after_mig[:key]).not_to eq anno1[:key]
31+
expect(anno1_after_mig[:key_prefix]).to eq 'mylegacyprefix'
32+
expect(anno1_after_mig[:key]).to eq 'mykey'
33+
34+
# Check non-legacy values unchanged
35+
anno2_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-2-guid')
36+
anno3_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-3-guid')
37+
expect(anno2.values).to eq(anno2_after_mig.values)
38+
expect(anno3.values).to eq(anno3_after_mig.values)
4439
end
4540
end
4641
end

0 commit comments

Comments
 (0)