Files
codex/codex-rs/state/migrations/0039_threads_recency_at.sql
Jeremy Rose 7dc7096ae1 [codex] Restore thread recency with compatible migration history (#28671)
## Summary

- Revert #28655, restoring the thread `recencyAt` behavior introduced by
#27910.
- Move `threads_recency_at` to migration 0039 so it no longer collides
with `external_agent_config_imports` at version 0038.
- Repair databases that already applied the recency migration as version
38 by moving the matching migration-history row to version 39 before
SQLx validation. The current version-38 migration can then apply
normally.

## Validation

- `just test -p codex-state
migrations::tests::repairs_recency_migration_that_was_applied_as_version_38`
- `just test -p codex-state -p codex-rollout -p codex-thread-store -p
codex-app-server-protocol -p codex-tui`: 3,439 passed; six TUI tests
could not open the machine's existing read-only incident database at
`~/.codex/sqlite/state_5.sqlite`.
- `just fix -p codex-state`
- `just fmt`
- Verified that state migration versions are unique.
2026-06-17 18:52:18 +00:00

29 lines
962 B
SQL

ALTER TABLE threads ADD COLUMN recency_at INTEGER NOT NULL DEFAULT 0;
ALTER TABLE threads ADD COLUMN recency_at_ms INTEGER NOT NULL DEFAULT 0;
UPDATE threads
SET recency_at = updated_at,
recency_at_ms = updated_at_ms;
-- Older binaries can open databases migrated by newer binaries. Seed recency
-- when one of those binaries inserts a thread without the new columns.
CREATE TRIGGER threads_recency_at_after_insert
AFTER INSERT ON threads
WHEN NEW.recency_at_ms = 0
BEGIN
UPDATE threads
SET recency_at = NEW.updated_at,
recency_at_ms = COALESCE(NEW.updated_at_ms, NEW.updated_at * 1000)
WHERE id = NEW.id;
END;
CREATE INDEX idx_threads_recency_at_ms
ON threads(recency_at_ms DESC, id DESC);
CREATE INDEX idx_threads_archived_cwd_recency_at_ms
ON threads(archived, cwd, recency_at_ms DESC, id DESC);
CREATE INDEX idx_threads_visible_recency_at_ms
ON threads(archived, recency_at_ms DESC, id DESC)
WHERE preview <> '';