-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: [#289] do not allow duplicate tags
Duplicate tag names were allowed. This introduce unique constrains. If there were duyplicate tags they are updated appeding the id as a suffix. For example, having two tags: ```json [ { tag_id: 1, name: "fractal" }, { tag_id: 2, name: "fractal" } ] ``` They would be renamed to: ```json [ { tag_id: 1, name: "fractal_1" }, { tag_id: 2, name: "fractal_2" } ] ``` From now on, duplicate tags are not allowed.
- Loading branch information
1 parent
01a7d2e
commit 7fedf15
Showing
10 changed files
with
63 additions
and
19 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
migrations/mysql/20230914155441_torrust_no_duplicate_tags.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- Step 1 & 2: Identify and update the duplicate names | ||
UPDATE torrust_torrent_tags | ||
JOIN ( | ||
SELECT name | ||
FROM torrust_torrent_tags | ||
GROUP BY name | ||
HAVING COUNT(*) > 1 | ||
) AS DuplicateNames ON torrust_torrent_tags.name = DuplicateNames.name | ||
SET torrust_torrent_tags.name = CONCAT(torrust_torrent_tags.name, '_', torrust_torrent_tags.tag_id); | ||
|
||
-- Step 3: Add the UNIQUE constraint to the name column | ||
ALTER TABLE torrust_torrent_tags ADD UNIQUE (name); |
13 changes: 13 additions & 0 deletions
13
migrations/sqlite3/20230914155441_torrust_no_duplicate_tags.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Step 1: Identify and update the duplicate names | ||
WITH DuplicateNames AS ( | ||
SELECT name | ||
FROM torrust_torrent_tags | ||
GROUP BY name | ||
HAVING COUNT(*) > 1 | ||
) | ||
UPDATE torrust_torrent_tags | ||
SET name = name || '_' || tag_id | ||
WHERE name IN (SELECT name FROM DuplicateNames); | ||
|
||
-- Step 2: Create a UNIQUE index on the name column | ||
CREATE UNIQUE INDEX idx_unique_name ON torrust_torrent_tags(name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters