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: failed db migration for dropping unitOfMeasureCode column

Brad Barbin6c36cc62967b
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index c9f5620648..854c6f2242 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -6872,6 +6872,7 @@ export type Database = {
Row: {
accountNumber: string | null
assetId: string | null
+ conversionFactor: number | null
createdAt: string
createdBy: string
currencyCode: string
@@ -6879,17 +6880,18 @@ export type Database = {
description: string | null
exchangeRate: number
id: string
+ inventoryUnitOfMeasureCode: string | null
invoiceId: string
invoiceLineType: Database["public"]["Enums"]["payableLineType"]
locationId: string | null
partId: string | null
purchaseOrderId: string | null
purchaseOrderLineId: string | null
+ purchaseUnitOfMeasureCode: string | null
quantity: number
serviceId: string | null
shelfId: string | null
totalAmount: number | null
- unitOfMeasureCode: string | null
unitPrice: number
updatedAt: string | null
updatedBy: string | null
@@ -6897,6 +6899,7 @@ export type Database = {
Insert: {
accountNumber?: string | null
assetId?: string | null
+ conversionFactor?: number | null
createdAt?: string
createdBy: string
currencyCode: string
@@ -6904,17 +6907,18 @@ export type Database = {
description?: string | null
exchangeRate?: number
id?: string
+ inventoryUnitOfMeasureCode?: string | null
invoiceId: string
invoiceLineType: Database["public"]["Enums"]["payableLineType"]
locationId?: string | null
partId?: string | null
purchaseOrderId?: string | null
purchaseOrderLineId?: string | null
+ purchaseUnitOfMeasureCode?: string | null
quantity?: number
serviceId?: string | null
shelfId?: string | null
totalAmount?: number | null
- unitOfMeasureCode?: string | null
unitPrice?: number
updatedAt?: string | null
updatedBy?: string | null
@@ -6922,6 +6926,7 @@ export type Database = {
Update: {
accountNumber?: string | null
assetId?: string | null
+ conversionFactor?: number | null
createdAt?: string
createdBy?: string
currencyCode?: string
@@ -6929,22 +6934,37 @@ export type Database = {
description?: string | null
exchangeRate?: number
id?: string
+ inventoryUnitOfMeasureCode?: string | null
invoiceId?: string
invoiceLineType?: Database["public"]["Enums"]["payableLineType"]
locationId?: string | null
partId?: string | null
purchaseOrderId?: string | null
purchaseOrderLineId?: string | null
+ purchaseUnitOfMeasureCode?: string | null
quantity?: number
serviceId?: string | null
shelfId?: string | null
totalAmount?: number | null
- unitOfMeasureCode?: string | null
unitPrice?: number
updatedAt?: string | null
updatedBy?: string | null
}
Relationships: [
+ {
+ foreignKeyName: "purchaseInvoiceLine_inventoryUnitOfMeasureCode_fkey"
+ columns: ["inventoryUnitOfMeasureCode"]
+ isOneToOne: false
+ referencedRelation: "unitOfMeasure"
+ referencedColumns: ["code"]
+ },
+ {
+ foreignKeyName: "purchaseInvoiceLine_purchaseUnitOfMeasureCode_fkey"
+ columns: ["purchaseUnitOfMeasureCode"]
+ isOneToOne: false
+ referencedRelation: "unitOfMeasure"
+ referencedColumns: ["code"]
+ },
{
foreignKeyName: "purchaseInvoiceLines_accountNumber_fkey"
columns: ["accountNumber"]
@@ -7106,13 +7126,6 @@ export type Database = {
referencedRelation: "shelf"
referencedColumns: ["id", "locationId"]
},
- {
- foreignKeyName: "purchaseInvoiceLines_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code"]
- },
{
foreignKeyName: "purchaseInvoiceLines_updatedBy_fkey"
columns: ["updatedBy"]
@@ -7782,7 +7795,6 @@ export type Database = {
serviceId: string | null
setupPrice: number | null
shelfId: string | null
- unitOfMeasureCode: string | null
unitPrice: number | null
updatedAt: string | null
updatedBy: string | null
@@ -7813,7 +7825,6 @@ export type Database = {
serviceId?: string | null
setupPrice?: number | null
shelfId?: string | null
- unitOfMeasureCode?: string | null
unitPrice?: number | null
updatedAt?: string | null
updatedBy?: string | null
@@ -7844,7 +7855,6 @@ export type Database = {
serviceId?: string | null
setupPrice?: number | null
shelfId?: string | null
- unitOfMeasureCode?: string | null
unitPrice?: number | null
updatedAt?: string | null
updatedBy?: string | null
@@ -7969,13 +7979,6 @@ export type Database = {
referencedRelation: "shelf"
referencedColumns: ["id", "locationId"]
},
- {
- foreignKeyName: "purchaseOrderLine_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code"]
- },
{
foreignKeyName: "purchaseOrderLine_updatedBy_fkey"
columns: ["updatedBy"]
@@ -14473,7 +14476,6 @@ export type Database = {
supplierId: string | null
supplierPartId: string | null
supplierServiceId: string | null
- unitOfMeasureCode: string | null
unitPrice: number | null
updatedAt: string | null
updatedBy: string | null
@@ -14633,13 +14635,6 @@ export type Database = {
referencedRelation: "shelf"
referencedColumns: ["id", "locationId"]
},
- {
- foreignKeyName: "purchaseOrderLine_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code"]
- },
{
foreignKeyName: "purchaseOrderLine_updatedBy_fkey"
columns: ["updatedBy"]
diff --git a/packages/database/supabase/functions/create-purchase-invoice-from-purchase-order/index.ts b/packages/database/supabase/functions/create-purchase-invoice-from-purchase-order/index.ts
index 14687b4467..e4639a05b9 100644
--- a/packages/database/supabase/functions/create-purchase-invoice-from-purchase-order/index.ts
+++ b/packages/database/supabase/functions/create-purchase-invoice-from-purchase-order/index.ts
@@ -1,9 +1,9 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { DB, getConnectionPool, getDatabaseClient } from "../lib/database.ts";
-import { Database } from "../lib/types.ts";
import { corsHeaders } from "../lib/headers.ts";
import { getSupabaseServiceRole } from "../lib/supabase.ts";
+import { Database } from "../lib/types.ts";
import { getNextSequence } from "../shared/get-next-sequence.ts";
const pool = getConnectionPool(1);
@@ -122,6 +122,9 @@ serve(async (req: Request) => {
description: line.description,
quantity: line.quantityToInvoice,
unitPrice: line.unitPrice ?? 0,
+ purchaseUnitOfMeasureCode: line.purchaseUnitOfMeasureCode,
+ inventoryUnitOfMeasureCode: line.inventoryUnitOfMeasureCode,
+ conversionFactor: line.conversionFactor,
// TODO: currency code and exchange rate
currencyCode: "USD",
exchangeRate: 1,
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index c9f5620648..854c6f2242 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -6872,6 +6872,7 @@ export type Database = {
Row: {
accountNumber: string | null
assetId: string | null
+ conversionFactor: number | null
createdAt: string
createdBy: string
currencyCode: string
@@ -6879,17 +6880,18 @@ export type Database = {
description: string | null
exchangeRate: number
id: string
+ inventoryUnitOfMeasureCode: string | null
invoiceId: string
invoiceLineType: Database["public"]["Enums"]["payableLineType"]
locationId: string | null
partId: string | null
purchaseOrderId: string | null
purchaseOrderLineId: string | null
+ purchaseUnitOfMeasureCode: string | null
quantity: number
serviceId: string | null
shelfId: string | null
totalAmount: number | null
- unitOfMeasureCode: string | null
unitPrice: number
updatedAt: string | null
updatedBy: string | null
@@ -6897,6 +6899,7 @@ export type Database = {
Insert: {
accountNumber?: string | null
assetId?: string | null
+ conversionFactor?: number | null
createdAt?: string
createdBy: string
currencyCode: string
@@ -6904,17 +6907,18 @@ export type Database = {
description?: string | null
exchangeRate?: number
id?: string
+ inventoryUnitOfMeasureCode?: string | null
invoiceId: string
invoiceLineType: Database["public"]["Enums"]["payableLineType"]
locationId?: string | null
partId?: string | null
purchaseOrderId?: string | null
purchaseOrderLineId?: string | null
+ purchaseUnitOfMeasureCode?: string | null
quantity?: number
serviceId?: string | null
shelfId?: string | null
totalAmount?: number | null
- unitOfMeasureCode?: string | null
unitPrice?: number
updatedAt?: string | null
updatedBy?: string | null
@@ -6922,6 +6926,7 @@ export type Database = {
Update: {
accountNumber?: string | null
assetId?: string | null
+ conversionFactor?: number | null
createdAt?: string
createdBy?: string
currencyCode?: string
@@ -6929,22 +6934,37 @@ export type Database = {
description?: string | null
exchangeRate?: number
id?: string
+ inventoryUnitOfMeasureCode?: string | null
invoiceId?: string
invoiceLineType?: Database["public"]["Enums"]["payableLineType"]
locationId?: string | null
partId?: string | null
purchaseOrderId?: string | null
purchaseOrderLineId?: string | null
+ purchaseUnitOfMeasureCode?: string | null
quantity?: number
serviceId?: string | null
shelfId?: string | null
totalAmount?: number | null
- unitOfMeasureCode?: string | null
unitPrice?: number
updatedAt?: string | null
updatedBy?: string | null
}
Relationships: [
+ {
+ foreignKeyName: "purchaseInvoiceLine_inventoryUnitOfMeasureCode_fkey"
+ columns: ["inventoryUnitOfMeasureCode"]
+ isOneToOne: false
+ referencedRelation: "unitOfMeasure"
+ referencedColumns: ["code"]
+ },
+ {
+ foreignKeyName: "purchaseInvoiceLine_purchaseUnitOfMeasureCode_fkey"
+ columns: ["purchaseUnitOfMeasureCode"]
+ isOneToOne: false
+ referencedRelation: "unitOfMeasure"
+ referencedColumns: ["code"]
+ },
{
foreignKeyName: "purchaseInvoiceLines_accountNumber_fkey"
columns: ["accountNumber"]
@@ -7106,13 +7126,6 @@ export type Database = {
referencedRelation: "shelf"
referencedColumns: ["id", "locationId"]
},
- {
- foreignKeyName: "purchaseInvoiceLines_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code"]
- },
{
foreignKeyName: "purchaseInvoiceLines_updatedBy_fkey"
columns: ["updatedBy"]
@@ -7782,7 +7795,6 @@ export type Database = {
serviceId: string | null
setupPrice: number | null
shelfId: string | null
- unitOfMeasureCode: string | null
unitPrice: number | null
updatedAt: string | null
updatedBy: string | null
@@ -7813,7 +7825,6 @@ export type Database = {
serviceId?: string | null
setupPrice?: number | null
shelfId?: string | null
- unitOfMeasureCode?: string | null
unitPrice?: number | null
updatedAt?: string | null
updatedBy?: string | null
@@ -7844,7 +7855,6 @@ export type Database = {
serviceId?: string | null
setupPrice?: number | null
shelfId?: string | null
- unitOfMeasureCode?: string | null
unitPrice?: number | null
updatedAt?: string | null
updatedBy?: string | null
@@ -7969,13 +7979,6 @@ export type Database = {
referencedRelation: "shelf"
referencedColumns: ["id", "locationId"]
},
- {
- foreignKeyName: "purchaseOrderLine_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code"]
- },
{
foreignKeyName: "purchaseOrderLine_updatedBy_fkey"
columns: ["updatedBy"]
@@ -14473,7 +14476,6 @@ export type Database = {
supplierId: string | null
supplierPartId: string | null
supplierServiceId: string | null
- unitOfMeasureCode: string | null
unitPrice: number | null
updatedAt: string | null
updatedBy: string | null
@@ -14633,13 +14635,6 @@ export type Database = {
referencedRelation: "shelf"
referencedColumns: ["id", "locationId"]
},
- {
- foreignKeyName: "purchaseOrderLine_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code"]
- },
{
foreignKeyName: "purchaseOrderLine_updatedBy_fkey"
columns: ["updatedBy"]
diff --git a/packages/database/supabase/functions/post-purchase-invoice/index.ts b/packages/database/supabase/functions/post-purchase-invoice/index.ts
index 66975652bd..bb234a5b13 100644
--- a/packages/database/supabase/functions/post-purchase-invoice/index.ts
+++ b/packages/database/supabase/functions/post-purchase-invoice/index.ts
@@ -562,7 +562,7 @@ serve(async (req: Request) => {
receivedQuantity: invoiceLineQuantityInInventoryUnit,
locationId: invoiceLine.locationId,
shelfId: invoiceLine.shelfId,
- unitOfMeasure: invoiceLine.unitOfMeasureCode ?? "EA",
+ unitOfMeasure: invoiceLine.inventoryUnitOfMeasureCode ?? "EA",
unitPrice: invoiceLine.unitPrice,
createdBy: invoiceLine.createdBy,
});
diff --git a/packages/database/supabase/migrations/20240402052512_purchasing-conversion-factors.sql b/packages/database/supabase/migrations/20240402052512_purchasing-conversion-factors.sql
index 2d6a8baba5..bd58060419 100644
--- a/packages/database/supabase/migrations/20240402052512_purchasing-conversion-factors.sql
+++ b/packages/database/supabase/migrations/20240402052512_purchasing-conversion-factors.sql
@@ -4,7 +4,7 @@ ALTER TABLE "purchaseOrderLine" ADD COLUMN "purchaseUnitOfMeasureCode" TEXT REFE
UPDATE "purchaseOrderLine" SET "inventoryUnitOfMeasureCode" = "unitOfMeasureCode";
ALTER TABLE "purchaseOrderLine" DROP CONSTRAINT "purchaseOrderLine_unitOfMeasureCode_fkey";
-ALTER TABLE "purchaseOrderLine" DROP COLUMN "unitOfMeasureCode";
+ALTER TABLE "purchaseOrderLine" DROP COLUMN "unitOfMeasureCode" CASCADE;
ALTER TABLE "purchaseInvoiceLine" ADD COLUMN "conversionFactor" NUMERIC(10, 2) DEFAULT 1;
ALTER TABLE "purchaseInvoiceLine" ADD COLUMN "inventoryUnitOfMeasureCode" TEXT REFERENCES "unitOfMeasure" ("code");
@@ -12,12 +12,12 @@ ALTER TABLE "purchaseInvoiceLine" ADD COLUMN "purchaseUnitOfMeasureCode" TEXT RE
UPDATE "purchaseInvoiceLine" SET "inventoryUnitOfMeasureCode" = "unitOfMeasureCode";
ALTER TABLE "purchaseInvoiceLine" DROP CONSTRAINT "purchaseInvoiceLines_unitOfMeasureCode_fkey";
-ALTER TABLE "purchaseInvoiceLine" DROP COLUMN "unitOfMeasureCode";
+ALTER TABLE "purchaseInvoiceLine" DROP COLUMN "unitOfMeasureCode" CASCADE;
ALTER TABLE "partSupplier" ADD COLUMN "unitPrice" NUMERIC(10, 2) DEFAULT 0;
-DROP VIEW "purchaseOrderLines";
+DROP VIEW IF EXISTS "purchaseOrderLines";
CREATE OR REPLACE VIEW "purchaseOrderLines" WITH(SECURITY_INVOKER=true) AS
SELECT
pol.*,