Add foreign key constraints and perform on Cascade Delete (#1219)

This commit is contained in:
Charles Bochet
2023-08-15 20:52:23 +02:00
committed by GitHub
parent 83b900e016
commit 7a330b4a02
3 changed files with 68 additions and 43 deletions

View File

@ -99,15 +99,7 @@ export class UserService {
workspaceId: string;
userId: string;
}) {
const {
workspaceMember,
company,
comment,
attachment,
refreshToken,
activity,
activityTarget,
} = this.prismaService.client;
const { workspaceMember, refreshToken } = this.prismaService.client;
const user = await this.findUnique({
where: {
id: userId,
@ -141,35 +133,15 @@ export class UserService {
select: { id: true },
});
} else {
const where = { authorId: userId };
const activities = await activity.findMany({
where,
});
await this.prismaService.client.$transaction([
workspaceMember.deleteMany({
where: { userId },
}),
company.deleteMany({
where: { accountOwnerId: userId },
}),
comment.deleteMany({
where,
}),
attachment.deleteMany({
where,
}),
refreshToken.deleteMany({
where: { userId },
}),
...activities.map(({ id: activityId }) =>
activityTarget.deleteMany({
where: { activityId },
}),
),
activity.deleteMany({
where,
}),
this.delete({ where: { id: userId } }),
]);
}

View File

@ -0,0 +1,53 @@
-- DropForeignKey
ALTER TABLE "activities" DROP CONSTRAINT "activities_authorId_fkey";
-- DropForeignKey
ALTER TABLE "attachments" DROP CONSTRAINT "attachments_activityId_fkey";
-- DropForeignKey
ALTER TABLE "attachments" DROP CONSTRAINT "attachments_authorId_fkey";
-- DropForeignKey
ALTER TABLE "favorites" DROP CONSTRAINT "favorites_companyId_fkey";
-- DropForeignKey
ALTER TABLE "favorites" DROP CONSTRAINT "favorites_personId_fkey";
-- DropForeignKey
ALTER TABLE "pipeline_progresses" DROP CONSTRAINT "pipeline_progresses_pipelineId_fkey";
-- DropForeignKey
ALTER TABLE "pipeline_progresses" DROP CONSTRAINT "pipeline_progresses_pipelineStageId_fkey";
-- DropForeignKey
ALTER TABLE "pipeline_stages" DROP CONSTRAINT "pipeline_stages_pipelineId_fkey";
-- DropForeignKey
ALTER TABLE "comments" DROP CONSTRAINT "comments_authorId_fkey";
-- AddForeignKey
ALTER TABLE "activities" ADD CONSTRAINT "activities_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pipeline_stages" ADD CONSTRAINT "pipeline_stages_pipelineId_fkey" FOREIGN KEY ("pipelineId") REFERENCES "pipelines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pipeline_progresses" ADD CONSTRAINT "pipeline_progresses_pipelineId_fkey" FOREIGN KEY ("pipelineId") REFERENCES "pipelines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pipeline_progresses" ADD CONSTRAINT "pipeline_progresses_pipelineStageId_fkey" FOREIGN KEY ("pipelineStageId") REFERENCES "pipeline_stages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_activityId_fkey" FOREIGN KEY ("activityId") REFERENCES "activities"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "favorites" ADD CONSTRAINT "favorites_personId_fkey" FOREIGN KEY ("personId") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "favorites" ADD CONSTRAINT "favorites_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -233,7 +233,7 @@ model Company {
employees Int?
people Person[]
accountOwner User? @relation(fields: [accountOwnerId], references: [id])
accountOwner User? @relation(fields: [accountOwnerId], references: [id], onDelete: SetNull)
accountOwnerId String?
/// @TypeGraphQL.omit(input: true, output: true)
workspace Workspace @relation(fields: [workspaceId], references: [id])
@ -346,9 +346,9 @@ model Activity {
activityTargets ActivityTarget[]
comments Comment[]
attachments Attachment[]
author User @relation(fields: [authorId], references: [id], name: "authoredActivities")
author User @relation(fields: [authorId], references: [id], name: "authoredActivities", onDelete: Cascade)
authorId String
assignee User? @relation(fields: [assigneeId], references: [id], name: "assignedActivities")
assignee User? @relation(fields: [assigneeId], references: [id], name: "assignedActivities", onDelete: SetNull)
assigneeId String?
/// @TypeGraphQL.omit(input: true, output: true)
workspace Workspace @relation(fields: [workspaceId], references: [id])
@ -371,7 +371,7 @@ model Comment {
/// @Validator.IsString()
body String
author User @relation(fields: [authorId], references: [id])
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String
activity Activity? @relation(fields: [activityId], references: [id], onDelete: Cascade)
activityId String?
@ -462,7 +462,7 @@ model PipelineStage {
pipelineProgresses PipelineProgress[]
///
pipeline Pipeline @relation(fields: [pipelineId], references: [id])
pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade)
pipelineId String
/// @TypeGraphQL.omit(input: true, output: true)
workspace Workspace @relation(fields: [workspaceId], references: [id])
@ -491,11 +491,11 @@ model PipelineProgress {
closeDate DateTime?
probability Int?
pipeline Pipeline @relation(fields: [pipelineId], references: [id])
pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade)
pipelineId String
pipelineStage PipelineStage @relation(fields: [pipelineStageId], references: [id])
pipelineStage PipelineStage @relation(fields: [pipelineStageId], references: [id], onDelete: Cascade)
pipelineStageId String
pointOfContact Person? @relation("PointOfContactPipelineProgress", fields: [pointOfContactId], references: [id])
pointOfContact Person? @relation("PointOfContactPipelineProgress", fields: [pointOfContactId], references: [id], onDelete: SetNull)
pointOfContactId String?
/// @TypeGraphQL.omit(input: true, output: true)
@ -540,9 +540,9 @@ model Attachment {
/// @TypeGraphQL.omit(input: true, output: true)
workspaceId String
author User @relation(fields: [authorId], references: [id], name: "authoredAttachments")
author User @relation(fields: [authorId], references: [id], name: "authoredAttachments", onDelete: Cascade)
authorId String
activity Activity @relation(fields: [activityId], references: [id])
activity Activity @relation(fields: [activityId], references: [id], onDelete: Cascade)
activityId String
/// @TypeGraphQL.omit(input: true, output: false)
@ -559,10 +559,10 @@ model Favorite {
id String @id @default(uuid())
workspaceId String?
/// @TypeGraphQL.omit(input: true, output: false)
person Person? @relation(fields: [personId], references: [id])
person Person? @relation(fields: [personId], references: [id], onDelete: Cascade)
personId String?
/// @TypeGraphQL.omit(input: true, output: false)
company Company? @relation(fields: [companyId], references: [id])
company Company? @relation(fields: [companyId], references: [id], onDelete: Cascade)
companyId String?
/// @TypeGraphQL.omit(input: true, output: false)
workspaceMember WorkspaceMember? @relation(fields: [workspaceMemberId], references: [id])