Commit 913af875 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'sh-fix-label-uniquness-migration' into 'master'

Fix broken label uniqueness label migration

The previous implementation of the migration failed on staging because
the migration was attempted to remove labels from projects that did not
actually have duplicates. This occurred because the SQL query did not
account for the project ID when selecting the labels.

To replicate the problem:

1. Disable the uniqueness validation in app/models/label.rb.
2. Create a duplicate label "bug" in project A.
3. Create the same label in project B with label "bug".

The migration will attempt to remove the label in B even if there are no duplicates.

To fix the issue, include the project ID when selecting the labels.

Closes #23609

See merge request !7030
parent f567f75f
......@@ -7,9 +7,9 @@ class AddUniqueIndexToLabels < ActiveRecord::Migration
disable_ddl_transaction!
def up
select_all('SELECT title, COUNT(id) as cnt FROM labels GROUP BY project_id, title HAVING COUNT(id) > 1').each do |label|
select_all('SELECT title, project_id, COUNT(id) as cnt FROM labels GROUP BY project_id, title HAVING COUNT(id) > 1').each do |label|
label_title = quote_string(label['title'])
duplicated_ids = select_all("SELECT id FROM labels WHERE title = '#{label_title}' ORDER BY id ASC").map{ |label| label['id'] }
duplicated_ids = select_all("SELECT id FROM labels WHERE project_id = #{label['project_id']} AND title = '#{label_title}' ORDER BY id ASC").map{ |label| label['id'] }
label_id = duplicated_ids.first
duplicated_ids.delete(label_id)
......
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