28 lines
1.2 KiB
PL/PgSQL
28 lines
1.2 KiB
PL/PgSQL
-- Bind only uniquely proven, active exact paths. Unresolved historical rows stay
|
|
-- visible for review; no name guessing or automatic permission assignment.
|
|
BEGIN;
|
|
ALTER TABLE "material_permission_pack_departments"
|
|
ADD COLUMN "organization_unit_id" TEXT;
|
|
|
|
WITH unique_paths AS (
|
|
SELECT path, MIN(id) AS id
|
|
FROM organization_units WHERE is_active = TRUE
|
|
GROUP BY path HAVING COUNT(*) = 1
|
|
)
|
|
UPDATE material_permission_pack_departments AS membership
|
|
SET organization_unit_id = unit.id
|
|
FROM unique_paths AS unit
|
|
WHERE membership.department_path = unit.path;
|
|
|
|
CREATE UNIQUE INDEX "material_permission_pack_dept_unit_key"
|
|
ON "material_permission_pack_departments"("organization_unit_id");
|
|
CREATE INDEX "material_permission_pack_departments_pack_id_idx"
|
|
ON "material_permission_pack_departments"("pack_id");
|
|
ALTER TABLE "material_permission_pack_departments"
|
|
ADD CONSTRAINT "material_permission_pack_departments_organization_unit_id_fkey"
|
|
FOREIGN KEY ("organization_unit_id") REFERENCES "organization_units"("id")
|
|
ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
DROP INDEX "material_permission_pack_dept_pack_path_key";
|
|
DROP INDEX "material_permission_pack_dept_path_key";
|
|
COMMIT;
|