Commit 59141572 authored by dfrazao-gitlab's avatar dfrazao-gitlab

Inconsistent schema - indexes

- Fix approval_rules_code_owners_rule_type index

Changelog: fixed

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/349549
parent f88e9e0b
# frozen_string_literal: true
class FixApprovalRulesCodeOwnersRuleTypeIndex < Gitlab::Database::Migration[1.0]
INDEX_NAME = 'index_approval_rules_code_owners_rule_type'
OLD_INDEX_NAME = 'index_approval_rules_code_owners_rule_type_old'
TABLE = :approval_merge_request_rules
COLUMN = :merge_request_id
WHERE_CONDITION = 'rule_type = 2'
disable_ddl_transaction!
def up
rename_index TABLE, INDEX_NAME, OLD_INDEX_NAME if index_exists_by_name?(TABLE, INDEX_NAME) && !index_exists_by_name?(TABLE, OLD_INDEX_NAME)
add_concurrent_index TABLE, COLUMN, where: WHERE_CONDITION, name: INDEX_NAME
remove_concurrent_index_by_name TABLE, OLD_INDEX_NAME
end
def down
# No-op
end
end
77cc8fc86f2c6a5ed017dde40dd4db796821a35e6ce4d8dcbe24b2cdaccbb5d9
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require_migration!('fix_approval_rules_code_owners_rule_type_index')
RSpec.describe FixApprovalRulesCodeOwnersRuleTypeIndex, :migration do
let(:table_name) { :approval_merge_request_rules }
let(:index_name) { 'index_approval_rules_code_owners_rule_type' }
it 'correctly migrates up and down' do
reversible_migration do |migration|
migration.before -> {
expect(subject.index_exists_by_name?(table_name, index_name)).to be_truthy
}
migration.after -> {
expect(subject.index_exists_by_name?(table_name, index_name)).to be_truthy
}
end
end
context 'when the index already exists' do
before do
subject.add_concurrent_index table_name, :merge_request_id, where: 'rule_type = 2', name: index_name
end
it 'keeps the index' do
migrate!
expect(subject.index_exists_by_name?(table_name, index_name)).to be_truthy
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