Skip to content

Update LFK script to generate constant for migration

Thong Kuah requested to merge use_constant_migration_scripts_lfk into master

What does this MR do and why?

Update LFK script to generate constant for migration

Screenshots or screen recordings

Migration generated and ran successfully

Writing db/post_migrate/20230802101627_remove_users_members_user_id_fk.rb
>> bin/rails db:migrate
main: == [advisory_lock_connection] object_id: 224400, pg_backend_pid: 92276
main: == 20230802101627 RemoveUsersMembersUserIdFk: migrating =======================
main: -- transaction_open?()
main:    -> 0.0000s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("LOCK TABLE users, members IN ACCESS EXCLUSIVE MODE")
main:    -> 0.0004s
main: -- remove_foreign_key(:members, :users, {:name=>"fk_2e88fb7ce9"})
main:    -> 0.0025s
main: == 20230802101627 RemoveUsersMembersUserIdFk: migrated (0.0320s) ==============

main: == [advisory_lock_connection] object_id: 224400, pg_backend_pid: 92276
ci: == [advisory_lock_connection] object_id: 224620, pg_backend_pid: 92278
ci: == 20230802101627 RemoveUsersMembersUserIdFk: migrating =======================
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- execute("LOCK TABLE users, members IN ACCESS EXCLUSIVE MODE")
ci:    -> 0.0004s
ci: -- remove_foreign_key(:members, :users, {:name=>"fk_2e88fb7ce9"})
ci:    -> 0.0025s
ci: == 20230802101627 RemoveUsersMembersUserIdFk: migrated (0.0176s) ==============

ci: == [advisory_lock_connection] object_id: 224620, pg_backend_pid: 92278
Adding test to spec/models/member_spec.rb...
Updating spec/lib/gitlab/database/no_cross_db_foreign_keys_spec.rb...
Nothing changed.
The following files have been generated/modified:
/Users/tkuah/code/gdk-ee/gitlab/config/gitlab_loose_foreign_keys.yml
db/post_migrate/20230802101627_remove_users_members_user_id_fk.rb
spec/models/member_spec.rb

(arm64) tkgl2:gitlab tkuah$ git diff db/post_migrate/
(arm64) tkgl2:gitlab tkuah$ git s
On branch use_constant_migration_scripts_lfk
Your branch is up to date with 'origin/use_constant_migration_scripts_lfk'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   config/gitlab_loose_foreign_keys.yml
	modified:   db/structure.sql
	modified:   spec/models/member_spec.rb

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	db/post_migrate/20230802101627_remove_users_members_user_id_fk.rb
	db/schema_migrations/20230802101627

no changes added to commit (use "git add" and/or "git commit -a")
(arm64) tkgl2:gitlab tkuah$ cat db/post_migrate/20230802101627_remove_users_members_user_id_fk.rb
# frozen_string_literal: true

class RemoveUsersMembersUserIdFk < Gitlab::Database::Migration[2.1]
  disable_ddl_transaction!

  FOREIGN_KEY_NAME = "fk_2e88fb7ce9"

  def up
    return unless foreign_key_exists?(:members, :users, name: FOREIGN_KEY_NAME)

    with_lock_retries do
      remove_foreign_key_if_exists(:members, :users,
        name: FOREIGN_KEY_NAME, reverse_lock_order: true)
    end
  end

  def down
    add_concurrent_foreign_key(:members, :users,
      name: FOREIGN_KEY_NAME, column: :user_id,
      target_column: :id, on_delete: :cascade)
  end
end

How to set up and validate locally

  1. Run scripts/decomposition/generate-loose-foreign-key members user_id to test this out

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Thong Kuah

Merge request reports

Loading