You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT * FROM `buildpack_lifecycle_data` WHERE (`build_guid` NOT IN (SELECT `guid` FROM `builds`))
The issue is that when trying to delete orphaned buildpack_lifecycle_data, there are still buildpack_lifecycle_buildpacks with buildpack_lifecycle_data_guids. These buildpack_lifecycle_buildpacks have their own foreign key constraint, and this causes the following error:
Sequel::ForeignKeyConstraintViolation: Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails (`ccdb`.`buildpack_lifecycle_buildpacks`, CONSTRAINT `fk_blbuildpack_bldata_guid` FOREIGN KEY (`buildpack_lifecycle_data_guid`) REFERENCES `buildpack_lifecycle_data` (`guid`) ON DELETE RESTRICT) (Sequel::ForeignKeyConstraintViolation)
Steps to Reproduce
Spin up an environment that predates the migration (CAPI 1.172.0) or otherwise revert the migration.
Push an app. This should create a build, buildpack_lifecycle_data, and buildpack_lifecycle_buildpack
Manually delete the build from the DB <--it's unclear yet how this happens in real envs
Run the migration (or execute SELECT * FROM buildpack_lifecycle_data WHERE (build_guid NOT IN (SELECT guid FROM builds))
Observe the error
Expected result
The migration successfully deletes the orphaned buildpack_lifecycle_data AND buildpack_lifecycle_buildpack(s)
Current result
error as described above
Possible Fix
I'm not sure, but I think possibly swapping Lines 11 and 12 of the migration would fix the migration.
This way the delete cascade is applied to the buildpack_lifecycle_data_guid on buildpack_lifecycle_buildpacks BEFORE attempting to delete orphaned buildpack_lifecycle_data
- Reproduced reported issue by enhancing the unit test.
- Fixed ForeignKeyConstraintViolation by re-ordering migration
statements.
Fixes issue cloudfoundry#3803.
@sethboyles - I was able to reproduce the issue by enhancing the (already existing) unit test for this migration. Changing the order of migration statements (as you suggested) fixes the problem (e.g. makes the unit test pass).
Issue
In this commit: ad9b5a1#diff-214a865c0b7fb0f3d7d40880ce1e6b38ebf38dd2cb104703663844da306dddc3R11, when adding a foreign key constraint to
build_guid
onbuildpack_lifecycle_data
, the migration also attempts to delete allbuildpack_lifecycle_data
that has abuild_guid
no longer present in thebuilds
table.They query it runs is this:
The issue is that when trying to delete orphaned
buildpack_lifecycle_data
, there are stillbuildpack_lifecycle_buildpacks
withbuildpack_lifecycle_data_guids
. Thesebuildpack_lifecycle_buildpacks
have their own foreign key constraint, and this causes the following error:Steps to Reproduce
build
,buildpack_lifecycle_data
, andbuildpack_lifecycle_buildpack
SELECT * FROM buildpack_lifecycle_data WHERE (build_guid NOT IN (SELECT guid FROM builds))
Expected result
The migration successfully deletes the orphaned
buildpack_lifecycle_data
ANDbuildpack_lifecycle_buildpack(s)
Current result
error as described above
Possible Fix
I'm not sure, but I think possibly swapping Lines 11 and 12 of the migration would fix the migration.
cloud_controller_ng/db/migrations/20240115163000_add_delete_cascade_to_foreign_keys.rb
Lines 11 to 12 in ad9b5a1
This way the delete cascade is applied to the
buildpack_lifecycle_data_guid
onbuildpack_lifecycle_buildpacks
BEFORE attempting to delete orphanedbuildpack_lifecycle_data
cloud_controller_ng/db/migrations/20240115163000_add_delete_cascade_to_foreign_keys.rb
Line 29 in ad9b5a1
@philippthun
The text was updated successfully, but these errors were encountered: