JuliusBrussee/caveman · error

migrate sqlite

Error message

migrate sqlite %q: %w

What it means

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.

Solutions

  1. Read the wrapped SQLite error (e.g. 'file is not a database', 'database is locked', 'readonly database').
  2. If the file is corrupt and disposable, move it aside and let Open recreate it.
  3. Close other processes holding the DB or remove stale -wal/-shm files when safe.
  4. Check disk space and that the filesystem is writable.
  5. Verify the file is actually a SQLite db: `file <path>` or `sqlite3 <path> '.tables'`.

Example fix

// before (corrupt file blocks startup)
store, err := store.Open("/data/caveman.db", logger)
// after (guard: move corrupt db aside first)
if !isSQLiteFile("/data/caveman.db") { os.Rename("/data/caveman.db", "/data/caveman.db.bak") }
store, err := store.Open("/data/caveman.db", logger)
Defensive patterns

Strategy: try-catch

Validate before calling

f, err := os.Open(path)
if err == nil {
    hdr := make([]byte, 16); f.Read(hdr); f.Close()
    if !bytes.HasPrefix(hdr, []byte("SQLite format 3")) { /* not a sqlite db — replace or recover */ }
}

Try / catch

store, err := store.Open(path, logger)
if err != nil && strings.Contains(err.Error(), "migrate sqlite") {
    // restore from backup or move corrupt file aside, then retry once
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/495624f1305b69fc. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/store/store.go:361

	if path == ":memory:" {
		return "file::memory:?" + pragmas
	}
	// A file: URI so a path containing '?' or '#' cannot be truncated into a
	// different database file by the driver's DSN split.
	u := url.URL{Scheme: "file", OmitHost: true, Path: path}
	return u.String() + "?" + pragmas
}

// Open opens (creating if needed) the SQLite database at path and migrates the
// schema. logger may be nil.
func Open(path string, logger *slog.Logger) (*Store, error) {
	db, err := sql.Open("sqlite", sqliteDSN(path))
	if err != nil {
		return nil, fmt.Errorf("open sqlite %q: %w", path, err)
	}
	if _, err := db.Exec(schema); err != nil {
		_ = db.Close()
		return nil, fmt.Errorf("migrate sqlite %q: %w", path, err)
	}
	if path != ":memory:" {
		_ = os.Chmod(path, 0o600)
	}
	for _, m := range migrations {
		if _, err := db.Exec(m); err != nil && !strings.Contains(err.Error(), "duplicate column name") {
			_ = db.Close()
			return nil, fmt.Errorf("migrate sqlite %q: %w", path, err)
		}
	}
	return &Store{db: db, logger: logger, persistent: path != ":memory:", middlewareWriter: make(chan struct{}, 1)}, nil
}

// Persistent reports whether this store retains middleware choices on restart.
func (s *Store) Persistent() bool { return s != nil && s.persistent }

// Close closes the underlying database.
func (s *Store) Close() error { return s.db.Close() }

View on GitHub (pinned to 3ee70a1026)