{"record":{"id":"65db14a256134458","repo":"benbjohnson/litestream","slug":"set-journal-mode-w","errorCode":null,"errorMessage":"set journal mode: %w","messagePattern":"set journal mode: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/litestream-test/populate.go","lineNumber":85,"sourceCode":"}\n\nfunc (c *PopulateCommand) populateDatabase(ctx context.Context, targetBytes int64) error {\n\tif err := os.Remove(c.DB); err != nil && !os.IsNotExist(err) {\n\t\tslog.Warn(\"Could not remove existing database\", \"error\", err)\n\t}\n\n\tdb, err := sql.Open(\"sqlite3\", c.DB)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"open database: %w\", err)\n\t}\n\tdefer db.Close()\n\n\tif _, err := db.Exec(fmt.Sprintf(\"PRAGMA page_size = %d\", c.PageSize)); err != nil {\n\t\treturn fmt.Errorf(\"set page size: %w\", err)\n\t}\n\n\tif _, err := db.Exec(\"PRAGMA journal_mode = WAL\"); err != nil {\n\t\treturn fmt.Errorf(\"set journal mode: %w\", err)\n\t}\n\n\tif _, err := db.Exec(\"PRAGMA synchronous = NORMAL\"); err != nil {\n\t\treturn fmt.Errorf(\"set synchronous: %w\", err)\n\t}\n\n\tfor i := 0; i < c.TableCount; i++ {\n\t\ttableName := fmt.Sprintf(\"test_table_%d\", i)\n\n\t\tcreateSQL := fmt.Sprintf(`\n\t\t\tCREATE TABLE %s (\n\t\t\t\tid INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\t\t\tdata BLOB,\n\t\t\t\ttext_field TEXT,\n\t\t\t\tint_field INTEGER,\n\t\t\t\tfloat_field REAL,\n\t\t\t\ttimestamp INTEGER\n\t\t\t)","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/benbjohnson/litestream/blob/4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3/cmd/litestream-test/populate.go#L67-L103","documentation":"populateDatabase in the litestream-test helper wraps the failure to execute PRAGMA page_size in this error. The PRAGMA runs immediately after opening the test database to set the page size before WAL mode is enabled; SQLite only honors page_size before any table is created. If the PRAGMA fails, the test database cannot be sized as requested and population aborts.","triggerScenarios":"The db.Exec(\"PRAGMA page_size = N\") call fails: invalid page size value, a locked or corrupted database file, an unwritable database path, or the connection has already been used so page_size is read-only.","commonSituations":"Configuring PageSize to a value not a power of two between 512 and 65536; pointing DB at a file in a non-existent directory; another process (or a stale litestream instance) holds a lock on the database file.","solutions":["Use a valid SQLite page size: a power of two between 512 and 65536 (default 4096).","Verify the parent directory of the database file exists and is writable.","Ensure no other process holds a lock on the database file when populating.","Enable PRAGMA journal_mode = WAL before setting page_size if the database already exists with data."],"exampleFix":"// before\ncfg.PageSize = 3000 // invalid\n// after\ncfg.PageSize = 4096 // power of two, 512..65536","handlingStrategy":"validation","validationCode":"if cfg.PageSize != 0 && (cfg.PageSize < 512 || cfg.PageSize > 65536 || cfg.PageSize&(cfg.PageSize-1) != 0) {\n    return fmt.Errorf(\"invalid page size %d: must be power of two between 512 and 65536\", cfg.PageSize)\n}","typeGuard":null,"tryCatchPattern":"if _, err := db.Exec(fmt.Sprintf(\"PRAGMA page_size = %d\", cfg.PageSize)); err != nil {\n    var sqliteErr sqlite.Error\n    if errors.As(err, &sqliteErr) {\n        log.Printf(\"page_size pragma failed with code %d\", sqliteErr.Code)\n    }\n    return fmt.Errorf(\"set page size: %w\", err)\n}","preventionTips":["Always use power-of-two page sizes between 512 and 65536.","Set PRAGMA page_size before creating any tables; it is a no-op afterwards on an existing database.","Check disk and directory permissions before opening the database.","Run against a fresh database file to avoid lock conflicts."],"tags":["sqlite","pragma","database"],"backgroundTag":"database-query-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"}