{"record":{"id":"548e4c635bbf2fc8","repo":"JuliusBrussee/caveman","slug":"migrate-sqlite-metadata-q-w","errorCode":null,"errorMessage":"migrate sqlite metadata %q: %w","messagePattern":"migrate sqlite metadata %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/ccr/store_sqlite.go","lineNumber":221,"sourceCode":"\t\t\treturn nil, fmt.Errorf(\"configure sqlite journal: %w\", err)\n\t\t}\n\t\t// A filesystem that cannot back -shm (NFS, SMB, some roaming profiles)\n\t\t// leaves the DSN's journal_mode request unapplied. Requiring the WAL\n\t\t// sidecars there would report a perfectly good database as replaced.\n\t\tmode, err := sqliteJournalMode(db)\n\t\tif err != nil {\n\t\t\tcloseSQLiteAfterOpenFailure(db, canonicalPath)\n\t\t\treturn nil, fmt.Errorf(\"read sqlite journal mode: %w\", err)\n\t\t}\n\t\twalJournal = strings.EqualFold(mode, \"wal\")\n\t}\n\tif err := RetryOnBusy(func() error { _, e := db.Exec(schema); return e }); err != nil {\n\t\tcloseSQLiteAfterOpenFailure(db, canonicalPath)\n\t\treturn nil, fmt.Errorf(\"migrate sqlite %q: %w\", canonicalPath, err)\n\t}\n\tif err := RetryOnBusy(func() error { return ensureMetadataColumn(db) }); err != nil {\n\t\tcloseSQLiteAfterOpenFailure(db, canonicalPath)\n\t\treturn nil, fmt.Errorf(\"migrate sqlite metadata %q: %w\", canonicalPath, err)\n\t}\n\tif err := RetryOnBusy(func() error { return ensureDataRefColumn(db) }); err != nil {\n\t\t_ = db.Close()\n\t\treturn nil, fmt.Errorf(\"migrate sqlite data_ref %q: %w\", canonicalPath, err)\n\t}\n\tif err := configureStorageBudget(db, maxBytes); err != nil {\n\t\tcloseSQLiteAfterOpenFailure(db, canonicalPath)\n\t\treturn nil, fmt.Errorf(\"configure sqlite storage budget %q: %w\", canonicalPath, err)\n\t}\n\topened, err := inspectSQLiteGeneration(canonicalPath)\n\tif err != nil {\n\t\tcloseSQLiteAfterOpenFailure(db, canonicalPath)\n\t\treturn nil, err\n\t}\n\tif !before.sameExisting(opened) || (walJournal && (opened[1] == nil || opened[2] == nil)) {\n\t\tcloseSQLiteAfterOpenFailure(db, canonicalPath)\n\t\treturn nil, ErrStorageChanged\n\t}","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/engine/ccr/store_sqlite.go#L203-L239","documentation":"This error wraps failure of ensureMetadataColumn, the migration step that adds the metadata column to the CCR schema (run inside RetryOnBusy). It fires when ALTER TABLE ADD COLUMN fails or remains busy beyond retries, aborting the open.","triggerScenarios":"ensureMetadataColumn's ALTER TABLE / introspection queries fail due to lock contention, a read-only database, or a legacy schema state that cannot be altered.","commonSituations":"Upgrading an app while an older version still holds the db open; databases synced by cloud tools (Dropbox/Drive) that lock files; read-only mounts.","solutions":["Stop all other processes using the database before upgrading, then retry the open","Ensure the db file and its directory are writable (ALTER TABLE needs write access to the file and its journal/WAL sidecars)","Exclude the database from cloud-sync folders that hold locks","Inspect ensureMetadataColumn's underlying SQL error by unwrapping the %w chain"],"exampleFix":"// before\n// db in ~/Dropbox/app/ccr.db -> locks during sync -> busy on ALTER TABLE\npath := filepath.Join(home, \"/Dropbox/app/ccr.db\")\n// after\npath := \"/var/lib/app/ccr.db\" // local, non-synced directory","handlingStrategy":"retry","validationCode":"// stop old version holding the db before upgrading\nif err := exec.Command(\"pgrep\", \"-f\", \"app-old\").Run(); err == nil {\n    return fmt.Errorf(\"old instance still running; stop it before upgrade\")\n}","typeGuard":null,"tryCatchPattern":"store, err := ccr.Open(dbPath)\nif err != nil && strings.Contains(err.Error(), \"migrate sqlite metadata\") {\n    return fmt.Errorf(\"metadata migration failed; is another version using %s? %w\", dbPath, err)\n}","preventionTips":["Use a supervisor lock so only one app version owns the db","Exclude the db directory from Dropbox/Drive sync","Test upgrades against a copy of a legacy database first"],"tags":["sqlite","migration","alter-table"],"backgroundTag":"database-write-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"}