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 3
Brad Barbin24be5c7e275f
diff --git a/packages/database/supabase/migrations/20230123004206_claims.sql b/packages/database/supabase/migrations/20230123004206_claims.sqlindex 728db10abd..d8efe2481d 100644--- a/packages/database/supabase/migrations/20230123004206_claims.sql+++ b/packages/database/supabase/migrations/20230123004206_claims.sql@@ -36,6 +36,7 @@ CREATE OR REPLACE FUNCTION get_my_claims() RETURNS "jsonb"AS $$DECLARE retval jsonb;BEGIN+ -- TODO: we should be able to use (current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'select raw_app_meta_data from auth.users into retval where id = auth.uid();return retval;@@ -47,6 +48,7 @@ CREATE OR REPLACE FUNCTION get_my_claim(claim text) RETURNS "jsonb"AS $$DECLARE retval jsonb;BEGIN+ -- TODO: we should be able to use (current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'select coalesce(raw_app_meta_data->claim, null) from auth.users into retval where id = auth.uid();return retval;@@ -70,6 +72,7 @@ CREATE OR REPLACE FUNCTION get_permission_companies(claim text) RETURNS integer[AS $$DECLARE retval integer[];BEGIN+ -- TODO: (current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'->select jsonb_to_integer_array(coalesce(raw_app_meta_data->claim, '[]')) from auth.users into retval where id = auth.uid();return retval;@@ -81,6 +84,7 @@ CREATE OR REPLACE FUNCTION get_permission_companies_as_text(claim text) RETURNSAS $$DECLARE retval text[];BEGIN+ -- TODO: (current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'->select jsonb_to_text_array(coalesce(raw_app_meta_data->claim, '[]')) from auth.users into retval where id = auth.uid();return retval;@@ -105,7 +109,8 @@ CREATE OR REPLACE FUNCTION has_company_permission(claim text, company integer) RDECLAREclaim_value integer[];BEGIN- SELECT jsonb_to_integer_array(coalesce(raw_app_meta_data->'settings_update', '[]')) INTO claim_value FROM auth.users WHERE id = auth.uid();+ -- TODO: (current_setting('request.jwt.claims', true)::jsonb)->'app_metadata'->claim+ SELECT jsonb_to_integer_array(coalesce(raw_app_meta_data->claim, '[]')) INTO claim_value FROM auth.users WHERE id = auth.uid();IF claim_value IS NULL THENreturn false;ELSIF 0 = ANY(claim_value::integer[]) THENdiff --git a/packages/database/supabase/migrations/20230126050441_abac.sql b/packages/database/supabase/migrations/20230126050441_abac.sqlindex 4fbdf16f2c..4479c00ac5 100644--- a/packages/database/supabase/migrations/20230126050441_abac.sql+++ b/packages/database/supabase/migrations/20230126050441_abac.sql@@ -141,8 +141,8 @@ CREATE POLICY "Customers with sales_update can update contacts from their organiCREATE POLICY "Employees with purchasing_delete can delete supplier contacts" ON "contact"FOR DELETEUSING (- coalesce(get_my_claim('purchasing_delete')::boolean, false) = true- AND has_role('employee')+ has_role('employee')+ AND has_company_permission('purchasing_delete', "companyId")AND id IN (SELECT "contactId" FROM "supplierContact")@@ -151,12 +151,12 @@ CREATE POLICY "Employees with purchasing_delete can delete supplier contacts" ONCREATE POLICY "Suppliers with purchasing_delete can delete contacts from their organization" ON "contact"FOR DELETEUSING (- coalesce(get_my_claim('purchasing_delete')::boolean, false) = true- AND has_role('supplier')+ has_role('supplier')+ AND has_company_permission('purchasing_delete', "companyId")AND (id IN (SELECT "contactId" FROM "supplierContact" WHERE "supplierId" IN (- SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid()+ SELECT "supplierId" FROM "supplierAccount" sa WHERE id::uuid = auth.uid())))@@ -165,8 +165,8 @@ CREATE POLICY "Suppliers with purchasing_delete can delete contacts from their oCREATE POLICY "Employees with sales_delete can delete customer contacts" ON "contact"FOR DELETEUSING (- coalesce(get_my_claim('sales_delete')::boolean, false) = true- AND has_role('employee')+ has_role('employee')+ AND has_company_permission('sales_delete', "companyId")AND id IN (SELECT "contactId" FROM "customerContact")@@ -175,12 +175,12 @@ CREATE POLICY "Employees with sales_delete can delete customer contacts" ON "conCREATE POLICY "Customers with sales_delete can delete contacts from their organization" ON "contact"FOR DELETEUSING (- coalesce(get_my_claim('sales_delete')::boolean, false) = true- AND has_role('customer')+ has_role('customer')+ AND has_company_permission('sales_delete', "companyId")AND (id IN (SELECT "contactId" FROM "customerContact" WHERE "customerId" IN (- SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid()+ SELECT "customerId" FROM "customerAccount" sa WHERE id::uuid = auth.uid())))@@ -192,19 +192,31 @@ ALTER TABLE "customerType" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with sales_view can view customer types" ON "customerType"FOR SELECT- USING (coalesce(get_my_claim('sales_view')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('sales_view', "companyId")+ );CREATE POLICY "Employees with sales_create can create customer types" ON "customerType"FOR INSERT- WITH CHECK (coalesce(get_my_claim('sales_create')::boolean,false) AND has_role('employee'));+ WITH CHECK (+ has_role('employee')+ AND has_company_permission('sales_create', "companyId")+ );CREATE POLICY "Employees with sales_update can update customer types" ON "customerType"FOR UPDATE- USING (coalesce(get_my_claim('sales_update')::boolean,false) AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('sales_update', "companyId")+ );CREATE POLICY "Employees with sales_delete can delete customer types" ON "customerType"FOR DELETE- USING (coalesce(get_my_claim('sales_delete')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('sales_delete', "companyId")+ );-- customer@@ -212,13 +224,16 @@ ALTER TABLE "customer" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with sales_view can view customer" ON "customer"FOR SELECT- USING (coalesce(get_my_claim('sales_view')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('sales_view', "companyId")+ );CREATE POLICY "Customers with sales_view can their own organization" ON "customer"FOR SELECTUSING (- coalesce(get_my_claim('sales_view')::boolean, false) = true- AND has_role('customer')+ has_role('customer')+ AND has_company_permission('sales_view', "companyId")AND id IN (SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid())@@ -226,17 +241,23 @@ CREATE POLICY "Customers with sales_view can their own organization" ON "customeCREATE POLICY "Employees with sales_create can create customers" ON "customer"FOR INSERT- WITH CHECK (coalesce(get_my_claim('sales_create')::boolean,false) AND has_role('employee'));+ WITH CHECK (+ has_role('employee')+ AND has_company_permission('sales_create', "companyId")+ );CREATE POLICY "Employees with sales_update can update customers" ON "customer"FOR UPDATE- USING (coalesce(get_my_claim('sales_update')::boolean,false) AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('sales_update', "companyId")+ );-CREATE POLICY "Customers with sales_update can their own organization" ON "customer"+CREATE POLICY "Customers with sales_update can update their own organization" ON "customer"FOR UPDATEUSING (- coalesce(get_my_claim('sales_update')::boolean, false) = true- AND has_role('customer')+ has_role('customer')+ AND has_company_permission('sales_update', "companyId")AND id IN (SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid())@@ -244,7 +265,10 @@ CREATE POLICY "Customers with sales_update can their own organization" ON "custoCREATE POLICY "Employees with sales_delete can delete customers" ON "customer"FOR DELETE- USING (coalesce(get_my_claim('sales_delete')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('sales_delete', "companyId")+ );-- customerContact@@ -252,13 +276,23 @@ ALTER TABLE "customerContact" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with sales_view can view customer contact" ON "customerContact"FOR SELECT- USING (coalesce(get_my_claim('sales_view')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('sales_view')+ )+ OR "customerId" IN (+ SELECT "customerId" FROM "customer" WHERE "companyId" = ANY(get_permission_companies('sales_view'))+ )+ )+ );CREATE POLICY "Customers with sales_view can their own customer contacts" ON "customerContact"FOR SELECTUSING (- coalesce(get_my_claim('sales_view')::boolean, false) = true- AND has_role('customer')+ has_role('customer')+ -- TODO: get_permission_companies('sales_view').length > 0AND "customerId" IN (SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid())@@ -266,12 +300,23 @@ CREATE POLICY "Customers with sales_view can their own customer contacts" ON "cuCREATE POLICY "Employees with sales_create can create customer contacts" ON "customerContact"FOR INSERT- WITH CHECK (coalesce(get_my_claim('sales_create')::boolean,false) AND has_role('employee'));+ WITH CHECK (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('sales_create')+ )+ OR "customerId" IN (+ SELECT "customerId" FROM "customer" WHERE "companyId" = ANY(get_permission_companies('sales_create'))+ )+ )+ );CREATE POLICY "Customers with sales_create can create customer contacts" ON "customerContact"FOR INSERT- WITH CHECK (coalesce(get_my_claim('sales_create')::boolean, false) = true- AND has_role('customer')+ WITH CHECK (+ has_role('customer')+ -- TODO: get_permission_companies('sales_create').length > 0AND "customerId" IN (SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid())@@ -279,12 +324,23 @@ CREATE POLICY "Customers with sales_create can create customer contacts" ON "cusCREATE POLICY "Employees with sales_update can update customer contacts" ON "customerContact"FOR UPDATE- USING (coalesce(get_my_claim('sales_update')::boolean,false) AND has_role('employee'));+ USING (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('sales_update')+ )+ OR "customerId" IN (+ SELECT "customerId" FROM "customer" WHERE "companyId" = ANY(get_permission_companies('sales_update'))+ )+ )+ );CREATE POLICY "Customers with sales_update can update their customer contacts" ON "customerContact"FOR UPDATE- USING (coalesce(get_my_claim('sales_update')::boolean, false) = true- AND has_role('customer')+ USING (+ has_role('customer')+ -- TODO: get_permission_companies('sales_update').length > 0AND "customerId" IN (SELECT "customerId" FROM "customerAccount" WHERE id::uuid = auth.uid())@@ -292,7 +348,17 @@ CREATE POLICY "Customers with sales_update can update their customer contacts" OCREATE POLICY "Employees with sales_delete can delete customer contacts" ON "customerContact"FOR DELETE- USING (coalesce(get_my_claim('sales_delete')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('sales_delete')+ )+ OR "customerId" IN (+ SELECT "customerId" FROM "customer" WHERE "companyId" = ANY(get_permission_companies('sales_delete'))+ )+ )+ );-- supplierType@@ -300,19 +366,31 @@ ALTER TABLE "supplierType" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with purchasing_view can view supplier types" ON "supplierType"FOR SELECT- USING (coalesce(get_my_claim('purchasing_view')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('purchasing_view', "companyId")+ );CREATE POLICY "Employees with purchasing_create can create supplier types" ON "supplierType"FOR INSERT- WITH CHECK (coalesce(get_my_claim('purchasing_create')::boolean,false) AND has_role('employee'));+ WITH CHECK (+ has_role('employee')+ AND has_company_permission('purchasing_create', "companyId")+ );CREATE POLICY "Employees with purchasing_update can update supplier types" ON "supplierType"FOR UPDATE- USING (coalesce(get_my_claim('purchasing_update')::boolean,false) AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('purchasing_update', "companyId")+ );CREATE POLICY "Employees with purchasing_delete can delete supplier types" ON "supplierType"FOR DELETE- USING (coalesce(get_my_claim('purchasing_delete')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('purchasing_delete', "companyId")+ );-- supplier@@ -320,13 +398,16 @@ ALTER TABLE "supplier" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with purchasing_view can view supplier" ON "supplier"FOR SELECT- USING (coalesce(get_my_claim('purchasing_view')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('purchasing_view', "companyId")+ );CREATE POLICY "Suppliers with purchasing_view can their own organization" ON "supplier"FOR SELECTUSING (- coalesce(get_my_claim('purchasing_view')::boolean, false) = true- AND has_role('supplier')+ has_role('supplier')+ AND has_company_permission('purchasing_view', "companyId")AND id IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -334,17 +415,23 @@ CREATE POLICY "Suppliers with purchasing_view can their own organization" ON "suCREATE POLICY "Employees with purchasing_create can create suppliers" ON "supplier"FOR INSERT- WITH CHECK (coalesce(get_my_claim('purchasing_create')::boolean,false) AND has_role('employee'));+ WITH CHECK (+ has_role('employee')+ AND has_company_permission('purchasing_create', "companyId")+ );CREATE POLICY "Employees with purchasing_update can update suppliers" ON "supplier"FOR UPDATE- USING (coalesce(get_my_claim('purchasing_update')::boolean,false) AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('purchasing_update', "companyId")+ );-CREATE POLICY "Suppliers with purchasing_update can their own organization" ON "supplier"+CREATE POLICY "Suppliers with purchasing_update can update their own organization" ON "supplier"FOR UPDATEUSING (- coalesce(get_my_claim('purchasing_update')::boolean, false) = true- AND has_role('supplier')+ has_role('supplier')+ AND has_company_permission('purchasing_update', "companyId")AND id IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -352,7 +439,10 @@ CREATE POLICY "Suppliers with purchasing_update can their own organization" ON "CREATE POLICY "Employees with purchasing_delete can delete suppliers" ON "supplier"FOR DELETE- USING (coalesce(get_my_claim('purchasing_delete')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND has_company_permission('purchasing_delete', "companyId")+ );-- supplierContact@@ -360,13 +450,23 @@ ALTER TABLE "supplierContact" ENABLE ROW LEVEL SECURITY;CREATE POLICY "Employees with purchasing_view can view supplier contact" ON "supplierContact"FOR SELECT- USING (coalesce(get_my_claim('purchasing_view')::boolean, false) = true AND has_role('employee'));+ USING (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('purchasing_view')+ )+ OR "supplierId" IN (+ SELECT "supplierId" FROM "supplier" WHERE "companyId" = ANY(get_permission_companies('purchasing_view'))+ )+ )+ );CREATE POLICY "Suppliers with purchasing_view can their own supplier contacts" ON "supplierContact"FOR SELECTUSING (- coalesce(get_my_claim('purchasing_view')::boolean, false) = true- AND has_role('supplier')+ has_role('supplier')+ -- TODO: get_permission_companies('purchasing_view').length > 0AND "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -374,12 +474,23 @@ CREATE POLICY "Suppliers with purchasing_view can their own supplier contacts" OCREATE POLICY "Employees with purchasing_create can create supplier contacts" ON "supplierContact"FOR INSERT- WITH CHECK (coalesce(get_my_claim('purchasing_create')::boolean,false) AND has_role('employee'));+ WITH CHECK (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('purchasing_create')+ )+ OR "supplierId" IN (+ SELECT "supplierId" FROM "supplier" WHERE "companyId" = ANY(get_permission_companies('purchasing_create'))+ )+ )+ );CREATE POLICY "Suppliers with purchasing_create can create supplier contacts" ON "supplierContact"FOR INSERT- WITH CHECK (coalesce(get_my_claim('purchasing_create')::boolean, false) = true- AND has_role('supplier')+ WITH CHECK (+ has_role('supplier')+ -- TODO: get_permission_companies('purchasing_create').length > 0AND "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -387,12 +498,23 @@ CREATE POLICY "Suppliers with purchasing_create can create supplier contacts" ONCREATE POLICY "Employees with purchasing_update can update supplier contacts" ON "supplierContact"FOR UPDATE- USING (coalesce(get_my_claim('purchasing_update')::boolean,false) AND has_role('employee'));+ USING (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('purchasing_update')+ )+ OR "supplierId" IN (+ SELECT "supplierId" FROM "supplier" WHERE "companyId" = ANY(get_permission_companies('purchasing_update'))+ )+ )+ );CREATE POLICY "Suppliers with purchasing_update can update their supplier contacts" ON "supplierContact"FOR UPDATE- USING (coalesce(get_my_claim('purchasing_update')::boolean, false) = true- AND has_role('supplier')+ USING (+ has_role('supplier')+ -- TODO: get_permission_companies('purchasing_update').length > 0AND "supplierId" IN (SELECT "supplierId" FROM "supplierAccount" WHERE id::uuid = auth.uid())@@ -400,5 +522,14 @@ CREATE POLICY "Suppliers with purchasing_update can update their supplier contacCREATE POLICY "Employees with purchasing_delete can delete supplier contacts" ON "supplierContact"FOR DELETE- USING (coalesce(get_my_claim('purchasing_delete')::boolean, false) = true AND has_role('employee'));-+ USING (+ has_role('employee')+ AND (+ 0 = ANY(+ get_permission_companies('purchasing_delete')+ )+ OR "supplierId" IN (+ SELECT "supplierId" FROM "supplier" WHERE "companyId" = ANY(get_permission_companies('purchasing_delete'))+ )+ )+ );\ No newline at end of file