-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP * WIP * Ensure property name is unique * Fix tests * Add migration script * Polishing * Hardcode the error message
- Loading branch information
1 parent
5e9cc7b
commit aca6706
Showing
6 changed files
with
168 additions
and
8 deletions.
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
11 changes: 11 additions & 0 deletions
11
.../docs/en/api/fastagency/studio/helpers/check_model_name_uniqueness_and_raise.md
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,11 @@ | ||
--- | ||
# 0.5 - API | ||
# 2 - Release | ||
# 3 - Contributing | ||
# 5 - Template Page | ||
# 10 - Default | ||
search: | ||
boost: 0.5 | ||
--- | ||
|
||
::: fastagency.studio.helpers.check_model_name_uniqueness_and_raise |
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
21 changes: 21 additions & 0 deletions
21
migrations/20240910072037_make_json_str_name_unique_per_user/migration.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,21 @@ | ||
-- Update records with duplicate names for the same user | ||
UPDATE "Model" | ||
SET json_str = jsonb_set( | ||
json_str::jsonb, | ||
'{name}', | ||
-- Append a random 5-digit suffix to the name | ||
to_jsonb(json_str->>'name' || '_' || LPAD(FLOOR(RANDOM() * 100000)::text, 5, '0')) | ||
) | ||
-- Select records with duplicate names | ||
WHERE (user_uuid, json_str->>'name') IN ( | ||
SELECT user_uuid, json_str->>'name' | ||
FROM "Model" | ||
GROUP BY user_uuid, json_str->>'name' | ||
HAVING COUNT(*) > 1 | ||
) | ||
-- Exclude the first occurrence of each duplicate set | ||
AND uuid NOT IN ( | ||
SELECT DISTINCT ON (user_uuid, json_str->>'name') uuid | ||
FROM "Model" | ||
ORDER BY user_uuid, json_str->>'name', created_at | ||
); |
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