File tree Expand file tree Collapse file tree
migrations/20240821145041_add_import_id Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff 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
91100model 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
You can’t perform that action at this time.
0 commit comments