-- +goose Up -- Free governance.identities for the persistent identity table while keeping -- the existing people-to-channel-identity table working until later slices -- retire it. ALTER TABLE governance.identities RENAME TO person_identities; ALTER INDEX governance.identities_pkey RENAME TO person_identities_pkey; ALTER TABLE governance.person_identities RENAME CONSTRAINT identities_person_id_fkey TO person_identities_person_id_fkey; CREATE TABLE governance.identities ( id text PRIMARY KEY, kind text NOT NULL, display_name text NOT NULL, lifecycle text NOT NULL, version bigint NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE TABLE governance.identity_aliases ( alias text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id) ); CREATE UNIQUE INDEX idx_governance_identity_aliases_alias ON governance.identity_aliases (lower(alias)); CREATE TABLE governance.identity_events ( id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, identity_id text NOT NULL, event_type text NOT NULL, from_lifecycle text NOT NULL, to_lifecycle text NOT NULL, display_name text NOT NULL, actor_id text NOT NULL, source text NOT NULL, request_id text NOT NULL UNIQUE, at timestamptz NOT NULL, org_id text CHECK (org_id IS NULL OR org_id <> '') ); CREATE INDEX idx_governance_identity_events_identity ON governance.identity_events (identity_id, id); CREATE INDEX idx_governance_identity_events_org_at ON governance.identity_events (org_id, at DESC, id DESC); CREATE TABLE governance.identity_subjects ( issuer text NOT NULL, subject text NOT NULL, identity_id text NOT NULL REFERENCES governance.identities (id), bound_at timestamptz NOT NULL, name text NOT NULL DEFAULT '', expires_at timestamptz, last_used_at timestamptz, PRIMARY KEY (issuer, subject) ); CREATE INDEX idx_governance_identity_subjects_identity ON governance.identity_subjects (identity_id); CREATE TABLE governance.orgs ( id text PRIMARY KEY, name text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE TABLE governance.workspaces ( id text NOT NULL, org_id text NOT NULL REFERENCES governance.orgs (id), name text NOT NULL, environment text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (org_id, id) ); CREATE TABLE governance.memberships ( identity_id text NOT NULL REFERENCES governance.identities (id), org_id text NOT NULL REFERENCES governance.orgs (id), workspace_id text, role text NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); CREATE UNIQUE INDEX idx_governance_memberships_identity ON governance.memberships (identity_id, org_id, COALESCE(workspace_id, '')); CREATE INDEX idx_governance_memberships_org ON governance.memberships (org_id); CREATE TABLE governance.org_agents ( identity_id text PRIMARY KEY REFERENCES governance.identities (id), org_id text NOT NULL REFERENCES governance.orgs (id), created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_org_agents_org ON governance.org_agents (org_id); CREATE TABLE governance.accounts ( identity_id text PRIMARY KEY REFERENCES governance.identities (id), email text NOT NULL UNIQUE CHECK (email = lower(email)), password_hash text NOT NULL, recovery_hash text, failed_attempts integer NOT NULL DEFAULT 0, locked_until timestamptz ); CREATE TABLE governance.account_sessions ( token_hash text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id) ON DELETE CASCADE, expires_at timestamptz NOT NULL, issued_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_account_sessions_identity ON governance.account_sessions (identity_id); CREATE INDEX idx_governance_account_sessions_expiry ON governance.account_sessions (expires_at); CREATE TABLE governance.authentication_settings ( singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton), public_url text NOT NULL, oidc_issuer text NOT NULL DEFAULT '', oidc_audience text NOT NULL DEFAULT '', oidc_client_id text NOT NULL DEFAULT '' ); CREATE TABLE governance.passkeys ( credential_id text PRIMARY KEY, identity_id text NOT NULL REFERENCES governance.identities (id), name text NOT NULL, credential jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX idx_governance_passkeys_identity ON governance.passkeys (identity_id); CREATE TABLE governance.passkey_ceremonies ( token_hash text PRIMARY KEY, identity_id text NOT NULL, registration boolean NOT NULL, session_data jsonb NOT NULL, expires_at timestamptz NOT NULL ); CREATE INDEX idx_governance_passkey_ceremonies_expiry ON governance.passkey_ceremonies (expires_at); CREATE TABLE governance.access_policies ( level text NOT NULL CHECK (level IN ('instance', 'org', 'workspace', 'object')), org_id text NOT NULL DEFAULT '', workspace_id text NOT NULL DEFAULT '', object_kind text NOT NULL DEFAULT '', object_id text NOT NULL DEFAULT '', policy_id text NOT NULL, text text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (level, org_id, workspace_id, object_kind, object_id, policy_id) ); CREATE TABLE governance.access_policy_versions ( level text NOT NULL CHECK (level IN ('instance', 'org', 'workspace', 'object')), org_id text NOT NULL DEFAULT '', workspace_id text NOT NULL DEFAULT '', version bigint NOT NULL DEFAULT 1, updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (level, org_id, workspace_id) ); CREATE TABLE governance.access_policy_revision ( only_row boolean PRIMARY KEY DEFAULT true CHECK (only_row), version bigint NOT NULL DEFAULT 1, updated_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO governance.access_policy_revision (only_row) VALUES (true); CREATE TABLE governance.access_denials ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, org_id text NOT NULL, principal text NOT NULL, action text NOT NULL, resource_kind text NOT NULL, resource_id text NOT NULL, level text NOT NULL, policies text[] NOT NULL DEFAULT '{}', minute_at timestamptz NOT NULL DEFAULT date_trunc('minute', now()), count bigint NOT NULL DEFAULT 1, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE UNIQUE INDEX idx_governance_access_denials_dedup ON governance.access_denials (org_id, principal, action, resource_kind, resource_id, level, policies, minute_at); -- +goose Down DROP INDEX governance.idx_governance_access_denials_dedup; DROP TABLE governance.access_denials; DROP TABLE governance.access_policy_revision; DROP TABLE governance.access_policy_versions; DROP TABLE governance.access_policies; DROP TABLE governance.passkey_ceremonies; DROP TABLE governance.passkeys; DROP TABLE governance.authentication_settings; DROP TABLE governance.account_sessions; DROP TABLE governance.accounts; DROP TABLE governance.org_agents; DROP TABLE governance.memberships; DROP TABLE governance.workspaces; DROP TABLE governance.orgs; DROP TABLE governance.identity_subjects; DROP TABLE governance.identity_events; DROP TABLE governance.identity_aliases; DROP TABLE governance.identities; ALTER TABLE governance.person_identities RENAME CONSTRAINT person_identities_person_id_fkey TO identities_person_id_fkey; ALTER INDEX governance.person_identities_pkey RENAME TO identities_pkey; ALTER TABLE governance.person_identities RENAME TO identities;