fix: bd migrate respects config.json database name and fixes I/O errors
Fixes #204 Multiple critical bugs in bd migrate: 1. **Respects config.json database name**: migrate now loads config.json and uses the configured database name instead of hardcoding beads.db. Users with custom database names (e.g., beady.db) will no longer have their databases renamed. 2. **Fixes disk I/O error (522)**: Clean up orphaned WAL files before reopening database to update schema version. This prevents SQLite error 522 (disk I/O error) when WAL files exist from previous database sessions. 3. **Creates backup before migration**: First migration now creates a timestamped backup (*.backup-pre-migrate-*.db) before renaming database, consistent with hash-id migration behavior. 4. **Cleans up orphaned WAL files**: Removes .db-wal and .db-shm files after migrating old database to prevent stale files. Changes: - Load config.json in migrate command to get target database name - Use cfg.Database instead of hardcoded beads.CanonicalDatabaseName - Add loadOrCreateConfig() helper function - Add cleanupWALFiles() to remove orphaned WAL/SHM files - Create backup before first migration - Clean up WAL files before version update and after migration - Add TestMigrateRespectsConfigJSON test All migration tests passing. Amp-Thread-ID: https://ampcode.com/threads/T-e5b9ddd0-621b-418b-bc52-ba9462975c39 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -125,3 +125,53 @@ func TestFormatDBList(t *testing.T) {
|
||||
t.Errorf("Expected version 0.16.0, got %s", result[1]["version"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateRespectsConfigJSON(t *testing.T) {
|
||||
// Test that migrate respects custom database name from config.json
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0750); err != nil {
|
||||
t.Fatalf("Failed to create .beads directory: %v", err)
|
||||
}
|
||||
|
||||
// Create config.json with custom database name
|
||||
configPath := filepath.Join(beadsDir, "config.json")
|
||||
configData := `{"database": "beady.db", "version": "0.21.1", "jsonl_export": "beady.jsonl"}`
|
||||
if err := os.WriteFile(configPath, []byte(configData), 0600); err != nil {
|
||||
t.Fatalf("Failed to create config.json: %v", err)
|
||||
}
|
||||
|
||||
// Create old database with custom name
|
||||
oldDBPath := filepath.Join(beadsDir, "beady.db")
|
||||
store, err := sqlite.New(oldDBPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
if err := store.SetMetadata(ctx, "bd_version", "0.21.1"); err != nil {
|
||||
t.Fatalf("Failed to set version: %v", err)
|
||||
}
|
||||
_ = store.Close()
|
||||
|
||||
// Load config
|
||||
cfg, err := loadOrCreateConfig(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// Verify config respects custom database name
|
||||
if cfg.Database != "beady.db" {
|
||||
t.Errorf("Expected database name 'beady.db', got %s", cfg.Database)
|
||||
}
|
||||
|
||||
expectedPath := filepath.Join(beadsDir, "beady.db")
|
||||
actualPath := cfg.DatabasePath(beadsDir)
|
||||
if actualPath != expectedPath {
|
||||
t.Errorf("Expected path %s, got %s", expectedPath, actualPath)
|
||||
}
|
||||
|
||||
// Verify database exists at custom path
|
||||
if _, err := os.Stat(actualPath); os.IsNotExist(err) {
|
||||
t.Errorf("Database does not exist at custom path: %s", actualPath)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user