Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: add security invoker to mrp views

Brad Barbin160dbf5f48dc
diff --git a/packages/database/supabase/migrations/20250616011758_mrp-view-security.sql b/packages/database/supabase/migrations/20250616011758_mrp-view-security.sql
new file mode 100644
index 0000000000..227cbe121b
--- /dev/null
+++ b/packages/database/supabase/migrations/20250616011758_mrp-view-security.sql
@@ -0,0 +1,104 @@
+DROP VIEW IF EXISTS "openSalesOrderLines";
+CREATE OR REPLACE VIEW "openSalesOrderLines" WITH (security_invoker=true) AS (
+ SELECT
+ sol."id",
+ sol."salesOrderId",
+ sol."itemId",
+ sol."promisedDate",
+ sol."methodType",
+ sol."unitOfMeasureCode",
+ sol."quantityToSend",
+ sol."salesOrderLineType",
+ sol."companyId",
+ COALESCE(sol."locationId", so."locationId") AS "locationId",
+ i."replenishmentSystem",
+ i."itemTrackingType",
+ ir."leadTime" AS "leadTime"
+ FROM "salesOrderLine" sol
+ INNER JOIN "salesOrder" so ON sol."salesOrderId" = so."id"
+ INNER JOIN "item" i ON sol."itemId" = i."id"
+ INNER JOIN "itemReplenishment" ir ON i."id" = ir."itemId"
+ WHERE
+ sol."salesOrderLineType" != 'Service'
+ AND sol."methodType" != 'Make'
+ AND so."status" IN ('To Ship', 'To Ship and Invoice')
+);
+
+DROP VIEW IF EXISTS "openJobMaterialLines";
+CREATE OR REPLACE VIEW "openJobMaterialLines" WITH (security_invoker=true) AS (
+ SELECT
+ jm."id",
+ jm."jobId",
+ jm."itemId",
+ jm."quantityToIssue",
+ jm."unitOfMeasureCode",
+ jm."companyId",
+ i1."replenishmentSystem",
+ i1."itemTrackingType",
+ ir."leadTime" AS "leadTime",
+ j."locationId",
+ j."dueDate"
+ FROM "jobMaterial" jm
+ INNER JOIN "job" j ON jm."jobId" = j."id"
+ INNER JOIN "item" i1 ON jm."itemId" = i1."id"
+ INNER JOIN "item" i2 ON j."itemId" = i2."id"
+ INNER JOIN "itemReplenishment" ir ON i2."id" = ir."itemId"
+ WHERE j."status" IN (
+ 'Planned',
+ 'Ready',
+ 'In Progress',
+ 'Paused'
+ )
+ AND jm."methodType" != 'Make'
+);
+
+DROP VIEW IF EXISTS "openProductionOrders";
+CREATE OR REPLACE VIEW "openProductionOrders" WITH (security_invoker=true) AS (
+ SELECT
+ j."id",
+ j."itemId",
+ j."productionQuantity" - j."quantityReceivedToInventory" AS "quantityToReceive",
+ j."unitOfMeasureCode",
+ j."companyId",
+ i."replenishmentSystem",
+ i."itemTrackingType",
+ ir."leadTime" AS "leadTime",
+ j."locationId",
+ j."dueDate"
+ FROM "job" j
+ INNER JOIN "item" i ON j."itemId" = i."id"
+ INNER JOIN "itemReplenishment" ir ON i."id" = ir."itemId"
+ WHERE j."status" IN (
+ 'Planned',
+ 'Ready',
+ 'In Progress',
+ 'Paused'
+ )
+ AND j."salesOrderId" IS NULL
+);
+
+DROP VIEW IF EXISTS "openPurchaseOrderLines";
+CREATE OR REPLACE VIEW "openPurchaseOrderLines" WITH (security_invoker=true) AS (
+ SELECT
+ pol."id",
+ pol."purchaseOrderId",
+ pol."itemId",
+ pol."quantityToReceive" * pol."conversionFactor" AS "quantityToReceive",
+ i."unitOfMeasureCode",
+ pol."purchaseOrderLineType",
+ pol."companyId",
+ pol."locationId",
+ po."orderDate",
+ COALESCE(pol."promisedDate", pod."receiptPromisedDate") AS "promisedDate",
+ i."replenishmentSystem",
+ i."itemTrackingType",
+ ir."leadTime" AS "leadTime"
+ FROM "purchaseOrderLine" pol
+ INNER JOIN "purchaseOrder" po ON pol."purchaseOrderId" = po."id"
+ INNER JOIN "purchaseOrderDelivery" pod ON pod."id" = po."id"
+ INNER JOIN "item" i ON pol."itemId" = i."id"
+ INNER JOIN "itemReplenishment" ir ON i."id" = ir."itemId"
+ WHERE
+ pol."purchaseOrderLineType" != 'Service'
+ AND po."status" IN ('To Receive', 'To Receive and Invoice', 'Planned')
+);
\ No newline at end of file