Skip to content

Commit

Permalink
cascade db
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Aug 9, 2023
1 parent d58cce6 commit 0b063e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
17 changes: 17 additions & 0 deletions prisma/migrations/20230809202431_cascade/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- DropForeignKey
ALTER TABLE "NetworkMemberNotation" DROP CONSTRAINT "NetworkMemberNotation_nodeid_fkey";

-- DropForeignKey
ALTER TABLE "Notation" DROP CONSTRAINT "Notation_nwid_fkey";

-- DropForeignKey
ALTER TABLE "network_members" DROP CONSTRAINT "network_members_nwid_fkey";

-- AddForeignKey
ALTER TABLE "network_members" ADD CONSTRAINT "network_members_nwid_fkey" FOREIGN KEY ("nwid") REFERENCES "network"("nwid") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Notation" ADD CONSTRAINT "Notation_nwid_fkey" FOREIGN KEY ("nwid") REFERENCES "network"("nwid") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NetworkMemberNotation" ADD CONSTRAINT "NetworkMemberNotation_nodeid_fkey" FOREIGN KEY ("nodeid") REFERENCES "network_members"("nodeid") ON DELETE CASCADE ON UPDATE CASCADE;
6 changes: 3 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ enum Role {
model network_members {
nodeid Int @id @default(autoincrement())
id String
nwid_ref network @relation(fields: [nwid], references: [nwid])
nwid_ref network @relation(fields: [nwid], references: [nwid], onDelete: Cascade)
nwid String
lastSeen DateTime?
online Boolean? @default(false)
Expand Down Expand Up @@ -93,7 +93,7 @@ model Notation {
updatedTime DateTime @updatedAt
isActive Boolean @default(true)
nwid String
network network @relation(fields: [nwid], references: [nwid])
network network @relation(fields: [nwid], references: [nwid], onDelete: Cascade)
networkMembers NetworkMemberNotation[]
icon String?
orderIndex Int?
Expand All @@ -106,7 +106,7 @@ model NetworkMemberNotation {
notationId Int
nodeid Int
label Notation @relation(fields: [notationId], references: [id])
member network_members @relation(fields: [nodeid], references: [nodeid])
member network_members @relation(fields: [nodeid], references: [nodeid], onDelete: Cascade)
@@id([notationId, nodeid])
}

Expand Down
27 changes: 8 additions & 19 deletions src/server/api/routers/networkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,32 +212,21 @@ export const networkRouter = createTRPCRouter({
)
.mutation(async ({ ctx, input }) => {
try {
// Delete networkMembers
await ctx.prisma.network_members
.deleteMany({
where: {
nwid: input.nwid,
},
})
.catch(() => []);

// Delete network
await ctx.prisma.network
.deleteMany({
where: {
authorId: ctx.session.user.id,
nwid: input.nwid,
},
})
.catch(() => []);

// Delete ZT network
const createCentralNw = await ztController.network_delete(
input.nwid,
input.central,
);

if (input.central) return createCentralNw;

// Delete network
await ctx.prisma.network.deleteMany({
where: {
authorId: ctx.session.user.id,
nwid: input.nwid,
},
});
} catch (error) {
if (error instanceof z.ZodError) {
return throwError(`Invalid routes provided ${error.message}`);
Expand Down

0 comments on commit 0b063e4

Please sign in to comment.