Creates the correlation tables when missing. Version-gated via `session_schema_migrations` like the LCM schema; idempotent.
(
conn: &Connection,
)
| 390 | /// Creates the correlation tables when missing. Version-gated via |
| 391 | /// `session_schema_migrations` like the LCM schema; idempotent. |
| 392 | pub(crate) async fn ensure_git_correlation_schema( |
| 393 | conn: &Connection, |
| 394 | ) -> Result<(), GitCorrelationError> { |
| 395 | if schema_version(conn) |
| 396 | .await |
| 397 | .is_some_and(|version| version >= GIT_CORRELATION_SCHEMA_VERSION) |
| 398 | { |
| 399 | return Ok(()); |
| 400 | } |
| 401 | conn.execute_batch( |
| 402 | "CREATE TABLE IF NOT EXISTS session_schema_migrations ( |
| 403 | name TEXT PRIMARY KEY, |
| 404 | version INTEGER NOT NULL, |
| 405 | applied_at INTEGER NOT NULL DEFAULT (unixepoch()) |
| 406 | ); |
| 407 | CREATE TABLE IF NOT EXISTS session_git_spans ( |
| 408 | span_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 409 | provider TEXT NOT NULL DEFAULT '', |
| 410 | session_id TEXT NOT NULL, |
| 411 | thread_id TEXT, |
| 412 | branch TEXT, |
| 413 | worktree TEXT NOT NULL, |
| 414 | first_ts INTEGER NOT NULL, |
| 415 | last_ts INTEGER NOT NULL, |
| 416 | event_count INTEGER NOT NULL DEFAULT 1, |
| 417 | source TEXT NOT NULL CHECK(source IN ('hook_route', 'ingest', 'backfill')), |
| 418 | created_at INTEGER NOT NULL DEFAULT (unixepoch()), |
| 419 | updated_at INTEGER NOT NULL DEFAULT (unixepoch()), |
| 420 | CHECK(first_ts <= last_ts) |
| 421 | ); |
| 422 | CREATE INDEX IF NOT EXISTS idx_session_git_spans_session |
| 423 | ON session_git_spans(provider, session_id, last_ts); |
| 424 | CREATE INDEX IF NOT EXISTS idx_session_git_spans_branch |
| 425 | ON session_git_spans(branch, last_ts); |
| 426 | CREATE INDEX IF NOT EXISTS idx_session_git_spans_worktree |
| 427 | ON session_git_spans(worktree, last_ts); |
| 428 | CREATE TABLE IF NOT EXISTS commit_sessions ( |
| 429 | commit_sha TEXT NOT NULL, |
| 430 | provider TEXT NOT NULL DEFAULT '', |
| 431 | session_id TEXT NOT NULL, |
| 432 | branch TEXT, |
| 433 | worktree TEXT, |
| 434 | committed_at INTEGER NOT NULL, |
| 435 | span_overlap_kind TEXT NOT NULL |
| 436 | CHECK(span_overlap_kind IN ('within_span', 'extended_window', 'reflog')), |
| 437 | span_id INTEGER, |
| 438 | created_at INTEGER NOT NULL DEFAULT (unixepoch()), |
| 439 | PRIMARY KEY(commit_sha, provider, session_id) |
| 440 | ); |
| 441 | CREATE INDEX IF NOT EXISTS idx_commit_sessions_session |
| 442 | ON commit_sessions(provider, session_id, committed_at); |
| 443 | CREATE INDEX IF NOT EXISTS idx_commit_sessions_branch |
| 444 | ON commit_sessions(branch, committed_at); |
| 445 | CREATE TABLE IF NOT EXISTS git_correlation_meta ( |
| 446 | key TEXT PRIMARY KEY, |
| 447 | value INTEGER NOT NULL, |
| 448 | updated_at INTEGER NOT NULL DEFAULT (unixepoch()) |
| 449 | );", |