Implement soft-delete for clients, projects, and time entries
Replace hard deletes with deletedAt timestamp flags on all three entities. Deleting a client or project only sets its own deletedAt; child records are excluded implicitly by filtering on parent deletedAt in every read query. Raw SQL statistics queries also filter out soft-deleted parents. FK ON DELETE CASCADE removed from Project→Client and TimeEntry→Project.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
-- AlterTable: add deleted_at column to clients
|
||||
ALTER TABLE "clients" ADD COLUMN "deleted_at" TIMESTAMP(3);
|
||||
|
||||
-- AlterTable: add deleted_at column to projects
|
||||
ALTER TABLE "projects" ADD COLUMN "deleted_at" TIMESTAMP(3);
|
||||
|
||||
-- AlterTable: add deleted_at column to time_entries
|
||||
ALTER TABLE "time_entries" ADD COLUMN "deleted_at" TIMESTAMP(3);
|
||||
|
||||
-- DropForeignKey: remove cascade from projects -> clients
|
||||
ALTER TABLE "projects" DROP CONSTRAINT "projects_client_id_fkey";
|
||||
|
||||
-- DropForeignKey: remove cascade from time_entries -> projects
|
||||
ALTER TABLE "time_entries" DROP CONSTRAINT "time_entries_project_id_fkey";
|
||||
|
||||
-- AddForeignKey: re-add without onDelete cascade
|
||||
ALTER TABLE "projects" ADD CONSTRAINT "projects_client_id_fkey" FOREIGN KEY ("client_id") REFERENCES "clients"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey: re-add without onDelete cascade
|
||||
ALTER TABLE "time_entries" ADD CONSTRAINT "time_entries_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -25,11 +25,12 @@ model User {
|
||||
}
|
||||
|
||||
model Client {
|
||||
id String @id @default(uuid())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
id String @id @default(uuid())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
deletedAt DateTime? @map("deleted_at")
|
||||
|
||||
userId String @map("user_id") @db.VarChar(255)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
@@ -41,17 +42,18 @@ model Client {
|
||||
}
|
||||
|
||||
model Project {
|
||||
id String @id @default(uuid())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
color String? @db.VarChar(7) // Hex color code
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
id String @id @default(uuid())
|
||||
name String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
color String? @db.VarChar(7) // Hex color code
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
deletedAt DateTime? @map("deleted_at")
|
||||
|
||||
userId String @map("user_id") @db.VarChar(255)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
clientId String @map("client_id")
|
||||
client Client @relation(fields: [clientId], references: [id], onDelete: Cascade)
|
||||
client Client @relation(fields: [clientId], references: [id])
|
||||
|
||||
timeEntries TimeEntry[]
|
||||
ongoingTimers OngoingTimer[]
|
||||
@@ -62,18 +64,19 @@ model Project {
|
||||
}
|
||||
|
||||
model TimeEntry {
|
||||
id String @id @default(uuid())
|
||||
startTime DateTime @map("start_time") @db.Timestamptz()
|
||||
endTime DateTime @map("end_time") @db.Timestamptz()
|
||||
breakMinutes Int @default(0) @map("break_minutes")
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
id String @id @default(uuid())
|
||||
startTime DateTime @map("start_time") @db.Timestamptz()
|
||||
endTime DateTime @map("end_time") @db.Timestamptz()
|
||||
breakMinutes Int @default(0) @map("break_minutes")
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
deletedAt DateTime? @map("deleted_at")
|
||||
|
||||
userId String @map("user_id") @db.VarChar(255)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
projectId String @map("project_id")
|
||||
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
|
||||
@@index([userId])
|
||||
@@index([userId, startTime])
|
||||
|
||||
Reference in New Issue
Block a user