Skip to content

Commit 32c1a36

Browse files
committed
feat: add User and Action models to Prisma schema, replacing ModerationAction and updating relationships
1 parent 304fabc commit 32c1a36

3 files changed

Lines changed: 130 additions & 1 deletion

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the `ModerationAction` table. If the table is not empty, all the data it contains will be lost.
5+
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint.
6+
- You are about to drop the column `discordId` on the `User` table. All the data in the column will be lost.
7+
- You are about to drop the column `id` on the `User` table. All the data in the column will be lost.
8+
- Added the required column `discordUserId` to the `User` table without a default value. This is not possible if the table is not empty.
9+
- Added the required column `userId` to the `User` table without a default value. This is not possible if the table is not empty.
10+
11+
*/
12+
-- DropIndex
13+
DROP INDEX "ModerationAction_createdAt_idx";
14+
15+
-- DropIndex
16+
DROP INDEX "ModerationAction_status_idx";
17+
18+
-- DropIndex
19+
DROP INDEX "ModerationAction_moderatorId_idx";
20+
21+
-- DropIndex
22+
DROP INDEX "ModerationAction_targetId_idx";
23+
24+
-- DropTable
25+
PRAGMA foreign_keys=off;
26+
DROP TABLE "ModerationAction";
27+
PRAGMA foreign_keys=on;
28+
29+
-- CreateTable
30+
CREATE TABLE "Action" (
31+
"actionId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
32+
"userId" INTEGER NOT NULL,
33+
"type" TEXT NOT NULL,
34+
"status" TEXT NOT NULL DEFAULT 'ACTIVE',
35+
"duration" INTEGER,
36+
"moderatorUserId" INTEGER NOT NULL,
37+
"reason" TEXT NOT NULL,
38+
"note" TEXT,
39+
"createdAt" INTEGER NOT NULL,
40+
"expiresAt" INTEGER,
41+
"revertingActionId" INTEGER,
42+
CONSTRAINT "Action_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("userId") ON DELETE RESTRICT ON UPDATE CASCADE,
43+
CONSTRAINT "Action_moderatorUserId_fkey" FOREIGN KEY ("moderatorUserId") REFERENCES "User" ("userId") ON DELETE RESTRICT ON UPDATE CASCADE,
44+
CONSTRAINT "Action_revertingActionId_fkey" FOREIGN KEY ("revertingActionId") REFERENCES "Action" ("actionId") ON DELETE SET NULL ON UPDATE CASCADE
45+
);
46+
47+
-- RedefineTables
48+
PRAGMA defer_foreign_keys=ON;
49+
PRAGMA foreign_keys=OFF;
50+
CREATE TABLE "new_User" (
51+
"userId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
52+
"discordUserId" TEXT NOT NULL,
53+
"role" TEXT NOT NULL DEFAULT 'MEMBER'
54+
);
55+
DROP TABLE "User";
56+
ALTER TABLE "new_User" RENAME TO "User";
57+
CREATE UNIQUE INDEX "User_discordUserId_key" ON "User"("discordUserId");
58+
PRAGMA foreign_keys=ON;
59+
PRAGMA defer_foreign_keys=OFF;
60+
61+
-- CreateIndex
62+
CREATE UNIQUE INDEX "Action_revertingActionId_key" ON "Action"("revertingActionId");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `duration` on the `Action` table. All the data in the column will be lost.
5+
6+
*/
7+
-- RedefineTables
8+
PRAGMA defer_foreign_keys=ON;
9+
PRAGMA foreign_keys=OFF;
10+
CREATE TABLE "new_Action" (
11+
"actionId" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
12+
"userId" INTEGER NOT NULL,
13+
"type" TEXT NOT NULL,
14+
"status" TEXT NOT NULL DEFAULT 'ACTIVE',
15+
"moderatorUserId" INTEGER NOT NULL,
16+
"reason" TEXT NOT NULL,
17+
"note" TEXT,
18+
"createdAt" INTEGER NOT NULL,
19+
"expiresAt" INTEGER,
20+
"revertingActionId" INTEGER,
21+
CONSTRAINT "Action_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("userId") ON DELETE RESTRICT ON UPDATE CASCADE,
22+
CONSTRAINT "Action_moderatorUserId_fkey" FOREIGN KEY ("moderatorUserId") REFERENCES "User" ("userId") ON DELETE RESTRICT ON UPDATE CASCADE,
23+
CONSTRAINT "Action_revertingActionId_fkey" FOREIGN KEY ("revertingActionId") REFERENCES "Action" ("actionId") ON DELETE SET NULL ON UPDATE CASCADE
24+
);
25+
INSERT INTO "new_Action" ("actionId", "createdAt", "expiresAt", "moderatorUserId", "note", "reason", "revertingActionId", "status", "type", "userId") SELECT "actionId", "createdAt", "expiresAt", "moderatorUserId", "note", "reason", "revertingActionId", "status", "type", "userId" FROM "Action";
26+
DROP TABLE "Action";
27+
ALTER TABLE "new_Action" RENAME TO "Action";
28+
CREATE UNIQUE INDEX "Action_revertingActionId_key" ON "Action"("revertingActionId");
29+
PRAGMA foreign_keys=ON;
30+
PRAGMA defer_foreign_keys=OFF;

prisma/schema.prisma

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,48 @@ enum ActionStatus {
4444
enum ActionReason {
4545
SPAM
4646
SCAM
47-
DISRUPTION
47+
DISRESPCTING
4848
NSFW
4949
HATE_SPEECH
5050
SELF_PROMOTION
5151
JOB_POSTING
5252
FOR_HIRE
5353
OTHER
54+
}
55+
56+
model User {
57+
userId Int @id @default(autoincrement())
58+
discordUserId String @unique
59+
role Role @default(MEMBER)
60+
61+
// A user can have many actions performed on them
62+
actions Action[] @relation("UserActions")
63+
64+
// A user (as moderator) can perform many moderation actions
65+
moderatedActions Action[] @relation("ModeratorActions")
66+
}
67+
68+
model Action {
69+
actionId Int @id @default(autoincrement())
70+
userId Int
71+
type ActionType
72+
status ActionStatus @default(ACTIVE)
73+
moderatorUserId Int
74+
reason String
75+
note String?
76+
createdAt Int
77+
expiresAt Int?
78+
revertingActionId Int? @unique
79+
80+
// Relationship to the user this action was performed on
81+
user User @relation("UserActions", fields: [userId], references: [userId])
82+
83+
// Relationship to the moderator who performed this action
84+
moderator User @relation("ModeratorActions", fields: [moderatorUserId], references: [userId])
85+
86+
// Self-referential relationship: this action reverts another action
87+
revertingAction Action? @relation("ActionCorrections", fields: [revertingActionId], references: [actionId])
88+
89+
// Self-referential relationship: actions that revert this action
90+
revertedByActions Action[] @relation("ActionCorrections")
5491
}

0 commit comments

Comments
 (0)