Fix initial database creation in WAL mode
ober
361ce07aa51eb8483988c5e7f9721f6ff2bd8634
--- a/src/llm-search/db.ss +++ b/src/llm-search/db.ss @@ -39,8 +39,7 @@ (def (schema-init! db) (for-each (lambda (sql) (sqlite-exec db sql)) - '("PRAGMA journal_mode = WAL" - "PRAGMA synchronous = NORMAL" + '("PRAGMA synchronous = NORMAL" "PRAGMA busy_timeout = 5000" "CREATE TABLE IF NOT EXISTS source ( @@ -102,7 +101,11 @@ mtime_ns INTEGER NOT NULL, size_bytes INTEGER NOT NULL, line_count INTEGER NOT NULL, - last_indexed_at TEXT)")) + last_indexed_at TEXT)" + + ;; A new jsqlite database needs a main file before entering WAL mode. + ;; Otherwise only -wal/-shm sidecars exist and close cannot checkpoint. + "PRAGMA journal_mode = WAL")) ;; Ensure source records exist (for-each --- a/test/test-db.ss +++ b/test/test-db.ss @@ -1,6 +1,7 @@ ;;; test/test-db.ss — Unit tests for database layer -(import (jerboa prelude)) +(import (jerboa prelude) + (only (llm-search db) db-open db-close)) (def passed 0) (def failed 0) @@ -100,6 +101,20 @@ (assert-true (hashtable? (hash-get parsed "error")) "error is hash table")) ;; --- Summary --- +(displayln "\n=== Database Persistence Tests ===") + +(let* ([suffix (abs (string-hash (format "~s" (current-time))))] + [db-path (path-join "/tmp" (format "llm-search-db-test-~a.db" suffix))] + [db (db-open db-path)]) + (assert-true (file-exists? db-path) "new WAL database creates its main file") + (db-close db) + (for-each + (lambda (path) + (when (file-exists? path) (delete-file path))) + (list db-path + (string-append db-path "-wal") + (string-append db-path "-shm") + (string-append db-path "-lock")))) (displayln "\n=== Results: " passed " passed, " failed " failed ===") (if (> failed 0)