Commit 42aafb30 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'mc_rocha-remove-duplicate-tokens-338191' into 'master'

Add data migration to remove duplicate DastSiteTokens with same token

See merge request gitlab-org/gitlab!69020
parents cf9a7a85 bf0b1a98
# frozen_string_literal: true
class RemoveDuplicateDastSiteTokensWithSameToken < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
INDEX_NAME = 'index_dast_site_token_on_token'
# rubocop: disable Migration/AddIndex
def up
execute("WITH duplicate_tokens AS(
SELECT id, rank() OVER (PARTITION BY token ORDER BY id) r FROM dast_site_tokens
)
DELETE FROM dast_site_tokens c USING duplicate_tokens t
WHERE c.id = t.id AND t.r > 1;")
add_index :dast_site_tokens, :token, name: INDEX_NAME, unique: true
end
# rubocop: disable Migration/RemoveIndex
def down
remove_index :dast_site_tokens, :token, name: INDEX_NAME
end
end
a7f4911fcb9ab939a6e5e9a6e5e927fd6828ff062324d8483d78c8f8a4ded4e6
\ No newline at end of file
...@@ -24804,6 +24804,8 @@ CREATE UNIQUE INDEX index_dast_site_profiles_pipelines_on_ci_pipeline_id ON dast ...@@ -24804,6 +24804,8 @@ CREATE UNIQUE INDEX index_dast_site_profiles_pipelines_on_ci_pipeline_id ON dast
CREATE UNIQUE INDEX index_dast_site_token_on_project_id_and_url ON dast_site_tokens USING btree (project_id, url); CREATE UNIQUE INDEX index_dast_site_token_on_project_id_and_url ON dast_site_tokens USING btree (project_id, url);
CREATE UNIQUE INDEX index_dast_site_token_on_token ON dast_site_tokens USING btree (token);
CREATE INDEX index_dast_site_tokens_on_project_id ON dast_site_tokens USING btree (project_id); CREATE INDEX index_dast_site_tokens_on_project_id ON dast_site_tokens USING btree (project_id);
CREATE INDEX index_dast_site_validations_on_dast_site_token_id ON dast_site_validations USING btree (dast_site_token_id); CREATE INDEX index_dast_site_validations_on_dast_site_token_id ON dast_site_validations USING btree (dast_site_token_id);
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe RemoveDuplicateDastSiteTokensWithSameToken do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:dast_site_tokens) { table(:dast_site_tokens) }
let!(:namespace) { namespaces.create!(id: 1, name: 'group', path: 'group') }
let!(:project1) { projects.create!(id: 1, namespace_id: namespace.id, path: 'project1') }
# create non duplicate dast site token
let!(:dast_site_token1) { dast_site_tokens.create!(project_id: project1.id, url: 'https://gitlab.com', token: SecureRandom.uuid) }
context 'when duplicate dast site tokens exists' do
# create duplicate dast site token
let_it_be(:duplicate_token) { 'duplicate_token' }
let_it_be(:other_duplicate_token) { 'other_duplicate_token' }
let!(:project2) { projects.create!(id: 2, namespace_id: namespace.id, path: 'project2') }
let!(:dast_site_token2) { dast_site_tokens.create!(project_id: project2.id, url: 'https://gitlab2.com', token: duplicate_token) }
let!(:dast_site_token3) { dast_site_tokens.create!(project_id: project2.id, url: 'https://gitlab3.com', token: duplicate_token) }
let!(:dast_site_token4) { dast_site_tokens.create!(project_id: project2.id, url: 'https://gitlab4.com', token: duplicate_token) }
let!(:project3) { projects.create!(id: 3, namespace_id: namespace.id, path: 'project3') }
let!(:dast_site_token5) { dast_site_tokens.create!(project_id: project3.id, url: 'https://gitlab2.com', token: other_duplicate_token) }
let!(:dast_site_token6) { dast_site_tokens.create!(project_id: project3.id, url: 'https://gitlab3.com', token: other_duplicate_token) }
let!(:dast_site_token7) { dast_site_tokens.create!(project_id: project3.id, url: 'https://gitlab4.com', token: other_duplicate_token) }
describe 'migration up' do
it 'does remove duplicated dast site tokens with the same token' do
expect(dast_site_tokens.count).to eq(7)
expect(dast_site_tokens.where(token: duplicate_token).size).to eq(3)
migrate!
expect(dast_site_tokens.count).to eq(3)
expect(dast_site_tokens.where(token: duplicate_token).size).to eq(1)
end
end
end
context 'when duplicate dast site tokens do not exist' do
let!(:dast_site_token5) { dast_site_tokens.create!(project_id: 1, url: 'https://gitlab5.com', token: SecureRandom.uuid) }
describe 'migration up' do
it 'does not remove any dast site tokens' do
expect { migrate! }.not_to change(dast_site_tokens, :count)
end
end
end
end
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