ALTER TABLE audit_events
  ADD COLUMN IF NOT EXISTS organization_id VARCHAR(40) NOT NULL DEFAULT 'org_system'
    REFERENCES organizations(organization_id);

CREATE INDEX IF NOT EXISTS idx_audit_events_organization_id ON audit_events(organization_id);

ALTER TABLE audit_events ENABLE ROW LEVEL SECURITY;

CREATE POLICY audit_events_org_isolation ON audit_events
  USING (
    organization_id = COALESCE(
      current_setting('app.organization_id', true),
      'org_system'
    )
  );

ALTER TABLE audit_events FORCE ROW LEVEL SECURITY;
