You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WHERE "group_archive_lecturers"."group_id" IN (SELECT "id" FROM "groups")
234
-
AND "group_archive_lecturers"."lecturer_id" NOT IN (SELECT DISTINCT "lecturer_id" FROM "group_lecturers" WHERE "group_lecturers"."group_id" = "group_archive_lecturers"."group_id");
235
-
-- Insert new lecturers
236
-
INSERT INTO "group_archive_lecturers" ("group_id", "lecturer_id", "created_at", "updated_at")
WHERE "group_lecturers"."lecturer_id" NOT IN (SELECT DISTINCT "lecturer_id" FROM "group_archive_lecturers" WHERE "group_archive_lecturers"."group_id" = "group_lecturers"."group_id");
231
+
232
+
-- Update the pivot table
233
+
MERGE INTO "group_archive_lecturers" AS "archive"
234
+
USING "group_lecturers" AS "current"
235
+
ON "archive"."group_id" = "current"."group_id" AND "archive"."lecturer_id" = "current"."lecturer_id"
236
+
-- when the row exists in both tables
237
+
WHEN MATCHED THEN DO NOTHING
238
+
-- when the row doesnt exist in the current table
239
+
WHEN NOT MATCHED BY SOURCE
240
+
AND EXISTS (SELECT 1 FROM "groups" WHERE "id" = "archive"."group_id")
241
+
THEN DELETE
242
+
-- when the row doesnt exist in the archive table
243
+
WHEN NOT MATCHED BY TARGET
244
+
THEN INSERT ("group_id", "lecturer_id", "updated_at", "created_at")
0 commit comments