Skip to content
Snippets Groups Projects

Finalize ci_job_artifacts conversion to bigint

Merged Alex Ives requested to merge 325615/finalize_ci_job_artifacts_bigint into master
Compare and Show latest version
1 file
+ 30
13
Compare changes
  • Side-by-side
  • Inline
@@ -28,6 +28,7 @@ def down
private
def swap
add_concurrent_index TABLE_NAME, :job_id_convert_to_bigint, unique: true, name: 'index_ci_job_artifact_on_job_id_convert_to_bigint'
# This is to replace the existing "ci_job_artifacts_pkey" PRIMARY KEY, btree (id)
add_concurrent_index TABLE_NAME, :id_convert_to_bigint, unique: true, name: 'index_ci_job_artifact_on_id_convert_to_bigint'
# This is to replace the existing "index_ci_job_artifacts_for_terraform_reports" btree (project_id, id) where (file_type = 18)
@@ -48,14 +49,26 @@ def swap
name: fk_artifacts_archive_id_tmp,
on_delete: :nullify
# Add a FK on `job_id_convert_to_bigint` to `ci_builds(id)`, the old FK (fk_rails_c5137cb2c1)
# is removed below since it won't be dropped automatically.
fk_ci_builds_job_id = concurrent_foreign_key_name(TABLE_NAME, :job_id, prefix: 'fk_rails_')
fk_ci_builds_job_id_tmp = "#{fk_ci_builds_job_id}_tmp"
add_concurrent_foreign_key TABLE_NAME, :ci_builds,
column: :job_id_convert_to_bigint,
name: fk_ci_builds_job_id_tmp,
on_delete: :cascade
# # Add a FK on `job_id_convert_to_bigint` to `ci_builds(id)`, the old FK (fk_rails_c5137cb2c1)
# # is removed below since it won't be dropped automatically.
# fk_ci_builds_job_id = concurrent_foreign_key_name(TABLE_NAME, :job_id, prefix: 'fk_rails_')
# fk_ci_builds_job_id_tmp = "#{fk_ci_builds_job_id}_tmp"
# add_concurrent_foreign_key TABLE_NAME, :ci_builds,
# column: :job_id_convert_to_bigint,
# name: fk_ci_builds_job_id_tmp,
# on_delete: :cascade
# Add a foreign key on `job_id_convert_to_bigint` before we swap the columns and drop the old FK ()
add_concurrent_foreign_key TABLE_NAME, :ci_builds, column: :job_id_convert_to_bigint, on_delete: :cascade, name: 'fk_rails_c5137cb2c1_tmp'
with_lock_retries(raise_on_exhaustion: true) do
# We'll need ACCESS EXCLUSIVE lock on the related tables,
@@ -88,7 +101,7 @@ def swap
change_column_default TABLE_NAME, :job_id_convert_to_bigint, 0
# Swap PK constraint
execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT ci_job_artifacts_pkey CASCADE" # this will drop fk_36c74129da
execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT ci_job_artifacts_pkey CASCADE" # this will drop ci_job_artifacts_pkey primary key
rename_index TABLE_NAME, 'index_ci_job_artifact_on_id_convert_to_bigint', 'ci_job_artifacts_pkey'
execute "ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT ci_job_artifacts_pkey PRIMARY KEY USING INDEX ci_job_artifacts_pkey"
@@ -102,13 +115,17 @@ def swap
execute 'DROP INDEX index_ci_job_artifacts_on_job_id_and_file_type'
rename_index TABLE_NAME, 'index_ci_job_artifacts_on_job_id_and_file_type_bigint', 'index_ci_job_artifacts_on_job_id_and_file_type'
rename_index TABLE_NAME, 'index_ci_job_artifact_on_job_id_convert_to_bigint', 'index_ci_job_artifact_on_job_id'
# Change the name of the temporary FK for project_pages_metadata(artifacts_archive_id) -> id
rename_constraint(:project_pages_metadata, fk_artifacts_archive_id_tmp, fk_artifacts_archive_id)
# Change the name of the old FK (will be dropped when old index is updated)
rename_constraint(TABLE_NAME, fk_ci_builds_job_id, "#{fk_ci_builds_job_id}_old_id")
# Change the name of the temporary FK for job_id -> ci_builds(id)
rename_constraint(TABLE_NAME, fk_ci_builds_job_id_tmp, fk_ci_builds_job_id)
# Drop original FK on the old int4 `job_id` (fk_rails_c5137cb2c1)
remove_foreign_key TABLE_NAME, name: 'fk_rails_c5137cb2c1'
# We swapped the columns but the FK for job_id is still using the temporary name for the job_id_convert_to_bigint column
# So we have to also swap the FK name now that we dropped the other one with the same
rename_constraint(TABLE_NAME, 'fk_rails_c5137cb2c1_tmp', 'fk_rails_c5137cb2c1')
end
end
end
Loading