ALTER TABLE users
    ADD COLUMN IF NOT EXISTS primary_org_division_id BIGINT REFERENCES org_divisions (id) ON DELETE SET NULL;

UPDATE users u
SET primary_org_division_id = s.division_id
FROM (
    SELECT DISTINCT ON (user_id) user_id, division_id
    FROM org_division_members
    ORDER BY user_id, id ASC
) AS s
WHERE u.id = s.user_id;

DROP TRIGGER IF EXISTS tr_org_division_members_updated ON org_division_members;

ALTER TABLE org_divisions DROP COLUMN IF EXISTS description;

CREATE INDEX IF NOT EXISTS idx_users_primary_org_division ON users (primary_org_division_id);
