{"record":{"id":"6114d6836934c426","repo":"benbjohnson/litestream","slug":"open-database-w-6114d6","errorCode":null,"errorMessage":"open database: %w","messagePattern":"open database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"cmd/litestream-test/shrink.go","lineNumber":71,"sourceCode":"\t\t\"checkpoint\", c.Checkpoint,\n\t)\n\n\treturn c.shrinkDatabase(ctx)\n}\n\nfunc (c *ShrinkCommand) shrinkDatabase(ctx context.Context) error {\n\tinitialSize, err := getDatabaseSize(c.DB)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get initial size: %w\", err)\n\t}\n\n\tslog.Info(\"Initial database size\",\n\t\t\"size_mb\", initialSize/1024/1024,\n\t)\n\n\tdb, err := sql.Open(\"sqlite3\", c.DB+\"?_journal_mode=WAL\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"open database: %w\", err)\n\t}\n\tdefer db.Close()\n\n\ttables, err := c.getTableList(db)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get table list: %w\", err)\n\t}\n\n\tslog.Info(\"Found tables\", \"count\", len(tables))\n\n\ttotalDeleted := int64(0)\n\tfor _, table := range tables {\n\t\tdeleted, err := c.deleteFromTable(db, table)\n\t\tif err != nil {\n\t\t\tslog.Error(\"Failed to delete from table\", \"table\", table, \"error\", err)\n\t\t\tcontinue\n\t\t}\n\t\ttotalDeleted += deleted","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/benbjohnson/litestream/blob/4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3/cmd/litestream-test/shrink.go#L53-L89","documentation":"shrinkDatabase opens the SQLite database with sql.Open(\"sqlite3\", c.DB+\"?_journal_mode=WAL\") via the mattn/go-sqlite3 driver. If the driver fails to construct the connection the error is wrapped as `open database: %w`. Note sql.Open does not actually connect; most real failures surface here only for bad DSN construction or missing driver registration, and connection errors can also surface on first use.","triggerScenarios":"The sqlite3 driver binary (mattn/go-sqlite3, requires CGO) is not registered or cgo is disabled at build time (`CGO_ENABLED=0`), producing 'unknown driver sqlite3'; the DSN string with ?_journal_mode=WAL is malformed; the file exists but is not a valid SQLite database.","commonSituations":"Building the tool with CGO_ENABLED=0 in a minimal container (very common with Go cross-compilation); using modernc.org/sqlite-only builds where the driver name differs; passing a path containing characters that break the DSN.","solutions":["Build with CGO_ENABLED=1 and a C toolchain since mattn/go-sqlite3 requires cgo","Confirm the blank import `_ \"github.com/mattn/go-sqlite3\"` exists in the built binary","If the binary must be pure-Go, switch the driver to modernc.org/sqlite and the DSN to `file:<path>?_journal_mode=WAL` with driver name \"sqlite\"","Run `go version -m <binary>` to verify which sqlite driver is linked"],"exampleFix":"// before (build)\nCGO_ENABLED=0 go build -o litestream-test ./cmd/litestream-test\n// after\nCGO_ENABLED=1 go build -o litestream-test ./cmd/litestream-test","handlingStrategy":"try-catch","validationCode":"// verify the binary links a sqlite driver (requires cgo for mattn/go-sqlite3)\n// go version -m ./litestream-test | grep sqlite\n// build with: CGO_ENABLED=1 go build ./cmd/litestream-test","typeGuard":null,"tryCatchPattern":"if err := runShrink(args); err != nil {\n    if strings.Contains(err.Error(), \"open database\") {\n        if strings.Contains(err.Error(), \"unknown driver\") {\n            log.Fatal(\"binary built without sqlite3 driver; rebuild with CGO_ENABLED=1\")\n        }\n    }\n    return err\n}","preventionTips":["Build with CGO_ENABLED=1 and a working C compiler when using mattn/go-sqlite3","Verify with a headless query (sqlite3 CLI) that the file is a valid SQLite database before shrinking","Keep the driver blank-import in main so sql.Open(\"sqlite3\", ...) resolves"],"tags":["sqlite","cgo","driver","database-open"],"backgroundTag":"database-open-failed","analyzedSha":"4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3","analyzedAt":"2026-09-06T18:29:25.564Z","contentChangedAt":"2026-09-06T18:29:25.564Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}