Add schema compatibility probe to prevent silent migration failures (bd-ckvw)

- Implement comprehensive schema probe in sqlite.New() that verifies all
  expected tables and columns after migrations
- Add retry logic: if probe fails, retry migrations once
- Return clear fatal error with missing schema elements if probe still fails
- Enhance daemon version gating: refuse RPC if client has newer minor version
- Improve checkVersionMismatch messaging: verify schema before claiming upgrade
- Add schema compatibility check to bd doctor command
- Add comprehensive tests for schema probing

This prevents the silent migration failure bug where:
1. Migrations fail silently
2. Database queries fail with 'no such column' errors
3. Import logic misinterprets as 'not found' and tries INSERT
4. Results in cryptic UNIQUE constraint errors

Fixes #262

Amp-Thread-ID: https://ampcode.com/threads/T-0d7ae2c0-9f12-4f9b-85d1-1291488af150
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-08 15:40:19 -08:00
parent 54702b59a2
commit f027de93b6
8 changed files with 460 additions and 7 deletions

View File

@@ -276,9 +276,14 @@ func checkVersionMismatch() {
fmt.Fprintf(os.Stderr, "%s\n\n", yellow("⚠️ Some features may not work correctly. Rebuild: go build -o bd ./cmd/bd"))
} else if cmp > 0 {
// Binary is newer than database
// Migrations should have already run in sqlite.New() - verify they succeeded
fmt.Fprintf(os.Stderr, "%s\n", yellow("⚠️ Your binary appears NEWER than the database."))
fmt.Fprintf(os.Stderr, "%s\n\n", yellow("⚠️ The database will be upgraded automatically."))
// Update stored version to current
// Note: Schema probe already ran in sqlite.New() (bd-ckvw)
// If we got here, migrations succeeded. Update version.
fmt.Fprintf(os.Stderr, "%s\n\n", yellow("⚠️ Database schema has been verified and upgraded."))
// Update stored version to current (only after schema verification passed)
_ = store.SetMetadata(ctx, "bd_version", Version)
}
}