Skip to content

Skip migration when the partitioned FK exists

Marius Bobin requested to merge 439065-mb-fix-existing-installs into master

What does this MR do and why?

!147648 (merged) reintroduces a migration that was previously no-op-ed in !146890 (diffs), but it doesn't handle the case where the previous migration was executed before the no-op, leading to this error:

ci: == [advisory_lock_connection] object_id: 132960, pg_backend_pid: 94630
main: == [advisory_lock_connection] object_id: 138660, pg_backend_pid: 94635
main: == 20240322124310 ReplaceUnpartitionedFkBetweenCiBuildsAndCiStages: migrating =
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("LOCK TABLE p_ci_stages, p_ci_builds IN ACCESS EXCLUSIVE MODE")
main:    -> 0.0004s
main: -- quote_table_name(:p_ci_builds)
main:    -> 0.0001s
main: -- quote_column_name(:tmp_fk_3a9eaa254d_p)
main:    -> 0.0000s
main: -- quote_column_name(:fk_3a9eaa254d_p)
main:    -> 0.0000s
main: -- execute("ALTER TABLE \"p_ci_builds\"\nRENAME CONSTRAINT \"tmp_fk_3a9eaa254d_p\" TO \"fk_3a9eaa254d_p\"\n")
main: == [advisory_lock_connection] object_id: 138660, pg_backend_pid: 94635
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

PG::DuplicateObject: ERROR:  constraint "fk_3a9eaa254d_p" for relation "p_ci_builds" already exists

Will it happen in production?

No, this bug affects only GDK because the previous migration was no-op-ed before it reached staging, So staging are production are safe to continue with the execution of the unaltered migration.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

bin/rails db:migrate:ci
ci: == [advisory_lock_connection] object_id: 121180, pg_backend_pid: 23907
ci: == 20240322124310 ReplaceUnpartitionedFkBetweenCiBuildsAndCiStages: migrating =
ci: -- transaction_open?(nil)
ci:    -> 0.0000s
ci: -- transaction_open?(nil)
ci:    -> 0.0000s
ci: -- execute("LOCK TABLE p_ci_stages, p_ci_builds IN ACCESS EXCLUSIVE MODE")
ci:    -> 0.0004s
ci: -- remove_foreign_key(:p_ci_builds, :p_ci_stages, {:name=>:tmp_fk_3a9eaa254d_p})
ci:    -> 0.0062s
ci: == 20240322124310 ReplaceUnpartitionedFkBetweenCiBuildsAndCiStages: migrated (0.0584s)

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Related to #439065 (closed)

Edited by Marius Bobin

Merge request reports

Loading