feat: add editor record migration

This commit is contained in:
DarkSky
2024-08-19 11:27:55 +08:00
parent 611925fa10
commit d04e766ce9
2 changed files with 49 additions and 2 deletions
@@ -0,0 +1,28 @@
/*
Warnings:
- Added the required column `created_by` to the `snapshot_histories` table without a default value. This is not possible if the table is not empty.
- Added the required column `created_by` to the `updates` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "snapshot_histories" ADD COLUMN "created_by" VARCHAR NOT NULL;
-- AlterTable
ALTER TABLE "snapshots" ADD COLUMN "created_by" VARCHAR,
ADD COLUMN "updated_by" VARCHAR;
-- AlterTable
ALTER TABLE "updates" ADD COLUMN "created_by" VARCHAR NOT NULL;
-- AddForeignKey
ALTER TABLE "snapshots" ADD CONSTRAINT "snapshots_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "snapshots" ADD CONSTRAINT "snapshots_updated_by_fkey" FOREIGN KEY ("updated_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "updates" ADD CONSTRAINT "updates_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "snapshot_histories" ADD CONSTRAINT "snapshot_histories_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+21 -2
View File
@@ -33,6 +33,10 @@ model User {
aiSessions AiSession[]
updatedRuntimeConfigs RuntimeConfig[]
userSnapshots UserSnapshot[]
createdSnapshot Snapshot[] @relation("createdSnapshot")
updatedSnapshot Snapshot[] @relation("updatedSnapshot")
createdUpdate Update[] @relation("createdUpdate")
createdHistory SnapshotHistory[] @relation("createdHistory")
@@index([email])
@@map("users")
@@ -241,9 +245,16 @@ model Snapshot {
// the `updated_at` field will not record the time of record changed,
// but the created time of last seen update that has been merged into snapshot.
updatedAt DateTime @map("updated_at") @db.Timestamptz(3)
createdBy String? @map("created_by") @db.VarChar
updatedBy String? @map("updated_by") @db.VarChar
// should not delete origin snapshot even if user is deleted
// we only delete the snapshot if the workspace is deleted
createdByUser User? @relation(name: "createdSnapshot", fields: [createdBy], references: [id], onDelete: SetNull)
updatedByUser User? @relation(name: "updatedSnapshot", fields: [updatedBy], references: [id], onDelete: SetNull)
// @deprecated use updatedAt only
seq Int? @default(0) @db.Integer
seq Int? @default(0) @db.Integer
// we need to clear all hanging updates and snapshots before enable the foreign key on workspaceId
// workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
@@ -274,9 +285,13 @@ model Update {
id String @map("guid") @db.VarChar
blob Bytes @db.ByteA
createdAt DateTime @map("created_at") @db.Timestamptz(3)
createdBy String @map("created_by") @db.VarChar
// delete update if createor's account is deleted
createdByUser User @relation(name: "createdUpdate", fields: [createdBy], references: [id], onDelete: Cascade)
// @deprecated use createdAt only
seq Int? @db.Integer
seq Int? @db.Integer
@@id([workspaceId, id, createdAt])
@@map("updates")
@@ -289,6 +304,10 @@ model SnapshotHistory {
blob Bytes @db.ByteA
state Bytes? @db.ByteA
expiredAt DateTime @map("expired_at") @db.Timestamptz(3)
createdBy String @map("created_by") @db.VarChar
// delete history if creator's account is deleted
createdByUser User @relation(name: "createdHistory", fields: [createdBy], references: [id], onDelete: Cascade)
@@id([workspaceId, id, timestamp])
@@map("snapshot_histories")