CREATE TABLE IF NOT EXISTS miragenflow_store_snapshots ( snapshot_key TEXT PRIMARY KEY, payload JSONB NOT NULL, updated_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE TABLE IF NOT EXISTS miragenflow_domain_rows ( root_key TEXT NOT NULL, entity_id TEXT NOT NULL, shape TEXT NOT NULL, payload JSONB, updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), PRIMARY KEY (root_key, entity_id) ); CREATE TABLE IF NOT EXISTS miragenflow_store_revisions ( snapshot_key TEXT PRIMARY KEY, revision BIGINT NOT NULL DEFAULT 0, updated_at TIMESTAMPTZ NOT NULL DEFAULT now() ); ALTER TABLE webdav_files ADD COLUMN IF NOT EXISTS sync_state TEXT NOT NULL DEFAULT 'synced'; ALTER TABLE webdav_sync_jobs ADD COLUMN IF NOT EXISTS intent TEXT; ALTER TABLE webdav_sync_jobs ADD COLUMN IF NOT EXISTS conflict_copy_path TEXT; ALTER TABLE generation_tasks ADD COLUMN IF NOT EXISTS event_sequence BIGINT NOT NULL DEFAULT 0; ALTER TABLE generation_tasks ADD COLUMN IF NOT EXISTS reserve_expires_at TIMESTAMPTZ; ALTER TABLE generation_tasks ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now(); ALTER TABLE generation_tasks ADD COLUMN IF NOT EXISTS lease_token TEXT; ALTER TABLE generation_attempts ADD COLUMN IF NOT EXISTS platform_idempotency_key TEXT; ALTER TABLE generation_attempts ADD COLUMN IF NOT EXISTS reconciliation_status TEXT; ALTER TABLE generation_attempts ADD COLUMN IF NOT EXISTS lease_expires_at TIMESTAMPTZ; ALTER TABLE generation_outputs ADD COLUMN IF NOT EXISTS unit_price_snapshot BIGINT; ALTER TABLE generation_outputs ADD COLUMN IF NOT EXISTS charged_amount BIGINT; ALTER TABLE balance_ledger ADD COLUMN IF NOT EXISTS task_id UUID; ALTER TABLE balance_ledger ADD COLUMN IF NOT EXISTS output_id UUID; ALTER TABLE balance_ledger ADD COLUMN IF NOT EXISTS bucket_id UUID; ALTER TABLE balance_ledger ADD COLUMN IF NOT EXISTS expires_at TIMESTAMPTZ; ALTER TABLE balance_ledger ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'posted'; CREATE UNIQUE INDEX IF NOT EXISTS generation_attempts_task_sequence_uq ON generation_attempts(task_id, sequence); CREATE UNIQUE INDEX IF NOT EXISTS generation_attempts_platform_idem_uq ON generation_attempts(platform_idempotency_key) WHERE platform_idempotency_key IS NOT NULL;