Artifact 1bd1b20e6e33e8b74948e2ff1a0c6a22a7484b62f66c73af442fb85497fe8267:
- File
db/db.go
— part of check-in
[faf57ad9c7]
at
2026-05-05 15:11:07
on branch trunk
— fix: checkpoint WAL on Close so vanilla fossil can read repo (#28)
Closes #27.
libfossil opens repos in WAL journal mode but the Close chain never ran wal_checkpoint, so closed repo files were a 4 KiB stub plus a fat -wal sidecar. Vanilla fossil's validity probe stats the main file before consulting the WAL → repo rejected as "not a valid repository".
- Adds a typed CheckpointMode (Passive / Full / Restart / Truncate) and a Checkpoint(mode) method on the public Repo (mirrored through internal repo and db.DB). CheckpointMode and its constants are re-exported from the package root so callers don't have to import db. - db.DB.Close now runs PRAGMA wal_checkpoint(TRUNCATE) before closing the underlying *sql.DB. Checkpoint errors are joined with the close error via errors.Join so the connection is always closed regardless of checkpoint outcome. - Skips the close-time checkpoint on the WASM/WASI build path where WAL isn't used. (user: [email protected], size: 5766) [annotate] [blame] [check-ins using]
package db import ( "database/sql" "errors" "fmt" "os" ) // CheckpointMode mirrors SQLite's PRAGMA wal_checkpoint(<mode>) argument. type CheckpointMode int const ( // CheckpointPassive checkpoints frames not held by readers; never blocks. // Appropriate for periodic background checkpoints. CheckpointPassive CheckpointMode = iota // CheckpointFull blocks new writers until all frames are checkpointed. CheckpointFull // CheckpointRestart is FULL plus restarts the WAL file. CheckpointRestart // CheckpointTruncate is RESTART plus truncates the WAL file to zero bytes. // Produces the most compact on-disk file and is what Close uses. CheckpointTruncate ) func (m CheckpointMode) String() string { switch m { case CheckpointPassive: return "PASSIVE" case CheckpointFull: return "FULL" case CheckpointRestart: return "RESTART" case CheckpointTruncate: return "TRUNCATE" default: return fmt.Sprintf("CheckpointMode(%d)", int(m)) } } // DB wraps a SQLite database connection. type DB struct { conn *sql.DB path string driver string } // Open opens a SQLite database with the registered driver and default pragmas. func Open(path string) (*DB, error) { return OpenWith(path, OpenConfig{}) } // OpenWith opens a SQLite database with explicit configuration. func OpenWith(path string, cfg OpenConfig) (*DB, error) { if path == "" { panic("db.OpenWith: path must not be empty") } if registered == nil { panic("db.OpenWith: no driver registered — import a driver package (e.g., _ \"github.com/danmestas/libfossil/db/driver/modernc\")") } driver := cfg.Driver if driver == "" { driver = registered.Name } pragmas := DefaultPragmas() for k, v := range cfg.Pragmas { pragmas[k] = v } // WASM targets: skip DSN pragmas (ncruces _pragma syntax fails under WASI // file locking). Open with nolock=1, then apply safe pragmas via SQL. var dsn string if wasmClearPragmas { suffix := wasmDSNSuffix() if suffix != "" { dsn = fmt.Sprintf("file:%s?%s", path, suffix) } else { dsn = path } } else { dsn = registered.BuildDSN(path, pragmas) } conn, err := sql.Open(driver, dsn) if err != nil { return nil, fmt.Errorf("db.Open(%s): %w", driver, err) } if wasmClearPragmas { // Apply safe pragmas via SQL. Skip journal_mode (WAL not supported on WASI). // Only apply known-safe pragmas to prevent SQL injection. safePragmas := map[string]bool{ "busy_timeout": true, "foreign_keys": true, } for k, v := range pragmas { if !safePragmas[k] { continue } if _, err := conn.Exec(fmt.Sprintf("PRAGMA %s = %s", k, v)); err != nil { conn.Close() return nil, fmt.Errorf("db.Open PRAGMA %s: %w", k, err) } } } return &DB{conn: conn, path: path, driver: driver}, nil } // SqlDB returns the underlying *sql.DB connection. func (d *DB) SqlDB() *sql.DB { if d == nil { panic("db.SqlDB: receiver must not be nil") } return d.conn } // Close runs PRAGMA wal_checkpoint(TRUNCATE) before closing the underlying // connection so the on-disk file is readable by external SQLite/fossil // tooling. The WASM/WASI build path does not use WAL, so the checkpoint is // skipped there. Checkpoint errors are joined with the close error so the // connection is always closed regardless of checkpoint outcome. func (d *DB) Close() error { var ckptErr error if !wasmClearPragmas { ckptErr = d.Checkpoint(CheckpointTruncate) } closeErr := d.conn.Close() return errors.Join(ckptErr, closeErr) } // Checkpoint runs PRAGMA wal_checkpoint(<mode>) against the database. // Safe to call on a live database. PASSIVE never blocks; TRUNCATE produces // the most compact on-disk layout. On a non-WAL database the underlying // PRAGMA is a no-op and returns nil. func (d *DB) Checkpoint(mode CheckpointMode) error { if d == nil { panic("db.Checkpoint: receiver must not be nil") } if mode < CheckpointPassive || mode > CheckpointTruncate { return fmt.Errorf("db.Checkpoint: invalid mode %d", int(mode)) } var busy, log, ckpt int err := d.conn.QueryRow(fmt.Sprintf("PRAGMA wal_checkpoint(%s)", mode)).Scan(&busy, &log, &ckpt) if err != nil { return fmt.Errorf("db.Checkpoint(%s): %w", mode, err) } return nil } func (d *DB) Path() string { return d.path } func (d *DB) Driver() string { return d.driver } func (d *DB) Exec(query string, args ...any) (sql.Result, error) { return d.conn.Exec(query, args...) } func (d *DB) QueryRow(query string, args ...any) *sql.Row { return d.conn.QueryRow(query, args...) } func (d *DB) Query(query string, args ...any) (*sql.Rows, error) { return d.conn.Query(query, args...) } func (d *DB) SetApplicationID(id int32) error { _, err := d.conn.Exec(fmt.Sprintf("PRAGMA application_id=%d", id)) return err } func (d *DB) ApplicationID() (int32, error) { var id int32 err := d.conn.QueryRow("PRAGMA application_id").Scan(&id) return id, err } type Tx struct { tx *sql.Tx } func (t *Tx) Exec(query string, args ...any) (sql.Result, error) { return t.tx.Exec(query, args...) } func (t *Tx) QueryRow(query string, args ...any) *sql.Row { return t.tx.QueryRow(query, args...) } func (t *Tx) Query(query string, args ...any) (*sql.Rows, error) { return t.tx.Query(query, args...) } func (d *DB) WithTx(fn func(tx *Tx) error) error { if d == nil { panic("db.WithTx: receiver must not be nil") } if fn == nil { panic("db.WithTx: fn must not be nil") } sqlTx, err := d.conn.Begin() if err != nil { return fmt.Errorf("db.WithTx begin: %w", err) } defer func() { if rbErr := sqlTx.Rollback(); rbErr != nil && rbErr != sql.ErrTxDone { fmt.Fprintf(os.Stderr, "db.WithTx: rollback failed: %v\n", rbErr) } }() if err := fn(&Tx{tx: sqlTx}); err != nil { return err } return sqlTx.Commit() }