Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a migration script to make existing property names unique #719

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
Loading