ALTER TABLE gateway.departments
    ADD COLUMN IF NOT EXISTS parent_id uuid REFERENCES gateway.departments(id),
    ADD COLUMN IF NOT EXISTS description text NOT NULL DEFAULT '';

CREATE INDEX IF NOT EXISTS departments_parent_active_idx
    ON gateway.departments (parent_id, active, code);

ALTER TABLE gateway.departments
    ADD CONSTRAINT departments_not_self_parent
    CHECK (parent_id IS NULL OR parent_id <> id);

COMMENT ON COLUMN gateway.departments.parent_id IS
    'Adjacency-list parent. Application writes reject cycles transactionally.';
