Skip to content

Commit 951b952

Browse files
committed
feat: add import ids
1 parent 0d46400 commit 951b952

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `importId` to the `OrderItem` table without a default value. This is not possible if the table is not empty.
5+
6+
*/
7+
-- AlterTable
8+
9+
-- Begin by allowing NULL values
10+
ALTER TABLE "OrderItem" ADD COLUMN "importId" UUID;
11+
12+
-- CreateTable
13+
CREATE TABLE "OrderItemImport" (
14+
"id" UUID NOT NULL,
15+
"date" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
16+
"name" TEXT NOT NULL,
17+
18+
CONSTRAINT "OrderItemImport_pkey" PRIMARY KEY ("id")
19+
);
20+
21+
22+
23+
-- AddForeignKey
24+
ALTER TABLE "OrderItem" ADD CONSTRAINT "OrderItem_importId_fkey" FOREIGN KEY ("importId") REFERENCES "OrderItemImport"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
25+
26+
-- Create import for everything so far called "Initial Import"
27+
INSERT INTO "OrderItemImport" ("id", "name") VALUES (gen_random_uuid (), 'Initial Import (academic year 22-23)');
28+
29+
-- Set importId to the initial import
30+
UPDATE "OrderItem" SET "importId" = (SELECT "id" FROM "OrderItemImport" WHERE "name" = 'Initial Import (academic year 22-23)');
31+
32+
-- Make non-NULL
33+
ALTER TABLE "OrderItem" ALTER COLUMN "importId" SET NOT NULL;

collection/prisma/schema.prisma

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,28 @@ model Order {
8787
academicYearReference AcademicYear @relation(fields: [academicYear], references: [year])
8888
}
8989

90+
// Imports of data, to allow rollback
91+
model OrderItemImport {
92+
id String @id @default(cuid()) @db.Uuid
93+
date DateTime @default(now())
94+
name String
95+
96+
OrderItem OrderItem[]
97+
}
98+
9099
// OrderItems are the items that make up an order
91100
model OrderItem {
92101
id Int @id @default(autoincrement())
93102
orderId Int
94103
quantity Int
95104
variantId Int
96105
collected Boolean
97-
Order Order @relation(fields: [orderId], references: [id])
98-
Variant Variant @relation(fields: [variantId], references: [id])
106+
// tag the import this came from
107+
importId String @db.Uuid
108+
109+
Import OrderItemImport @relation(fields: [importId], references: [id])
110+
Order Order @relation(fields: [orderId], references: [id])
111+
Variant Variant @relation(fields: [variantId], references: [id])
99112
}
100113

101114
// RootItems are what we sell, and have Variants

0 commit comments

Comments
 (0)