-- A person (contact data, per teacher) can now be in several groups: `attendees` -- becomes the membership and points to `people`. Existing attendees of the same -- teacher with the same phone (digits only) are merged into one person, and -- their student-app accounts into one account. -- All or nothing: a half-applied data migration cannot be retried. BEGIN; -- CreateTable CREATE TABLE "people" ( "id" TEXT NOT NULL, "created_by_id" TEXT NOT NULL, "full_name" TEXT NOT NULL, "email" TEXT, "phone" TEXT, "guardian_name" TEXT, "guardian_phone" TEXT, "notes" TEXT, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP(3) NOT NULL, CONSTRAINT "people_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "student_account_people" ( "account_id" TEXT NOT NULL, "person_id" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "student_account_people_pkey" PRIMARY KEY ("account_id","person_id") ); -- Grouping: same teacher + same phone digits. `group_rank` splits the (rare) -- rows that already shared a phone inside one group, which the per-group unique -- membership would reject: the extra ones become their own person. CREATE TEMP TABLE "_attendee_keys" AS SELECT a."id" AS attendee_id, a."group_id", a."created_at", g."created_by_id", NULLIF(regexp_replace(COALESCE(a."phone", ''), '\D', '', 'g'), '') AS phone_digits, ROW_NUMBER() OVER ( PARTITION BY g."created_by_id", a."group_id", NULLIF(regexp_replace(COALESCE(a."phone", ''), '\D', '', 'g'), '') ORDER BY a."created_at", a."id" ) AS group_rank FROM "attendees" a JOIN "groups" g ON g."id" = a."group_id"; CREATE TEMP TABLE "_attendee_person" AS SELECT k.attendee_id, k.created_by_id, -- The person reuses the id of its oldest attendee. FIRST_VALUE(k.attendee_id) OVER ( PARTITION BY k.created_by_id, CASE WHEN k.phone_digits IS NULL THEN k.attendee_id ELSE k.phone_digits || '#' || k.group_rank END ORDER BY k.created_at, k.attendee_id ) AS person_id, (k.phone_digits IS NOT NULL AND k.group_rank > 1) AS phone_clash FROM "_attendee_keys" k; INSERT INTO "people" ("id", "created_by_id", "full_name", "email", "phone", "guardian_name", "guardian_phone", "notes", "created_at", "updated_at") SELECT p.person_id, MIN(p.created_by_id), MIN(rep."full_name"), (ARRAY_AGG(a."email" ORDER BY a."created_at") FILTER (WHERE a."email" IS NOT NULL))[1], CASE WHEN BOOL_OR(p.phone_clash) THEN NULL ELSE NULLIF(TRIM(MIN(rep."phone")), '') END, (ARRAY_AGG(a."guardian_name" ORDER BY a."created_at") FILTER (WHERE a."guardian_name" IS NOT NULL))[1], (ARRAY_AGG(a."guardian_phone" ORDER BY a."created_at") FILTER (WHERE a."guardian_phone" IS NOT NULL))[1], NULLIF(CONCAT_WS(E'\n\n', STRING_AGG(DISTINCT NULLIF(TRIM(a."notes"), ''), E'\n\n'), CASE WHEN BOOL_OR(p.phone_clash) THEN 'Teléfono: ' || MIN(rep."phone") END ), ''), MIN(a."created_at"), CURRENT_TIMESTAMP FROM "_attendee_person" p JOIN "attendees" a ON a."id" = p.attendee_id JOIN "attendees" rep ON rep."id" = p.person_id GROUP BY p.person_id; -- AlterTable: attendees -> membership ALTER TABLE "attendees" ADD COLUMN "person_id" TEXT; UPDATE "attendees" a SET "person_id" = p.person_id FROM "_attendee_person" p WHERE p.attendee_id = a."id"; ALTER TABLE "attendees" ALTER COLUMN "person_id" SET NOT NULL; -- Student accounts: link them to the person instead of the membership. INSERT INTO "student_account_people" ("account_id", "person_id", "created_at") SELECT sa."account_id", p.person_id, MIN(sa."created_at") FROM "student_account_attendees" sa JOIN "_attendee_person" p ON p.attendee_id = sa."attendee_id" GROUP BY sa."account_id", p.person_id; -- A merged person may end up with several accounts: keep the one used most -- recently (else the oldest) and fold the others into it. CREATE TEMP TABLE "_account_merge" AS WITH ranked AS ( SELECT sap."person_id", sap."account_id", ROW_NUMBER() OVER ( PARTITION BY sap."person_id" ORDER BY ( SELECT MAX(s."last_used_at") FROM "student_sessions" s WHERE s."account_id" = sap."account_id" AND s."revoked_at" IS NULL ) DESC NULLS LAST, acc."created_at", acc."id" ) AS rn FROM "student_account_people" sap JOIN "student_accounts" acc ON acc."id" = sap."account_id" ) SELECT DISTINCT ON (d."account_id") d."account_id" AS old_id, k."account_id" AS keep_id FROM ranked d JOIN ranked k ON k."person_id" = d."person_id" AND k.rn = 1 WHERE d.rn > 1 AND d."account_id" <> k."account_id" ORDER BY d."account_id"; INSERT INTO "student_account_people" ("account_id", "person_id", "created_at") SELECT m.keep_id, sap."person_id", sap."created_at" FROM "student_account_people" sap JOIN "_account_merge" m ON m.old_id = sap."account_id" ON CONFLICT DO NOTHING; UPDATE "absence_notices" n SET "account_id" = m.keep_id FROM "_account_merge" m WHERE n."account_id" = m.old_id; UPDATE "reported_payments" r SET "account_id" = m.keep_id FROM "_account_merge" m WHERE r."account_id" = m.old_id; UPDATE "student_accounts" k SET "recovery_email" = o."recovery_email", "recovery_email_verified_at" = o."recovery_email_verified_at" FROM "_account_merge" m JOIN "student_accounts" o ON o."id" = m.old_id WHERE k."id" = m.keep_id AND k."recovery_email" IS NULL AND o."recovery_email" IS NOT NULL; -- Sessions, tokens and passkeys of the folded accounts go with them (cascade); -- their links are removed by hand because that FK is created further down. DELETE FROM "student_account_people" WHERE "account_id" IN (SELECT old_id FROM "_account_merge"); DELETE FROM "student_accounts" WHERE "id" IN (SELECT old_id FROM "_account_merge"); -- DropTable DROP TABLE "student_account_attendees"; -- AlterTable ALTER TABLE "attendees" DROP COLUMN "full_name", DROP COLUMN "email", DROP COLUMN "phone", DROP COLUMN "guardian_name", DROP COLUMN "guardian_phone", DROP COLUMN "notes"; -- CreateIndex CREATE INDEX "people_created_by_id_idx" ON "people"("created_by_id"); -- One person per phone and teacher. Partial: Prisma does not model it. CREATE UNIQUE INDEX "people_owner_phone_key" ON "people"("created_by_id", "phone") WHERE "phone" IS NOT NULL; -- CreateIndex CREATE INDEX "student_account_people_person_id_idx" ON "student_account_people"("person_id"); -- CreateIndex CREATE INDEX "attendees_person_id_idx" ON "attendees"("person_id"); -- CreateIndex CREATE UNIQUE INDEX "attendees_group_id_person_id_key" ON "attendees"("group_id", "person_id"); -- AddForeignKey ALTER TABLE "people" ADD CONSTRAINT "people_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "attendees" ADD CONSTRAINT "attendees_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "student_account_people" ADD CONSTRAINT "student_account_people_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "student_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "student_account_people" ADD CONSTRAINT "student_account_people_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE; DROP TABLE "_account_merge"; DROP TABLE "_attendee_person"; DROP TABLE "_attendee_keys"; COMMIT;