|
| 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 $$; |
0 commit comments