-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make Credentials unique by user by name (#2371)
* ref #2367, make creds unique by user by name * update cl * use random cred name
- Loading branch information
1 parent
a4b8645
commit 4ee43fa
Showing
6 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
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
33 changes: 33 additions & 0 deletions
33
priv/repo/migrations/20240810082050_add_uniqueness_to_credential_name_per_user.exs
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,33 @@ | ||
defmodule Lightning.Repo.Migrations.AddUniquenessToCredentialNamePerUser do | ||
use Ecto.Migration | ||
|
||
def up do | ||
# Identify duplicates and update them to ensure uniqueness | ||
execute """ | ||
WITH duplicates AS ( | ||
SELECT | ||
id, | ||
name, | ||
user_id, | ||
ROW_NUMBER() OVER (PARTITION BY LOWER(REPLACE(name, '-', ' ')), user_id ORDER BY id) AS row_num | ||
FROM | ||
credentials | ||
) | ||
UPDATE credentials | ||
SET name = credentials.name || '-' || duplicates.row_num | ||
FROM duplicates | ||
WHERE credentials.id = duplicates.id | ||
AND duplicates.row_num > 1; | ||
""" | ||
|
||
execute """ | ||
CREATE UNIQUE INDEX credentials_name_user_id_index ON credentials (LOWER(REPLACE(name, '-', ' ')), user_id); | ||
""" | ||
end | ||
|
||
def down do | ||
execute """ | ||
DROP INDEX credentials_name_user_id_index; | ||
""" | ||
end | ||
end |
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