From e88716758e7150c722e4c7a8f493c61e21f53940 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 15 Mar 2023 15:35:54 +0000 Subject: [PATCH] Remove event type dupes, assign ownership based on existing _user_eventtype.'B' (#7284) Co-authored-by: zomars --- .../migration.sql | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/prisma/migrations/20230222152136_assign_event_type_ownership/migration.sql diff --git a/packages/prisma/migrations/20230222152136_assign_event_type_ownership/migration.sql b/packages/prisma/migrations/20230222152136_assign_event_type_ownership/migration.sql new file mode 100644 index 0000000000..5f55d3b083 --- /dev/null +++ b/packages/prisma/migrations/20230222152136_assign_event_type_ownership/migration.sql @@ -0,0 +1,32 @@ + +DELETE FROM "EventType" WHERE id IN ( + SELECT id FROM "EventType" + INNER JOIN _user_eventtype ON "A" = id + WHERE "B" IN ( + SELECT "B" FROM "EventType" + INNER JOIN _user_eventtype ON "A" = id + WHERE "userId" IS NULL AND "teamId" IS NULL + GROUP BY slug, "B" HAVING count(*) > 1 + ) + EXCEPT + SELECT min(id) FROM "EventType" + INNER JOIN _user_eventtype ON "A" = id + WHERE "B" IN ( + SELECT "B" FROM "EventType" INNER JOIN _user_eventtype ON "A" = id + WHERE "userId" IS NULL AND "teamId" IS NULL + GROUP BY slug, "B" HAVING count(*) > 1 + ) + GROUP BY slug, "B" +); + +UPDATE "EventType" +SET "userId" = sub."B" +FROM ( + SELECT id, slug, "B" FROM "EventType" + INNER JOIN _user_eventtype ON "A" = id + WHERE "userId" IS NULL AND "teamId" IS NULL +) AS sub +WHERE "EventType".id = sub.id +AND NOT EXISTS ( + SELECT 1 FROM "EventType" WHERE slug = sub.slug AND "userId" = sub."B" +); \ No newline at end of file