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.

refactor: migrations for multi-tenant - batch 4

Brad Barbind3ceafa318a9
diff --git a/packages/database/supabase/migrations/20230123004536_attributes.sql b/packages/database/supabase/migrations/20230123004536_attributes.sql
index f1e9536260..afdecf249d 100644
--- a/packages/database/supabase/migrations/20230123004536_attributes.sql
+++ b/packages/database/supabase/migrations/20230123004536_attributes.sql
@@ -184,7 +184,7 @@ CREATE POLICY "Users with resource update can view/modify user attribute values"
SELECT "id" FROM "userAttributeCategory" WHERE
"companyId" = ANY(
coalesce(
- get_permission_companies('resource_update')::integer[],
+ get_permission_companies('resources_update')::integer[],
array[]::integer[]
)
)
@@ -201,7 +201,7 @@ CREATE POLICY "Users can view other users attributes if the category is public"
SELECT "id" FROM "userAttribute" WHERE "userAttributeCategoryId" IN (
SELECT "id" FROM "userAttributeCategory" WHERE "public" = true AND "companyId" = ANY(
coalesce(
- get_permission_companies('resource_view')::integer[],
+ get_permission_companies('resources_view')::integer[],
array[]::integer[]
)
)
diff --git a/packages/database/supabase/migrations/20230209041302_notes.sql b/packages/database/supabase/migrations/20230209041302_notes.sql
index 3411512ad2..ff07968173 100644
--- a/packages/database/supabase/migrations/20230209041302_notes.sql
+++ b/packages/database/supabase/migrations/20230209041302_notes.sql
@@ -4,11 +4,13 @@ CREATE TABLE "note" (
"note" TEXT NOT NULL,
"noteRichText" JSONB NOT NULL DEFAULT '{}'::jsonb,
"active" BOOLEAN NOT NULL DEFAULT true,
+ "companyId" INTEGER NOT NULL,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
"createdBy" TEXT NOT NULL,
"updatedAt" TIMESTAMP WITH TIME ZONE,
CONSTRAINT "notes_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "notes_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "notes_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE
);
@@ -19,25 +21,31 @@ ALTER TABLE "note" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees can view notes" ON "note"
FOR SELECT
USING (
- (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 can insert notes" ON "note"
FOR INSERT
WITH CHECK (
- (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 can update their own notes" ON "note"
FOR UPDATE
USING (
- (get_my_claim('role'::text)) = '"employee"'::jsonb
+ has_role('employee')
AND "createdBy"::uuid = auth.uid()
);
CREATE POLICY "Employees can delete their own notes" ON "note"
FOR DELETE
USING (
- (get_my_claim('role'::text)) = '"employee"'::jsonb
+ has_role('employee')
AND "createdBy"::uuid = auth.uid()
);
diff --git a/packages/database/supabase/migrations/20230215023746_abilities.sql b/packages/database/supabase/migrations/20230215023746_abilities.sql
index 33570506dd..bfdf2fc074 100644
--- a/packages/database/supabase/migrations/20230215023746_abilities.sql
+++ b/packages/database/supabase/migrations/20230215023746_abilities.sql
@@ -4,12 +4,14 @@ CREATE TABLE "ability" (
"curve" JSONB NOT NULL DEFAULT '{"data":[{"id":0,"week":0,"value":50},{"id":1,"week":1,"value":80},{"id":2,"week":2,"value":90},{"id":3,"week":3,"value":100}]}'::jsonb,
"shadowWeeks" NUMERIC NOT NULL DEFAULT 0,
"active" BOOLEAN NOT NULL DEFAULT true,
+ "companyId" INTEGER NOT NULL,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
"createdBy" TEXT NOT NULL,
"updatedAt" TIMESTAMP WITH TIME ZONE,
"updatedBy" TEXT,
CONSTRAINT "abilities_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "abilities_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "abilities_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "abilities_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE
);
@@ -19,29 +21,29 @@ ALTER TABLE "ability" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees with resources_view can view abilities" ON "ability"
FOR SELECT
USING (
- 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 abilities" ON "ability"
FOR INSERT
WITH 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 abilities" ON "ability"
FOR UPDATE
USING (
- 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 abilities" ON "ability"
FOR DELETE
USING (
- 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")
);
CREATE TABLE "employeeAbility" (
@@ -65,27 +67,55 @@ ALTER TABLE "employeeAbility" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees with resources_view can view employeeAbilities" ON "employeeAbility"
FOR SELECT
USING (
- 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 "employeeId" IN (
+ SELECT "employeeId" FROM "employee" WHERE "companyId" = ANY(get_permission_companies('resources_view'))
+ )
+ )
);
CREATE POLICY "Employees with resources_create can insert employeeAbilities" ON "employeeAbility"
FOR INSERT
WITH 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 "employeeId" IN (
+ SELECT "employeeId" FROM "employee" WHERE "companyId" = ANY(get_permission_companies('resources_create'))
+ )
+ )
);
CREATE POLICY "Employees with resources_update can update employeeAbilities" ON "employeeAbility"
FOR UPDATE
USING (
- 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 "employeeId" IN (
+ SELECT "employeeId" FROM "employee" WHERE "companyId" = ANY(get_permission_companies('resources_update'))
+ )
+ )
);
CREATE POLICY "Employees with resources_delete can delete employeeAbilities" ON "employeeAbility"
FOR DELETE
USING (
- 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 "employeeId" IN (
+ SELECT "employeeId" FROM "employee" WHERE "companyId" = ANY(get_permission_companies('resources_delete'))
+ )
+ )
);
diff --git a/packages/database/supabase/migrations/20230224035103_shifts.sql b/packages/database/supabase/migrations/20230224035103_shifts.sql
index 8bbfbe1abd..beb4f1c639 100644
--- a/packages/database/supabase/migrations/20230224035103_shifts.sql
+++ b/packages/database/supabase/migrations/20230224035103_shifts.sql
@@ -29,7 +29,7 @@ CREATE POLICY "Employees can view locations for their companies" ON "location"
FOR SELECT
USING (
auth.role() = 'authenticated'
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
AND "companyId" = ANY(
select "companyId" from "userToCompany" where "userId" = auth.uid()::text
)
@@ -39,21 +39,21 @@ CREATE POLICY "Employees with resources_create can insert locations" ON "locatio
FOR INSERT
WITH CHECK (
has_company_permission('resources_create', "companyId")
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
);
CREATE POLICY "Employees with resources_update can update locations" ON "location"
FOR UPDATE
USING (
has_company_permission('resources_update', "companyId")
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
);
CREATE POLICY "Employees with resources_delete can delete locations" ON "location"
FOR DELETE
USING (
has_company_permission('resources_delete', "companyId")
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
);
@@ -90,9 +90,8 @@ ALTER TABLE "shift" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees can view shifts" ON "shift"
FOR SELECT
USING (
- auth.role() = 'authenticated'
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
- AND "companyId" = ANY(
+ has_role('employee') AND
+ "companyId" = ANY(
select "companyId" from "userToCompany" where "userId" = auth.uid()::text
)
);
@@ -100,22 +99,22 @@ CREATE POLICY "Employees can view shifts" ON "shift"
CREATE POLICY "Employees with resources_create can insert shifts" ON "shift"
FOR INSERT
WITH CHECK (
+ has_role('employee') AND
has_company_permission('resources_create', "companyId")
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
);
CREATE POLICY "Employees with resources_update can update shifts" ON "shift"
FOR UPDATE
USING (
+ has_role('employee') AND
has_company_permission('resources_update', "companyId")
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
);
CREATE POLICY "Employees with resources_delete can delete shifts" ON "shift"
FOR DELETE
USING (
+ has_role('employee') AND
has_company_permission('resources_delete', "companyId")
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
);
@@ -136,8 +135,7 @@ ALTER TABLE "employeeShift" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees can view employee shifts" ON "employeeShift"
FOR SELECT
USING (
- auth.role() = 'authenticated'
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ has_role('employee')
AND "shiftId" IN (
SELECT "id" FROM "shift" WHERE "companyId" = ANY(
select "companyId" from "userToCompany" where "userId" = auth.uid()::text
@@ -148,43 +146,49 @@ CREATE POLICY "Employees can view employee shifts" ON "employeeShift"
CREATE POLICY "Employees with resources_create can insert employee shifts" ON "employeeShift"
FOR INSERT
WITH CHECK (
- "shiftId" IN (
- SELECT "id" FROM "shift" WHERE "companyId" = ANY(
- coalesce(
- get_permission_companies('resource_create')::integer[],
- array[]::integer[]
+ has_role('employee')
+ AND (
+ 0 = ANY(get_permission_companies('resources_create'))
+ OR (
+ "shiftId" IN (
+ SELECT "id" FROM "shift" WHERE "companyId" = ANY(
+ get_permission_companies('resources_create')
+ )
)
)
)
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
);
CREATE POLICY "Employees with resources_update can update employee shifts" ON "employeeShift"
FOR UPDATE
USING (
- "shiftId" IN (
- SELECT "id" FROM "shift" WHERE "companyId" = ANY(
- coalesce(
- get_permission_companies('resource_update')::integer[],
- array[]::integer[]
+ has_role('employee')
+ AND (
+ 0 = ANY(get_permission_companies('resources_update'))
+ OR (
+ "shiftId" IN (
+ SELECT "id" FROM "shift" WHERE "companyId" = ANY(
+ get_permission_companies('resources_update')
+ )
)
)
)
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
);
CREATE POLICY "Employees with resources_delete can delete employee shifts" ON "employeeShift"
FOR DELETE
USING (
- "shiftId" IN (
- SELECT "id" FROM "shift" WHERE "companyId" = ANY(
- coalesce(
- get_permission_companies('resource_delete')::integer[],
- array[]::integer[]
+ has_role('employee')
+ AND (
+ 0 = ANY(get_permission_companies('resources_delete'))
+ OR (
+ "shiftId" IN (
+ SELECT "id" FROM "shift" WHERE "companyId" = ANY(
+ get_permission_companies('resources_delete')
+ )
)
)
)
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
);
@@ -211,13 +215,13 @@ ALTER TABLE "employeeJob" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Employees can view employee jobs" ON "employeeJob"
FOR SELECT
USING (
- auth.role() = 'authenticated'
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ auth.role() = 'authenticated'
+ AND has_role('employee')
AND "id" IN (
SELECT "id" FROM "shift" WHERE "companyId" = ANY(
select "companyId" from "userToCompany" where "userId" = auth.uid()::text
)
- )
+ )
);
CREATE POLICY "Employees with resources_create can insert employee jobs" ON "employeeJob"
@@ -225,13 +229,10 @@ CREATE POLICY "Employees with resources_create can insert employee jobs" ON "emp
WITH CHECK (
"shiftId" IN (
SELECT "id" FROM "shift" WHERE "companyId" = ANY(
- coalesce(
- get_permission_companies('resource_create')::integer[],
- array[]::integer[]
- )
+ get_permission_companies('resources_create')
)
)
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
);
CREATE POLICY "Employees with resources_update can update employee jobs" ON "employeeJob"
@@ -239,13 +240,10 @@ CREATE POLICY "Employees with resources_update can update employee jobs" ON "emp
USING (
"shiftId" IN (
SELECT "id" FROM "shift" WHERE "companyId" = ANY(
- coalesce(
- get_permission_companies('resource_update')::integer[],
- array[]::integer[]
- )
+ get_permission_companies('resources_update')
)
)
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
);
CREATE POLICY "Employees with resources_delete can delete employee jobs" ON "employeeJob"
@@ -253,13 +251,10 @@ CREATE POLICY "Employees with resources_delete can delete employee jobs" ON "emp
USING (
"shiftId" IN (
SELECT "id" FROM "shift" WHERE "companyId" = ANY(
- coalesce(
- get_permission_companies('resource_delete')::integer[],
- array[]::integer[]
- )
+ get_permission_companies('resources_delete')
)
)
- AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ AND has_role('employee')
);