Commit ae79d6d9 authored by Stan Hu's avatar Stan Hu

Update foreign key constraint for personal access tokens

Previously the foreign key constraint for `user_id` on the
`personal_access_tokens` table did not have the ON DELETE CASCADE
option enabled, which made deleting users that had generated a
PAT impossible.

This migration adds a new foreign key constraint with this option and
drops the old one.

Closes https://gitlab.com/gitlab-org/gitlab/issues/191262
parent 62052283
---
title: Update foreign key constraint for personal access tokens
merge_request: 22305
author:
type: fixed
# frozen_string_literal: true
class UpdatePersonalAccessTokensUserIdForeignKey < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
CONSTRAINT_NAME = 'fk_personal_access_tokens_user_id'
disable_ddl_transaction!
def up
add_concurrent_foreign_key(:personal_access_tokens, :users, column: :user_id, on_delete: :cascade, name: CONSTRAINT_NAME)
remove_foreign_key_if_exists(:personal_access_tokens, column: :user_id, on_delete: nil)
end
def down
add_concurrent_foreign_key(:personal_access_tokens, :users, column: :user_id, on_delete: nil)
remove_foreign_key_if_exists(:personal_access_tokens, column: :user_id, on_delete: :cascade, name: CONSTRAINT_NAME)
end
end
......@@ -4687,7 +4687,7 @@ ActiveRecord::Schema.define(version: 2019_12_29_140154) do
add_foreign_key "pages_domains", "projects", name: "fk_ea2f6dfc6f", on_delete: :cascade
add_foreign_key "path_locks", "projects", name: "fk_5265c98f24", on_delete: :cascade
add_foreign_key "path_locks", "users"
add_foreign_key "personal_access_tokens", "users"
add_foreign_key "personal_access_tokens", "users", name: "fk_personal_access_tokens_user_id", on_delete: :cascade
add_foreign_key "plan_limits", "plans", on_delete: :cascade
add_foreign_key "pool_repositories", "projects", column: "source_project_id", on_delete: :nullify
add_foreign_key "pool_repositories", "shards", on_delete: :restrict
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment