-- Add phone to users ALTER TABLE users ADD COLUMN IF NOT EXISTS phone varchar(50); -- Add manager to roles ALTER TABLE roles ADD COLUMN IF NOT EXISTS manager_id integer REFERENCES users(id) ON DELETE SET NULL; -- Add link_to_user_profile to form fields ALTER TABLE form_fields ADD COLUMN IF NOT EXISTS link_to_user_profile boolean NOT NULL DEFAULT false; -- Create user profile audit log CREATE TABLE IF NOT EXISTS user_profile_audit_log ( id serial PRIMARY KEY, user_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE, organization_id integer NOT NULL REFERENCES organizations(id) ON DELETE CASCADE, action varchar(50) NOT NULL, field_id integer REFERENCES user_profile_fields(id), field_name text, old_value jsonb, new_value jsonb, changed_by integer REFERENCES users(id), changed_by_name text, metadata jsonb, created_at timestamp NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS user_profile_audit_log_user_created_idx ON user_profile_audit_log(user_id, created_at DESC);