Skip to content

Commit

Permalink
Add a migration script to make existing property names unique (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj authored Sep 10, 2024
1 parent 5fa018c commit 97864d6
Showing 1 changed file with 21 additions and 0 deletions.
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
);

0 comments on commit 97864d6

Please sign in to comment.