Skip to content

Commit 9161ff7

Browse files
authored
Enforce lifecycle_type on apps, droplets, and builds (#5402)
* Enforce lifecycle_type on apps, droplets, and builds * Use zero-downtime constraint pattern for lifecycle_type NOT NULL migration * Fix Postgres migration to SET NOT NULL on column after validating constraint The zero-downtime pattern requires SET NOT NULL after VALIDATE CONSTRAINT; without it db.schema still reports allow_null: true causing the spec to fail. * Set column default before adding NOT NULL constraint * Address review comments
1 parent 6a7105b commit 9161ff7

21 files changed

Lines changed: 183 additions & 215 deletions

‎app/actions/build_create.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def create_and_stage(package:, lifecycle:, metadata: nil, start_after_staging: f
5454
state: BuildModel::STAGING_STATE,
5555
package_guid: package.guid,
5656
app: package.app,
57+
lifecycle_type: lifecycle.type,
5758
staging_memory_in_mb: staging_details.staging_memory_in_mb,
5859
staging_disk_in_mb: staging_details.staging_disk_in_mb,
5960
staging_log_rate_limit: staging_details.staging_log_rate_limit_bytes_per_second,

‎app/actions/droplet_copy.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def copy(destination_app, user_audit_info)
2020
raise InvalidCopyError.new('source droplet is not staged') unless @source_droplet.staged?
2121
raise InvalidCopyError.new('source and destination lifecycle types do not match') unless @source_droplet.lifecycle_type == destination_app.lifecycle_type
2222

23-
new_droplet = DropletModel.new(state: DropletModel::COPYING_STATE, app: destination_app)
23+
new_droplet = DropletModel.new(state: DropletModel::COPYING_STATE, app: destination_app, lifecycle_type: destination_app.lifecycle_type)
2424

2525
# Needed to execute serializers and deserializers correctly on source and destination models
2626
CLONED_ATTRIBUTES.each do |attr|

‎app/actions/droplet_create.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def create(app, message, user_audit_info)
1414
droplet = DropletModel.new(
1515
app: app,
1616
state: DropletModel::AWAITING_UPLOAD_STATE,
17+
lifecycle_type: BuildpackLifecycleDataModel::LIFECYCLE_TYPE,
1718
process_types: message.process_types || DEFAULT_PROCESS_TYPES,
1819
execution_metadata: ''
1920
)
@@ -87,6 +88,7 @@ def droplet_from_build(build)
8788
app: build.app,
8889
package_guid: build.package_guid,
8990
state: DropletModel::STAGING_STATE,
91+
lifecycle_type: build.lifecycle_type,
9092
build: build
9193
)
9294
end

‎app/controllers/runtime/apps_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def upload_droplet(guid)
194194

195195
enqueued_job = nil
196196
DropletModel.db.transaction do
197-
droplet = DropletModel.create(app: process.app, state: DropletModel::PROCESSING_UPLOAD_STATE)
197+
droplet = DropletModel.create(app: process.app, state: DropletModel::PROCESSING_UPLOAD_STATE, lifecycle_type: BuildpackLifecycleDataModel::LIFECYCLE_TYPE)
198198
BuildpackLifecycleDataModel.create(droplet:)
199199

200200
droplet_upload_job = Jobs::V2::UploadDropletFromUser.new(droplet_path, droplet.guid)

‎app/fetchers/app_list_fetcher.rb‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,7 @@ def filter(message, dataset)
3333
dataset = dataset.where(guid: buildpack_lifecycle_data_dataset.select(:app_guid))
3434
end
3535

36-
if message.requested?(:lifecycle_type)
37-
case message.lifecycle_type
38-
when BuildpackLifecycleDataModel::LIFECYCLE_TYPE
39-
dataset = dataset.where(
40-
guid: BuildpackLifecycleDataModel.
41-
where(Sequel.~(app_guid: nil)).
42-
select(:app_guid)
43-
)
44-
when DockerLifecycleDataModel::LIFECYCLE_TYPE
45-
dataset = dataset.exclude(
46-
guid: BuildpackLifecycleDataModel.
47-
where(Sequel.~(app_guid: nil)).
48-
select(:app_guid)
49-
).exclude(
50-
guid: CNBLifecycleDataModel.
51-
where(Sequel.~(app_guid: nil)).
52-
select(:app_guid)
53-
)
54-
when CNBLifecycleDataModel::LIFECYCLE_TYPE
55-
dataset = dataset.where(
56-
guid: CNBLifecycleDataModel.
57-
where(Sequel.~(app_guid: nil)).
58-
select(:app_guid)
59-
)
60-
end
61-
end
36+
dataset = dataset.where(lifecycle_type: message.lifecycle_type) if message.requested?(:lifecycle_type)
6237

6338
if message.requested?(:organization_guids)
6439
dataset = dataset.

‎app/models/runtime/app_model.rb‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,6 @@ def validate
9797
validates_includes Lifecycles::TYPES, :lifecycle_type
9898
end
9999

100-
def lifecycle_type
101-
return self[:lifecycle_type] if self[:lifecycle_type].present?
102-
103-
# Fallback for records written before the lifecycle_type column
104-
# existed. Remove once existing rows are backfilled (see #5067).
105-
return BuildpackLifecycleDataModel::LIFECYCLE_TYPE if buildpack_lifecycle_data
106-
return CNBLifecycleDataModel::LIFECYCLE_TYPE if cnb_lifecycle_data
107-
108-
DockerLifecycleDataModel::LIFECYCLE_TYPE
109-
end
110-
111100
def lifecycle_data
112101
# The lifecycle_data row can be destroyed independently of this
113102
# app; fall back to a frozen empty instance so callers never see

‎app/models/runtime/build_model.rb‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,6 @@ def validate
5555
validates_includes Lifecycles::TYPES, :lifecycle_type
5656
end
5757

58-
def before_create
59-
# Inherit lifecycle_type from associated app if not explicitly set
60-
self[:lifecycle_type] = app&.lifecycle_type if self[:lifecycle_type].blank?
61-
62-
super
63-
end
64-
65-
def lifecycle_type
66-
return self[:lifecycle_type] if self[:lifecycle_type].present?
67-
68-
# Fallback for records written before the lifecycle_type column
69-
# existed. Remove once existing rows are backfilled (see #5067).
70-
return BuildpackLifecycleDataModel::LIFECYCLE_TYPE if buildpack_lifecycle_data
71-
return CNBLifecycleDataModel::LIFECYCLE_TYPE if cnb_lifecycle_data
72-
73-
DockerLifecycleDataModel::LIFECYCLE_TYPE
74-
end
75-
7658
def buildpack_lifecycle?
7759
lifecycle_type == BuildpackLifecycleDataModel::LIFECYCLE_TYPE
7860
end

‎app/models/runtime/droplet_model.rb‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ class DropletModel < Sequel::Model(:droplets)
4949
serializes_via_json :process_types
5050
serializes_via_json :sidecars
5151

52-
def before_create
53-
# Inherit lifecycle_type from associated app if not explicitly set
54-
self[:lifecycle_type] = app&.lifecycle_type if self[:lifecycle_type].blank?
55-
56-
super
57-
end
58-
5952
def around_destroy
6053
yield
6154
rescue Sequel::ForeignKeyConstraintViolation => e
@@ -175,17 +168,6 @@ def fail_to_stage!(reason='StagingError', details='staging failed')
175168
save_changes(raise_on_save_failure: true)
176169
end
177170

178-
def lifecycle_type
179-
return self[:lifecycle_type] if self[:lifecycle_type].present?
180-
181-
# Fallback for records written before the lifecycle_type column
182-
# existed. Remove once existing rows are backfilled (see #5067).
183-
return BuildpackLifecycleDataModel::LIFECYCLE_TYPE if buildpack_lifecycle_data
184-
return CNBLifecycleDataModel::LIFECYCLE_TYPE if cnb_lifecycle_data
185-
186-
DockerLifecycleDataModel::LIFECYCLE_TYPE
187-
end
188-
189171
def lifecycle_data
190172
# The lifecycle_data row can be destroyed independently of this
191173
# droplet; fall back to a frozen empty instance so callers never

‎app/presenters/v3/process_presenter.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ProcessPresenter < BasePresenter
1111
class << self
1212
# :labels and :annotations come from MetadataPresentationHelpers
1313
def associated_resources
14-
super + [{ app: %i[buildpack_lifecycle_data cnb_lifecycle_data] }]
14+
super + [:app]
1515
end
1616
end
1717

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Sequel.migration do
2+
no_transaction
3+
4+
up do
5+
tables = [
6+
{ table: :apps, guid_column: :app_guid },
7+
{ table: :droplets, guid_column: :droplet_guid },
8+
{ table: :builds, guid_column: :build_guid }
9+
]
10+
batch_size = 1000
11+
12+
tables.each do |t|
13+
table = t[:table]
14+
guid_column = t[:guid_column]
15+
16+
loop do
17+
guids = self[table].where(lifecycle_type: nil).limit(batch_size).select_map(:guid)
18+
break if guids.empty?
19+
20+
guids_with_buildpack = self[:buildpack_lifecycle_data].where(guid_column => guids).select_map(guid_column)
21+
guids_with_cnb = self[:cnb_lifecycle_data].where(guid_column => guids).select_map(guid_column) - guids_with_buildpack
22+
guids_docker = guids - guids_with_buildpack - guids_with_cnb
23+
24+
transaction do
25+
self[table].where(guid: guids_with_buildpack, lifecycle_type: nil).update(lifecycle_type: 'buildpack') unless guids_with_buildpack.empty?
26+
self[table].where(guid: guids_with_cnb, lifecycle_type: nil).update(lifecycle_type: 'cnb') unless guids_with_cnb.empty?
27+
self[table].where(guid: guids_docker, lifecycle_type: nil).update(lifecycle_type: 'docker') unless guids_docker.empty?
28+
end
29+
30+
break if guids.size < batch_size
31+
end
32+
end
33+
end
34+
35+
down do
36+
# Intentionally left empty: backfilled values are correct and should not be reverted
37+
end
38+
end

0 commit comments

Comments
 (0)