Skip to content

Commit e1bc35b

Browse files
committed
Clean up database schema
1 parent e34eef4 commit e1bc35b

File tree

8 files changed

+108
-109
lines changed

8 files changed

+108
-109
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "User" ADD COLUMN "enabled" BOOLEAN NOT NULL DEFAULT true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `boxNumber` to the `Item` table without a default value. This is not possible if the table is not empty.
5+
- Added the required column `palletNumber` to the `Item` table without a default value. This is not possible if the table is not empty.
6+
- Changed the type of `unitPrice` on the `Item` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
7+
8+
*/
9+
-- AlterTable
10+
ALTER TABLE "Item" ADD COLUMN "boxNumber" INTEGER NOT NULL,
11+
ADD COLUMN "palletNumber" INTEGER NOT NULL,
12+
DROP COLUMN "unitPrice",
13+
ADD COLUMN "unitPrice" MONEY NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- DropForeignKey
2+
ALTER TABLE "RequestedUnclaimedItem" DROP CONSTRAINT "RequestedUnclaimedItem_itemId_fkey";
3+
4+
-- DropForeignKey
5+
ALTER TABLE "RequestedUnclaimedItem" DROP CONSTRAINT "RequestedUnclaimedItem_requestId_fkey";
6+
7+
-- DropForeignKey
8+
ALTER TABLE "UnallocatedItemRequest" DROP CONSTRAINT "UnallocatedItemRequest_itemId_fkey";
9+
10+
-- DropForeignKey
11+
ALTER TABLE "UnallocatedItemRequest" DROP CONSTRAINT "UnallocatedItemRequest_partnerId_fkey";
12+
13+
-- DropForeignKey
14+
ALTER TABLE "UnclaimedItemRequest" DROP CONSTRAINT "UnclaimedItemRequest_partnerId_fkey";
15+
16+
-- AddForeignKey
17+
ALTER TABLE "UnallocatedItemRequest" ADD CONSTRAINT "UnallocatedItemRequest_partnerId_fkey" FOREIGN KEY ("partnerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
18+
19+
-- AddForeignKey
20+
ALTER TABLE "UnallocatedItemRequest" ADD CONSTRAINT "UnallocatedItemRequest_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE CASCADE ON UPDATE CASCADE;
21+
22+
-- AddForeignKey
23+
ALTER TABLE "UnclaimedItemRequest" ADD CONSTRAINT "UnclaimedItemRequest_partnerId_fkey" FOREIGN KEY ("partnerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
24+
25+
-- AddForeignKey
26+
ALTER TABLE "RequestedUnclaimedItem" ADD CONSTRAINT "RequestedUnclaimedItem_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "UnclaimedItem"("id") ON DELETE CASCADE ON UPDATE CASCADE;
27+
28+
-- AddForeignKey
29+
ALTER TABLE "RequestedUnclaimedItem" ADD CONSTRAINT "RequestedUnclaimedItem_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "UnclaimedItemRequest"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the `PartnerDetails` table. If the table is not empty, all the data it contains will be lost.
5+
- You are about to drop the `RequestedUnclaimedItem` table. If the table is not empty, all the data it contains will be lost.
6+
- You are about to drop the `UnclaimedItem` table. If the table is not empty, all the data it contains will be lost.
7+
- You are about to drop the `UnclaimedItemRequest` table. If the table is not empty, all the data it contains will be lost.
8+
9+
*/
10+
-- DropForeignKey
11+
ALTER TABLE "RequestedUnclaimedItem" DROP CONSTRAINT "RequestedUnclaimedItem_itemId_fkey";
12+
13+
-- DropForeignKey
14+
ALTER TABLE "RequestedUnclaimedItem" DROP CONSTRAINT "RequestedUnclaimedItem_requestId_fkey";
15+
16+
-- DropForeignKey
17+
ALTER TABLE "UnclaimedItemRequest" DROP CONSTRAINT "UnclaimedItemRequest_partnerId_fkey";
18+
19+
-- DropTable
20+
DROP TABLE "PartnerDetails";
21+
22+
-- DropTable
23+
DROP TABLE "RequestedUnclaimedItem";
24+
25+
-- DropTable
26+
DROP TABLE "UnclaimedItem";
27+
28+
-- DropTable
29+
DROP TABLE "UnclaimedItemRequest";
30+
31+
-- DropEnum
32+
DROP TYPE "OrganizationType";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `visible` to the `Item` table without a default value. This is not possible if the table is not empty.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "Item" ADD COLUMN "visible" BOOLEAN NOT NULL;

prisma/schema.prisma

+17-65
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ model User {
2020
name String
2121
passwordHash String
2222
type UserType
23+
enabled Boolean @default(true)
2324
2425
// Partner-only fields
2526
partnerDetails Json?
2627
unallocatedItemRequests UnallocatedItemRequest[]
27-
28-
// Deprecated
29-
unclaimedItemRequests UnclaimedItemRequest[]
3028
}
3129

3230
model UserInvite {
@@ -39,87 +37,41 @@ model UserInvite {
3937
expiration DateTime
4038
}
4139

42-
// Deprecated
43-
enum OrganizationType {
44-
NON_PROFIT
45-
FOR_PROFIT
46-
RELIGIOUS
47-
}
48-
49-
// Deprecated
50-
model PartnerDetails {
51-
id Int @id @default(autoincrement())
52-
53-
userId Int @unique
54-
55-
// Deprecated
56-
numberOfPatients Int
57-
// Deprecated
58-
organizationType OrganizationType
59-
}
60-
6140
model Item {
6241
id Int @id @default(autoincrement())
6342
64-
title String
65-
category String
43+
// Columns that define a general item
44+
title String
45+
category String
46+
expirationDate DateTime?
47+
unitSize Int
48+
49+
// Columns that define a unique line item
50+
donorName String
6651
quantity Int
67-
expirationDate DateTime?
68-
unitSize Int
52+
lotNumber Int
53+
palletNumber Int
54+
boxNumber Int
6955
unitType String
7056
datePosted DateTime
71-
lotNumber Int
72-
donorName String
73-
unitPrice Float
57+
unitPrice Decimal @db.Money
7458
maxRequestLimit String
7559
60+
visible Boolean
61+
7662
unallocatedItemRequests UnallocatedItemRequest[]
7763
}
7864

7965
model UnallocatedItemRequest {
8066
id Int @id @default(autoincrement())
8167
8268
// Points to User, but only valid for `PARTNER` users
83-
partner User @relation(fields: [partnerId], references: [id])
69+
partner User @relation(fields: [partnerId], references: [id], onDelete: Cascade)
8470
partnerId Int
8571
86-
item Item @relation(fields: [itemId], references: [id])
72+
item Item @relation(fields: [itemId], references: [id], onDelete: Cascade)
8773
itemId Int
8874
8975
quantity Int
9076
comments String
9177
}
92-
93-
// Deprecated
94-
model UnclaimedItem {
95-
id Int @id @default(autoincrement())
96-
97-
name String
98-
quantity Int
99-
expirationDate DateTime?
100-
RequestedUnclaimedItem RequestedUnclaimedItem[]
101-
}
102-
103-
// Deprecated
104-
model UnclaimedItemRequest {
105-
id Int @id @default(autoincrement())
106-
107-
// Points to User, but only valid for `PARTNER` users
108-
partner User @relation(fields: [partnerId], references: [id])
109-
partnerId Int
110-
111-
items RequestedUnclaimedItem[]
112-
}
113-
114-
// Deprecated
115-
model RequestedUnclaimedItem {
116-
id Int @id @default(autoincrement())
117-
118-
item UnclaimedItem @relation(fields: [itemId], references: [id])
119-
itemId Int
120-
121-
quantity Int
122-
123-
request UnclaimedItemRequest @relation(fields: [requestId], references: [id])
124-
requestId Int
125-
}

scripts/clear.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import { db } from "@/db";
44

55
async function run() {
66
await db.$transaction(async (tx) => {
7-
await tx.unallocatedItemRequest.deleteMany();
8-
await tx.item.deleteMany();
9-
await tx.requestedUnclaimedItem.deleteMany();
10-
await tx.unclaimedItemRequest.deleteMany();
11-
await tx.unclaimedItem.deleteMany();
12-
await tx.userInvite.deleteMany();
13-
await tx.partnerDetails.deleteMany();
147
await tx.user.deleteMany();
8+
await tx.userInvite.deleteMany();
9+
await tx.item.deleteMany();
10+
await tx.unallocatedItemRequest.deleteMany();
1511
});
1612
}
1713

scripts/seed.ts

+4-37
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import "dotenv/config";
22
import { exit } from "process";
33
import { db } from "@/db";
44
import { hash } from "argon2";
5-
import { OrganizationType, UserType } from "@prisma/client";
5+
import { UserType } from "@prisma/client";
66

77
async function run() {
88
await db.$transaction(async (tx) => {
9-
await tx.requestedUnclaimedItem.deleteMany();
10-
await tx.unclaimedItemRequest.deleteMany();
11-
await tx.unclaimedItem.deleteMany();
12-
await tx.partnerDetails.deleteMany();
139
await tx.user.deleteMany();
1410
await tx.userInvite.deleteMany();
11+
await tx.item.deleteMany();
12+
await tx.unallocatedItemRequest.deleteMany();
1513

1614
await tx.user.createMany({
1715
data: [
@@ -38,42 +36,11 @@ async function run() {
3836
passwordHash: await hash("root"),
3937
type: "PARTNER",
4038
name: "Partner",
41-
partnerDetails: {
42-
numberOfPatients: 10,
43-
organizationType: OrganizationType.NON_PROFIT,
44-
},
39+
partnerDetails: {},
4540
},
4641
],
4742
});
4843

49-
const banana = await tx.unclaimedItem.create({
50-
data: { name: "Banana", quantity: 10, expirationDate: new Date() },
51-
});
52-
53-
const apple = await tx.unclaimedItem.create({
54-
data: { name: "Apple", quantity: 100, expirationDate: new Date() },
55-
});
56-
57-
await tx.unclaimedItemRequest.create({
58-
data: {
59-
items: {
60-
createMany: {
61-
data: [
62-
{
63-
itemId: banana.id,
64-
quantity: 5,
65-
},
66-
{
67-
itemId: apple.id,
68-
quantity: 10,
69-
},
70-
],
71-
},
72-
},
73-
partner: { connect: { email: "partner@test.com" } },
74-
},
75-
});
76-
7744
await tx.userInvite.create({
7845
data: {
7946
email: "new-admin@test.com",

0 commit comments

Comments
 (0)