Skip to content

Commit

Permalink
Merge pull request #249 from giselles-ai/feat/delete-team-migration
Browse files Browse the repository at this point in the history
feat: Add cascade delete to team-related foreign key constraints
  • Loading branch information
shige authored Dec 18, 2024
2 parents d4d45c6 + 650931e commit f52650a
Show file tree
Hide file tree
Showing 4 changed files with 2,071 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const subscriptions = pgTable("subscriptions", {
dbId: serial("db_id").primaryKey(),
teamDbId: integer("team_db_id")
.notNull()
.references(() => teams.dbId),
.references(() => teams.dbId, { onDelete: "cascade" }),
status: text("status").$type<Stripe.Subscription.Status>().notNull(),
cancelAtPeriodEnd: boolean("cancel_at_period_end").notNull(),
cancelAt: timestamp("cancel_at"),
Expand Down Expand Up @@ -113,7 +113,7 @@ export const teamMemberships = pgTable(
.references(() => users.dbId),
teamDbId: integer("team_db_id")
.notNull()
.references(() => teams.dbId),
.references(() => teams.dbId, { onDelete: "cascade" }),
role: text("role").notNull().$type<TeamRole>(),
},
(teamMembership) => ({
Expand Down
15 changes: 15 additions & 0 deletions migrations/0019_oval_zaran.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ALTER TABLE "subscriptions" DROP CONSTRAINT "subscriptions_team_db_id_teams_db_id_fk";
--> statement-breakpoint
ALTER TABLE "team_memberships" DROP CONSTRAINT "team_memberships_team_db_id_teams_db_id_fk";
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_team_db_id_teams_db_id_fk" FOREIGN KEY ("team_db_id") REFERENCES "public"."teams"("db_id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "team_memberships" ADD CONSTRAINT "team_memberships_team_db_id_teams_db_id_fk" FOREIGN KEY ("team_db_id") REFERENCES "public"."teams"("db_id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit f52650a

Please sign in to comment.