Skip to content

Commit dbe0dfa

Browse files
committed
#3867 migration changes
1 parent ba79f6e commit dbe0dfa

4 files changed

Lines changed: 89 additions & 34 deletions

File tree

src/backend/src/prisma-query-args/bom.query-args.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const getMaterialQueryArgs = (organizationId: string) =>
2525
materialType: true,
2626
unit: true,
2727
manufacturer: true,
28-
reimbursementProduct: false,
28+
reimbursementProducts: false,
2929
reimbursementRequest: getReimbursementRequestQueryArgs(organizationId)
3030
}
3131
});
@@ -38,7 +38,7 @@ export const getMaterialPreviewQueryArgs = (organizationId: string) =>
3838
unit: true,
3939
manufacturer: true,
4040
materialType: true,
41-
reimbursementProduct: false,
41+
reimbursementProducts: false,
4242
reimbursementRequest: getReimbursementRequestQueryArgs(organizationId)
4343
}
4444
});

src/backend/src/prisma/migrations/20260115203541_bom_improvements/migration.sql

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-- DropForeignKey
2+
ALTER TABLE "Material" DROP CONSTRAINT "Material_manufacturerId_fkey";
3+
4+
-- AlterTable
5+
ALTER TABLE "Material" ALTER COLUMN "manufacturerId" DROP NOT NULL,
6+
ALTER COLUMN "manufacturerPartNumber" DROP NOT NULL,
7+
ALTER COLUMN "quantity" DROP NOT NULL,
8+
ALTER COLUMN "price" DROP NOT NULL,
9+
ALTER COLUMN "subtotal" DROP NOT NULL;
10+
11+
-- AlterTable
12+
ALTER TABLE "Reimbursement_Product" ADD COLUMN "materialId" TEXT;
13+
14+
-- CreateIndex
15+
CREATE INDEX "Reimbursement_Product_materialId_idx" ON "Reimbursement_Product"("materialId");
16+
17+
-- AddForeignKey
18+
ALTER TABLE "Reimbursement_Product" ADD CONSTRAINT "Reimbursement_Product_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material"("materialId") ON DELETE SET NULL ON UPDATE CASCADE;
19+
20+
-- AddForeignKey
21+
ALTER TABLE "Material" ADD CONSTRAINT "Material_manufacturerId_fkey" FOREIGN KEY ("manufacturerId") REFERENCES "Manufacturer"("id") ON DELETE SET NULL ON UPDATE CASCADE;
22+
23+
DO $$
24+
DECLARE
25+
material_record RECORD;
26+
new_reason_id TEXT;
27+
BEGIN
28+
-- Loop through all materials that are linked to RRs but don't have products yet
29+
FOR material_record IN
30+
SELECT
31+
m."materialId",
32+
m."name",
33+
m."subtotal",
34+
m."wbsElementId",
35+
m."reimbursementRequestId"
36+
FROM "Material" m
37+
WHERE m."reimbursementRequestId" IS NOT NULL
38+
AND m."dateDeleted" IS NULL
39+
AND EXISTS (
40+
-- Only migrate if the RR isn't deleted
41+
SELECT 1 FROM "Reimbursement_Request" rr
42+
WHERE rr."reimbursementRequestId" = m."reimbursementRequestId"
43+
AND rr."dateDeleted" IS NULL
44+
)
45+
AND EXISTS (
46+
-- Only migrate if the WBS element isn't deleted
47+
SELECT 1 FROM "WBS_Element" wbs
48+
WHERE wbs."wbsElementId" = m."wbsElementId"
49+
AND wbs."dateDeleted" IS NULL
50+
)
51+
AND NOT EXISTS (
52+
-- Skip if a product already links to this material for this RR
53+
SELECT 1 FROM "Reimbursement_Product" rp
54+
WHERE rp."materialId" = m."materialId"
55+
AND rp."reimbursementRequestId" = m."reimbursementRequestId"
56+
)
57+
LOOP
58+
-- Create a new reason for this material (can't reuse due to @unique and one to one relation)
59+
INSERT INTO "Reimbursement_Product_Reason" (
60+
"reimbursementProductReasonId",
61+
"wbsElementId"
62+
) VALUES (
63+
gen_random_uuid(),
64+
material_record."wbsElementId"
65+
)
66+
RETURNING "reimbursementProductReasonId" INTO new_reason_id;
67+
68+
-- Create the product linked to the material
69+
INSERT INTO "Reimbursement_Product" (
70+
"reimbursementProductId",
71+
"name",
72+
"cost",
73+
"materialId",
74+
"reimbursementProductReasonId",
75+
"reimbursementRequestId"
76+
) VALUES (
77+
gen_random_uuid(),
78+
material_record."name",
79+
COALESCE(material_record."subtotal", 0),
80+
material_record."materialId",
81+
new_reason_id,
82+
material_record."reimbursementRequestId"
83+
);
84+
END LOOP;
85+
END $$;

src/backend/src/prisma/schema.prisma

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ model Reimbursement_Product {
737737
dateDeleted DateTime?
738738
cost Int
739739
material Material? @relation(fields: [materialId], references: [materialId])
740-
materialId String? @unique
740+
materialId String?
741741
reimbursementProductReasonId String @unique
742742
reimbursementProductReason Reimbursement_Product_Reason @relation(fields: [reimbursementProductReasonId], references: [reimbursementProductReasonId])
743743
reimbursementRequestId String
@@ -924,7 +924,7 @@ model Material {
924924
notes String?
925925
reimbursementRequest Reimbursement_Request? @relation(fields: [reimbursementRequestId], references: [reimbursementRequestId])
926926
reimbursementRequestId String?
927-
reimbursementProduct Reimbursement_Product?
927+
reimbursementProducts Reimbursement_Product[]
928928
929929
@@index([assemblyId])
930930
@@index([materialTypeId])

0 commit comments

Comments
 (0)