29 lines
1008 B
SQL
29 lines
1008 B
SQL
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_name = 'sales_orders' AND column_name = 'org_id'
|
|
) THEN
|
|
ALTER TABLE sales_orders ADD COLUMN org_id bigint NOT NULL DEFAULT 0;
|
|
END IF;
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_name = 'purchase_orders' AND column_name = 'org_id'
|
|
) THEN
|
|
ALTER TABLE purchase_orders ADD COLUMN org_id bigint NOT NULL DEFAULT 0;
|
|
END IF;
|
|
END $$;
|
|
|
|
ALTER TABLE sales_orders ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE purchase_orders ENABLE ROW LEVEL SECURITY;
|
|
|
|
DROP POLICY IF EXISTS sales_orders_org_isolation ON sales_orders;
|
|
CREATE POLICY sales_orders_org_isolation
|
|
ON sales_orders
|
|
USING (org_id::text = current_setting('app.current_org_id', true));
|
|
|
|
DROP POLICY IF EXISTS purchase_orders_org_isolation ON purchase_orders;
|
|
CREATE POLICY purchase_orders_org_isolation
|
|
ON purchase_orders
|
|
USING (org_id::text = current_setting('app.current_org_id', true));
|