Add creator_id to deploy_tokens

What does this MR do and why?

Describe in detail what your merge request does and why.

  • Add a new column creator_id which will hold the id of the user who created the deploy token.
  • While creating new deploy tokens (group or project) set the creator_id as the id of the current user.
  • For old deploy tokens creator_id column would be NULL.
  • When the creator is deleted set the creator_id as NULL.

Database

AddCreatorIdToDeployTokens up

https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/9360/commands/33030

== 20220316095126 AddCreatorIdToDeployTokens: migrating =======================
-- add_column(:deploy_tokens, :creator_id, :bigint)
   -> 0.0018s
== 20220316095126 AddCreatorIdToDeployTokens: migrated (0.0018s) ==============

AddCreatorIdToDeployTokens down

== 20220316095126 AddCreatorIdToDeployTokens: reverting =======================
-- remove_column(:deploy_tokens, :creator_id, :bigint)
   -> 0.0018s
== 20220316095126 AddCreatorIdToDeployTokens: reverted (0.0019s) ==============

AddIndexToDeployTokensOnCreatorId up

https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/9331/commands/32920

== 20220316102900 AddIndexToDeployTokensOnCreatorId: migrating ================
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:deploy_tokens, :creator_id, {:name=>"index_deploy_tokens_on_creator_id", :algorithm=>:concurrently})
   -> 0.0046s
-- execute("SET statement_timeout TO 0")
   -> 0.0007s
-- add_index(:deploy_tokens, :creator_id, {:name=>"index_deploy_tokens_on_creator_id", :algorithm=>:concurrently})
   -> 0.0029s
-- execute("RESET statement_timeout")
   -> 0.0006s
== 20220316102900 AddIndexToDeployTokensOnCreatorId: migrated (0.0177s) =======

AddIndexToDeployTokensOnCreatorId down

== 20220316102900 AddIndexToDeployTokensOnCreatorId: reverting ================
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:deploy_tokens, :creator_id, {:name=>"index_deploy_tokens_on_creator_id", :algorithm=>:concurrently})
   -> 0.0023s
-- remove_index(:deploy_tokens, {:name=>"index_deploy_tokens_on_creator_id", :algorithm=>:concurrently, :column=>:creator_id})
   -> 0.0038s
== 20220316102900 AddIndexToDeployTokensOnCreatorId: reverted (0.0084s) =======

AddUserForeignKeyToDeployTokens up

https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/9263/commands/32759

== 20220316102902 AddUserForeignKeyToDeployTokens: migrating ==================
-- transaction_open?()
   -> 0.0000s
-- foreign_keys(:deploy_tokens)
   -> 0.0037s
-- transaction_open?()
   -> 0.0000s
-- execute("LOCK TABLE users, deploy_tokens IN SHARE ROW EXCLUSIVE MODE")
   -> 0.0007s
-- execute("ALTER TABLE deploy_tokens\nADD CONSTRAINT fk_7082f8a288\nFOREIGN KEY (creator_id)\nREFERENCES users (id)\nON DELETE SET NULL\nNOT VALID;\n")
   -> 0.0022s
-- execute("ALTER TABLE deploy_tokens VALIDATE CONSTRAINT fk_7082f8a288;")
   -> 0.0045s
== 20220316102902 AddUserForeignKeyToDeployTokens: migrated (0.0208s) =========

AddUserForeignKeyToDeployTokens down

== 20220316102902 AddUserForeignKeyToDeployTokens: reverting ==================
-- transaction_open?()
   -> 0.0000s
-- remove_foreign_key(:deploy_tokens, {:column=>:creator_id})
   -> 0.0040s
== 20220316102902 AddUserForeignKeyToDeployTokens: reverted (0.0073s) =========

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

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

  1. You must have ultimate license.
  2. Create a group or a project deploy token following the steps in the doc.
  3. Verify from the Rails console that the deploy token that is created has creator_id set to the user who created the user. DeployToken.last.creator_id.
  4. Verify that creator is returned DeployToken.last.user.

Relates to #https://gitlab.com/gitlab-org/gitlab/-/issues/353467

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 Huzaifa Iftikhar

Merge request reports

Loading