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 9
Brad Barbina08b71893c66
diff --git a/packages/database/supabase/migrations/20230123004536_attributes.sql b/packages/database/supabase/migrations/20230123004536_attributes.sqlindex afdecf249d..11a170f75a 100644--- a/packages/database/supabase/migrations/20230123004536_attributes.sql+++ b/packages/database/supabase/migrations/20230123004536_attributes.sql@@ -17,7 +17,7 @@ CREATE TABLE "userAttributeCategory" (CONSTRAINT "userAttributeCategory_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE);-CREATE INDEX "userAttributeCategory_companyId_index" ON "userAttributeCategory" ("companyId");+CREATE INDEX "userAttributeCategory_companyId_idx" ON "userAttributeCategory" ("companyId");-- ALTER TABLE "userAttributeCategory" ENABLE ROW LEVEL SECURITY;@@ -108,7 +108,7 @@ CREATE TABLE "userAttribute" ();-CREATE INDEX "userAttribute_userAttributeCategoryId_index" ON "userAttribute" ("userAttributeCategoryId");+CREATE INDEX "userAttribute_userAttributeCategoryId_idx" ON "userAttribute" ("userAttributeCategoryId");-- ALTER TABLE "userAttribute" ENABLE ROW LEVEL SECURITY;@@ -174,8 +174,8 @@ CREATE TABLE "userAttributeValue" (UNIQUE ( "userAttributeId", "userId"));-CREATE INDEX "userAttributeValue_userAttributeId_index" ON "userAttributeValue" ("userAttributeId");-CREATE INDEX "userAttributeValue_userId_index" ON "userAttributeValue" ("userId");+CREATE INDEX "userAttributeValue_userAttributeId_idx" ON "userAttributeValue" ("userAttributeId");+CREATE INDEX "userAttributeValue_userId_idx" ON "userAttributeValue" ("userId");ALTER TABLE "userAttributeValue" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Users with resource update can view/modify user attribute values" ON "userAttributeValue" FOR ALL USING (diff --git a/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql b/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sqlindex 33f5dff24c..5f97976475 100644--- a/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql+++ b/packages/database/supabase/migrations/20230123004612_suppliers-and-customers.sql@@ -145,7 +145,7 @@ CREATE TABLE "supplierContact" (CONSTRAINT "supplierContact_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL);-CREATE INDEX "supplierContact_supplierId_index" ON "supplierContact"("supplierId");+CREATE INDEX "supplierContact_supplierId_idx" ON "supplierContact"("supplierId");CREATE TABLE "supplierAccount" ("id" TEXT NOT NULL,@@ -158,7 +158,7 @@ CREATE TABLE "supplierAccount" (CONSTRAINT "supplierAccount_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE);-CREATE INDEX "supplierAccount_supplierId_index" ON "supplierAccount"("supplierId");+CREATE INDEX "supplierAccount_supplierId_idx" ON "supplierAccount"("supplierId");CREATE INDEX "supplierAccount_companyId_idx" ON "supplierAccount" ("companyId");CREATE TABLE "customerStatus" (@@ -255,7 +255,7 @@ CREATE TABLE "customerContact" (CONSTRAINT "customerContact_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON UPDATE CASCADE ON DELETE SET NULL);-CREATE INDEX "customerContact_customerId_index" ON "customerContact"("customerId");+CREATE INDEX "customerContact_customerId_idx" ON "customerContact"("customerId");CREATE TABLE "customerAccount" ("id" TEXT NOT NULL,@@ -268,7 +268,7 @@ CREATE TABLE "customerAccount" (CONSTRAINT "customerAccount_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE);-CREATE INDEX "customerAccount_customerId_index" ON "customerAccount"("customerId");+CREATE INDEX "customerAccount_customerId_idx" ON "customerAccount"("customerId");CREATE INDEX "customerAccount_companyId_idx" ON "customerAccount" ("companyId");diff --git a/packages/database/supabase/migrations/20230209041302_notes.sql b/packages/database/supabase/migrations/20230209041302_notes.sqlindex a947112831..ac53f9f729 100644--- a/packages/database/supabase/migrations/20230209041302_notes.sql+++ b/packages/database/supabase/migrations/20230209041302_notes.sql@@ -14,7 +14,7 @@ CREATE TABLE "note" (CONSTRAINT "notes_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE);-CREATE INDEX "notes_documentId_index" ON "note"("documentId");+CREATE INDEX "notes_documentId_idx" ON "note"("documentId");CREATE INDEX "notes_companyId_idx" ON "note" ("companyId");ALTER TABLE "note" ENABLE ROW LEVEL SECURITY;diff --git a/packages/database/supabase/migrations/20230224035103_shifts.sql b/packages/database/supabase/migrations/20230224035103_shifts.sqlindex 480aff149e..24ecf236c4 100644--- a/packages/database/supabase/migrations/20230224035103_shifts.sql+++ b/packages/database/supabase/migrations/20230224035103_shifts.sql@@ -29,8 +29,7 @@ ALTER TABLE "location" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees can view locations for their companies" ON "location"FOR SELECTUSING (- auth.role() = 'authenticated'- AND has_role('employee')+ has_role('employee')AND "companyId" = ANY(select "companyId" from "userToCompany" where "userId" = auth.uid()::text)diff --git a/packages/database/supabase/migrations/20230302030217_equipment.sql b/packages/database/supabase/migrations/20230302030217_equipment.sqlindex ba4b04db24..4e7459826a 100644--- a/packages/database/supabase/migrations/20230302030217_equipment.sql+++ b/packages/database/supabase/migrations/20230302030217_equipment.sql@@ -439,8 +439,8 @@ DECLAREequipment_type TEXT;BEGINequipment_type := (SELECT et."name" FROM public."equipmentType" et WHERE et.id = new."equipmentTypeId");- INSERT INTO public.search(name, description, entity, uuid, link)- VALUES (new.name, COALESCE(new.description, '') || ' ' || equipment_type, 'Resource', new.id, '/x/resources/equipment/list/' || new."equipmentTypeId" || '/' || new.id);+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")+ VALUES (new.name, COALESCE(new.description, '') || ' ' || equipment_type, 'Resource', new.id, '/x/resources/equipment/list/' || new."equipmentTypeId" || '/' || new.id, new."companyId");RETURN new;END;$$ LANGUAGE plpgsql SECURITY DEFINER;diff --git a/packages/database/supabase/migrations/20230330024716_parts.sql b/packages/database/supabase/migrations/20230330024716_parts.sqlindex 21c445bdc5..c4467a9199 100644--- a/packages/database/supabase/migrations/20230330024716_parts.sql+++ b/packages/database/supabase/migrations/20230330024716_parts.sql@@ -3,6 +3,7 @@ CREATE TABLE "partGroup" ("name" TEXT NOT NULL,"description" TEXT,"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,40 +11,43 @@ CREATE TABLE "partGroup" ("customFields" JSONB,CONSTRAINT "partGroup_pkey" PRIMARY KEY ("id"),- CONSTRAINT "partGroup_name_key" UNIQUE ("name"),+ CONSTRAINT "partGroup_name_key" UNIQUE ("name", "companyId"),+ CONSTRAINT "partGroup_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partGroup_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "partGroup_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));+CREATE INDEX "partGroup_companyId_idx" ON "partGroup" ("companyId");+ALTER TABLE "partGroup" ENABLE ROW LEVEL SECURITY;-CREATE POLICY "Employees with parts can view part groups" ON "partGroup"+CREATE POLICY "Employees with parts_view can view part groups" ON "partGroup"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));CREATE POLICY "Employees with parts_create can insert part groups" ON "partGroup"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Employees with parts_update can update part groups" ON "partGroup"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_delete can delete part groups" ON "partGroup"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE TYPE "partType" AS ENUM (@@ -98,9 +102,9 @@ CREATE TABLE "unitOfMeasure" (CONSTRAINT "unitOfMeasure_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "unitOfMeasure_code_index" ON "unitOfMeasure"("code");-CREATE INDEX "unitOfMeasure_name_index" ON "unitOfMeasure"("name");-CREATE INDEX "unitOfMeasure_companyId_index" ON "unitOfMeasure"("companyId");+CREATE INDEX "unitOfMeasure_code_idx" ON "unitOfMeasure"("code");+CREATE INDEX "unitOfMeasure_name_idx" ON "unitOfMeasure"("name");+CREATE INDEX "unitOfMeasure_companyId_idx" ON "unitOfMeasure"("companyId");ALTER TABLE "unitOfMeasure" ENABLE ROW LEVEL SECURITY;@@ -114,22 +118,22 @@ CREATE POLICY "Authenticated users can view units of measure" ON "unitOfMeasure"CREATE POLICY "Employees with parts_create can insert units of measure" ON "unitOfMeasure"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Employees with parts_update can update units of measure" ON "unitOfMeasure"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_delete can delete units of measure" ON "unitOfMeasure"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_delete', "companyId"));@@ -164,12 +168,12 @@ CREATE TABLE "part" (CONSTRAINT "part_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "part_name_index" ON "part"("name");+CREATE INDEX "part_name_idx" ON "part"("name");CREATE INDEX "part_companyId_idx" ON "part" ("companyId");-CREATE INDEX "part_partType_index" ON "part"("partType");-CREATE INDEX "part_partGroupId_index" ON "part"("partGroupId");-CREATE INDEX "part_replenishmentSystem_index" ON "part"("replenishmentSystem");-CREATE INDEX "part_active_blocked_index" ON "part"("active", "blocked");+CREATE INDEX "part_partType_idx" ON "part"("partType");+CREATE INDEX "part_partGroupId_idx" ON "part"("partGroupId");+CREATE INDEX "part_replenishmentSystem_idx" ON "part"("replenishmentSystem");+CREATE INDEX "part_active_blocked_idx" ON "part"("active", "blocked");ALTER publication supabase_realtime ADD TABLE "part";ALTER TABLE "part" ENABLE ROW LEVEL SECURITY;@@ -177,35 +181,38 @@ ALTER TABLE "part" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees can view parts" ON "part"FOR SELECTUSING (- (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee')+ AND "companyId" = ANY(+ select "companyId" from "userToCompany" where "userId" = auth.uid()::text+ ));CREATE POLICY "Employees with parts_create can insert parts" ON "part"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Employees with parts_update can update parts" ON "part"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_delete can delete parts" ON "part"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_delete', "companyId"));CREATE FUNCTION public.create_part_search_result()RETURNS TRIGGER AS $$BEGIN- INSERT INTO public.search(name, description, entity, uuid, link)- VALUES (new.id, new.id || ' ' || new.name || ' ' || COALESCE(new.description, ''), 'Part', new.id, '/x/part/' || new.id);+ INSERT INTO public.search(name, description, entity, uuid, link, "companyId")+ VALUES (new.id, new.id || ' ' || new.name || ' ' || COALESCE(new.description, ''), 'Part', new.id, '/x/part/' || new.id, new."companyId");RETURN new;END;$$ LANGUAGE plpgsql SECURITY DEFINER;@@ -217,15 +224,15 @@ CREATE TRIGGER create_part_search_resultCREATE FUNCTION public.create_part_related_records()RETURNS TRIGGER AS $$BEGIN- INSERT INTO public."partCost"("partId", "costingMethod", "createdBy")- VALUES (new.id, 'Standard', new."createdBy");+ INSERT INTO public."partCost"("partId", "costingMethod", "createdBy", "companyId")+ VALUES (new.id, 'Standard', new."createdBy", new."companyId");- INSERT INTO public."partReplenishment"("partId", "companyId", "createdBy")- VALUES (new.id, new."companyId", new."createdBy");+ INSERT INTO public."partReplenishment"("partId", "companyId", "createdBy", "companyId")+ VALUES (new.id, new."companyId", new."createdBy", new."companyId");- INSERT INTO public."partUnitSalePrice"("partId", "companyId", "currencyCode", "createdBy")+ INSERT INTO public."partUnitSalePrice"("partId", "companyId", "currencyCode", "createdBy", "companyId")-- TODO: get default currency- VALUES (new.id, new."companyId", 'USD', new."createdBy");+ VALUES (new.id, new."companyId", 'USD', new."createdBy", new."companyId");RETURN new;END;@@ -269,6 +276,7 @@ CREATE TABLE "partCost" ("standardCost" NUMERIC(15,5) NOT NULL DEFAULT 0,"unitCost" NUMERIC(15,5) NOT NULL DEFAULT 0,"costIsAdjusted" BOOLEAN NOT NULL DEFAULT false,+ "companyId" INTEGER NOT NULL,"createdBy" TEXT NOT NULL,"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),"updatedBy" TEXT,@@ -276,26 +284,27 @@ CREATE TABLE "partCost" ("customFields" JSONB,CONSTRAINT "partCost_partId_fkey" FOREIGN KEY ("partId") REFERENCES "part"("id") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "partCost_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partGroup_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "partGroup_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "partCost_partId_index" ON "partCost" ("partId");+CREATE INDEX "partCost_partId_idx" ON "partCost" ("partId");ALTER TABLE "partCost" ENABLE ROW LEVEL SECURITY;-CREATE POLICY "Employees with part_view can view part costs" ON "partCost"+CREATE POLICY "Employees with parts_view can view part costs" ON "partCost"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));CREATE POLICY "Employees with parts_update can update part costs" ON "partCost"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE TABLE "partUnitSalePrice" (@@ -321,7 +330,7 @@ CREATE TABLE "partUnitSalePrice" (CONSTRAINT "partUnitSalePrice_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "partUnitSalePrice_partId_index" ON "partUnitSalePrice"("partId");+CREATE INDEX "partUnitSalePrice_partId_idx" ON "partUnitSalePrice"("partId");CREATE INDEX "partUnitSalePrice_companyId_idx" ON "partUnitSalePrice"("companyId");ALTER TABLE "partUnitSalePrice" ENABLE ROW LEVEL SECURITY;@@ -329,15 +338,15 @@ ALTER TABLE "partUnitSalePrice" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with part_view can view part sale prices" ON "partUnitSalePrice"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));CREATE POLICY "Employees with parts_update can update part sale prices" ON "partUnitSalePrice"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE TABLE "partSupplier" (@@ -349,6 +358,7 @@ CREATE TABLE "partSupplier" ("minimumOrderQuantity" INTEGER DEFAULT 1,"conversionFactor" NUMERIC(15,5) NOT NULL DEFAULT 1,"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,@@ -359,11 +369,13 @@ CREATE TABLE "partSupplier" (CONSTRAINT "partSupplier_partId_fkey" FOREIGN KEY ("partId") REFERENCES "part"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partSupplier_supplierId_fkey" FOREIGN KEY ("supplierId") REFERENCES "supplier"("id") ON DELETE CASCADE,CONSTRAINT "partSupplier_part_supplier_unique" UNIQUE ("partId", "supplierId"),+ CONSTRAINT "partSupplier_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "partSupplier_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "partSupplier_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "partSupplier_partId_index" ON "partSupplier"("partId");+CREATE INDEX "partSupplier_partId_idx" ON "partSupplier"("partId");+CREATE INDEX "partSupplier_companyId_idx" ON "partSupplier"("companyId");ALTER TABLE "partSupplier" ENABLE ROW LEVEL SECURITY;@@ -371,39 +383,39 @@ CREATE POLICY "Employees with part/purchasing_view can view part suppliers" ON "FOR SELECTUSING ((- coalesce(get_my_claim('parts_view')::boolean,false) OR- coalesce(get_my_claim('purchasing_view')::boolean,false)+ has_company_permission('parts_view', "companyId") OR+ has_company_permission('purchasing_view', "companyId"))- AND (get_my_claim('role'::text)) = '"employee"'::jsonb- );--CREATE POLICY "Employees with parts_update can update part suppliers" ON "partSupplier"- FOR UPDATE- USING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ AND has_role('employee'));CREATE POLICY "Employees with parts_create can create part suppliers" ON "partSupplier"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId")+ );++CREATE POLICY "Employees with parts_update can update part suppliers" ON "partSupplier"+ FOR UPDATE+ USING (+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_delete can delete part suppliers" ON "partSupplier"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_delete', "companyId"));CREATE POLICY "Suppliers with parts_view can view their own part suppliers" ON "partSupplier"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb- AND "supplierId" IN (+ has_role('supplier') AND+ has_company_permission('parts_view', "companyId") AND+ "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid()));@@ -411,9 +423,9 @@ CREATE POLICY "Suppliers with parts_view can view their own part suppliers" ON "CREATE POLICY "Suppliers with parts_update can update their own part suppliers" ON "partSupplier"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean,false)- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb- AND "supplierId" IN (+ has_role('supplier') AND+ has_company_permission('parts_update', "companyId") AND+ "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid()));@@ -447,32 +459,32 @@ CREATE TABLE "partReplenishment" (CONSTRAINT "partReplenishment_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "partReplenishment_partId_index" ON "partReplenishment" ("partId");+CREATE INDEX "partReplenishment_partId_idx" ON "partReplenishment" ("partId");CREATE INDEX "partReplenishment_companyId_idx" ON "partReplenishment" ("companyId");-CREATE INDEX "partReplenishment_preferredSupplierId_index" ON "partReplenishment" ("preferredSupplierId");+CREATE INDEX "partReplenishment_preferredSupplierId_idx" ON "partReplenishment" ("preferredSupplierId");ALTER TABLE "partReplenishment" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with part_view can view part costs" ON "partReplenishment"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_view', "companyId"));CREATE POLICY "Employees with parts_update can update part costs" ON "partReplenishment"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Suppliers with parts can view parts they created or supply" ON "part"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_view', "companyId")AND ("createdBy" = auth.uid()::textOR (@@ -488,15 +500,15 @@ CREATE POLICY "Suppliers with parts can view parts they created or supply" ON "pCREATE POLICY "Supliers with parts_create can insert parts" ON "part"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Suppliers with parts_update can update parts that they created or supply" ON "part"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_create', "companyId")AND ("createdBy" = auth.uid()::textOR (@@ -512,8 +524,8 @@ CREATE POLICY "Suppliers with parts_update can update parts that they created orCREATE POLICY "Suppliers with parts_delete can delete parts that they created or supply" ON "part"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_create', "companyId")AND ("createdBy" = auth.uid()::textOR (@@ -526,11 +538,11 @@ CREATE POLICY "Suppliers with parts_delete can delete parts that they created or));-CREATE POLICY "Suppliers with parts can view part costs they supply" ON "partCost"+CREATE POLICY "Suppliers with parts_view can view part costs they supply" ON "partCost"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_view', "companyId")AND ("partId" IN (SELECT "partId" FROM "partSupplier" WHERE "supplierId" IN (@@ -543,8 +555,8 @@ CREATE POLICY "Suppliers with parts can view part costs they supply" ON "partCosCREATE POLICY "Suppliers with parts_update can update parts costs that they supply" ON "partCost"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_update', "companyId")AND ("partId" IN (SELECT "partId" FROM "partSupplier" WHERE "supplierId" IN (@@ -554,11 +566,11 @@ CREATE POLICY "Suppliers with parts_update can update parts costs that they supp));-CREATE POLICY "Suppliers with parts can view part replenishments they supply" ON "partReplenishment"+CREATE POLICY "Suppliers with parts_view can view part replenishments they supply" ON "partReplenishment"FOR SELECTUSING (- coalesce(get_my_claim('parts_view')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"supplier"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_create', "companyId")AND ("partId" IN (SELECT "partId" FROM "partSupplier" WHERE "supplierId" IN (@@ -571,8 +583,8 @@ CREATE POLICY "Suppliers with parts can view part replenishments they supply" ONCREATE POLICY "Suppliers with parts_update can update parts replenishments that they supply" ON "partReplenishment"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('supplier') AND+ has_company_permission('parts_update', "companyId")AND ("partId" IN (SELECT "partId" FROM "partSupplier" WHERE "supplierId" IN (@@ -592,6 +604,7 @@ CREATE TABLE "warehouse" ("requiresReceive" BOOLEAN NOT NULL DEFAULT false,"requiresShipment" 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,@@ -600,15 +613,19 @@ CREATE TABLE "warehouse" (CONSTRAINT "warehouse_pkey" PRIMARY KEY ("id"),CONSTRAINT "warehouse_name_key" UNIQUE ("name"),CONSTRAINT "warehouse_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "location"("id"),+ CONSTRAINT "warehouse_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id"),CONSTRAINT "warehouse_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "warehouse_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));+CREATE INDEX "warehouse_companyId_idx" ON "warehouse" ("companyId");+CREATE TABLE "shelf" ("id" TEXT NOT NULL,"locationId" TEXT NOT NULL,"warehouseId" TEXT,"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,@@ -618,43 +635,50 @@ CREATE TABLE "shelf" (CONSTRAINT "shelf_pkey" PRIMARY KEY ("id", "locationId"),CONSTRAINT "shelf_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "location"("id") ON DELETE CASCADE,CONSTRAINT "shelf_warehouseId_fkey" FOREIGN KEY ("warehouseId") REFERENCES "warehouse"("id") ON DELETE CASCADE,+ CONSTRAINT "shelf_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT "shelf_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),CONSTRAINT "shelf_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "shelf_id_locationId_index" ON "shelf" ("id", "locationId");-CREATE INDEX "shelf_warehouseId_index" ON "shelf" ("warehouseId");+CREATE INDEX "shelf_id_locationId_idx" ON "shelf" ("id", "locationId");+CREATE INDEX "shelf_warehouseId_idx" ON "shelf" ("warehouseId");+CREATE INDEX "shelf_companyId_idx" ON "shelf" ("companyId");+ALTER TABLE "shelf" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Authenticated users can view shelves" ON "shelf"FOR SELECTUSING (- auth.role() = 'authenticated'+ has_role('employee')+ AND "companyId" = ANY(+ select "companyId" from "userToCompany" where "userId" = auth.uid()::text+ ));CREATE POLICY "Employees with parts_create can insert shelves" ON "shelf"FOR INSERTWITH CHECK (- coalesce(get_my_claim('parts_create')::boolean,false)- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_create', "companyId"));CREATE POLICY "Employees with parts_update can update shelves" ON "shelf"FOR UPDATEUSING (- coalesce(get_my_claim('parts_update')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_update', "companyId"));CREATE POLICY "Employees with parts_delete can delete shelves" ON "shelf"FOR DELETEUSING (- coalesce(get_my_claim('parts_delete')::boolean, false) = true- AND (get_my_claim('role'::text)) = '"employee"'::jsonb+ has_role('employee') AND+ has_company_permission('parts_delete', "companyId"));+-- TODO: resume hereCREATE TABLE "partPlanning" ("partId" TEXT NOT NULL,@@ -686,7 +710,7 @@ CREATE TABLE "partPlanning" (CONSTRAINT "partPlanning_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "partPlanning_partId_locationId_index" ON "partPlanning" ("partId", "locationId");+CREATE INDEX "partPlanning_partId_locationId_idx" ON "partPlanning" ("partId", "locationId");ALTER TABLE "partPlanning" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with parts can view part planning" ON "partPlanning"@@ -730,8 +754,8 @@ CREATE TABLE "partInventory" (CONSTRAINT "partInventory_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "partInventory_partId_locationId_index" ON "partInventory" ("partId", "locationId");-CREATE INDEX "partInventory_shelfId_index" ON "partInventory" ("defaultShelfId");+CREATE INDEX "partInventory_partId_locationId_idx" ON "partInventory" ("partId", "locationId");+CREATE INDEX "partInventory_shelfId_idx" ON "partInventory" ("defaultShelfId");ALTER TABLE "partInventory" ENABLE ROW LEVEL SECURITY;diff --git a/packages/database/supabase/migrations/20230412120726_services.sql b/packages/database/supabase/migrations/20230412120726_services.sqlindex 96b8c52254..abf4975a84 100644--- a/packages/database/supabase/migrations/20230412120726_services.sql+++ b/packages/database/supabase/migrations/20230412120726_services.sql@@ -76,7 +76,7 @@ CREATE TABLE "serviceSupplier" (CONSTRAINT "serviceSupplier_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"));-CREATE INDEX "serviceSupplier_serviceId_index" ON "serviceSupplier"("serviceId");+CREATE INDEX "serviceSupplier_serviceId_idx" ON "serviceSupplier"("serviceId");ALTER TABLE "serviceSupplier" ENABLE ROW LEVEL SECURITY;diff --git a/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql b/packages/database/supabase/migrations/20230702114301_purchasing-rls.sqlindex 940f35ddb7..4d34590eb4 100644--- a/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql+++ b/packages/database/supabase/migrations/20230702114301_purchasing-rls.sql@@ -63,8 +63,8 @@ CREATE POLICY "Suppliers with purchasing_view can search for their own purchaseCREATE FUNCTION public.create_purchase_order_search_result()RETURNS TRIGGER AS $$BEGIN- INSERT INTO public.search(name, entity, uuid, link)- VALUES (new."purchaseOrderId", 'Purchase Order', new.id, '/x/purchase-order/' || new.id);+ INSERT INTO public.search(name, entity, uuid, link, "companyId")+ VALUES (new."purchaseOrderId", 'Purchase Order', new.id, '/x/purchase-order/' || new.id, new."companyId");RETURN new;END;$$ LANGUAGE plpgsql SECURITY DEFINER;diff --git a/packages/database/supabase/migrations/20240204025905_rfq.sql b/packages/database/supabase/migrations/20240204025905_rfq.sqlindex 4811622278..93cfb4dc21 100644--- a/packages/database/supabase/migrations/20240204025905_rfq.sql+++ b/packages/database/supabase/migrations/20240204025905_rfq.sql@@ -22,7 +22,7 @@ CREATE TABLE "requestForQuote" (CONSTRAINT "requestForQuote_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE);-CREATE INDEX "requestForQuote_locationId_index" ON "requestForQuote" ("locationId");+CREATE INDEX "requestForQuote_locationId_idx" ON "requestForQuote" ("locationId");CREATE TABLE "requestForQuoteLine" ("id" TEXT NOT NULL DEFAULT xid(),@@ -52,9 +52,9 @@ CREATE TABLE "requestForQuoteLine" (CONSTRAINT "requestForQuoteLine_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE);-CREATE INDEX "requestForQuoteLine_requestForQuoteId_index" ON "requestForQuoteLine" ("requestForQuoteId");-CREATE INDEX "requestForQuoteLine_partId_index" ON "requestForQuoteLine" ("partId");-CREATE INDEX "requestForQuoteLine_locationId_index" ON "requestForQuoteLine" ("locationId");+CREATE INDEX "requestForQuoteLine_requestForQuoteId_idx" ON "requestForQuoteLine" ("requestForQuoteId");+CREATE INDEX "requestForQuoteLine_partId_idx" ON "requestForQuoteLine" ("partId");+CREATE INDEX "requestForQuoteLine_locationId_idx" ON "requestForQuoteLine" ("locationId");CREATE TABLE "requestForQuoteSupplier" ("id" TEXT NOT NULL DEFAULT xid(),@@ -80,8 +80,8 @@ CREATE TABLE "requestForQuoteSupplier" (CONSTRAINT "requestForQuoteSupplier_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE);-CREATE INDEX "requestForQuoteSupplier_requestForQuoteId_index" ON "requestForQuoteSupplier" ("requestForQuoteId");-CREATE INDEX "requestForQuoteSupplier_supplierId_index" ON "requestForQuoteSupplier" ("supplierId");+CREATE INDEX "requestForQuoteSupplier_requestForQuoteId_idx" ON "requestForQuoteSupplier" ("requestForQuoteId");+CREATE INDEX "requestForQuoteSupplier_supplierId_idx" ON "requestForQuoteSupplier" ("supplierId");CREATE TABLE "requestForQuoteSupplierLine" ("id" TEXT NOT NULL DEFAULT xid(),diff --git a/packages/database/supabase/migrations/20240311021818_custom-fields.sql b/packages/database/supabase/migrations/20240311021818_custom-fields.sqlindex cb613e8fa4..f57f1e28f3 100644--- a/packages/database/supabase/migrations/20240311021818_custom-fields.sql+++ b/packages/database/supabase/migrations/20240311021818_custom-fields.sql@@ -25,8 +25,8 @@ CREATE TABLE "customFieldTable" (CONSTRAINT "customFieldTable_module_table_name_key" UNIQUE ("module", "table", "name"));-CREATE INDEX "customFieldTable_module_index" ON "customFieldTable" ("module");-CREATE INDEX "customFieldTable_table_index" ON "customFieldTable" ("table");+CREATE INDEX "customFieldTable_module_idx" ON "customFieldTable" ("module");+CREATE INDEX "customFieldTable_table_idx" ON "customFieldTable" ("table");ALTER TABLE "customFieldTable" ENABLE ROW LEVEL SECURITY;