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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
refactor: migrations for multi-tenant - batch 8
Brad Barbinf9224897b772
diff --git a/packages/database/supabase/migrations/20230329021751_contractors.sql b/packages/database/supabase/migrations/20230329021751_contractors.sqlindex f3623c9c88..258e893cb3 100644--- a/packages/database/supabase/migrations/20230329021751_contractors.sql+++ b/packages/database/supabase/migrations/20230329021751_contractors.sql@@ -2,6 +2,7 @@ CREATE TABLE "contractor" ("id" TEXT NOT NULL,"hoursPerWeek" INTEGER NOT NULL DEFAULT 0,"active" BOOLEAN NOT NULL DEFAULT true,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -10,38 +11,41 @@ CREATE TABLE "contractor" (CONSTRAINT "contractor_pkey" PRIMARY KEY ("id"),CONSTRAINT "contractor_id_fkey" FOREIGN KEY ("id") REFERENCES "supplierContact"("id"),+ CONSTRAINT "contractor_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "contractor_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "contractor_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));+CREATE INDEX "contractor_companyId_idx" ON "contractor" ("companyId");+ALTER TABLE "contractor" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with resources_view can view contractors" ON "contractor"FOR SELECTUSING (- coalesce(get_my_claim('resources_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('resources_view', "companyId"));CREATE POLICY "Employees with resources_create can insert contractors" ON "contractor"FOR INSERTWITH CHECK (- coalesce(get_my_claim('resources_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('resources_create', "companyId"));CREATE POLICY "Employees with resources_update can update contractors" ON "contractor"FOR UPDATEUSING (- coalesce(get_my_claim('resources_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('resources_update', "companyId"));CREATE POLICY "Employees with resources_delete can delete contractors" ON "contractor"FOR DELETEUSING (- coalesce(get_my_claim('resources_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('resources_delete', "companyId"));@@ -59,32 +63,68 @@ CREATE TABLE "contractorAbility" (ALTER TABLE "contractorAbility" ENABLE ROW LEVEL SECURITY;-CREATE POLICY "Employees with resources_view can view contractorAbilities" ON "contractorAbility"+CREATE POLICY "Employees with resources_view can view contractor abilities" ON "contractorAbility"FOR SELECTUSING (- coalesce(get_my_claim('resources_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('resources_view'))+ OR (+ "contractorId" IN (+ SELECT "id" FROM "contractor" WHERE "companyId" = ANY(+ get_permission_companies('resources_view')+ )+ )+ )+ ));-CREATE POLICY "Employees with resources_create can insert contractorAbilities" ON "contractorAbility"+CREATE POLICY "Employees with resources_create can insert contractor abilities" ON "contractorAbility"FOR INSERTWITH CHECK (- coalesce(get_my_claim('resources_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('resources_create'))+ OR (+ "contractorId" IN (+ SELECT "id" FROM "contractor" WHERE "companyId" = ANY(+ get_permission_companies('resources_create')+ )+ )+ )+ ));-CREATE POLICY "Employees with resources_update can update contractorAbilities" ON "contractorAbility"+CREATE POLICY "Employees with resources_update can update contractor abilities" ON "contractorAbility"FOR UPDATEUSING (- coalesce(get_my_claim('resources_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('resources_update'))+ OR (+ "contractorId" IN (+ SELECT "id" FROM "contractor" WHERE "companyId" = ANY(+ get_permission_companies('resources_update')+ )+ )+ )+ ));-CREATE POLICY "Employees with resources_delete can delete contractorAbilities" ON "contractorAbility"+CREATE POLICY "Employees with resources_delete can delete contractor abilities" ON "contractorAbility"FOR DELETEUSING (- coalesce(get_my_claim('resources_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('resources_delete'))+ OR (+ "contractorId" IN (+ SELECT "id" FROM "contractor" WHERE "companyId" = ANY(+ get_permission_companies('resources_delete')+ )+ )+ )+ ));@@ -93,6 +133,7 @@ CREATE OR REPLACE VIEW "contractors" ASp.id AS "supplierContactId",p."active",p."hoursPerWeek",+ p."companyId",p."customFields",s.id AS "supplierId",s.name AS "supplierName",@@ -110,4 +151,4 @@ CREATE OR REPLACE VIEW "contractors" ASLEFT JOIN "contractorAbility" paON pa."contractorId" = p.idWHERE p."active" = true- GROUP BY p.id, p.active, p."hoursPerWeek", p."customFields", s.id, c.id, s.name, c."firstName", c."lastName", c."email";\ No newline at end of file+ GROUP BY p.id, p.active, p."hoursPerWeek", p."customFields", p."companyId", s.id, c.id, s.name, c."firstName", c."lastName", c."email";\ No newline at end of filediff --git a/packages/database/supabase/migrations/20230330024715_accounts.sql b/packages/database/supabase/migrations/20230330024715_accounts.sqlindex 8d9796a385..f3b6947240 100644--- a/packages/database/supabase/migrations/20230330024715_accounts.sql+++ b/packages/database/supabase/migrations/20230330024715_accounts.sql@@ -7,6 +7,7 @@ CREATE TABLE "currency" ("decimalPlaces" INTEGER NOT NULL DEFAULT 2,"isBaseCurrency" BOOLEAN NOT NULL DEFAULT false,"active" BOOLEAN NOT NULL DEFAULT true,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -14,12 +15,14 @@ CREATE TABLE "currency" ("customFields" JSONB,CONSTRAINT "currency_pkey" PRIMARY KEY ("id"),- CONSTRAINT "currency_code_key" UNIQUE ("code"),+ CONSTRAINT "currency_code_key" UNIQUE ("code", "companyId"),+ CONSTRAINT "currency_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "currency_exchangeRate_check" CHECK ("exchangeRate" > 0),CONSTRAINT "currency_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "currency_code_index" ON "currency" ("code");+CREATE INDEX "currency_code_idx" ON "currency" ("code");+CREATE INDEX "currency_companyId_idx" ON "currency" ("companyId");ALTER TABLE "currency" ENABLE ROW LEVEL SECURITY;@@ -32,22 +35,22 @@ CREATE POLICY "Authenticated users can view currencies" ON "currency"CREATE POLICY "Employees with accounting_create can insert currencies" ON "currency"FOR INSERTWITH CHECK (- coalesce(get_my_claim('accounting_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_create', "companyId"));CREATE POLICY "Employees with accounting_update can update currencies" ON "currency"FOR UPDATEUSING (- coalesce(get_my_claim('accounting_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_update', "companyId"));CREATE POLICY "Employees with accounting_delete can delete currencies" ON "currency"FOR DELETEUSING (- coalesce(get_my_claim('accounting_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_delete', "companyId"));@@ -97,6 +100,7 @@ CREATE TABLE "accountCategory" ("category" TEXT NOT NULL,"class" "glAccountClass" NOT NULL,"incomeBalance" "glIncomeBalance" NOT NULL,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -105,39 +109,42 @@ CREATE TABLE "accountCategory" (CONSTRAINT "accountCategory_pkey" PRIMARY KEY ("id"),CONSTRAINT "accountCategory_unique_category" UNIQUE ("category"),+ CONSTRAINT "accountCategory_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id"),CONSTRAINT "accountCategory_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "accountCategory_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));+CREATE INDEX "accountCategory_companyId_idx" ON "accountCategory" ("companyId");+ALTER TABLE "accountCategory" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with accounting_view can view account categories" ON "accountCategory"FOR SELECTUSING (- coalesce(get_my_claim('accounting_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_view', "companyId"));CREATE POLICY "Employees with accounting_create can insert account categories" ON "accountCategory"FOR INSERTWITH CHECK (- coalesce(get_my_claim('accounting_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_create', "companyId"));CREATE POLICY "Employees with accounting_update can update account categories" ON "accountCategory"FOR UPDATEUSING (- coalesce(get_my_claim('accounting_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_update', "companyId"));CREATE POLICY "Employees with accounting_delete can delete account categories" ON "accountCategory"FOR DELETEUSING (- coalesce(get_my_claim('accounting_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_delete', "companyId"));CREATE TYPE "glConsolidatedRate" AS ENUM (@@ -171,30 +178,66 @@ ALTER TABLE "accountSubcategory" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with accounting_view can view account subcategories" ON "accountSubcategory"FOR SELECTUSING (- coalesce(get_my_claim('accounting_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('accounting_view'))+ OR (+ "accountCategoryId" IN (+ SELECT "id" FROM "accountCategory" WHERE "companyId" = ANY(+ get_permission_companies('accounting_view')+ )+ )+ )+ ));CREATE POLICY "Employees with accounting_create can insert account subcategories" ON "accountSubcategory"FOR INSERTWITH CHECK (- coalesce(get_my_claim('accounting_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('accounting_create'))+ OR (+ "accountCategoryId" IN (+ SELECT "id" FROM "accountCategory" WHERE "companyId" = ANY(+ get_permission_companies('accounting_create')+ )+ )+ )+ ));CREATE POLICY "Employees with accounting_update can update account subcategories" ON "accountSubcategory"FOR UPDATEUSING (- coalesce(get_my_claim('accounting_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('accounting_update'))+ OR (+ "accountCategoryId" IN (+ SELECT "id" FROM "accountCategory" WHERE "companyId" = ANY(+ get_permission_companies('accounting_update')+ )+ )+ )+ ));CREATE POLICY "Employees with accounting_delete can delete account subcategories" ON "accountSubcategory"FOR DELETEUSING (- coalesce(get_my_claim('accounting_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND (+ 0 = ANY(get_permission_companies('accounting_delete'))+ OR (+ "accountCategoryId" IN (+ SELECT "id" FROM "accountCategory" WHERE "companyId" = ANY(+ get_permission_companies('accounting_delete')+ )+ )+ )+ ));CREATE TABLE "account" (@@ -209,6 +252,7 @@ CREATE TABLE "account" ("consolidatedRate" "glConsolidatedRate","directPosting" BOOLEAN NOT NULL DEFAULT false,"active" BOOLEAN NOT NULL DEFAULT true,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -219,52 +263,54 @@ CREATE TABLE "account" (CONSTRAINT "account_number_key" UNIQUE ("number"),CONSTRAINT "account_name_key" UNIQUE ("name"),CONSTRAINT "account_accountCategoryId_fkey" FOREIGN KEY ("accountCategoryId") REFERENCES "accountCategory"("id"),+ CONSTRAINT "account_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id"),CONSTRAINT "account_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "account_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "account_number_idx" ON "account" ("number");-CREATE INDEX "account_type_idx" ON "account" ("type");-CREATE INDEX "account_incomeBalance_idx" ON "account" ("incomeBalance");-CREATE INDEX "account_accountCategoryId_idx" ON "account" ("accountCategoryId");-CREATE INDEX "account_accountSubcategoryId_idx" ON "account" ("accountSubcategoryId");+CREATE INDEX "account_number_idx" ON "account" ("number", "companyId");+CREATE INDEX "account_type_idx" ON "account" ("type", "companyId");+CREATE INDEX "account_incomeBalance_idx" ON "account" ("incomeBalance", "companyId");+CREATE INDEX "account_accountCategoryId_idx" ON "account" ("accountCategoryId", "companyId");+CREATE INDEX "account_class_idx" ON "account" ("class", "companyId");+CREATE INDEX "account_companyId_idx" ON "account" ("companyId");ALTER TABLE "account" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Certain employees can view accounts" ON "account"FOR SELECTUSING (+ has_role('employee') AND(- coalesce(get_my_claim('accounting_view')::boolean, false) = true OR- coalesce(get_my_claim('parts_view')::boolean, false) = true OR- coalesce(get_my_claim('resources_view')::boolean, false) = true OR- coalesce(get_my_claim('sales_view')::boolean, false) = true OR- coalesce(get_my_claim('purchasing_view')::boolean, false) = true+ has_company_permission('accounting_view', "companyId") OR+ has_company_permission('parts_view', "companyId") OR+ has_company_permission('resources_view', "companyId") OR+ has_company_permission('sales_view', "companyId") OR+ has_company_permission('purchasing_view', "companyId"))- AND (get_my_claim('role'::text)) = '"employee"'::jsonb);CREATE POLICY "Employees with accounting_create can insert accounts" ON "account"FOR INSERTWITH CHECK (- coalesce(get_my_claim('accounting_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_create', "companyId"));CREATE POLICY "Employees with accounting_update can update accounts" ON "account"FOR UPDATEUSING (- coalesce(get_my_claim('accounting_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_update', "companyId"));CREATE POLICY "Employees with accounting_delete can delete accounts" ON "account"FOR DELETEUSING (- coalesce(get_my_claim('accounting_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('accounting_delete', "companyId"));CREATE OR REPLACE VIEW "accountCategories" AS@@ -273,6 +319,7 @@ CREATE OR REPLACE VIEW "accountCategories" AS"category","class","incomeBalance",+ "companyId","createdBy","createdAt","updatedBy",diff --git a/packages/database/supabase/migrations/20230330024716_parts.sql b/packages/database/supabase/migrations/20230330024716_parts.sqlindex 19d75c4b1f..21c445bdc5 100644--- a/packages/database/supabase/migrations/20230330024716_parts.sql+++ b/packages/database/supabase/migrations/20230330024716_parts.sql@@ -314,7 +314,7 @@ CREATE TABLE "partUnitSalePrice" ("customFields" JSONB,CONSTRAINT "partUnitSalePrice_partId_fkey" FOREIGN KEY ("partId") REFERENCES "part"("id") ON DELETE CASCADE ON UPDATE CASCADE,- CONSTRAINT "partUnitSalePrice_currencyCode_fkey" FOREIGN KEY ("currencyCode") REFERENCES "currency"("code") ON DELETE SET NULL ON UPDATE CASCADE,+ CONSTRAINT "partUnitSalePrice_currencyCode_fkey" FOREIGN KEY ("currencyCode", "companyId") REFERENCES "currency"("code", "companyId") ON DELETE SET NULL ON UPDATE CASCADE,CONSTRAINT "partUnitSalePrice_salesUnitOfMeasureId_fkey" FOREIGN KEY ("salesUnitOfMeasureCode", "companyId") REFERENCES "unitOfMeasure"("code", "companyId") ON DELETE SET NULL,CONSTRAINT "partUnitSalePrice_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partUnitSalePrice_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),diff --git a/packages/database/supabase/migrations/20230510035345_purchasing.sql b/packages/database/supabase/migrations/20230510035345_purchasing.sqlindex 01633331b2..d4d6596955 100644--- a/packages/database/supabase/migrations/20230510035345_purchasing.sql+++ b/packages/database/supabase/migrations/20230510035345_purchasing.sql@@ -176,6 +176,7 @@ CREATE TABLE "purchaseOrder" ("supplierLocationId" TEXT,"supplierContactId" TEXT,"supplierReference" TEXT,+ "companyId" INTEGER NOT NULL,"closedAt" DATE,"closedBy" TEXT,"customFields" JSONB,@@ -189,15 +190,17 @@ CREATE TABLE "purchaseOrder" (CONSTRAINT "purchaseOrder_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "supplier" ("id") ON DELETE CASCADE,CONSTRAINT "purchaseOrder_supplierLocationId_fkey" FOREIGN KEY ("supplierLocationId") REFERENCES "supplierLocation" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,CONSTRAINT "purchaseOrder_supplierContactId_fkey" FOREIGN KEY ("supplierContactId") REFERENCES "supplierContact" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,+ CONSTRAINT "purchaseOrder_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON UPDATE CASCADE ON DELETE CASCADE,CONSTRAINT "purchaseOrder_closedBy_fkey" FOREIGN KEY ("closedBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "purchaseOrder_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "purchaseOrder_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT);-CREATE INDEX "purchaseOrder_purchaseOrderId_idx" ON "purchaseOrder" ("purchaseOrderId");-CREATE INDEX "purchaseOrder_supplierId_idx" ON "purchaseOrder" ("supplierId");-CREATE INDEX "purchaseOrder_supplierContactId_idx" ON "purchaseOrder" ("supplierContactId");-CREATE INDEX "purchaseOrder_status_idx" ON "purchaseOrder" ("status");+CREATE INDEX "purchaseOrder_purchaseOrderId_idx" ON "purchaseOrder" ("purchaseOrderId", "companyId");+CREATE INDEX "purchaseOrder_supplierId_idx" ON "purchaseOrder" ("supplierId", "companyId");+CREATE INDEX "purchaseOrder_supplierContactId_idx" ON "purchaseOrder" ("supplierContactId", "companyId");+CREATE INDEX "purchaseOrder_status_idx" ON "purchaseOrder" ("status", "companyId");+CREATE INDEX "purchaseOrder_companyId_idx" ON "purchaseOrder" ("companyId");CREATE TYPE "purchaseOrderLineType" AS ENUM ('Comment',@@ -315,6 +318,7 @@ CREATE TABLE "purchaseOrderPayment" ("paymentTermId" TEXT,"paymentComplete" BOOLEAN NOT NULL DEFAULT FALSE,"currencyCode" TEXT NOT NULL DEFAULT 'USD',+ "companyId" INTEGER NOT NULL,"updatedAt" TIMESTAMP WITH TIME ZONE,"updatedBy" TEXT,"customFields" JSONB,@@ -325,12 +329,11 @@ CREATE TABLE "purchaseOrderPayment" (CONSTRAINT "purchaseOrderPayment_invoiceSupplierLocationId_fkey" FOREIGN KEY ("invoiceSupplierLocationId") REFERENCES "supplierLocation" ("id") ON DELETE CASCADE,CONSTRAINT "purchaseOrderPayment_invoiceSupplierContactId_fkey" FOREIGN KEY ("invoiceSupplierContactId") REFERENCES "supplierContact" ("id") ON DELETE CASCADE,CONSTRAINT "purchaseOrderPayment_paymentTermId_fkey" FOREIGN KEY ("paymentTermId") REFERENCES "paymentTerm" ("id") ON DELETE CASCADE,- CONSTRAINT "purchaseOrderPayment_currencyCode_fkey" FOREIGN KEY ("currencyCode") REFERENCES "currency" ("code") ON DELETE CASCADE ON UPDATE CASCADE+ CONSTRAINT "purchaseOrderPayment_currencyCode_fkey" FOREIGN KEY ("currencyCode", "companyId") REFERENCES "currency" ("code", "companyId") ON DELETE CASCADE ON UPDATE CASCADE);CREATE INDEX "purchaseOrderPayment_invoiceSupplierId_idx" ON "purchaseOrderPayment" ("invoiceSupplierId");-CREATE INDEX "purchaseOrderPayment_invoiceSupplierLocationId_idx" ON "purchaseOrderPayment" ("invoiceSupplierLocationId");-CREATE INDEX "purchaseOrderPayment_invoiceSupplierContactId_idx" ON "purchaseOrderPayment" ("invoiceSupplierContactId");+CREATE INDEX "purchaseOrderPayment_companyId_idx" ON "purchaseOrderPayment" ("companyId");CREATE TABLE "purchaseOrderDelivery" ("id" TEXT NOT NULL,@@ -345,6 +348,7 @@ CREATE TABLE "purchaseOrderDelivery" ("dropShipment" BOOLEAN NOT NULL DEFAULT FALSE,"customerId" TEXT,"customerLocationId" TEXT,+ "companyId" INTEGER NOT NULL,"updatedBy" TEXT,"updatedAt" TIMESTAMP WITH TIME ZONE,"customFields" JSONB,@@ -359,6 +363,8 @@ CREATE TABLE "purchaseOrderDelivery" (CONSTRAINT "purchaseOrderDelivery_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT);+CREATE INDEX "purchaseOrderDelivery_companyId_idx" ON "purchaseOrderDelivery" ("companyId");+CREATE TYPE "purchaseOrderTransactionType" AS ENUM ('Edit','Favorite',@@ -447,7 +453,8 @@ CREATE OR REPLACE VIEW "purchaseOrders" ASCREATE OR REPLACE VIEW "purchaseOrderSuppliers" ASSELECT DISTINCTs."id",- s."name"+ s."name",+ s."companyId"FROM "supplier" sINNER JOIN "purchaseOrder" p ON p."supplierId" = s."id";diff --git a/packages/database/supabase/migrations/20230924004608_accounts-payable.sql b/packages/database/supabase/migrations/20230924004608_accounts-payable.sqlindex 14ef0fd852..df5ac2eb80 100644--- a/packages/database/supabase/migrations/20230924004608_accounts-payable.sql+++ b/packages/database/supabase/migrations/20230924004608_accounts-payable.sql@@ -31,6 +31,7 @@ CREATE TABLE "purchaseInvoice" ("totalAmount" NUMERIC(10, 2) NOT NULL DEFAULT 0,"totalTax" NUMERIC(10, 2) NOT NULL DEFAULT 0,"balance" NUMERIC(10, 2) NOT NULL DEFAULT 0,+ "companyId" INTEGER NOT NULL,"customFields" JSONB,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),@@ -43,16 +44,16 @@ CREATE TABLE "purchaseInvoice" (CONSTRAINT "purchaseInvoice_invoiceSupplierLocationId_fkey" FOREIGN KEY ("invoiceSupplierLocationId") REFERENCES "supplierLocation" ("id"),CONSTRAINT "purchaseInvoice_invoiceSupplierContactId_fkey" FOREIGN KEY ("invoiceSupplierContactId") REFERENCES "supplierContact" ("id"),CONSTRAINT "purchaseInvoice_paymentTermId_fkey" FOREIGN KEY ("paymentTermId") REFERENCES "paymentTerm" ("id"),- CONSTRAINT "purchaseInvoice_currencyCode_fkey" FOREIGN KEY ("currencyCode") REFERENCES "currency" ("code"),+ CONSTRAINT "purchaseInvoice_currencyCode_fkey" FOREIGN KEY ("currencyCode", "companyId") REFERENCES "currency" ("code", "companyId"),+ CONSTRAINT "purchaseInvoice_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "purchaseInvoice_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id"),CONSTRAINT "purchaseInvoice_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id"));-CREATE INDEX "purchaseInvoice_invoiceId_idx" ON "purchaseInvoice" ("invoiceId");-CREATE INDEX "purchaseInvoice_status_idx" ON "purchaseInvoice" ("status");-CREATE INDEX "purchaseInvoice_supplierId_idx" ON "purchaseInvoice" ("supplierId");-CREATE INDEX "purchaseInvoice_dateDue_idx" ON "purchaseInvoice" ("dateDue");-CREATE INDEX "purchaseInvoice_datePaid_idx" ON "purchaseInvoice" ("datePaid");+CREATE INDEX "purchaseInvoice_invoiceId_idx" ON "purchaseInvoice" ("invoiceId", "companyId");+CREATE INDEX "purchaseInvoice_status_idx" ON "purchaseInvoice" ("status", "companyId");+CREATE INDEX "purchaseInvoice_supplierId_idx" ON "purchaseInvoice" ("supplierId", "companyId");+CREATE INDEX "purchaseInvoice_companyId_idx" ON "purchaseInvoice" ("companyId");ALTER publication supabase_realtime ADD TABLE "purchaseInvoice";@@ -193,7 +194,7 @@ CREATE TABLE "purchaseInvoiceLine" (CONSTRAINT "purchaseInvoiceLines_shelfId_fkey" FOREIGN KEY ("shelfId", "locationId") REFERENCES "shelf" ("id", "locationId") ON DELETE SET NULL ON UPDATE CASCADE,CONSTRAINT "purchaseInvoiceLines_accountNumber_fkey" FOREIGN KEY ("accountNumber") REFERENCES "account" ("number") ON UPDATE CASCADE ON DELETE RESTRICT,-- CONSTRAINT "purchaseInvoiceLines_assetId_fkey" FOREIGN KEY ("assetId") REFERENCES "fixedAsset" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,- CONSTRAINT "purchaseInvoiceLines_currencyCode_fkey" FOREIGN KEY ("currencyCode") REFERENCES "currency" ("code") ON UPDATE CASCADE ON DELETE RESTRICT,+ CONSTRAINT "purchaseInvoiceLines_currencyCode_fkey" FOREIGN KEY ("currencyCode", "companyId") REFERENCES "currency" ("code", "companyId") ON UPDATE CASCADE ON DELETE RESTRICT,CONSTRAINT "purchaseInvoiceLines_inventoryUnitOfMeasureCode_fkey" FOREIGN KEY ("inventoryUnitOfMeasureCode", "companyId") REFERENCES "unitOfMeasure" ("code", "companyId") ON UPDATE CASCADE ON DELETE SET NULL,CONSTRAINT "purchaseInvoiceLines_purchaseUnitOfMeasureCode_fkey" FOREIGN KEY ("purchaseUnitOfMeasureCode", "companyId") REFERENCES "unitOfMeasure" ("code", "companyId") ON UPDATE CASCADE ON DELETE SET NULL,CONSTRAINT "purchaseInvoiceLines_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,@@ -299,6 +300,7 @@ CREATE TABLE "purchasePayment" ("currencyCode" TEXT NOT NULL,"exchangeRate" NUMERIC(10, 4) NOT NULL DEFAULT 1,"totalAmount" NUMERIC(10, 2) NOT NULL DEFAULT 0,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),"updatedBy" TEXT,@@ -307,11 +309,14 @@ CREATE TABLE "purchasePayment" (CONSTRAINT "purchasePayment_pkey" PRIMARY KEY ("id"),CONSTRAINT "purchasePayment_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "supplier" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,- CONSTRAINT "purchasePayment_currencyCode_fkey" FOREIGN KEY ("currencyCode") REFERENCES "currency" ("code") ON UPDATE CASCADE ON DELETE RESTRICT,+ CONSTRAINT "purchasePayment_currencyCode_fkey" FOREIGN KEY ("currencyCode", "companyId") REFERENCES "currency" ("code", "companyId") ON UPDATE CASCADE ON DELETE RESTRICT,+ CONSTRAINT "purchasePayment_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON UPDATE CASCADE ON DELETE CASCADE,CONSTRAINT "purchasePayment_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,CONSTRAINT "purchasePayment_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON UPDATE CASCADE ON DELETE RESTRICT);+CREATE INDEX "purchasePayment_companyId_idx" ON "purchasePayment" ("companyId");+ALTER TABLE "purchasePayment" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with invoicing_view can view AP payments" ON "purchasePayment"diff --git a/packages/database/supabase/migrations/20240412224648_sales-orders.sql b/packages/database/supabase/migrations/20240412224648_sales-orders.sqlindex 0854d4b3ea..e2d4d80fe7 100644--- a/packages/database/supabase/migrations/20240412224648_sales-orders.sql+++ b/packages/database/supabase/migrations/20240412224648_sales-orders.sql@@ -19,6 +19,7 @@ CREATE TABLE "salesOrder" ("customerLocationId" TEXT,"customerContactId" TEXT,"customerReference" TEXT,+ "companyId" INTEGER NOT NULL,"closedAt" DATE,"closedBy" TEXT,"customFields" JSONB,@@ -36,17 +37,18 @@ CREATE TABLE "salesOrder" (CONSTRAINT "salesOrder_customerLocationId_fkey" FOREIGN KEY ("customerLocationId") REFERENCES "customerLocation" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,CONSTRAINT "salesOrder_customerContactId_fkey" FOREIGN KEY ("customerContactId") REFERENCES "customerContact" ("id") ON UPDATE CASCADE ON DELETE RESTRICT,CONSTRAINT "salesOrder_quoteId_fkey" FOREIGN KEY ("quoteId") REFERENCES "quote"("id") ON UPDATE CASCADE ON DELETE RESTRICT,+ CONSTRAINT "salesOrder_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "salesOrder_closedBy_fkey" FOREIGN KEY ("closedBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "salesOrder_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "salesOrder_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "salesOrder_assignee_fkey" FOREIGN KEY ("assignee") REFERENCES "user" ("id") ON DELETE SET NULL);-CREATE INDEX "salesOrder_salesOrderId_idx" ON "salesOrder" ("salesOrderId");-CREATE INDEX "salesOrder_customerId_idx" ON "salesOrder" ("customerId");-CREATE INDEX "salesOrder_customerContactId_idx" ON "salesOrder" ("customerContactId");-CREATE INDEX "salesOrder_status_idx" ON "salesOrder" ("status");-CREATE INDEX "salesOrder_quoteId_idx" ON "salesOrder" ("quoteId");+CREATE INDEX "salesOrder_salesOrderId_idx" ON "salesOrder" ("salesOrderId", "companyId");+CREATE INDEX "salesOrder_customerId_idx" ON "salesOrder" ("customerId", "companyId");+CREATE INDEX "salesOrder_status_idx" ON "salesOrder" ("status", "companyId");+CREATE INDEX "salesOrder_quoteId_idx" ON "salesOrder" ("quoteId", "companyId");+CREATE INDEX "salesOrder_companyId_idx" ON "salesOrder" ("companyId");CREATE TYPE "salesOrderLineType" AS ENUM ('Comment',@@ -144,6 +146,7 @@ CREATE TABLE "salesOrderPayment" ("paymentTermId" TEXT,"paymentComplete" BOOLEAN NOT NULL DEFAULT FALSE,"currencyCode" TEXT NOT NULL DEFAULT 'USD',+ "companyId" INTEGER NOT NULL,"updatedAt" TIMESTAMP WITH TIME ZONE,"updatedBy" TEXT,"customFields" JSONB,@@ -154,12 +157,14 @@ CREATE TABLE "salesOrderPayment" (CONSTRAINT "salesOrderPayment_invoiceCustomerLocationId_fkey" FOREIGN KEY ("invoiceCustomerLocationId") REFERENCES "customerLocation" ("id") ON DELETE CASCADE,CONSTRAINT "salesOrderPayment_invoiceCustomerContactId_fkey" FOREIGN KEY ("invoiceCustomerContactId") REFERENCES "customerContact" ("id") ON DELETE CASCADE,CONSTRAINT "salesOrderPayment_paymentTermId_fkey" FOREIGN KEY ("paymentTermId") REFERENCES "paymentTerm" ("id") ON DELETE CASCADE,- CONSTRAINT "salesOrderPayment_currencyCode_fkey" FOREIGN KEY ("currencyCode") REFERENCES "currency" ("code") ON DELETE CASCADE ON UPDATE CASCADE+ CONSTRAINT "salesOrderPayment_currencyCode_fkey" FOREIGN KEY ("currencyCode", "companyId") REFERENCES "currency" ("code", "companyId") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "salesOrderPayment_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE CASCADE ON UPDATE CASCADE);CREATE INDEX "salesOrderPayment_invoiceCustomerId_idx" ON "salesOrderPayment" ("invoiceCustomerId");CREATE INDEX "salesOrderPayment_invoiceCustomerLocationId_idx" ON "salesOrderPayment" ("invoiceCustomerLocationId");CREATE INDEX "salesOrderPayment_invoiceCustomerContactId_idx" ON "salesOrderPayment" ("invoiceCustomerContactId");+CREATE INDEX "salesOrderPayment_companyId_idx" ON "salesOrderPayment" ("companyId");CREATE TABLE "salesOrderShipment" ("id" TEXT NOT NULL,@@ -176,6 +181,7 @@ CREATE TABLE "salesOrderShipment" ("customerLocationId" TEXT,"supplierId" TEXT,"supplierLocationId" TEXT,+ "companyId" INTEGER NOT NULL,"updatedBy" TEXT,"updatedAt" TIMESTAMP WITH TIME ZONE,"customFields" JSONB,@@ -188,6 +194,7 @@ CREATE TABLE "salesOrderShipment" (CONSTRAINT "salesOrderShipment_shippingTermId_fkey" FOREIGN KEY ("shippingTermId") REFERENCES "shippingTerm" ("id") ON DELETE CASCADE,CONSTRAINT "salesOrderShipment_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "customer" ("id") ON DELETE CASCADE,CONSTRAINT "salesOrderShipment_customerLocationId_fkey" FOREIGN KEY ("customerLocationId") REFERENCES "customerLocation" ("id") ON DELETE CASCADE,+ CONSTRAINT "salesOrderShipment_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "salesOrderShipment_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT,CONSTRAINT "salesOrderShipment_assignee_fkey" FOREIGN KEY ("assignee") REFERENCES "user" ("id") ON DELETE SET NULL);@@ -280,7 +287,8 @@ CREATE OR REPLACE VIEW "salesOrders" ASCREATE OR REPLACE VIEW "salesOrderCustomers" ASSELECT DISTINCTc."id",- c."name"+ c."name",+ c."companyId"FROM "customer" cINNER JOIN "salesOrder" s ON s."customerId" = c."id";