From 7a06b67ccbf410341a0b809e6558e8f8ba40337e Mon Sep 17 00:00:00 2001 From: Rami Abdou Date: Tue, 23 Sep 2025 21:48:03 -0700 Subject: [PATCH] migration to fix cascading deletes --- .../migrations/20250924044343_fix_removal.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 packages/db/src/migrations/20250924044343_fix_removal.ts diff --git a/packages/db/src/migrations/20250924044343_fix_removal.ts b/packages/db/src/migrations/20250924044343_fix_removal.ts new file mode 100644 index 000000000..c66945658 --- /dev/null +++ b/packages/db/src/migrations/20250924044343_fix_removal.ts @@ -0,0 +1,41 @@ +import { type Kysely } from 'kysely'; + +export async function up(db: Kysely) { + // applications + + await db.schema + .alterTable('applications') + .dropConstraint('applications_referral_id_fkey') + .execute(); + + await db.schema + .alterTable('applications') + .addForeignKeyConstraint( + 'applications_referral_id_fkey', + ['referral_id'], + 'referrals', + ['id'] + ) + .onDelete('cascade') + .execute(); + + // opportunity_reports + + await db.schema + .alterTable('opportunity_reports') + .dropConstraint('opportunity_reports_reporter_id_fkey') + .execute(); + + await db.schema + .alterTable('opportunity_reports') + .addForeignKeyConstraint( + 'opportunity_reports_reporter_id_fkey', + ['reporter_id'], + 'students', + ['id'] + ) + .onDelete('cascade') + .execute(); +} + +export async function down(_: Kysely) {}