JuliusBrussee/caveman · error

migrate sqlite metadata

Error message

migrate sqlite metadata %q: %w

What it means

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.

Solutions

  1. Stop all other processes using the database before upgrading, then retry the open
  2. Ensure the db file and its directory are writable (ALTER TABLE needs write access to the file and its journal/WAL sidecars)
  3. Exclude the database from cloud-sync folders that hold locks
  4. Inspect ensureMetadataColumn's underlying SQL error by unwrapping the %w chain

Example fix

// before
// db in ~/Dropbox/app/ccr.db -> locks during sync -> busy on ALTER TABLE
path := filepath.Join(home, "/Dropbox/app/ccr.db")
// after
path := "/var/lib/app/ccr.db" // local, non-synced directory
Defensive patterns

Strategy: retry

Validate before calling

// stop old version holding the db before upgrading
if err := exec.Command("pgrep", "-f", "app-old").Run(); err == nil {
    return fmt.Errorf("old instance still running; stop it before upgrade")
}

Try / catch

store, err := ccr.Open(dbPath)
if err != nil && strings.Contains(err.Error(), "migrate sqlite metadata") {
    return fmt.Errorf("metadata migration failed; is another version using %s? %w", dbPath, err)
}

Prevention

When it happens

Trigger: ensureMetadataColumn's ALTER TABLE / introspection queries fail due to lock contention, a read-only database, or a legacy schema state that cannot be altered.

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

Related errors


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

Appendix: source

Thrown at engine/ccr/store_sqlite.go:221

			return nil, fmt.Errorf("configure sqlite journal: %w", err)
		}
		// A filesystem that cannot back -shm (NFS, SMB, some roaming profiles)
		// leaves the DSN's journal_mode request unapplied. Requiring the WAL
		// sidecars there would report a perfectly good database as replaced.
		mode, err := sqliteJournalMode(db)
		if err != nil {
			closeSQLiteAfterOpenFailure(db, canonicalPath)
			return nil, fmt.Errorf("read sqlite journal mode: %w", err)
		}
		walJournal = strings.EqualFold(mode, "wal")
	}
	if err := RetryOnBusy(func() error { _, e := db.Exec(schema); return e }); err != nil {
		closeSQLiteAfterOpenFailure(db, canonicalPath)
		return nil, fmt.Errorf("migrate sqlite %q: %w", canonicalPath, err)
	}
	if err := RetryOnBusy(func() error { return ensureMetadataColumn(db) }); err != nil {
		closeSQLiteAfterOpenFailure(db, canonicalPath)
		return nil, fmt.Errorf("migrate sqlite metadata %q: %w", canonicalPath, err)
	}
	if err := RetryOnBusy(func() error { return ensureDataRefColumn(db) }); err != nil {
		_ = db.Close()
		return nil, fmt.Errorf("migrate sqlite data_ref %q: %w", canonicalPath, err)
	}
	if err := configureStorageBudget(db, maxBytes); err != nil {
		closeSQLiteAfterOpenFailure(db, canonicalPath)
		return nil, fmt.Errorf("configure sqlite storage budget %q: %w", canonicalPath, err)
	}
	opened, err := inspectSQLiteGeneration(canonicalPath)
	if err != nil {
		closeSQLiteAfterOpenFailure(db, canonicalPath)
		return nil, err
	}
	if !before.sameExisting(opened) || (walJournal && (opened[1] == nil || opened[2] == nil)) {
		closeSQLiteAfterOpenFailure(db, canonicalPath)
		return nil, ErrStorageChanged
	}

View on GitHub (pinned to 3ee70a1026)