forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge torrust#293: Do not allow empty or duplicate tag names
7fedf15 feat!: [torrust#289] do not allow duplicate tags (Jose Celano) 01a7d2e feat!: [torrust#289] do not allow empty tag names (Jose Celano) Pull request description: ### Do not allow empty tag names It was allowed to use empty strings like "" or " " for a tag name. After merging this PR, it will not be allowed anymore. If there are some empty names in the database, they are not renamed. A migration to rename empty names was not added because there can be more than one tag, for example: - "" - " " - " " - Etcetera We could have generated names like "no tag 1", "no tag 2", but it's not likely that admins have created empty tags. ### Do not allow duplicate tag names It was allowed to use empty strings like "" or " " for a tag name. And It was also allowed to have duplicate tags. After merging this PR, it will not be allowed anymore. If there are duplicate tag names, the duplication is removed by appending the tag id as a suffix to the tag name. ACKs for top commit: josecelano: ACK 7fedf15 Tree-SHA512: 79200e9048a500a5b268a32db43e64f071042dee94f4200d6f7bfc3199120d1b30f2a2fd07d4d1b8ed01a438d4687261cc988cb9c372716579094be4ad43100c
- Loading branch information
Showing
11 changed files
with
82 additions
and
26 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
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