{"record":{"id":"495624f1305b69fc","repo":"JuliusBrussee/caveman","slug":"migrate-sqlite-q-w-495624","errorCode":null,"errorMessage":"migrate sqlite %q: %w","messagePattern":"migrate sqlite %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/store/store.go","lineNumber":361,"sourceCode":"\tif path == \":memory:\" {\n\t\treturn \"file::memory:?\" + pragmas\n\t}\n\t// A file: URI so a path containing '?' or '#' cannot be truncated into a\n\t// different database file by the driver's DSN split.\n\tu := url.URL{Scheme: \"file\", OmitHost: true, Path: path}\n\treturn u.String() + \"?\" + pragmas\n}\n\n// Open opens (creating if needed) the SQLite database at path and migrates the\n// schema. logger may be nil.\nfunc Open(path string, logger *slog.Logger) (*Store, error) {\n\tdb, err := sql.Open(\"sqlite\", sqliteDSN(path))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"open sqlite %q: %w\", path, err)\n\t}\n\tif _, err := db.Exec(schema); err != nil {\n\t\t_ = db.Close()\n\t\treturn nil, fmt.Errorf(\"migrate sqlite %q: %w\", path, err)\n\t}\n\tif path != \":memory:\" {\n\t\t_ = os.Chmod(path, 0o600)\n\t}\n\tfor _, m := range migrations {\n\t\tif _, err := db.Exec(m); err != nil && !strings.Contains(err.Error(), \"duplicate column name\") {\n\t\t\t_ = db.Close()\n\t\t\treturn nil, fmt.Errorf(\"migrate sqlite %q: %w\", path, err)\n\t\t}\n\t}\n\treturn &Store{db: db, logger: logger, persistent: path != \":memory:\", middlewareWriter: make(chan struct{}, 1)}, nil\n}\n\n// Persistent reports whether this store retains middleware choices on restart.\nfunc (s *Store) Persistent() bool { return s != nil && s.persistent }\n\n// Close closes the underlying database.\nfunc (s *Store) Close() error { return s.db.Close() }","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/store/store.go#L343-L379","documentation":"Store.Open applies the base schema with db.Exec(schema); if that fails the handle is closed and this error is returned. It means SQLite rejected the initial CREATE TABLE statements — usually the database file is corrupt, locked, not a SQLite file, or on an unreadable path.","triggerScenarios":"Calling store.Open(path, logger) where executing the base schema fails: file is not a valid SQLite database, disk I/O error, database is locked by another process, or read-only filesystem.","commonSituations":"Pointing the proxy at a non-SQLite file (e.g. a config or WAL fragment), a leftover locked db from a crashed process, a full disk, or an NFS/read-only mount.","solutions":["Read the wrapped SQLite error (e.g. 'file is not a database', 'database is locked', 'readonly database').","If the file is corrupt and disposable, move it aside and let Open recreate it.","Close other processes holding the DB or remove stale -wal/-shm files when safe.","Check disk space and that the filesystem is writable.","Verify the file is actually a SQLite db: `file <path>` or `sqlite3 <path> '.tables'`."],"exampleFix":"// before (corrupt file blocks startup)\nstore, err := store.Open(\"/data/caveman.db\", logger)\n// after (guard: move corrupt db aside first)\nif !isSQLiteFile(\"/data/caveman.db\") { os.Rename(\"/data/caveman.db\", \"/data/caveman.db.bak\") }\nstore, err := store.Open(\"/data/caveman.db\", logger)","handlingStrategy":"try-catch","validationCode":"f, err := os.Open(path)\nif err == nil {\n    hdr := make([]byte, 16); f.Read(hdr); f.Close()\n    if !bytes.HasPrefix(hdr, []byte(\"SQLite format 3\")) { /* not a sqlite db — replace or recover */ }\n}","typeGuard":null,"tryCatchPattern":"store, err := store.Open(path, logger)\nif err != nil && strings.Contains(err.Error(), \"migrate sqlite\") {\n    // restore from backup or move corrupt file aside, then retry once\n}","preventionTips":["Back up the DB file before upgrading the binary.","Ensure only one process opens the DB at a time.","Monitor disk space on the volume hosting the DB."],"tags":["sqlite","migration","schema"],"backgroundTag":"database-query-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}