fix: remove Beads Classic SQLite backend and migration infrastructure
SQLite is dead. Dolt is the only backend. - Remove BackendSQLite constant and all SQLite branches from configfile - GetBackend() now always returns "dolt" (ignores BEADS_BACKEND env var for sqlite) - DatabasePath() no longer has SQLite path resolution (GH#2016 compat removed) - Delete 13 SQLite→Dolt migration files (migrate_auto, migrate_dolt, migrate_shim, migrate_import, migrate_safety and their tests/nocgo variants) - Remove --to-dolt flag from bd migrate command - Remove autoMigrateSQLiteToDolt() call from main.go startup path - Delete broken_migration doctor check/fix (SQLite residue detection) - Rescue CheckEmbeddedModeConcurrency into its own file (Dolt-specific, not legacy) - Update federation and configfile tests to reflect Dolt-only world - Net: -7,045 lines Closes: hq-x8jhj, hq-1jaou Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a1484df4a0
commit
87493ce917
26 changed files with 144 additions and 7045 deletions
|
|
@ -739,19 +739,6 @@ func runDiagnostics(path string) doctorResult {
|
|||
result.Checks = append(result.Checks, classicArtifactsCheck)
|
||||
// Don't fail overall check for classic artifacts, just warn
|
||||
|
||||
// Check 34: Broken migration state (metadata says dolt but no dolt dir)
|
||||
// Fixes GH#2016
|
||||
brokenMigrationCheck := convertWithCategory(doctor.CheckBrokenMigrationState(path), doctor.CategoryMaintenance)
|
||||
result.Checks = append(result.Checks, brokenMigrationCheck)
|
||||
if brokenMigrationCheck.Status == statusError {
|
||||
result.OverallOK = false
|
||||
}
|
||||
|
||||
// Check 35: SQLite residue after migration
|
||||
sqliteResidueCheck := convertWithCategory(doctor.CheckSQLiteResidue(path), doctor.CategoryMaintenance)
|
||||
result.Checks = append(result.Checks, sqliteResidueCheck)
|
||||
// Don't fail overall check for residue, just warn
|
||||
|
||||
// Check 36: Embedded mode concurrency issues (GH#2086)
|
||||
concurrencyCheck := convertWithCategory(doctor.CheckEmbeddedModeConcurrency(path), doctor.CategoryRuntime)
|
||||
result.Checks = append(result.Checks, concurrencyCheck)
|
||||
|
|
|
|||
|
|
@ -119,8 +119,6 @@ var agentEnrichers = map[string]enricher{
|
|||
"Child-Parent Dependencies": enrichChildParentDeps,
|
||||
"Merge Artifacts": enrichMergeArtifacts,
|
||||
"Classic Artifacts": enrichClassicArtifacts,
|
||||
"Broken Migration State": enrichBrokenMigration,
|
||||
"SQLite Residue": enrichSQLiteResidue,
|
||||
"Embedded Mode Concurrency": enrichEmbeddedConcurrency,
|
||||
"Pending Migrations": enrichPendingMigrations,
|
||||
"KV Sync Status": enrichKVSync,
|
||||
|
|
@ -505,28 +503,6 @@ func enrichClassicArtifacts(dc DoctorCheck) agentEnrichment {
|
|||
}
|
||||
}
|
||||
|
||||
func enrichBrokenMigration(dc DoctorCheck) agentEnrichment {
|
||||
return agentEnrichment{
|
||||
severity: "blocking",
|
||||
explanation: fmt.Sprintf("Broken migration state: %s. The metadata says the backend is Dolt, but the Dolt database directory is missing or empty. This can happen if a migration was interrupted.", dc.Message),
|
||||
observed: dc.Message + "\n" + dc.Detail,
|
||||
expected: "If metadata says Dolt, then .beads/dolt/ exists and is a valid Dolt database",
|
||||
commands: []string{"bd doctor --fix", "rm -rf .beads/dolt && bd init"},
|
||||
sourceFiles: []string{"cmd/bd/doctor/broken_migration.go:CheckBrokenMigrationState"},
|
||||
}
|
||||
}
|
||||
|
||||
func enrichSQLiteResidue(dc DoctorCheck) agentEnrichment {
|
||||
return agentEnrichment{
|
||||
severity: "advisory",
|
||||
explanation: fmt.Sprintf("SQLite residue after migration: %s. The old SQLite database file still exists after migrating to Dolt. It's safe to remove once you've verified the Dolt database works.", dc.Message),
|
||||
observed: dc.Message,
|
||||
expected: "No SQLite database files after Dolt migration",
|
||||
commands: []string{"bd doctor --fix"},
|
||||
sourceFiles: []string{"cmd/bd/doctor/broken_migration.go:CheckSQLiteResidue"},
|
||||
}
|
||||
}
|
||||
|
||||
func enrichEmbeddedConcurrency(dc DoctorCheck) agentEnrichment {
|
||||
return agentEnrichment{
|
||||
severity: "advisory",
|
||||
|
|
|
|||
|
|
@ -1,267 +0,0 @@
|
|||
package doctor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// CheckBrokenMigrationState detects when metadata.json claims the backend is
|
||||
// Dolt, but no Dolt database directory exists and no Dolt server is reachable.
|
||||
// This happens when a migration fails partway or writes to the wrong server.
|
||||
// Fixes GH#2016.
|
||||
func CheckBrokenMigrationState(path string) DoctorCheck {
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
if _, err := os.Stat(beadsDir); os.IsNotExist(err) {
|
||||
return DoctorCheck{
|
||||
Name: "Broken Migration State",
|
||||
Status: StatusOK,
|
||||
Message: "N/A (no .beads directory)",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil || cfg == nil {
|
||||
return DoctorCheck{
|
||||
Name: "Broken Migration State",
|
||||
Status: StatusOK,
|
||||
Message: "N/A (no config)",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
// Only relevant when backend claims to be Dolt
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return DoctorCheck{
|
||||
Name: "Broken Migration State",
|
||||
Status: StatusOK,
|
||||
Message: fmt.Sprintf("Backend is %q, not dolt", cfg.GetBackend()),
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
// In server mode, there is no local dolt/ directory — data lives on the
|
||||
// remote Dolt SQL server. This is expected and NOT a broken state.
|
||||
if cfg.IsDoltServerMode() {
|
||||
return DoctorCheck{
|
||||
Name: "Broken Migration State",
|
||||
Status: StatusOK,
|
||||
Message: "Dolt server mode (no local dolt/ directory expected)",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
// Check if dolt/ directory exists (required for embedded mode)
|
||||
doltDir := getDatabasePath(beadsDir)
|
||||
doltDirExists := false
|
||||
if _, err := os.Stat(doltDir); err == nil {
|
||||
doltDirExists = true
|
||||
}
|
||||
|
||||
if doltDirExists {
|
||||
return DoctorCheck{
|
||||
Name: "Broken Migration State",
|
||||
Status: StatusOK,
|
||||
Message: "Dolt directory exists",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
// Backend says dolt but no dolt/ dir — check if a SQLite DB exists
|
||||
// that we could restore from
|
||||
sqliteExists := false
|
||||
for _, name := range []string{"beads.db", "beads.db.migrated"} {
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, name)); err == nil {
|
||||
sqliteExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// Also check for backups
|
||||
backupExists := false
|
||||
entries, _ := os.ReadDir(beadsDir)
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() && filepath.Ext(e.Name()) == ".db" {
|
||||
if len(e.Name()) > 10 && e.Name()[:6] == "beads." {
|
||||
backupExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
detail := "metadata.json says backend=dolt but no dolt/ directory found."
|
||||
if sqliteExists {
|
||||
detail += " A SQLite database file exists and may contain your data."
|
||||
}
|
||||
if backupExists {
|
||||
detail += " Pre-migration backup file(s) found."
|
||||
}
|
||||
|
||||
fix := "Run 'bd doctor --fix' to restore the SQLite backend in metadata.json"
|
||||
if !sqliteExists && !backupExists {
|
||||
fix = "No SQLite data found. Run 'bd init' to create a fresh database, or restore from git"
|
||||
}
|
||||
|
||||
return DoctorCheck{
|
||||
Name: "Broken Migration State",
|
||||
Status: StatusError,
|
||||
Message: "Dolt backend configured but no Dolt database found",
|
||||
Detail: detail,
|
||||
Fix: fix,
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckEmbeddedModeConcurrency detects when a project is using Dolt embedded
|
||||
// mode but shows signs of concurrent access issues (lock files, multiple
|
||||
// processes). Recommends switching to server mode for multi-agent workflows.
|
||||
// Addresses GH#2086.
|
||||
func CheckEmbeddedModeConcurrency(path string) DoctorCheck {
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil || cfg == nil {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "N/A (no config)",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
// Only relevant for Dolt backend in embedded mode
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "N/A (not dolt backend)",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.IsDoltServerMode() {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "Using server mode (multi-writer supported)",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
// In embedded mode — check for signs of concurrent access problems
|
||||
var issues []string
|
||||
|
||||
// Check for dolt-access.lock (advisory lock from embedded mode)
|
||||
accessLockPath := filepath.Join(beadsDir, "dolt-access.lock")
|
||||
if _, err := os.Stat(accessLockPath); err == nil {
|
||||
issues = append(issues, "dolt-access.lock present (embedded mode advisory lock)")
|
||||
}
|
||||
|
||||
// Check for noms LOCK files in dolt database directories
|
||||
doltDir := getDatabasePath(beadsDir)
|
||||
if entries, err := os.ReadDir(doltDir); err == nil {
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
nomsLock := filepath.Join(doltDir, entry.Name(), ".dolt", "noms", "LOCK")
|
||||
if _, err := os.Stat(nomsLock); err == nil {
|
||||
issues = append(issues, fmt.Sprintf("noms LOCK in %s (Dolt database lock)", entry.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for stale noms LOCK at the top level too
|
||||
topNomsDir := filepath.Join(doltDir, ".dolt", "noms")
|
||||
if _, err := os.Stat(filepath.Join(topNomsDir, "LOCK")); err == nil {
|
||||
issues = append(issues, "noms LOCK in dolt root (Dolt database lock)")
|
||||
}
|
||||
|
||||
if len(issues) == 0 {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "No concurrent access issues detected",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusWarning,
|
||||
Message: fmt.Sprintf("Embedded mode with %d lock indicator(s) — concurrent access may cause failures", len(issues)),
|
||||
Detail: fmt.Sprintf("Detected: %s. Embedded mode is single-writer only. "+
|
||||
"If you run multiple bd processes (e.g., multiple Claude Code windows), "+
|
||||
"switch to server mode for reliable concurrent access.", joinIssues(issues)),
|
||||
Fix: "Start the Dolt server: bd dolt start",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
// joinIssues joins issue strings with semicolons.
|
||||
func joinIssues(issues []string) string {
|
||||
result := ""
|
||||
for i, s := range issues {
|
||||
if i > 0 {
|
||||
result += "; "
|
||||
}
|
||||
result += s
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// CheckSQLiteResidue detects when a migration completed (backend=dolt) but
|
||||
// the original SQLite file still exists with data. This is not critical
|
||||
// but indicates the migration didn't fully clean up.
|
||||
func CheckSQLiteResidue(path string) DoctorCheck {
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil || cfg == nil {
|
||||
return DoctorCheck{
|
||||
Name: "SQLite Residue",
|
||||
Status: StatusOK,
|
||||
Message: "N/A",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return DoctorCheck{
|
||||
Name: "SQLite Residue",
|
||||
Status: StatusOK,
|
||||
Message: "N/A (not dolt backend)",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
info, err := os.Stat(sqlitePath)
|
||||
if os.IsNotExist(err) {
|
||||
return DoctorCheck{
|
||||
Name: "SQLite Residue",
|
||||
Status: StatusOK,
|
||||
Message: "No leftover SQLite database",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil || info.Size() == 0 {
|
||||
return DoctorCheck{
|
||||
Name: "SQLite Residue",
|
||||
Status: StatusOK,
|
||||
Message: "N/A",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
||||
return DoctorCheck{
|
||||
Name: "SQLite Residue",
|
||||
Status: StatusWarning,
|
||||
Message: fmt.Sprintf("beads.db still exists (%d bytes) after migration to Dolt", info.Size()),
|
||||
Detail: "This file may be a leftover from migration. Verify your Dolt data is complete, then rename or remove it.",
|
||||
Fix: "Rename beads.db to beads.db.migrated",
|
||||
Category: CategoryMaintenance,
|
||||
}
|
||||
}
|
||||
|
|
@ -1,387 +0,0 @@
|
|||
package doctor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
func TestCheckBrokenMigrationState_NoBeadsDir(t *testing.T) {
|
||||
check := CheckBrokenMigrationState(t.TempDir())
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for missing .beads dir, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_SQLiteBackend(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendSQLite, Database: "beads.db"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for sqlite backend, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_DoltWithDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK when dolt dir exists, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_DoltServerMode(t *testing.T) {
|
||||
// In server mode, no local dolt/ directory is expected. This should NOT
|
||||
// be reported as a broken migration state.
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendDolt,
|
||||
Database: "dolt",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: 3307,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for server mode (no local dolt/ dir expected), got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_DoltWithoutDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Backend says dolt but no dolt/ directory
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusError {
|
||||
t.Errorf("expected ERROR when dolt dir missing, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
if check.Fix == "" {
|
||||
t.Error("expected a fix suggestion")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_DoltWithoutDirButSQLiteExists(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create a SQLite DB file (recoverable case)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "beads.db"), []byte("sqlite-data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusError {
|
||||
t.Errorf("expected ERROR, got %s", check.Status)
|
||||
}
|
||||
if check.Detail == "" || check.Fix == "" {
|
||||
t.Error("expected detail and fix for recoverable broken state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_BackupFilePresent(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// No beads.db, no dolt/, but a backup file exists
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "beads.backup-pre-dolt-20260226-100000.db"), []byte("backup-data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusError {
|
||||
t.Errorf("expected ERROR, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
if !strings.Contains(check.Detail, "backup") {
|
||||
t.Errorf("detail should mention backup, got: %s", check.Detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBrokenMigrationState_OnlyMigratedFileExists(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Only beads.db.migrated exists (no beads.db, no dolt/)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "beads.db.migrated"), []byte("old-data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckBrokenMigrationState(dir)
|
||||
if check.Status != StatusError {
|
||||
t.Errorf("expected ERROR when only .migrated exists, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
// Detail should mention SQLite data exists
|
||||
if !strings.Contains(check.Detail, "SQLite") {
|
||||
t.Errorf("detail should mention SQLite, got: %s", check.Detail)
|
||||
}
|
||||
}
|
||||
|
||||
// --- CheckEmbeddedModeConcurrency tests ---
|
||||
|
||||
func TestCheckEmbeddedModeConcurrency_NoConfig(t *testing.T) {
|
||||
check := CheckEmbeddedModeConcurrency(t.TempDir())
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for missing config, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmbeddedModeConcurrency_SQLiteBackend(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{Backend: configfile.BackendSQLite}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckEmbeddedModeConcurrency(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for sqlite backend, got %s", check.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmbeddedModeConcurrency_ServerMode(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendDolt,
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckEmbeddedModeConcurrency(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for server mode, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
if !strings.Contains(check.Message, "server mode") {
|
||||
t.Errorf("message should mention server mode, got: %s", check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmbeddedModeConcurrency_EmbeddedNoLocks(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckEmbeddedModeConcurrency(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for embedded mode with no locks, got %s", check.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmbeddedModeConcurrency_EmbeddedWithAccessLock(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create dolt-access.lock (sign of concurrent embedded access)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "dolt-access.lock"), []byte("locked"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckEmbeddedModeConcurrency(dir)
|
||||
if check.Status != StatusWarning {
|
||||
t.Errorf("expected WARNING for embedded mode with access lock, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
if !strings.Contains(check.Fix, "bd dolt start") {
|
||||
t.Errorf("fix should recommend starting Dolt server, got: %s", check.Fix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmbeddedModeConcurrency_EmbeddedWithNomsLock(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
// Create dolt database dir with noms LOCK
|
||||
nomsDir := filepath.Join(beadsDir, "dolt", "mydb", ".dolt", "noms")
|
||||
if err := os.MkdirAll(nomsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(nomsDir, "LOCK"), []byte("locked"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckEmbeddedModeConcurrency(dir)
|
||||
if check.Status != StatusWarning {
|
||||
t.Errorf("expected WARNING for embedded mode with noms LOCK, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
if !strings.Contains(check.Detail, "noms LOCK") {
|
||||
t.Errorf("detail should mention noms LOCK, got: %s", check.Detail)
|
||||
}
|
||||
}
|
||||
|
||||
// --- CheckSQLiteResidue tests ---
|
||||
|
||||
func TestCheckSQLiteResidue_NoBeadsDir(t *testing.T) {
|
||||
check := CheckSQLiteResidue(t.TempDir())
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for missing .beads dir, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSQLiteResidue_SQLiteBackend(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{Backend: configfile.BackendSQLite, Database: "beads.db"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Create beads.db — should NOT be reported as residue when backend=sqlite
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "beads.db"), []byte("real-data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check := CheckSQLiteResidue(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for sqlite backend (beads.db is not residue), got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSQLiteResidue_ZeroByteFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Zero-byte beads.db should NOT be reported as residue
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "beads.db"), []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check := CheckSQLiteResidue(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK for zero-byte beads.db, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSQLiteResidue_NoResidue(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckSQLiteResidue(dir)
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("expected OK when no beads.db, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSQLiteResidue_HasResidue(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{Backend: configfile.BackendDolt, Database: "dolt"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create leftover beads.db
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "beads.db"), []byte("leftover-data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckSQLiteResidue(dir)
|
||||
if check.Status != StatusWarning {
|
||||
t.Errorf("expected WARNING for leftover beads.db, got %s: %s", check.Status, check.Message)
|
||||
}
|
||||
}
|
||||
96
cmd/bd/doctor/embedded_concurrency.go
Normal file
96
cmd/bd/doctor/embedded_concurrency.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package doctor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// CheckEmbeddedModeConcurrency detects when a project is using Dolt embedded
|
||||
// mode but shows signs of concurrent access issues (lock files, multiple
|
||||
// processes). Recommends switching to server mode for multi-agent workflows.
|
||||
// Addresses GH#2086.
|
||||
func CheckEmbeddedModeConcurrency(path string) DoctorCheck {
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil || cfg == nil {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "N/A (no config)",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.IsDoltServerMode() {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "Using server mode (multi-writer supported)",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
// In embedded mode — check for signs of concurrent access problems
|
||||
var issues []string
|
||||
|
||||
// Check for dolt-access.lock (advisory lock from embedded mode)
|
||||
accessLockPath := filepath.Join(beadsDir, "dolt-access.lock")
|
||||
if _, err := os.Stat(accessLockPath); err == nil {
|
||||
issues = append(issues, "dolt-access.lock present (embedded mode advisory lock)")
|
||||
}
|
||||
|
||||
// Check for noms LOCK files in dolt database directories
|
||||
doltDir := getDatabasePath(beadsDir)
|
||||
if entries, err := os.ReadDir(doltDir); err == nil {
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
nomsLock := filepath.Join(doltDir, entry.Name(), ".dolt", "noms", "LOCK")
|
||||
if _, err := os.Stat(nomsLock); err == nil {
|
||||
issues = append(issues, fmt.Sprintf("noms LOCK in %s (Dolt database lock)", entry.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for stale noms LOCK at the top level too
|
||||
topNomsDir := filepath.Join(doltDir, ".dolt", "noms")
|
||||
if _, err := os.Stat(filepath.Join(topNomsDir, "LOCK")); err == nil {
|
||||
issues = append(issues, "noms LOCK in dolt root (Dolt database lock)")
|
||||
}
|
||||
|
||||
if len(issues) == 0 {
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusOK,
|
||||
Message: "No concurrent access issues detected",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
return DoctorCheck{
|
||||
Name: "Embedded Mode Concurrency",
|
||||
Status: StatusWarning,
|
||||
Message: fmt.Sprintf("Embedded mode with %d lock indicator(s) — concurrent access may cause failures", len(issues)),
|
||||
Detail: fmt.Sprintf("Detected: %s. Embedded mode is single-writer only. "+
|
||||
"If you run multiple bd processes (e.g., multiple Claude Code windows), "+
|
||||
"switch to server mode for reliable concurrent access.", joinIssues(issues)),
|
||||
Fix: "Start the Dolt server: bd dolt start",
|
||||
Category: CategoryRuntime,
|
||||
}
|
||||
}
|
||||
|
||||
// joinIssues joins issue strings with semicolons.
|
||||
func joinIssues(issues []string) string {
|
||||
result := ""
|
||||
for i, s := range issues {
|
||||
if i > 0 {
|
||||
result += "; "
|
||||
}
|
||||
result += s
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ func TestCheckFederationRemotesAPI_NonDoltBackend(t *testing.T) {
|
|||
|
||||
// Write a config with sqlite backend
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
@ -31,8 +31,8 @@ func TestCheckFederationRemotesAPI_NonDoltBackend(t *testing.T) {
|
|||
if check.Status != StatusOK {
|
||||
t.Errorf("expected StatusOK for non-Dolt backend, got %s", check.Status)
|
||||
}
|
||||
if !strings.Contains(check.Message, "non-Dolt") {
|
||||
t.Errorf("expected message about non-Dolt backend, got %q", check.Message)
|
||||
if !strings.Contains(check.Message, "N/A") {
|
||||
t.Errorf("expected N/A message, got %q", check.Message)
|
||||
}
|
||||
if check.Category != CategoryFederation {
|
||||
t.Errorf("expected CategoryFederation, got %q", check.Category)
|
||||
|
|
@ -141,7 +141,7 @@ func TestCheckFederationPeerConnectivity_NonDoltBackend(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
@ -153,8 +153,8 @@ func TestCheckFederationPeerConnectivity_NonDoltBackend(t *testing.T) {
|
|||
if check.Status != StatusOK {
|
||||
t.Errorf("expected StatusOK for non-Dolt backend, got %s", check.Status)
|
||||
}
|
||||
if !strings.Contains(check.Message, "non-Dolt") {
|
||||
t.Errorf("expected message about non-Dolt backend, got %q", check.Message)
|
||||
if !strings.Contains(check.Message, "N/A") {
|
||||
t.Errorf("expected N/A message, got %q", check.Message)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -191,7 +191,7 @@ func TestCheckFederationSyncStaleness_NonDoltBackend(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
@ -213,7 +213,7 @@ func TestCheckFederationConflicts_NonDoltBackend(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
@ -257,7 +257,7 @@ func TestCheckDoltServerModeMismatch_NonDoltBackend(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
@ -338,7 +338,7 @@ func TestCheckFederationChecks_CategoryIsFederation(t *testing.T) {
|
|||
|
||||
// Write a sqlite config so all checks return quickly with N/A
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
@ -449,7 +449,7 @@ func TestCheckFederationRemotesAPI_AllCheckNames(t *testing.T) {
|
|||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendSQLite,
|
||||
Backend: "sqlite",
|
||||
}
|
||||
data, _ := json.Marshal(cfg)
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), data, 0600); err != nil {
|
||||
|
|
|
|||
|
|
@ -1,110 +0,0 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// BrokenMigrationState restores the SQLite backend in metadata.json when the
|
||||
// Dolt migration failed and left the database in an inconsistent state.
|
||||
//
|
||||
// Pre-conditions (re-verified before applying):
|
||||
// - metadata.json has backend=dolt
|
||||
// - No dolt/ directory exists
|
||||
// - A SQLite database file (beads.db or beads.db.migrated) exists
|
||||
//
|
||||
// Fixes GH#2016.
|
||||
func BrokenMigrationState(path string) error {
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load metadata.json: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
return fmt.Errorf("metadata.json not found in %s", beadsDir)
|
||||
}
|
||||
|
||||
// Re-verify conditions before applying fix
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return fmt.Errorf("backend is %q, not dolt — fix not applicable", cfg.GetBackend())
|
||||
}
|
||||
|
||||
doltDir := getDatabasePath(beadsDir)
|
||||
if _, err := os.Stat(doltDir); err == nil {
|
||||
return fmt.Errorf("dolt/ directory exists — backend may be valid, not fixing")
|
||||
}
|
||||
|
||||
// Check for SQLite data to restore
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
migratedPath := filepath.Join(beadsDir, "beads.db.migrated")
|
||||
|
||||
hasSQLite := false
|
||||
if _, err := os.Stat(sqlitePath); err == nil {
|
||||
hasSQLite = true
|
||||
}
|
||||
|
||||
// If beads.db.migrated exists but beads.db doesn't, rename it back
|
||||
if !hasSQLite {
|
||||
if _, err := os.Stat(migratedPath); err == nil {
|
||||
if err := os.Rename(migratedPath, sqlitePath); err != nil {
|
||||
return fmt.Errorf("failed to restore beads.db from .migrated: %w", err)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, " Restored beads.db from beads.db.migrated\n")
|
||||
hasSQLite = true
|
||||
}
|
||||
}
|
||||
|
||||
if !hasSQLite {
|
||||
return fmt.Errorf("no SQLite database found to restore — run 'bd init' for fresh database")
|
||||
}
|
||||
|
||||
// Update metadata.json to point back to SQLite
|
||||
cfg.Backend = configfile.BackendSQLite
|
||||
cfg.Database = "beads.db"
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
return fmt.Errorf("failed to update metadata.json: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, " Restored backend to SQLite in metadata.json\n")
|
||||
fmt.Fprintf(os.Stderr, " Run 'bd migrate --to-dolt' when ready to migrate\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
// SQLiteResidue renames a leftover beads.db to beads.db.migrated after
|
||||
// verifying the Dolt backend is active. Guards against accidentally
|
||||
// renaming the active SQLite database on a SQLite-backend project.
|
||||
func SQLiteResidue(path string) error {
|
||||
beadsDir := filepath.Join(path, ".beads")
|
||||
|
||||
// Safety guard: only rename if backend is Dolt
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil || cfg == nil {
|
||||
return fmt.Errorf("cannot load metadata.json: cannot verify backend")
|
||||
}
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return fmt.Errorf("backend is %q, not dolt — refusing to rename beads.db", cfg.GetBackend())
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
|
||||
if _, err := os.Stat(sqlitePath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("beads.db not found — nothing to clean up")
|
||||
}
|
||||
|
||||
// Don't overwrite an existing .migrated file from a previous migration attempt
|
||||
if _, err := os.Stat(migratedPath); err == nil {
|
||||
return fmt.Errorf("beads.db.migrated already exists — remove or move it first, then re-run 'bd doctor --fix'")
|
||||
}
|
||||
|
||||
if err := os.Rename(sqlitePath, migratedPath); err != nil {
|
||||
return fmt.Errorf("failed to rename beads.db: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, " Renamed beads.db to beads.db.migrated\n")
|
||||
return nil
|
||||
}
|
||||
|
|
@ -8,73 +8,6 @@ import (
|
|||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// TestDatabaseConfigFix_DBMismatch tests that DatabaseConfig fixes database path mismatches.
|
||||
func TestDatabaseConfigFix_DBMismatch(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.Mkdir(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
// Create actual database file
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(dbPath, []byte("test"), 0600); err != nil {
|
||||
t.Fatalf("Failed to create db: %v", err)
|
||||
}
|
||||
|
||||
// Create metadata.json pointing to wrong database (SQLite backend)
|
||||
cfg := &configfile.Config{
|
||||
Database: "wrong.db",
|
||||
Backend: configfile.BackendSQLite,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Failed to save config: %v", err)
|
||||
}
|
||||
|
||||
// Run the fix
|
||||
if err := DatabaseConfig(tmpDir); err != nil {
|
||||
t.Fatalf("DatabaseConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify the config was updated
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load updated config: %v", err)
|
||||
}
|
||||
|
||||
if updatedCfg.Database != "beads.db" {
|
||||
t.Errorf("Expected Database to be 'beads.db', got %q", updatedCfg.Database)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabaseConfigFix_NoMismatch tests that DatabaseConfig returns error when no mismatch exists.
|
||||
func TestDatabaseConfigFix_NoMismatch(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.Mkdir(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
|
||||
// Create database matching config
|
||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(dbPath, []byte("test"), 0600); err != nil {
|
||||
t.Fatalf("Failed to create db: %v", err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: configfile.BackendSQLite,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Failed to save config: %v", err)
|
||||
}
|
||||
|
||||
err := DatabaseConfig(tmpDir)
|
||||
if err == nil {
|
||||
t.Error("Expected error when no mismatch detected")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabaseConfigFix_DoltBackend tests that DatabaseConfig returns a clear error for Dolt backends.
|
||||
func TestDatabaseConfigFix_DoltBackend(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
|
|
|||
|
|
@ -334,14 +334,10 @@ func applyFixList(path string, fixes []doctorCheck) {
|
|||
err = fix.ClassicArtifacts(path)
|
||||
case "Remote Consistency":
|
||||
err = fix.RemoteConsistency(path)
|
||||
case "Broken Migration State":
|
||||
err = fix.BrokenMigrationState(path)
|
||||
case "Dolt Schema":
|
||||
// GH#2160: Pre-#2142 migrations may have wrong database configured.
|
||||
// Probe the server and backfill dolt_database in metadata.json.
|
||||
err = fix.FixMissingDoltDatabase(path)
|
||||
case "SQLite Residue":
|
||||
err = fix.SQLiteResidue(path)
|
||||
case "Git Merge Driver":
|
||||
err = doctor.FixMergeDriver()
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -415,11 +415,6 @@ var rootCmd = &cobra.Command{
|
|||
}
|
||||
}
|
||||
|
||||
// Auto-migrate SQLite to Dolt if a legacy beads.db is detected (bd-3dx).
|
||||
// This must run BEFORE database path resolution because FindDatabasePath()
|
||||
// only looks for Dolt databases — a SQLite-only .beads/ would be invisible.
|
||||
autoMigrateSQLiteToDolt()
|
||||
|
||||
// Initialize database path
|
||||
if dbPath == "" {
|
||||
// Use public API to find database (same logic as extensions)
|
||||
|
|
@ -499,19 +494,6 @@ var rootCmd = &cobra.Command{
|
|||
// Load config to get database name and server connection settings
|
||||
cfg, cfgErr := configfile.Load(beadsDir)
|
||||
if cfgErr == nil && cfg != nil {
|
||||
// Guard: if backend is explicitly SQLite, the auto-migration was
|
||||
// skipped (user opted to stay on SQLite). There is no SQLite store
|
||||
// in the current codebase, so give a clear error with options.
|
||||
// Fixes GH#2016.
|
||||
if cfg.GetBackend() == configfile.BackendSQLite {
|
||||
fmt.Fprintf(os.Stderr, "This beads database uses the SQLite backend.\n")
|
||||
fmt.Fprintf(os.Stderr, "bd v0.50+ requires Dolt. To migrate, run:\n\n")
|
||||
fmt.Fprintf(os.Stderr, " bd migrate --to-dolt\n\n")
|
||||
fmt.Fprintf(os.Stderr, "To stay on SQLite, use bd v0.49.6.\n")
|
||||
fmt.Fprintf(os.Stderr, "To override (if already migrated): export BEADS_BACKEND=dolt\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Always set database name (needed for bootstrap to find
|
||||
// prefix-based databases like "beads_hq"; see #1669)
|
||||
doltCfg.Database = cfg.GetDoltDatabase()
|
||||
|
|
|
|||
|
|
@ -22,9 +22,6 @@ var migrateCmd = &cobra.Command{
|
|||
|
||||
Without subcommand, checks and updates database metadata to current version.
|
||||
|
||||
Backend migration flags:
|
||||
--to-dolt Migrate from SQLite to Dolt backend
|
||||
|
||||
Subcommands:
|
||||
hooks Plan git hook migration to marker-managed format
|
||||
issues Move issues between repositories
|
||||
|
|
@ -53,13 +50,6 @@ Subcommands:
|
|||
return
|
||||
}
|
||||
|
||||
// Handle --to-dolt flag (SQLite to Dolt migration)
|
||||
toDolt, _ := cmd.Flags().GetBool("to-dolt")
|
||||
if toDolt {
|
||||
handleToDoltMigration(dryRun, autoYes)
|
||||
return
|
||||
}
|
||||
|
||||
// Find .beads directory
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
|
|
@ -724,15 +714,9 @@ func handleToSeparateBranch(branch string, dryRun bool) {
|
|||
}
|
||||
}
|
||||
|
||||
// copyFile copies a file from src to dst
|
||||
func copyFile(src, dst string) error {
|
||||
// nolint:gosec // G304: src is validated migration backup path
|
||||
data, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// nolint:gosec // G306: backup file needs to be readable by other tools
|
||||
return os.WriteFile(dst, data, 0644)
|
||||
// listMigrations returns registered Dolt schema migrations.
|
||||
func listMigrations() []string {
|
||||
return dolt.ListMigrations()
|
||||
}
|
||||
|
||||
// migrateSyncCmd is the "bd migrate sync <branch>" subcommand that
|
||||
|
|
@ -762,7 +746,6 @@ Example:
|
|||
func init() {
|
||||
migrateCmd.Flags().Bool("yes", false, "Auto-confirm prompts")
|
||||
migrateCmd.Flags().Bool("dry-run", false, "Show what would be done without making changes")
|
||||
migrateCmd.Flags().Bool("to-dolt", false, "Migrate from SQLite to Dolt backend")
|
||||
migrateCmd.Flags().Bool("update-repo-id", false, "Update repository ID (use after changing git remote)")
|
||||
migrateCmd.Flags().Bool("inspect", false, "Show migration plan and database state for AI agent analysis")
|
||||
migrateCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output migration statistics in JSON format")
|
||||
|
|
|
|||
|
|
@ -1,186 +0,0 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
)
|
||||
|
||||
// autoMigrateSQLiteToDolt finds the .beads directory and delegates to
|
||||
// doAutoMigrateSQLiteToDolt for the actual migration logic.
|
||||
func autoMigrateSQLiteToDolt() {
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return
|
||||
}
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
}
|
||||
|
||||
// doAutoMigrateSQLiteToDolt detects a legacy SQLite beads.db in the given
|
||||
// .beads directory and automatically migrates it to Dolt. This runs once,
|
||||
// transparently, on the first bd command after upgrading to a Dolt-only CLI.
|
||||
//
|
||||
// The migration is best-effort: failures produce warnings, not fatal errors.
|
||||
// After a successful migration, beads.db is renamed to beads.db.migrated.
|
||||
//
|
||||
// Edge cases handled:
|
||||
// - beads.db is 0 bytes → not a real database, remove it
|
||||
// - metadata.json backend already "dolt" → stale leftover, rename to .migrated
|
||||
// - beads.db.migrated already exists → migration already completed, skip
|
||||
// - beads.db + dolt/ both exist → leftover SQLite, rename it
|
||||
// - Dolt directory already exists → no migration needed
|
||||
// - Corrupted SQLite → warn and skip
|
||||
// - Dolt server not running → warn and skip (retry on next command)
|
||||
func doAutoMigrateSQLiteToDolt(beadsDir string) {
|
||||
// Check for SQLite database
|
||||
sqlitePath := findSQLiteDB(beadsDir)
|
||||
if sqlitePath == "" {
|
||||
return // No SQLite database, nothing to migrate
|
||||
}
|
||||
|
||||
// Skip backup/migrated files
|
||||
base := filepath.Base(sqlitePath)
|
||||
if strings.Contains(base, ".backup") || strings.Contains(base, ".migrated") {
|
||||
return
|
||||
}
|
||||
|
||||
// Guard: if the file is empty (0 bytes), it's not a real SQLite database.
|
||||
// This happens when a process creates beads.db but crashes before writing.
|
||||
// Remove the empty file to prevent repeated failed migration attempts.
|
||||
if info, err := os.Stat(sqlitePath); err == nil && info.Size() == 0 {
|
||||
debug.Logf("auto-migrate-sqlite: removing empty %s (not a valid database)", base)
|
||||
_ = os.Remove(sqlitePath)
|
||||
return
|
||||
}
|
||||
|
||||
// Guard: if metadata.json explicitly indicates SQLite backend, the user has
|
||||
// opted to keep SQLite. Do NOT auto-migrate. Fixes GH#2016.
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
// Use GetBackend() for SQLite check (normalizes case, checks env var)
|
||||
if cfg.GetBackend() == configfile.BackendSQLite {
|
||||
debug.Logf("auto-migrate-sqlite: skipping — backend explicitly set to sqlite")
|
||||
return
|
||||
}
|
||||
// Use raw field for Dolt check: only match when metadata.json was
|
||||
// explicitly written with "dolt" (by a prior migration). Empty/missing
|
||||
// Backend field means legacy pre-Dolt config that needs migration.
|
||||
if strings.EqualFold(cfg.Backend, configfile.BackendDolt) {
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
if err := os.Rename(sqlitePath, migratedPath); err == nil {
|
||||
debug.Logf("auto-migrate-sqlite: renamed stale %s (backend already dolt)", base)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Check if Dolt already exists — if so, SQLite is leftover from a prior migration
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
if _, err := os.Stat(doltPath); err == nil {
|
||||
// Dolt exists alongside SQLite. Rename the leftover SQLite file.
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
// No .migrated file yet — rename now
|
||||
if err := os.Rename(sqlitePath, migratedPath); err == nil {
|
||||
debug.Logf("auto-migrate-sqlite: renamed leftover %s to %s", filepath.Base(sqlitePath), filepath.Base(migratedPath))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Phase 1: Backup — ALWAYS backup before touching anything
|
||||
fmt.Fprintf(os.Stderr, "Backing up SQLite database...\n")
|
||||
backupPath, backupErr := backupSQLite(sqlitePath)
|
||||
if backupErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration aborted (backup failed): %v\n", backupErr)
|
||||
fmt.Fprintf(os.Stderr, "Hint: check disk space and permissions, then retry\n")
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, " Backup saved to %s\n", filepath.Base(backupPath))
|
||||
|
||||
// Extract data from SQLite (read-only)
|
||||
fmt.Fprintf(os.Stderr, "Extracting data from SQLite...\n")
|
||||
data, err := extractFromSQLite(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration failed (extract): %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Hint: run 'bd migrate --to-dolt' manually, or remove %s to skip\n", base)
|
||||
fmt.Fprintf(os.Stderr, " Your backup is at: %s\n", backupPath)
|
||||
return
|
||||
}
|
||||
|
||||
if data.issueCount == 0 {
|
||||
debug.Logf("auto-migrate-sqlite: SQLite database is empty, migrating empty database")
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, " Found %d issues\n", data.issueCount)
|
||||
|
||||
// Determine database name from prefix
|
||||
dbName := "beads"
|
||||
if data.prefix != "" {
|
||||
dbName = data.prefix
|
||||
}
|
||||
|
||||
// Resolve server connection settings once — used for verification and import
|
||||
resolvedPort := doltserver.DefaultConfig(beadsDir).Port
|
||||
resolvedHost := "127.0.0.1"
|
||||
resolvedUser := "root"
|
||||
resolvedPassword := ""
|
||||
resolvedTLS := false
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
resolvedHost = cfg.GetDoltServerHost()
|
||||
if cfg.DoltServerPort > 0 {
|
||||
resolvedPort = cfg.DoltServerPort
|
||||
}
|
||||
resolvedUser = cfg.GetDoltServerUser()
|
||||
resolvedPassword = cfg.GetDoltServerPassword()
|
||||
resolvedTLS = cfg.GetDoltServerTLS()
|
||||
}
|
||||
|
||||
// Run shared migration phases: verify → import → commit → verify data → finalize
|
||||
doltPath = filepath.Join(beadsDir, "dolt")
|
||||
params := &migrationParams{
|
||||
beadsDir: beadsDir,
|
||||
sqlitePath: sqlitePath,
|
||||
backupPath: backupPath,
|
||||
data: data,
|
||||
dbName: dbName,
|
||||
serverHost: resolvedHost,
|
||||
serverPort: resolvedPort,
|
||||
serverUser: resolvedUser,
|
||||
serverPassword: resolvedPassword,
|
||||
doltCfg: &dolt.Config{
|
||||
Path: doltPath,
|
||||
Database: dbName,
|
||||
CreateIfMissing: true, // migration creates a new Dolt database
|
||||
ServerHost: resolvedHost,
|
||||
ServerPort: resolvedPort,
|
||||
ServerUser: resolvedUser,
|
||||
ServerPassword: resolvedPassword,
|
||||
ServerTLS: resolvedTLS,
|
||||
},
|
||||
}
|
||||
|
||||
imported, skipped, migErr := runMigrationPhases(ctx, params)
|
||||
if migErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration failed: %v\n", migErr)
|
||||
return
|
||||
}
|
||||
|
||||
if skipped > 0 {
|
||||
fmt.Fprintf(os.Stderr, "Migrated %d issues from SQLite to Dolt (%d skipped)\n", imported, skipped)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Migrated %d issues from SQLite to Dolt ✓\n", imported)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
//go:build !cgo
|
||||
|
||||
package main
|
||||
|
||||
// autoMigrateSQLiteToDolt uses the sqlite3 CLI shim for non-CGO builds.
|
||||
// This enables automatic SQLite→Dolt migration without requiring the
|
||||
// ncruces/go-sqlite3 CGO driver.
|
||||
func autoMigrateSQLiteToDolt() {
|
||||
shimMigrateSQLiteToDolt()
|
||||
}
|
||||
|
|
@ -1,711 +0,0 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
// createTestSQLiteDB creates a minimal SQLite database with the beads schema
|
||||
// and populates it with the given number of test issues.
|
||||
func createTestSQLiteDB(t *testing.T, dbPath string, prefix string, issueCount int) {
|
||||
t.Helper()
|
||||
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test SQLite DB: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Create minimal schema matching what extractFromSQLite expects
|
||||
for _, stmt := range []string{
|
||||
`CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT)`,
|
||||
`CREATE TABLE IF NOT EXISTS issues (
|
||||
id TEXT PRIMARY KEY,
|
||||
content_hash TEXT DEFAULT '',
|
||||
title TEXT DEFAULT '',
|
||||
description TEXT DEFAULT '',
|
||||
design TEXT DEFAULT '',
|
||||
acceptance_criteria TEXT DEFAULT '',
|
||||
notes TEXT DEFAULT '',
|
||||
status TEXT DEFAULT 'open',
|
||||
priority INTEGER DEFAULT 2,
|
||||
issue_type TEXT DEFAULT 'task',
|
||||
assignee TEXT DEFAULT '',
|
||||
estimated_minutes INTEGER,
|
||||
created_at TEXT DEFAULT '',
|
||||
created_by TEXT DEFAULT '',
|
||||
owner TEXT DEFAULT '',
|
||||
updated_at TEXT DEFAULT '',
|
||||
closed_at TEXT,
|
||||
external_ref TEXT,
|
||||
compaction_level INTEGER DEFAULT 0,
|
||||
compacted_at TEXT DEFAULT '',
|
||||
compacted_at_commit TEXT,
|
||||
original_size INTEGER DEFAULT 0,
|
||||
sender TEXT DEFAULT '',
|
||||
ephemeral INTEGER DEFAULT 0,
|
||||
pinned INTEGER DEFAULT 0,
|
||||
is_template INTEGER DEFAULT 0,
|
||||
crystallizes INTEGER DEFAULT 0,
|
||||
mol_type TEXT DEFAULT '',
|
||||
work_type TEXT DEFAULT '',
|
||||
quality_score REAL,
|
||||
source_system TEXT DEFAULT '',
|
||||
source_repo TEXT DEFAULT '',
|
||||
close_reason TEXT DEFAULT '',
|
||||
event_kind TEXT DEFAULT '',
|
||||
actor TEXT DEFAULT '',
|
||||
target TEXT DEFAULT '',
|
||||
payload TEXT DEFAULT '',
|
||||
await_type TEXT DEFAULT '',
|
||||
await_id TEXT DEFAULT '',
|
||||
timeout_ns INTEGER DEFAULT 0,
|
||||
waiters TEXT DEFAULT '',
|
||||
hook_bead TEXT DEFAULT '',
|
||||
role_bead TEXT DEFAULT '',
|
||||
agent_state TEXT DEFAULT '',
|
||||
last_activity TEXT DEFAULT '',
|
||||
role_type TEXT DEFAULT '',
|
||||
rig TEXT DEFAULT '',
|
||||
due_at TEXT DEFAULT '',
|
||||
defer_until TEXT DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS labels (issue_id TEXT, label TEXT)`,
|
||||
`CREATE TABLE IF NOT EXISTS dependencies (
|
||||
issue_id TEXT, depends_on_id TEXT, type TEXT DEFAULT '',
|
||||
created_by TEXT DEFAULT '', created_at TEXT DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS events (
|
||||
issue_id TEXT, event_type TEXT DEFAULT '', actor TEXT DEFAULT '',
|
||||
old_value TEXT, new_value TEXT, comment TEXT, created_at TEXT DEFAULT ''
|
||||
)`,
|
||||
} {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("failed to create schema: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Set prefix
|
||||
if prefix != "" {
|
||||
if _, err := db.Exec(`INSERT INTO config (key, value) VALUES ('issue_prefix', ?)`, prefix); err != nil {
|
||||
t.Fatalf("failed to set prefix: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Insert test issues
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
for i := 0; i < issueCount; i++ {
|
||||
id := prefix + "-autotest-" + time.Now().Format("150405") + "-" + string(rune('a'+i))
|
||||
_, err := db.Exec(`INSERT INTO issues (id, title, status, priority, issue_type, created_at, updated_at)
|
||||
VALUES (?, ?, 'open', 2, 'task', ?, ?)`,
|
||||
id, "Test issue "+id, now, now)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to insert test issue: %v", err)
|
||||
}
|
||||
// Add a label
|
||||
if _, err := db.Exec(`INSERT INTO labels (issue_id, label) VALUES (?, 'test-label')`, id); err != nil {
|
||||
t.Fatalf("failed to insert label: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_NoBeadsDir(t *testing.T) {
|
||||
// doAutoMigrateSQLiteToDolt should be a no-op for nonexistent dirs
|
||||
doAutoMigrateSQLiteToDolt("/nonexistent/path/.beads")
|
||||
// No panic or error = pass
|
||||
}
|
||||
|
||||
func TestAutoMigrate_NoSQLiteDB(t *testing.T) {
|
||||
// .beads dir exists but has no .db files
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
// Should return without doing anything
|
||||
}
|
||||
|
||||
func TestAutoMigrate_DoltAlreadyExists(t *testing.T) {
|
||||
// .beads has both beads.db and dolt/ — should rename beads.db
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("fake"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// beads.db should be renamed to beads.db.migrated
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed")
|
||||
}
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
t.Errorf("beads.db.migrated should exist: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_DoltExistsWithMigrated(t *testing.T) {
|
||||
// .beads has beads.db, beads.db.migrated, and dolt/ — should not rename again
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("fake"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if err := os.WriteFile(migratedPath, []byte("old"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Both files should still exist (no overwrite)
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("beads.db should still exist (not renamed because .migrated already exists)")
|
||||
}
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
t.Error("beads.db.migrated should still exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_FullMigration(t *testing.T) {
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write metadata.json with server config so migration can connect.
|
||||
// Backend is empty to simulate a legacy pre-Dolt config (the real-world
|
||||
// upgrade scenario). Explicit "sqlite" backend means the user opted to
|
||||
// keep SQLite and migration should be skipped (see GH#2016).
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "", // legacy config — no backend field
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to write test metadata.json: %v", err)
|
||||
}
|
||||
|
||||
// Create SQLite database with test data
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "mig", 3)
|
||||
|
||||
// Run auto-migration
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify: beads.db renamed
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed to .migrated")
|
||||
}
|
||||
if _, err := os.Stat(sqlitePath + ".migrated"); err != nil {
|
||||
t.Errorf("beads.db.migrated should exist: %v", err)
|
||||
}
|
||||
|
||||
// Verify: metadata.json updated
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load updated config: %v", err)
|
||||
}
|
||||
if updatedCfg.Backend != configfile.BackendDolt {
|
||||
t.Errorf("backend should be 'dolt', got %q", updatedCfg.Backend)
|
||||
}
|
||||
if updatedCfg.Database != "dolt" {
|
||||
t.Errorf("database should be 'dolt', got %q", updatedCfg.Database)
|
||||
}
|
||||
if updatedCfg.DoltDatabase != "mig" {
|
||||
t.Errorf("dolt_database should be 'mig', got %q", updatedCfg.DoltDatabase)
|
||||
}
|
||||
|
||||
// Verify: config.yaml has sync.mode
|
||||
configYaml := filepath.Join(beadsDir, "config.yaml")
|
||||
if data, err := os.ReadFile(configYaml); err == nil {
|
||||
if !strings.Contains(string(data), "dolt-native") {
|
||||
t.Error("config.yaml should contain sync.mode = dolt-native")
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up Dolt test database
|
||||
dropTestDatabase("mig", testDoltServerPort)
|
||||
}
|
||||
|
||||
func TestAutoMigrate_CorruptedSQLite(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write a corrupt file as beads.db
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("this is not a sqlite database"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Should warn but not panic
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// beads.db should still exist (not renamed since migration failed)
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("beads.db should still exist after failed migration")
|
||||
}
|
||||
// dolt/ should not exist
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt")); !os.IsNotExist(err) {
|
||||
t.Error("dolt/ should not exist after failed migration")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_ExtractFromSQLite(t *testing.T) {
|
||||
// Test that extractFromSQLite correctly reads test data
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "ext", 5)
|
||||
|
||||
ctx := t.Context()
|
||||
data, err := extractFromSQLite(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractFromSQLite failed: %v", err)
|
||||
}
|
||||
|
||||
if data.prefix != "ext" {
|
||||
t.Errorf("expected prefix 'ext', got %q", data.prefix)
|
||||
}
|
||||
if data.issueCount != 5 {
|
||||
t.Errorf("expected 5 issues, got %d", data.issueCount)
|
||||
}
|
||||
if len(data.issues) != 5 {
|
||||
t.Errorf("expected 5 issues in slice, got %d", len(data.issues))
|
||||
}
|
||||
|
||||
// Verify labels were loaded
|
||||
hasLabels := false
|
||||
for _, issue := range data.issues {
|
||||
if len(issue.Labels) > 0 {
|
||||
hasLabels = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasLabels {
|
||||
t.Error("expected at least one issue to have labels")
|
||||
}
|
||||
|
||||
// Verify config was loaded
|
||||
if data.config["issue_prefix"] != "ext" {
|
||||
t.Errorf("config should contain issue_prefix=ext, got %v", data.config)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_SkipsWhenBackendExplicitlySQLite(t *testing.T) {
|
||||
// When metadata.json explicitly says backend=sqlite, the user has opted to
|
||||
// keep SQLite. Auto-migration must NOT proceed. Fixes GH#2016.
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write metadata.json with explicit sqlite backend
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: configfile.BackendSQLite,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
// Create a real SQLite database with data
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "keep", 5)
|
||||
|
||||
// Run auto-migration — should be skipped
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// beads.db should still exist (NOT renamed)
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("beads.db should still exist when backend=sqlite — migration should be skipped")
|
||||
}
|
||||
|
||||
// No dolt/ directory should be created
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt")); !os.IsNotExist(err) {
|
||||
t.Error("dolt/ should not be created when backend=sqlite")
|
||||
}
|
||||
|
||||
// metadata.json should be unchanged
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load config: %v", err)
|
||||
}
|
||||
if updatedCfg.Backend != configfile.BackendSQLite {
|
||||
t.Errorf("backend should still be 'sqlite', got %q", updatedCfg.Backend)
|
||||
}
|
||||
}
|
||||
|
||||
// createOldSchemaTestSQLiteDB creates a SQLite database with the pre-v0.49
|
||||
// schema that is missing the 'owner' and 'created_by' columns.
|
||||
// Tests that extractFromSQLite handles old schemas gracefully.
|
||||
func createOldSchemaTestSQLiteDB(t *testing.T, dbPath string, prefix string, issueCount int) {
|
||||
t.Helper()
|
||||
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test SQLite DB: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Old schema — missing owner, created_by, and other newer columns
|
||||
for _, stmt := range []string{
|
||||
`CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT)`,
|
||||
`CREATE TABLE IF NOT EXISTS issues (
|
||||
id TEXT PRIMARY KEY,
|
||||
content_hash TEXT DEFAULT '',
|
||||
title TEXT DEFAULT '',
|
||||
description TEXT DEFAULT '',
|
||||
design TEXT DEFAULT '',
|
||||
acceptance_criteria TEXT DEFAULT '',
|
||||
notes TEXT DEFAULT '',
|
||||
status TEXT DEFAULT 'open',
|
||||
priority INTEGER DEFAULT 2,
|
||||
issue_type TEXT DEFAULT 'task',
|
||||
assignee TEXT DEFAULT '',
|
||||
estimated_minutes INTEGER,
|
||||
created_at TEXT DEFAULT '',
|
||||
updated_at TEXT DEFAULT '',
|
||||
closed_at TEXT,
|
||||
external_ref TEXT,
|
||||
compaction_level INTEGER DEFAULT 0,
|
||||
compacted_at TEXT DEFAULT '',
|
||||
compacted_at_commit TEXT,
|
||||
original_size INTEGER DEFAULT 0,
|
||||
sender TEXT DEFAULT '',
|
||||
ephemeral INTEGER DEFAULT 0,
|
||||
pinned INTEGER DEFAULT 0,
|
||||
is_template INTEGER DEFAULT 0,
|
||||
crystallizes INTEGER DEFAULT 0,
|
||||
mol_type TEXT DEFAULT '',
|
||||
work_type TEXT DEFAULT '',
|
||||
quality_score REAL,
|
||||
source_system TEXT DEFAULT '',
|
||||
source_repo TEXT DEFAULT '',
|
||||
close_reason TEXT DEFAULT '',
|
||||
event_kind TEXT DEFAULT '',
|
||||
actor TEXT DEFAULT '',
|
||||
target TEXT DEFAULT '',
|
||||
payload TEXT DEFAULT '',
|
||||
await_type TEXT DEFAULT '',
|
||||
await_id TEXT DEFAULT '',
|
||||
timeout_ns INTEGER DEFAULT 0,
|
||||
waiters TEXT DEFAULT '',
|
||||
hook_bead TEXT DEFAULT '',
|
||||
role_bead TEXT DEFAULT '',
|
||||
agent_state TEXT DEFAULT '',
|
||||
last_activity TEXT DEFAULT '',
|
||||
role_type TEXT DEFAULT '',
|
||||
rig TEXT DEFAULT '',
|
||||
due_at TEXT DEFAULT '',
|
||||
defer_until TEXT DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS labels (issue_id TEXT, label TEXT)`,
|
||||
`CREATE TABLE IF NOT EXISTS dependencies (
|
||||
issue_id TEXT, depends_on_id TEXT, type TEXT DEFAULT '',
|
||||
created_at TEXT DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS events (
|
||||
issue_id TEXT, event_type TEXT DEFAULT '', actor TEXT DEFAULT '',
|
||||
old_value TEXT, new_value TEXT, comment TEXT, created_at TEXT DEFAULT ''
|
||||
)`,
|
||||
} {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("failed to create schema: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if prefix != "" {
|
||||
if _, err := db.Exec(`INSERT INTO config (key, value) VALUES ('issue_prefix', ?)`, prefix); err != nil {
|
||||
t.Fatalf("failed to set prefix: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
for i := 0; i < issueCount; i++ {
|
||||
id := prefix + "-old-" + time.Now().Format("150405") + "-" + string(rune('a'+i))
|
||||
_, err := db.Exec(`INSERT INTO issues (id, title, status, priority, issue_type, created_at, updated_at)
|
||||
VALUES (?, ?, 'open', 2, 'task', ?, ?)`,
|
||||
id, "Old schema issue "+id, now, now)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to insert test issue: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_ExtractFromOldSchemaSQLite(t *testing.T) {
|
||||
// Test that extractFromSQLite handles old schemas missing 'owner',
|
||||
// 'created_by', and other newer columns. Fixes the "no such column"
|
||||
// error that affects pre-v0.49 beads databases.
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createOldSchemaTestSQLiteDB(t, sqlitePath, "old", 3)
|
||||
|
||||
ctx := t.Context()
|
||||
data, err := extractFromSQLite(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractFromSQLite failed on old schema: %v", err)
|
||||
}
|
||||
|
||||
if data.prefix != "old" {
|
||||
t.Errorf("expected prefix 'old', got %q", data.prefix)
|
||||
}
|
||||
if data.issueCount != 3 {
|
||||
t.Errorf("expected 3 issues, got %d", data.issueCount)
|
||||
}
|
||||
// Verify missing columns default to empty strings
|
||||
for _, issue := range data.issues {
|
||||
if issue.Owner != "" {
|
||||
t.Errorf("owner should default to empty for old schema, got %q", issue.Owner)
|
||||
}
|
||||
if issue.CreatedBy != "" {
|
||||
t.Errorf("created_by should default to empty for old schema, got %q", issue.CreatedBy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_FullMigrationOldSchema(t *testing.T) {
|
||||
// Test that full auto-migration works with old-schema SQLite databases.
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createOldSchemaTestSQLiteDB(t, sqlitePath, "oldmig", 4)
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify migration succeeded
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed after successful migration")
|
||||
}
|
||||
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load config: %v", err)
|
||||
}
|
||||
if updatedCfg.Backend != configfile.BackendDolt {
|
||||
t.Errorf("backend should be 'dolt', got %q", updatedCfg.Backend)
|
||||
}
|
||||
|
||||
dropTestDatabase("oldmig", testDoltServerPort)
|
||||
}
|
||||
|
||||
func TestAutoMigrate_Idempotent(t *testing.T) {
|
||||
// Calling doAutoMigrateSQLiteToDolt twice should be safe
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// No SQLite DB — should be no-op both times
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
}
|
||||
|
||||
func TestAutoMigrate_SkipsWhenBackendAlreadyDolt(t *testing.T) {
|
||||
// When metadata.json already says backend=dolt (dolt-server mode, no local dolt/ dir),
|
||||
// a stale beads.db should be cleaned up without attempting SQLite extraction.
|
||||
// This is the exact scenario from the gastown rig: dolt-server mode stores data
|
||||
// remotely, so there's no local dolt/ directory. The migration code was incorrectly
|
||||
// trying to extract from the (empty) beads.db because it only checked for dolt/ dir,
|
||||
// not the configured backend.
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write metadata.json indicating dolt backend with server mode (no local dolt/ dir)
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendDolt,
|
||||
Database: "dolt",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltDatabase: "gastown",
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: 3307,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
// Create a stale beads.db with some content (non-zero, simulates a leftover
|
||||
// SQLite file that's not empty but is not a valid database either)
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("not-a-real-database"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Run auto-migration — should NOT attempt SQLite extraction
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// beads.db should be renamed to .migrated (cleaned up)
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed to .migrated when backend is already dolt")
|
||||
}
|
||||
if _, err := os.Stat(sqlitePath + ".migrated"); err != nil {
|
||||
t.Errorf("beads.db.migrated should exist: %v", err)
|
||||
}
|
||||
|
||||
// metadata.json should be UNCHANGED (not overwritten by migration)
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reload config: %v", err)
|
||||
}
|
||||
if updatedCfg.DoltDatabase != "gastown" {
|
||||
t.Errorf("dolt_database should still be 'gastown', got %q (migration overwrote config)", updatedCfg.DoltDatabase)
|
||||
}
|
||||
if updatedCfg.DoltMode != configfile.DoltModeServer {
|
||||
t.Errorf("dolt_mode should still be 'server', got %q", updatedCfg.DoltMode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_SkipsWhenBackendDoltEmbedded(t *testing.T) {
|
||||
// Same as above but for embedded mode where dolt/ dir DOES exist.
|
||||
// Even with a stale beads.db, if metadata says dolt, skip extraction.
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Backend: configfile.BackendDolt,
|
||||
Database: "dolt",
|
||||
DoltMode: configfile.DoltModeEmbedded,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("fake-sqlite-data"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// beads.db should be renamed (the existing dolt/ check handles this,
|
||||
// but the metadata check should also catch it independently)
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrate_ZeroByteBeadsDB(t *testing.T) {
|
||||
// A 0-byte beads.db is not a valid SQLite database. The migration should
|
||||
// clean it up without attempting extraction (which would fail with
|
||||
// "no such table: issues" and print a confusing warning).
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// No metadata.json, no dolt/ — just a bare 0-byte beads.db
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte{}, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// 0-byte file should be removed (not a real database)
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("0-byte beads.db should have been removed")
|
||||
}
|
||||
// Should NOT create a dolt/ directory
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt")); !os.IsNotExist(err) {
|
||||
t.Error("dolt/ should not be created for a 0-byte SQLite file")
|
||||
}
|
||||
}
|
||||
|
||||
// Verify the migrated metadata.json is valid JSON
|
||||
func TestAutoMigrate_MetadataJSONValid(t *testing.T) {
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write initial metadata.json (empty backend = legacy pre-Dolt config)
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "", // legacy config — triggers auto-migration
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "json", 1)
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Read and parse metadata.json
|
||||
data, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read metadata.json: %v", err)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
t.Errorf("metadata.json is not valid JSON: %v\nContent: %s", err, string(data))
|
||||
}
|
||||
|
||||
// Clean up
|
||||
dropTestDatabase("json", testDoltServerPort)
|
||||
}
|
||||
|
|
@ -1,685 +0,0 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
"github.com/steveyegge/beads/internal/ui"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
// handleToDoltMigration migrates from SQLite to Dolt backend.
|
||||
// 1. Finds SQLite .db files in .beads/
|
||||
// 2. Creates Dolt database in `.beads/dolt/`
|
||||
// 3. Imports all issues, labels, dependencies, events
|
||||
// 4. Copies all config values
|
||||
// 5. Updates `metadata.json` to use Dolt
|
||||
func handleToDoltMigration(dryRun bool, autoYes bool) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Find .beads directory
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
exitWithError("no_beads_directory", "No .beads directory found. Run 'bd init' first.",
|
||||
"run 'bd init' to initialize bd")
|
||||
}
|
||||
|
||||
// Find SQLite database by scanning for .db files
|
||||
sqlitePath := findSQLiteDB(beadsDir)
|
||||
if sqlitePath == "" {
|
||||
exitWithError("no_sqlite_database", "No SQLite database found to migrate",
|
||||
"no .db files found in "+beadsDir)
|
||||
}
|
||||
|
||||
// Dolt path
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
|
||||
// Track whether the dolt directory pre-existed so cleanup doesn't
|
||||
// destroy data from a prior migration or server start.
|
||||
doltDirExisted := false
|
||||
if _, err := os.Stat(doltPath); err == nil {
|
||||
doltDirExisted = true
|
||||
}
|
||||
|
||||
// Extract all data from SQLite
|
||||
data, err := extractFromSQLite(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
exitWithError("extraction_failed", err.Error(), "")
|
||||
}
|
||||
|
||||
// Show migration plan
|
||||
printMigrationPlan("SQLite to Dolt", sqlitePath, doltPath, data)
|
||||
|
||||
// Dry run mode
|
||||
if dryRun {
|
||||
printDryRun(sqlitePath, doltPath, data, true)
|
||||
return
|
||||
}
|
||||
|
||||
// Prompt for confirmation
|
||||
if !autoYes && !jsonOutput {
|
||||
if !confirmBackendMigration("SQLite", "Dolt", true) {
|
||||
fmt.Println("Migration canceled")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Create backup
|
||||
backupPath := strings.TrimSuffix(sqlitePath, ".db") + ".backup-pre-dolt-" + time.Now().Format("20060102-150405") + ".db"
|
||||
if err := copyFile(sqlitePath, backupPath); err != nil {
|
||||
exitWithError("backup_failed", err.Error(), "")
|
||||
}
|
||||
printSuccess(fmt.Sprintf("Created backup: %s", filepath.Base(backupPath)))
|
||||
|
||||
// Create Dolt database
|
||||
printProgress("Creating Dolt database...")
|
||||
|
||||
// Respect existing config's database name to avoid creating phantom catalog
|
||||
// entries when a user has renamed their database (GH#2051).
|
||||
dbName := ""
|
||||
if existingCfg, _ := configfile.Load(beadsDir); existingCfg != nil && existingCfg.DoltDatabase != "" {
|
||||
dbName = existingCfg.DoltDatabase
|
||||
} else if data.prefix != "" {
|
||||
dbName = data.prefix
|
||||
} else {
|
||||
dbName = "beads"
|
||||
}
|
||||
doltStore, err := dolt.New(ctx, &dolt.Config{
|
||||
Path: doltPath,
|
||||
Database: dbName,
|
||||
CreateIfMissing: true, // migration creates a new Dolt database
|
||||
AutoStart: !doltserver.IsDaemonManaged() && os.Getenv("BEADS_DOLT_AUTO_START") != "0",
|
||||
})
|
||||
if err != nil {
|
||||
exitWithError("dolt_create_failed", err.Error(), "")
|
||||
}
|
||||
|
||||
// Import data with cleanup on failure
|
||||
imported, skipped, importErr := importToDolt(ctx, doltStore, data)
|
||||
if importErr != nil {
|
||||
_ = doltStore.Close()
|
||||
if !doltDirExisted {
|
||||
_ = os.RemoveAll(doltPath)
|
||||
}
|
||||
hint := "import failed"
|
||||
if !doltDirExisted {
|
||||
hint = "partial Dolt directory has been cleaned up"
|
||||
}
|
||||
exitWithError("import_failed", importErr.Error(), hint)
|
||||
}
|
||||
|
||||
// Set sync.mode to dolt-native in the DB.
|
||||
if err := doltStore.SetConfig(ctx, "sync.mode", "dolt-native"); err != nil {
|
||||
printWarning(fmt.Sprintf("failed to set sync.mode in DB: %v", err))
|
||||
} else {
|
||||
printSuccess("Set sync.mode = dolt-native in database")
|
||||
}
|
||||
|
||||
// Commit the migration
|
||||
commitMsg := fmt.Sprintf("Migrate from SQLite: %d issues imported", imported)
|
||||
if err := doltStore.Commit(ctx, commitMsg); err != nil {
|
||||
printWarning(fmt.Sprintf("failed to create Dolt commit: %v", err))
|
||||
}
|
||||
|
||||
_ = doltStore.Close()
|
||||
|
||||
printSuccess(fmt.Sprintf("Imported %d issues (%d skipped)", imported, skipped))
|
||||
|
||||
// Load and update metadata.json
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
cfg.Backend = configfile.BackendDolt
|
||||
cfg.Database = "dolt"
|
||||
cfg.DoltDatabase = dbName
|
||||
// Don't set DoltServerPort — let doltserver.DefaultConfig derive it from
|
||||
// the project path at runtime. Writing an explicit port here would freeze
|
||||
// a value that conflicts with the hash-based port in doltserver.DerivePort.
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
exitWithError("config_save_failed", err.Error(),
|
||||
"data was imported but metadata.json was not updated - manually set backend to 'dolt'")
|
||||
}
|
||||
|
||||
printSuccess("Updated metadata.json to use Dolt backend")
|
||||
|
||||
// Write sync.mode to config.yaml so viper-based code reads the correct mode.
|
||||
// The DB config table was already updated above; this fixes the split-brain
|
||||
// where config.yaml still says "git-portable" (GH #1723, #1794).
|
||||
if err := config.SaveConfigValue("sync.mode", string(config.SyncModeDoltNative), beadsDir); err != nil {
|
||||
printWarning(fmt.Sprintf("failed to write sync.mode to config.yaml: %v (set manually: sync.mode: dolt-native)", err))
|
||||
} else {
|
||||
printSuccess("Set sync.mode = dolt-native in config.yaml")
|
||||
}
|
||||
|
||||
// Check if git hooks need updating for Dolt compatibility
|
||||
if hooksNeedDoltUpdate(beadsDir) {
|
||||
printWarning("Git hooks need updating for Dolt backend")
|
||||
if !jsonOutput {
|
||||
fmt.Println(" The pre-commit and post-merge hooks use JSONL sync which doesn't apply to Dolt.")
|
||||
fmt.Println(" Run 'bd hooks install --force' to update them.")
|
||||
}
|
||||
}
|
||||
|
||||
// Final status
|
||||
printFinalStatus("dolt", imported, skipped, backupPath, doltPath, sqlitePath, true)
|
||||
}
|
||||
|
||||
// hooksNeedDoltUpdate checks if installed git hooks lack the Dolt backend skip logic.
|
||||
func hooksNeedDoltUpdate(beadsDir string) bool {
|
||||
repoRoot := filepath.Dir(beadsDir)
|
||||
hooksDir := filepath.Join(repoRoot, ".git", "hooks")
|
||||
|
||||
postMergePath := filepath.Join(hooksDir, "post-merge")
|
||||
// #nosec G304 -- postMergePath is derived from the local repo's .git/hooks directory.
|
||||
content, err := os.ReadFile(postMergePath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
if strings.Contains(contentStr, "bd-shim") {
|
||||
return false
|
||||
}
|
||||
if !strings.Contains(contentStr, "bd") {
|
||||
return false
|
||||
}
|
||||
if strings.Contains(contentStr, `"backend"`) && strings.Contains(contentStr, `"dolt"`) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// extractFromSQLite extracts all data from a SQLite database using raw SQL.
|
||||
// This is the CGO path — it reads SQLite directly via the ncruces/go-sqlite3 driver.
|
||||
// For non-CGO builds, see migrate_shim.go which uses the sqlite3 CLI instead.
|
||||
func extractFromSQLite(ctx context.Context, dbPath string) (*migrationData, error) {
|
||||
// Escape '#' in the path to prevent URI fragment truncation (GH#2115).
|
||||
escapedPath := strings.ReplaceAll(dbPath, "#", "%23")
|
||||
db, err := sql.Open("sqlite3", "file:"+escapedPath+"?mode=ro")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open SQLite database: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Get prefix from config table
|
||||
prefix := ""
|
||||
_ = db.QueryRowContext(ctx, "SELECT value FROM config WHERE key = 'issue_prefix'").Scan(&prefix)
|
||||
|
||||
// Get all config
|
||||
config := make(map[string]string)
|
||||
configRows, err := db.QueryContext(ctx, "SELECT key, value FROM config")
|
||||
if err == nil {
|
||||
defer configRows.Close()
|
||||
for configRows.Next() {
|
||||
var k, v string
|
||||
if err := configRows.Scan(&k, &v); err == nil {
|
||||
config[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issueCols, _ := sqliteTableColumns(ctx, db, "issues")
|
||||
depCols, _ := sqliteTableColumns(ctx, db, "dependencies")
|
||||
|
||||
// All columns that may be missing in older beads database schemas.
|
||||
// Uses sqliteOptionalTextExpr which checks PRAGMA table_info before querying.
|
||||
// Fixes "no such column" errors for pre-v0.49 databases (GH#2016).
|
||||
opt := func(col, fallback string) string {
|
||||
return sqliteOptionalTextExpr(issueCols, col, fallback)
|
||||
}
|
||||
|
||||
// Get all issues
|
||||
//nolint:gosec // SQL fragments come from fixed column names discovered via PRAGMA table_info.
|
||||
// Build query with ALL non-id columns optional to handle any schema version.
|
||||
// Core columns (id, title, status, priority, created_at, updated_at) are
|
||||
// assumed present in all versions. Everything else uses opt() to gracefully
|
||||
// handle missing columns in older databases.
|
||||
issueQuery := fmt.Sprintf(`
|
||||
SELECT id, %s, %s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s, %s, %s,
|
||||
%s, %s, %s,
|
||||
%s,
|
||||
%s, %s, %s, %s,
|
||||
%s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s, %s, %s,
|
||||
%s, %s, %s, %s,
|
||||
%s, %s, %s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s, %s,
|
||||
%s, %s, %s
|
||||
FROM issues
|
||||
ORDER BY created_at, id
|
||||
`,
|
||||
opt("content_hash", "''"), opt("title", "''"), opt("description", "''"),
|
||||
opt("design", "''"), opt("acceptance_criteria", "''"), opt("notes", "''"),
|
||||
opt("status", "''"), opt("priority", "0"), opt("issue_type", "''"),
|
||||
opt("assignee", "''"), opt("estimated_minutes", "NULL"),
|
||||
opt("created_at", "''"), opt("created_by", "''"), opt("owner", "''"),
|
||||
opt("updated_at", "''"), opt("closed_at", "NULL"), opt("external_ref", "NULL"), opt("spec_id", "''"),
|
||||
opt("compaction_level", "0"), opt("compacted_at", "''"), opt("compacted_at_commit", "NULL"),
|
||||
opt("original_size", "0"),
|
||||
opt("sender", "''"), opt("ephemeral", "0"), opt("wisp_type", "''"), opt("pinned", "0"),
|
||||
opt("is_template", "0"), opt("crystallizes", "0"),
|
||||
opt("mol_type", "''"), opt("work_type", "''"), opt("quality_score", "NULL"),
|
||||
opt("source_system", "''"), opt("source_repo", "''"), opt("close_reason", "''"), opt("closed_by_session", "''"),
|
||||
opt("event_kind", "''"), opt("actor", "''"), opt("target", "''"), opt("payload", "''"),
|
||||
opt("await_type", "''"), opt("await_id", "''"), opt("timeout_ns", "0"), opt("waiters", "''"),
|
||||
opt("hook_bead", "''"), opt("role_bead", "''"), opt("agent_state", "''"),
|
||||
opt("last_activity", "''"), opt("role_type", "''"), opt("rig", "''"),
|
||||
opt("due_at", "''"), opt("defer_until", "''"), opt("metadata", "'{}'"),
|
||||
)
|
||||
issueRows, err := db.QueryContext(ctx, issueQuery)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query issues: %w", err)
|
||||
}
|
||||
defer issueRows.Close()
|
||||
|
||||
var issues []*types.Issue
|
||||
for issueRows.Next() {
|
||||
var issue types.Issue
|
||||
var estMin sql.NullInt64
|
||||
var extRef, compactCommit sql.NullString
|
||||
var qualScore sql.NullFloat64
|
||||
var timeoutNs int64
|
||||
var waitersJSON string
|
||||
var closedAt, compactedAt, lastActivity, dueAt, deferUntil sql.NullString
|
||||
var specID, wispType, closedBySession, metadataRaw sql.NullString
|
||||
var createdByVal, ownerVal sql.NullString
|
||||
if err := issueRows.Scan(
|
||||
&issue.ID, &issue.ContentHash, &issue.Title, &issue.Description,
|
||||
&issue.Design, &issue.AcceptanceCriteria, &issue.Notes,
|
||||
&issue.Status, &issue.Priority, &issue.IssueType,
|
||||
&issue.Assignee, &estMin,
|
||||
&issue.CreatedAt, &createdByVal, &ownerVal,
|
||||
&issue.UpdatedAt, &closedAt, &extRef, &specID,
|
||||
&issue.CompactionLevel, &compactedAt, &compactCommit,
|
||||
&issue.OriginalSize,
|
||||
&issue.Sender, &issue.Ephemeral, &wispType, &issue.Pinned,
|
||||
&issue.IsTemplate, &issue.Crystallizes,
|
||||
&issue.MolType, &issue.WorkType, &qualScore,
|
||||
&issue.SourceSystem, &issue.SourceRepo, &issue.CloseReason, &closedBySession,
|
||||
&issue.EventKind, &issue.Actor, &issue.Target, &issue.Payload,
|
||||
&issue.AwaitType, &issue.AwaitID, &timeoutNs, &waitersJSON,
|
||||
&issue.HookBead, &issue.RoleBead, &issue.AgentState,
|
||||
&lastActivity, &issue.RoleType, &issue.Rig,
|
||||
&dueAt, &deferUntil, &metadataRaw,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan issue: %w", err)
|
||||
}
|
||||
if createdByVal.Valid {
|
||||
issue.CreatedBy = createdByVal.String
|
||||
}
|
||||
if ownerVal.Valid {
|
||||
issue.Owner = ownerVal.String
|
||||
}
|
||||
if estMin.Valid {
|
||||
v := int(estMin.Int64)
|
||||
issue.EstimatedMinutes = &v
|
||||
}
|
||||
if extRef.Valid {
|
||||
issue.ExternalRef = &extRef.String
|
||||
}
|
||||
if specID.Valid {
|
||||
issue.SpecID = specID.String
|
||||
}
|
||||
if compactCommit.Valid {
|
||||
issue.CompactedAtCommit = &compactCommit.String
|
||||
}
|
||||
if wispType.Valid {
|
||||
issue.WispType = types.WispType(wispType.String)
|
||||
}
|
||||
if closedBySession.Valid {
|
||||
issue.ClosedBySession = closedBySession.String
|
||||
}
|
||||
if qualScore.Valid {
|
||||
v := float32(qualScore.Float64)
|
||||
issue.QualityScore = &v
|
||||
}
|
||||
if metadataRaw.Valid && strings.TrimSpace(metadataRaw.String) != "" {
|
||||
issue.Metadata = normalizedJSONBytes(metadataRaw.String)
|
||||
}
|
||||
issue.ClosedAt = parseNullTime(closedAt.String)
|
||||
issue.CompactedAt = parseNullTime(compactedAt.String)
|
||||
issue.LastActivity = parseNullTime(lastActivity.String)
|
||||
issue.DueAt = parseNullTime(dueAt.String)
|
||||
issue.DeferUntil = parseNullTime(deferUntil.String)
|
||||
issue.Timeout = time.Duration(timeoutNs)
|
||||
if waitersJSON != "" {
|
||||
_ = json.Unmarshal([]byte(waitersJSON), &issue.Waiters)
|
||||
}
|
||||
issues = append(issues, &issue)
|
||||
}
|
||||
|
||||
// Get labels
|
||||
labelsMap := make(map[string][]string)
|
||||
labelRows, err := db.QueryContext(ctx, "SELECT issue_id, label FROM labels ORDER BY issue_id, label")
|
||||
if err == nil {
|
||||
defer labelRows.Close()
|
||||
for labelRows.Next() {
|
||||
var issueID, label string
|
||||
if err := labelRows.Scan(&issueID, &label); err == nil {
|
||||
labelsMap[issueID] = append(labelsMap[issueID], label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get dependencies
|
||||
depsMap := make(map[string][]*types.Dependency)
|
||||
depOpt := func(col, fallback string) string {
|
||||
return sqliteOptionalTextExpr(depCols, col, fallback)
|
||||
}
|
||||
//nolint:gosec // SQL fragments come from fixed column names discovered via PRAGMA table_info.
|
||||
depQuery := fmt.Sprintf(`
|
||||
SELECT issue_id, depends_on_id, COALESCE(type,''), %s, COALESCE(created_at,''), %s, %s
|
||||
FROM dependencies
|
||||
ORDER BY created_at, issue_id, depends_on_id
|
||||
`, depOpt("created_by", "''"), depOpt("metadata", "'{}'"), depOpt("thread_id", "''"))
|
||||
depRows, err := db.QueryContext(ctx, depQuery)
|
||||
if err == nil {
|
||||
defer depRows.Close()
|
||||
for depRows.Next() {
|
||||
var dep types.Dependency
|
||||
var metadata, threadID, depCreatedBy sql.NullString
|
||||
if err := depRows.Scan(&dep.IssueID, &dep.DependsOnID, &dep.Type, &depCreatedBy, &dep.CreatedAt, &metadata, &threadID); err == nil {
|
||||
if depCreatedBy.Valid {
|
||||
dep.CreatedBy = depCreatedBy.String
|
||||
}
|
||||
if metadata.Valid {
|
||||
dep.Metadata = strings.TrimSpace(metadata.String)
|
||||
}
|
||||
if dep.Metadata == "" {
|
||||
dep.Metadata = "{}"
|
||||
}
|
||||
if threadID.Valid {
|
||||
dep.ThreadID = threadID.String
|
||||
}
|
||||
depsMap[dep.IssueID] = append(depsMap[dep.IssueID], &dep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get events
|
||||
eventsMap := make(map[string][]*types.Event)
|
||||
eventRows, err := db.QueryContext(ctx, `
|
||||
SELECT issue_id, COALESCE(event_type,''), COALESCE(actor,''), old_value, new_value, comment, COALESCE(created_at,'')
|
||||
FROM events
|
||||
ORDER BY created_at, rowid
|
||||
`)
|
||||
if err == nil {
|
||||
defer eventRows.Close()
|
||||
for eventRows.Next() {
|
||||
var issueID string
|
||||
var event types.Event
|
||||
var oldVal, newVal, comment sql.NullString
|
||||
if err := eventRows.Scan(&issueID, &event.EventType, &event.Actor, &oldVal, &newVal, &comment, &event.CreatedAt); err == nil {
|
||||
if oldVal.Valid {
|
||||
event.OldValue = &oldVal.String
|
||||
}
|
||||
if newVal.Valid {
|
||||
event.NewValue = &newVal.String
|
||||
}
|
||||
if comment.Valid {
|
||||
event.Comment = &comment.String
|
||||
}
|
||||
eventsMap[issueID] = append(eventsMap[issueID], &event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get comments (legacy table may be absent; ignore errors).
|
||||
commentsMap := make(map[string][]*types.Comment)
|
||||
commentRows, err := db.QueryContext(ctx, `
|
||||
SELECT issue_id, COALESCE(author,''), COALESCE(text,''), COALESCE(created_at,'')
|
||||
FROM comments
|
||||
ORDER BY created_at, rowid
|
||||
`)
|
||||
if err == nil {
|
||||
defer commentRows.Close()
|
||||
for commentRows.Next() {
|
||||
var issueID, author, text, createdAt string
|
||||
if err := commentRows.Scan(&issueID, &author, &text, &createdAt); err == nil {
|
||||
commentsMap[issueID] = append(commentsMap[issueID], &types.Comment{
|
||||
IssueID: issueID,
|
||||
Author: author,
|
||||
Text: text,
|
||||
CreatedAt: parseNullableSQLiteTime(createdAt),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assign labels and dependencies to issues
|
||||
for _, issue := range issues {
|
||||
if labels, ok := labelsMap[issue.ID]; ok {
|
||||
issue.Labels = labels
|
||||
}
|
||||
if deps, ok := depsMap[issue.ID]; ok {
|
||||
issue.Dependencies = deps
|
||||
}
|
||||
}
|
||||
|
||||
return &migrationData{
|
||||
issues: issues,
|
||||
labelsMap: labelsMap,
|
||||
depsMap: depsMap,
|
||||
eventsMap: eventsMap,
|
||||
commentsMap: commentsMap,
|
||||
config: config,
|
||||
prefix: prefix,
|
||||
issueCount: len(issues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sqliteTableColumns(ctx context.Context, db *sql.DB, table string) (map[string]bool, error) {
|
||||
rows, err := db.QueryContext(ctx, fmt.Sprintf("PRAGMA table_info(%s)", table))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
cols := make(map[string]bool)
|
||||
for rows.Next() {
|
||||
var cid int
|
||||
var name, colType string
|
||||
var notNull, pk int
|
||||
var dflt sql.NullString
|
||||
if err := rows.Scan(&cid, &name, &colType, ¬Null, &dflt, &pk); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cols[name] = true
|
||||
}
|
||||
return cols, nil
|
||||
}
|
||||
|
||||
func parseNullableSQLiteTime(raw string) time.Time {
|
||||
if t := parseNullTime(raw); t != nil {
|
||||
return *t
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// Helper functions for output (CGO build only — used by handleToDoltMigration)
|
||||
|
||||
func exitWithError(code, message, hint string) {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": code,
|
||||
"message": message,
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: %s\n", message)
|
||||
if hint != "" {
|
||||
fmt.Fprintf(os.Stderr, "Hint: %s\n", hint)
|
||||
}
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func printSuccess(message string) {
|
||||
if !jsonOutput {
|
||||
fmt.Printf("%s\n", ui.RenderPass("✓ "+message))
|
||||
}
|
||||
}
|
||||
|
||||
func printWarning(message string) {
|
||||
if !jsonOutput {
|
||||
fmt.Printf("%s\n", ui.RenderWarn("Warning: "+message))
|
||||
}
|
||||
}
|
||||
|
||||
func printProgress(message string) {
|
||||
if !jsonOutput {
|
||||
fmt.Printf("%s\n", message)
|
||||
}
|
||||
}
|
||||
|
||||
func printMigrationPlan(title, source, target string, data *migrationData) {
|
||||
if jsonOutput {
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s Migration\n", title)
|
||||
fmt.Printf("%s\n\n", strings.Repeat("=", len(title)+10))
|
||||
fmt.Printf("Source: %s\n", source)
|
||||
fmt.Printf("Target: %s\n", target)
|
||||
fmt.Printf("Issues to migrate: %d\n", data.issueCount)
|
||||
|
||||
eventCount, commentCount := migrationRecordCounts(data)
|
||||
fmt.Printf("Events to migrate: %d\n", eventCount)
|
||||
fmt.Printf("Comments to migrate: %d\n", commentCount)
|
||||
fmt.Printf("Config keys: %d\n", len(data.config))
|
||||
|
||||
if data.prefix != "" {
|
||||
fmt.Printf("Issue prefix: %s\n", data.prefix)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func printDryRun(source, target string, data *migrationData, withBackup bool) {
|
||||
eventCount, commentCount := migrationRecordCounts(data)
|
||||
|
||||
if jsonOutput {
|
||||
result := map[string]interface{}{
|
||||
"dry_run": true,
|
||||
"source": source,
|
||||
"target": target,
|
||||
"issue_count": data.issueCount,
|
||||
"event_count": eventCount,
|
||||
"comment_count": commentCount,
|
||||
"config_keys": len(data.config),
|
||||
"prefix": data.prefix,
|
||||
"would_backup": withBackup,
|
||||
}
|
||||
outputJSON(result)
|
||||
} else {
|
||||
fmt.Println("Dry run mode - no changes will be made")
|
||||
fmt.Println("Would perform:")
|
||||
step := 1
|
||||
if withBackup {
|
||||
fmt.Printf(" %d. Create backup of source database\n", step)
|
||||
step++
|
||||
}
|
||||
fmt.Printf(" %d. Create target database at %s\n", step, target)
|
||||
step++
|
||||
fmt.Printf(" %d. Import %d issues with labels and dependencies\n", step, data.issueCount)
|
||||
step++
|
||||
fmt.Printf(" %d. Import %d events (issue history)\n", step, eventCount)
|
||||
step++
|
||||
fmt.Printf(" %d. Import %d comments (legacy comments table)\n", step, commentCount)
|
||||
step++
|
||||
fmt.Printf(" %d. Copy %d config values\n", step, len(data.config))
|
||||
step++
|
||||
fmt.Printf(" %d. Update metadata.json\n", step)
|
||||
}
|
||||
}
|
||||
|
||||
func migrationRecordCounts(data *migrationData) (eventCount int, commentCount int) {
|
||||
for _, events := range data.eventsMap {
|
||||
eventCount += len(events)
|
||||
}
|
||||
for _, comments := range data.commentsMap {
|
||||
commentCount += len(comments)
|
||||
}
|
||||
return eventCount, commentCount
|
||||
}
|
||||
|
||||
func confirmBackendMigration(from, to string, withBackup bool) bool {
|
||||
fmt.Printf("This will:\n")
|
||||
step := 1
|
||||
if withBackup {
|
||||
fmt.Printf(" %d. Create a backup of your %s database\n", step, from)
|
||||
step++
|
||||
}
|
||||
fmt.Printf(" %d. Create a %s database and import all data\n", step, to)
|
||||
step++
|
||||
fmt.Printf(" %d. Update metadata.json to use %s backend\n", step, to)
|
||||
step++
|
||||
fmt.Printf(" %d. Keep your %s database (can be deleted after verification)\n\n", step, from)
|
||||
fmt.Printf("Continue? [y/N] ")
|
||||
var response string
|
||||
_, _ = fmt.Scanln(&response)
|
||||
return strings.ToLower(response) == "y" || strings.ToLower(response) == "yes"
|
||||
}
|
||||
|
||||
func printFinalStatus(backend string, imported, skipped int, backupPath, newPath, oldPath string, toDolt bool) {
|
||||
if jsonOutput {
|
||||
result := map[string]interface{}{
|
||||
"status": "success",
|
||||
"backend": backend,
|
||||
"issues_imported": imported,
|
||||
"issues_skipped": skipped,
|
||||
}
|
||||
if backupPath != "" {
|
||||
result["backup_path"] = backupPath
|
||||
}
|
||||
if toDolt {
|
||||
result["dolt_path"] = newPath
|
||||
} else {
|
||||
result["sqlite_path"] = newPath
|
||||
}
|
||||
outputJSON(result)
|
||||
} else {
|
||||
fmt.Println()
|
||||
fmt.Printf("%s\n", ui.RenderPass("✓ Migration complete!"))
|
||||
fmt.Println()
|
||||
fmt.Printf("Your beads now use %s storage.\n", strings.ToUpper(backend))
|
||||
if backupPath != "" {
|
||||
fmt.Printf("Backup: %s\n", backupPath)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("Next steps:")
|
||||
fmt.Println(" - Verify data: bd list")
|
||||
fmt.Println(" - After verification, you can delete the old database:")
|
||||
fmt.Printf(" rm %s\n", oldPath)
|
||||
}
|
||||
}
|
||||
|
||||
// listMigrations returns registered Dolt migrations (CGO build).
|
||||
func listMigrations() []string {
|
||||
return dolt.ListMigrations()
|
||||
}
|
||||
|
|
@ -1,284 +0,0 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/git"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
)
|
||||
|
||||
// setupDoltMigrateWorkspace creates a temp beads workspace with a Dolt database
|
||||
// and a git repo configured with a remote (needed for ComputeRepoID/GetCloneID).
|
||||
// Returns (workspace root path, beadsDir, config).
|
||||
func setupDoltMigrateWorkspace(t *testing.T) (string, string, *configfile.Config) {
|
||||
t.Helper()
|
||||
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads directory: %v", err)
|
||||
}
|
||||
|
||||
// Copy cached git template (bd-ktng optimization)
|
||||
initGitTemplate()
|
||||
if gitTemplateErr != nil {
|
||||
t.Fatalf("git template init failed: %v", gitTemplateErr)
|
||||
}
|
||||
if err := copyGitDir(gitTemplateDir, dir); err != nil {
|
||||
t.Fatalf("failed to copy git template: %v", err)
|
||||
}
|
||||
cmd := exec.Command("git", "config", "remote.origin.url", "https://github.com/test/migrate-metadata.git")
|
||||
cmd.Dir = dir
|
||||
_ = cmd.Run()
|
||||
|
||||
// Create Dolt config
|
||||
cfg := &configfile.Config{
|
||||
Database: "dolt",
|
||||
Backend: configfile.BackendDolt,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to save config: %v", err)
|
||||
}
|
||||
|
||||
// Create the Dolt store via factory (bootstraps the database with schema)
|
||||
ctx := context.Background()
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
// Create local marker directory (server mode doesn't create it automatically)
|
||||
if err := os.MkdirAll(doltPath, 0750); err != nil {
|
||||
t.Fatalf("failed to create dolt dir: %v", err)
|
||||
}
|
||||
store, err := dolt.New(ctx, &dolt.Config{
|
||||
Path: doltPath,
|
||||
Database: "beads",
|
||||
})
|
||||
if err != nil {
|
||||
t.Skipf("skipping: Dolt server not available: %v", err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatalf("failed to close Dolt store: %v", err)
|
||||
}
|
||||
|
||||
return dir, beadsDir, cfg
|
||||
}
|
||||
|
||||
// saveMigrateGlobals saves package-level globals that handleDoltMetadataUpdate uses
|
||||
// and returns a cleanup function to restore them. Must be deferred by caller.
|
||||
func saveMigrateGlobals(t *testing.T) func() {
|
||||
t.Helper()
|
||||
|
||||
oldRootCtx := rootCtx
|
||||
oldJsonOutput := jsonOutput
|
||||
|
||||
return func() {
|
||||
rootCtx = oldRootCtx
|
||||
jsonOutput = oldJsonOutput
|
||||
beads.ResetCaches()
|
||||
git.ResetCaches()
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltFullMetadata verifies that handleDoltMetadataUpdate writes
|
||||
// all 3 metadata fields (bd_version, repo_id, clone_id) to a Dolt database
|
||||
// that has none.
|
||||
func TestMigrateDoltFullMetadata(t *testing.T) {
|
||||
cleanup := saveMigrateGlobals(t)
|
||||
defer cleanup()
|
||||
|
||||
dir, beadsDir, cfg := setupDoltMigrateWorkspace(t)
|
||||
|
||||
// Set globals for handleDoltMetadataUpdate
|
||||
rootCtx = context.Background()
|
||||
jsonOutput = false
|
||||
|
||||
// Change to workspace dir so ComputeRepoID/GetCloneID can find the git repo
|
||||
oldWD, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
defer func() { _ = os.Chdir(oldWD) }()
|
||||
|
||||
beads.ResetCaches()
|
||||
git.ResetCaches()
|
||||
|
||||
// Run the function under test
|
||||
handleDoltMetadataUpdate(cfg, beadsDir, false)
|
||||
|
||||
// Verify all 3 metadata fields were written
|
||||
ctx := context.Background()
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reopen store for verification: %v", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
bdVersion, err := store.GetMetadata(ctx, "bd_version")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMetadata(bd_version) error: %v", err)
|
||||
}
|
||||
if bdVersion != Version {
|
||||
t.Errorf("bd_version = %q, want %q", bdVersion, Version)
|
||||
}
|
||||
|
||||
repoID, err := store.GetMetadata(ctx, "repo_id")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMetadata(repo_id) error: %v", err)
|
||||
}
|
||||
if repoID == "" {
|
||||
t.Error("repo_id was not set by handleDoltMetadataUpdate")
|
||||
}
|
||||
|
||||
cloneID, err := store.GetMetadata(ctx, "clone_id")
|
||||
if err != nil {
|
||||
t.Fatalf("GetMetadata(clone_id) error: %v", err)
|
||||
}
|
||||
if cloneID == "" {
|
||||
t.Error("clone_id was not set by handleDoltMetadataUpdate")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltMetadataIdempotent verifies that running handleDoltMetadataUpdate
|
||||
// on a database with all 3 fields already set does not overwrite them.
|
||||
func TestMigrateDoltMetadataIdempotent(t *testing.T) {
|
||||
cleanup := saveMigrateGlobals(t)
|
||||
defer cleanup()
|
||||
|
||||
dir, beadsDir, cfg := setupDoltMigrateWorkspace(t)
|
||||
|
||||
rootCtx = context.Background()
|
||||
jsonOutput = false
|
||||
|
||||
oldWD, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
defer func() { _ = os.Chdir(oldWD) }()
|
||||
|
||||
beads.ResetCaches()
|
||||
git.ResetCaches()
|
||||
|
||||
// First run: set all metadata
|
||||
handleDoltMetadataUpdate(cfg, beadsDir, false)
|
||||
|
||||
// Read original values
|
||||
ctx := context.Background()
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open store: %v", err)
|
||||
}
|
||||
origVersion, _ := store.GetMetadata(ctx, "bd_version")
|
||||
origRepoID, _ := store.GetMetadata(ctx, "repo_id")
|
||||
origCloneID, _ := store.GetMetadata(ctx, "clone_id")
|
||||
_ = store.Close()
|
||||
|
||||
if origVersion == "" || origRepoID == "" || origCloneID == "" {
|
||||
t.Fatalf("first run didn't set all fields: version=%q, repo_id=%q, clone_id=%q",
|
||||
origVersion, origRepoID, origCloneID)
|
||||
}
|
||||
|
||||
beads.ResetCaches()
|
||||
git.ResetCaches()
|
||||
|
||||
// Second run: should be a no-op (all fields present and version matches)
|
||||
handleDoltMetadataUpdate(cfg, beadsDir, false)
|
||||
|
||||
// Verify values did not change
|
||||
store2, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reopen store: %v", err)
|
||||
}
|
||||
defer func() { _ = store2.Close() }()
|
||||
|
||||
newVersion, _ := store2.GetMetadata(ctx, "bd_version")
|
||||
if newVersion != origVersion {
|
||||
t.Errorf("bd_version changed from %q to %q (should be idempotent)", origVersion, newVersion)
|
||||
}
|
||||
|
||||
newRepoID, _ := store2.GetMetadata(ctx, "repo_id")
|
||||
if newRepoID != origRepoID {
|
||||
t.Errorf("repo_id changed from %q to %q (should be idempotent)", origRepoID, newRepoID)
|
||||
}
|
||||
|
||||
newCloneID, _ := store2.GetMetadata(ctx, "clone_id")
|
||||
if newCloneID != origCloneID {
|
||||
t.Errorf("clone_id changed from %q to %q (should be idempotent)", origCloneID, newCloneID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltMetadataPartial verifies that handleDoltMetadataUpdate only
|
||||
// sets missing fields and does not overwrite existing ones.
|
||||
// This is the critical test for the early-return fix: when bd_version already
|
||||
// matches but repo_id/clone_id are missing, the function must still set them.
|
||||
func TestMigrateDoltMetadataPartial(t *testing.T) {
|
||||
cleanup := saveMigrateGlobals(t)
|
||||
defer cleanup()
|
||||
|
||||
dir, beadsDir, cfg := setupDoltMigrateWorkspace(t)
|
||||
|
||||
rootCtx = context.Background()
|
||||
jsonOutput = false
|
||||
|
||||
oldWD, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get working directory: %v", err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatalf("failed to chdir: %v", err)
|
||||
}
|
||||
defer func() { _ = os.Chdir(oldWD) }()
|
||||
|
||||
beads.ResetCaches()
|
||||
git.ResetCaches()
|
||||
|
||||
// Pre-set only bd_version (simulate a pre-Phase-3 database that has
|
||||
// already been migrated for version but not for identity fields)
|
||||
ctx := context.Background()
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open store: %v", err)
|
||||
}
|
||||
if err := store.SetMetadata(ctx, "bd_version", Version); err != nil {
|
||||
t.Fatalf("failed to pre-set bd_version: %v", err)
|
||||
}
|
||||
_ = store.Close()
|
||||
|
||||
// Run handleDoltMetadataUpdate — the old code would early-return here
|
||||
// since bd_version matches. The new code should still set repo_id/clone_id.
|
||||
handleDoltMetadataUpdate(cfg, beadsDir, false)
|
||||
|
||||
// Verify: bd_version should remain as Version (not overwritten)
|
||||
store2, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reopen store: %v", err)
|
||||
}
|
||||
defer func() { _ = store2.Close() }()
|
||||
|
||||
bdVersion, _ := store2.GetMetadata(ctx, "bd_version")
|
||||
if bdVersion != Version {
|
||||
t.Errorf("bd_version = %q, want %q (should not change)", bdVersion, Version)
|
||||
}
|
||||
|
||||
// repo_id and clone_id should have been set despite bd_version already matching
|
||||
repoID, _ := store2.GetMetadata(ctx, "repo_id")
|
||||
if repoID == "" {
|
||||
t.Error("repo_id was not set during partial metadata update (early-return bug)")
|
||||
}
|
||||
|
||||
cloneID, _ := store2.GetMetadata(ctx, "clone_id")
|
||||
if cloneID == "" {
|
||||
t.Error("clone_id was not set during partial metadata update (early-return bug)")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,275 +0,0 @@
|
|||
//go:build !cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/ui"
|
||||
)
|
||||
|
||||
// handleToDoltMigration performs SQLite→Dolt migration for non-CGO builds
|
||||
// using the system sqlite3 CLI for data extraction. This delegates to the
|
||||
// same runMigrationPhases path used by auto-migration (shimMigrateSQLiteToDolt).
|
||||
func handleToDoltMigration(dryRun bool, autoYes bool) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Find .beads directory
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": "no_beads_directory",
|
||||
"message": "No .beads directory found. Run 'bd init' first.",
|
||||
})
|
||||
} else {
|
||||
FatalErrorWithHint("no .beads directory found", "run 'bd init' to initialize bd")
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Find SQLite database
|
||||
sqlitePath := findSQLiteDB(beadsDir)
|
||||
if sqlitePath == "" {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": "no_sqlite_database",
|
||||
"message": "No SQLite database found to migrate",
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: No SQLite database found to migrate\n")
|
||||
fmt.Fprintf(os.Stderr, "Hint: no .db files found in %s\n", beadsDir)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Verify sqlite3 CLI is available (required for non-CGO builds)
|
||||
sqlite3Path, err := exec.LookPath("sqlite3")
|
||||
if err != nil {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": "sqlite3_not_found",
|
||||
"message": "The sqlite3 CLI tool is required for migration in non-CGO builds.",
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: sqlite3 CLI tool not found\n")
|
||||
fmt.Fprintf(os.Stderr, "This binary was built without CGO, so the sqlite3 CLI is needed for data extraction.\n")
|
||||
fmt.Fprintf(os.Stderr, "Hint: install sqlite3, or rebuild with CGO_ENABLED=1\n")
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
_ = sqlite3Path
|
||||
|
||||
// Extract data from SQLite via CLI
|
||||
data, err := extractViaSQLiteCLI(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": "extraction_failed",
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Dolt path
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
|
||||
// Show migration plan
|
||||
if !jsonOutput {
|
||||
fmt.Printf("SQLite to Dolt Migration\n")
|
||||
fmt.Printf("========================\n\n")
|
||||
fmt.Printf("Source: %s\n", sqlitePath)
|
||||
fmt.Printf("Target: %s\n", doltPath)
|
||||
fmt.Printf("Issues to migrate: %d\n", data.issueCount)
|
||||
eventCount, commentCount := countMigrationRecords(data)
|
||||
fmt.Printf("Events to migrate: %d\n", eventCount)
|
||||
fmt.Printf("Comments to migrate: %d\n", commentCount)
|
||||
fmt.Printf("Config keys: %d\n", len(data.config))
|
||||
if data.prefix != "" {
|
||||
fmt.Printf("Issue prefix: %s\n", data.prefix)
|
||||
}
|
||||
fmt.Printf("Note: using sqlite3 CLI for extraction (non-CGO build)\n")
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// Dry run mode
|
||||
if dryRun {
|
||||
eventCount, commentCount := countMigrationRecords(data)
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"dry_run": true,
|
||||
"source": sqlitePath,
|
||||
"target": doltPath,
|
||||
"issue_count": data.issueCount,
|
||||
"event_count": eventCount,
|
||||
"comment_count": commentCount,
|
||||
"config_keys": len(data.config),
|
||||
"prefix": data.prefix,
|
||||
"would_backup": true,
|
||||
"nocgo": true,
|
||||
})
|
||||
} else {
|
||||
fmt.Println("Dry run mode - no changes will be made")
|
||||
fmt.Println("Would perform:")
|
||||
fmt.Printf(" 1. Create backup of source database\n")
|
||||
fmt.Printf(" 2. Create target database at %s\n", doltPath)
|
||||
fmt.Printf(" 3. Import %d issues with labels and dependencies\n", data.issueCount)
|
||||
fmt.Printf(" 4. Import %d events (issue history)\n", eventCount)
|
||||
fmt.Printf(" 5. Import %d comments (legacy comments table)\n", commentCount)
|
||||
fmt.Printf(" 6. Copy %d config values\n", len(data.config))
|
||||
fmt.Printf(" 7. Update metadata.json\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Prompt for confirmation
|
||||
if !autoYes && !jsonOutput {
|
||||
fmt.Printf("This will:\n")
|
||||
fmt.Printf(" 1. Create a backup of your SQLite database\n")
|
||||
fmt.Printf(" 2. Create a Dolt database and import all data\n")
|
||||
fmt.Printf(" 3. Update metadata.json to use Dolt backend\n")
|
||||
fmt.Printf(" 4. Keep your SQLite database (can be deleted after verification)\n\n")
|
||||
fmt.Printf("Continue? [y/N] ")
|
||||
var response string
|
||||
_, _ = fmt.Scanln(&response)
|
||||
if strings.ToLower(response) != "y" && strings.ToLower(response) != "yes" {
|
||||
fmt.Println("Migration canceled")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Backup SQLite database
|
||||
fmt.Fprintf(os.Stderr, "Backing up SQLite database...\n")
|
||||
backupPath, err := backupSQLite(sqlitePath)
|
||||
if err != nil {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": "backup_failed",
|
||||
"message": err.Error(),
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: backup failed: %s\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
if !jsonOutput {
|
||||
fmt.Printf("%s\n", ui.RenderPass("✓ Created backup: "+filepath.Base(backupPath)))
|
||||
}
|
||||
|
||||
// Determine database name
|
||||
dbName := "beads"
|
||||
if existingCfg, _ := configfile.Load(beadsDir); existingCfg != nil && existingCfg.DoltDatabase != "" {
|
||||
dbName = existingCfg.DoltDatabase
|
||||
} else if data.prefix != "" {
|
||||
dbName = data.prefix
|
||||
}
|
||||
|
||||
// Resolve server connection settings
|
||||
resolvedHost := "127.0.0.1"
|
||||
resolvedPort := 0
|
||||
resolvedUser := "root"
|
||||
resolvedPassword := ""
|
||||
resolvedTLS := false
|
||||
autoStart := os.Getenv("GT_ROOT") == "" && os.Getenv("BEADS_DOLT_AUTO_START") != "0"
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
resolvedHost = cfg.GetDoltServerHost()
|
||||
resolvedPort = doltserver.DefaultConfig(beadsDir).Port
|
||||
resolvedUser = cfg.GetDoltServerUser()
|
||||
resolvedPassword = cfg.GetDoltServerPassword()
|
||||
resolvedTLS = cfg.GetDoltServerTLS()
|
||||
}
|
||||
|
||||
// Run shared migration phases
|
||||
params := &migrationParams{
|
||||
beadsDir: beadsDir,
|
||||
sqlitePath: sqlitePath,
|
||||
backupPath: backupPath,
|
||||
data: data,
|
||||
dbName: dbName,
|
||||
serverHost: resolvedHost,
|
||||
serverPort: resolvedPort,
|
||||
serverUser: resolvedUser,
|
||||
serverPassword: resolvedPassword,
|
||||
doltCfg: &dolt.Config{
|
||||
Path: doltPath,
|
||||
Database: dbName,
|
||||
CreateIfMissing: true,
|
||||
ServerHost: resolvedHost,
|
||||
ServerPort: resolvedPort,
|
||||
ServerUser: resolvedUser,
|
||||
ServerPassword: resolvedPassword,
|
||||
ServerTLS: resolvedTLS,
|
||||
AutoStart: autoStart,
|
||||
},
|
||||
}
|
||||
|
||||
imported, skipped, migErr := runMigrationPhases(ctx, params)
|
||||
if migErr != nil {
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"error": "migration_failed",
|
||||
"message": migErr.Error(),
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: migration failed: %s\n", migErr)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Print results
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"status": "success",
|
||||
"backend": "dolt",
|
||||
"issues_imported": imported,
|
||||
"issues_skipped": skipped,
|
||||
"backup_path": backupPath,
|
||||
"dolt_path": doltPath,
|
||||
})
|
||||
} else {
|
||||
if skipped > 0 {
|
||||
fmt.Printf("%s\n", ui.RenderPass(fmt.Sprintf("✓ Imported %d issues (%d skipped)", imported, skipped)))
|
||||
} else {
|
||||
fmt.Printf("%s\n", ui.RenderPass(fmt.Sprintf("✓ Imported %d issues", imported)))
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Printf("%s\n", ui.RenderPass("✓ Migration complete!"))
|
||||
fmt.Println()
|
||||
fmt.Printf("Your beads now use DOLT storage.\n")
|
||||
fmt.Printf("Backup: %s\n", backupPath)
|
||||
fmt.Println()
|
||||
fmt.Println("Next steps:")
|
||||
fmt.Println(" - Verify data: bd list")
|
||||
fmt.Println(" - After verification, you can delete the old database:")
|
||||
fmt.Printf(" rm %s\n", sqlitePath)
|
||||
}
|
||||
}
|
||||
|
||||
// countMigrationRecords counts events and comments across all issues.
|
||||
// This is the nocgo variant of migrationRecordCounts (defined in migrate_dolt.go for CGO builds).
|
||||
func countMigrationRecords(data *migrationData) (eventCount int, commentCount int) {
|
||||
for _, events := range data.eventsMap {
|
||||
eventCount += len(events)
|
||||
}
|
||||
for _, comments := range data.commentsMap {
|
||||
commentCount += len(comments)
|
||||
}
|
||||
return eventCount, commentCount
|
||||
}
|
||||
|
||||
// listMigrations returns an empty list (no Dolt migrations without CGO).
|
||||
func listMigrations() []string {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,433 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// migrationData holds all data extracted from the source database.
|
||||
type migrationData struct {
|
||||
issues []*types.Issue
|
||||
labelsMap map[string][]string
|
||||
depsMap map[string][]*types.Dependency
|
||||
eventsMap map[string][]*types.Event
|
||||
commentsMap map[string][]*types.Comment
|
||||
config map[string]string
|
||||
prefix string
|
||||
issueCount int
|
||||
}
|
||||
|
||||
// findSQLiteDB looks for a SQLite .db file in the beads directory.
|
||||
// Returns the path to the first .db file found, or empty string if none.
|
||||
func findSQLiteDB(beadsDir string) string {
|
||||
// Check common names first
|
||||
for _, name := range []string{"beads.db", "issues.db"} {
|
||||
p := filepath.Join(beadsDir, name)
|
||||
if info, err := os.Stat(p); err == nil && !info.IsDir() {
|
||||
return p
|
||||
}
|
||||
}
|
||||
// Scan for any .db file
|
||||
entries, err := os.ReadDir(beadsDir)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".db") &&
|
||||
!strings.Contains(entry.Name(), "backup") {
|
||||
return filepath.Join(beadsDir, entry.Name())
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// parseNullTime parses a time string into *time.Time. Returns nil for empty strings.
|
||||
func parseNullTime(s string) *time.Time {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, "2006-01-02T15:04:05.999999999Z07:00", "2006-01-02 15:04:05"} {
|
||||
if t, err := time.Parse(layout, s); err == nil {
|
||||
return &t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// importToDolt imports all data to Dolt, returning (imported, skipped, error)
|
||||
func importToDolt(ctx context.Context, store *dolt.DoltStore, data *migrationData) (int, int, error) {
|
||||
// Set all config values first
|
||||
for key, value := range data.config {
|
||||
if err := store.SetConfig(ctx, key, value); err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to set config %s: %w", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := store.UnderlyingDB().BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
imported := 0
|
||||
skipped := 0
|
||||
seenIDs := make(map[string]bool)
|
||||
total := len(data.issues)
|
||||
|
||||
for i, issue := range data.issues {
|
||||
if !jsonOutput && total > 100 && (i+1)%100 == 0 {
|
||||
fmt.Printf(" Importing issues: %d/%d\r", i+1, total)
|
||||
}
|
||||
|
||||
if seenIDs[issue.ID] {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
seenIDs[issue.ID] = true
|
||||
|
||||
if issue.ContentHash == "" {
|
||||
issue.ContentHash = issue.ComputeContentHash()
|
||||
}
|
||||
|
||||
// Normalize metadata: nil/empty → "{}"
|
||||
metadataStr := "{}"
|
||||
if len(issue.Metadata) > 0 {
|
||||
metadataStr = string(issue.Metadata)
|
||||
}
|
||||
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO issues (
|
||||
id, content_hash, title, description, design, acceptance_criteria, notes,
|
||||
status, priority, issue_type, assignee, estimated_minutes,
|
||||
created_at, created_by, owner, updated_at, closed_at, external_ref, spec_id,
|
||||
compaction_level, compacted_at, compacted_at_commit, original_size,
|
||||
sender, ephemeral, wisp_type, pinned, is_template, crystallizes,
|
||||
mol_type, work_type, quality_score, source_system, source_repo, close_reason, closed_by_session,
|
||||
event_kind, actor, target, payload,
|
||||
await_type, await_id, timeout_ns, waiters,
|
||||
hook_bead, role_bead, agent_state, last_activity, role_type, rig,
|
||||
due_at, defer_until, metadata
|
||||
) VALUES (
|
||||
?, ?, ?, ?, ?, ?, ?,
|
||||
?, ?, ?, ?, ?,
|
||||
?, ?, ?, ?, ?, ?, ?,
|
||||
?, ?, ?, ?,
|
||||
?, ?, ?, ?, ?, ?,
|
||||
?, ?, ?, ?, ?, ?, ?,
|
||||
?, ?, ?, ?,
|
||||
?, ?, ?, ?,
|
||||
?, ?, ?, ?, ?, ?,
|
||||
?, ?, ?
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
content_hash = VALUES(content_hash),
|
||||
title = VALUES(title),
|
||||
description = VALUES(description),
|
||||
design = VALUES(design),
|
||||
acceptance_criteria = VALUES(acceptance_criteria),
|
||||
notes = VALUES(notes),
|
||||
status = VALUES(status),
|
||||
priority = VALUES(priority),
|
||||
issue_type = VALUES(issue_type),
|
||||
assignee = VALUES(assignee),
|
||||
estimated_minutes = VALUES(estimated_minutes),
|
||||
created_at = VALUES(created_at),
|
||||
created_by = VALUES(created_by),
|
||||
owner = VALUES(owner),
|
||||
updated_at = VALUES(updated_at),
|
||||
closed_at = VALUES(closed_at),
|
||||
external_ref = VALUES(external_ref),
|
||||
spec_id = VALUES(spec_id),
|
||||
compaction_level = VALUES(compaction_level),
|
||||
compacted_at = VALUES(compacted_at),
|
||||
compacted_at_commit = VALUES(compacted_at_commit),
|
||||
original_size = VALUES(original_size),
|
||||
sender = VALUES(sender),
|
||||
ephemeral = VALUES(ephemeral),
|
||||
wisp_type = VALUES(wisp_type),
|
||||
pinned = VALUES(pinned),
|
||||
is_template = VALUES(is_template),
|
||||
crystallizes = VALUES(crystallizes),
|
||||
mol_type = VALUES(mol_type),
|
||||
work_type = VALUES(work_type),
|
||||
quality_score = VALUES(quality_score),
|
||||
source_system = VALUES(source_system),
|
||||
source_repo = VALUES(source_repo),
|
||||
close_reason = VALUES(close_reason),
|
||||
closed_by_session = VALUES(closed_by_session),
|
||||
event_kind = VALUES(event_kind),
|
||||
actor = VALUES(actor),
|
||||
target = VALUES(target),
|
||||
payload = VALUES(payload),
|
||||
await_type = VALUES(await_type),
|
||||
await_id = VALUES(await_id),
|
||||
timeout_ns = VALUES(timeout_ns),
|
||||
waiters = VALUES(waiters),
|
||||
hook_bead = VALUES(hook_bead),
|
||||
role_bead = VALUES(role_bead),
|
||||
agent_state = VALUES(agent_state),
|
||||
last_activity = VALUES(last_activity),
|
||||
role_type = VALUES(role_type),
|
||||
rig = VALUES(rig),
|
||||
due_at = VALUES(due_at),
|
||||
defer_until = VALUES(defer_until),
|
||||
metadata = VALUES(metadata)
|
||||
`,
|
||||
issue.ID, issue.ContentHash, issue.Title, issue.Description, issue.Design, issue.AcceptanceCriteria, issue.Notes,
|
||||
issue.Status, issue.Priority, issue.IssueType, nullableString(issue.Assignee), nullableIntPtr(issue.EstimatedMinutes),
|
||||
issue.CreatedAt, issue.CreatedBy, issue.Owner, issue.UpdatedAt, issue.ClosedAt, nullableStringPtr(issue.ExternalRef), issue.SpecID,
|
||||
issue.CompactionLevel, issue.CompactedAt, nullableStringPtr(issue.CompactedAtCommit), nullableInt(issue.OriginalSize),
|
||||
issue.Sender, issue.Ephemeral, issue.WispType, issue.Pinned, issue.IsTemplate, issue.Crystallizes,
|
||||
issue.MolType, issue.WorkType, nullableFloat32Ptr(issue.QualityScore), issue.SourceSystem, issue.SourceRepo, issue.CloseReason, issue.ClosedBySession,
|
||||
issue.EventKind, issue.Actor, issue.Target, issue.Payload,
|
||||
issue.AwaitType, issue.AwaitID, issue.Timeout.Nanoseconds(), formatJSONArray(issue.Waiters),
|
||||
issue.HookBead, issue.RoleBead, issue.AgentState, issue.LastActivity, issue.RoleType, issue.Rig,
|
||||
issue.DueAt, issue.DeferUntil, metadataStr,
|
||||
)
|
||||
if err != nil {
|
||||
return imported, skipped, fmt.Errorf("failed to insert issue %s: %w", issue.ID, err)
|
||||
}
|
||||
|
||||
// Reconcile labels for deterministic re-imports.
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM labels WHERE issue_id = ?`, issue.ID); err != nil {
|
||||
return imported, skipped, fmt.Errorf("failed to reset labels for issue %s: %w", issue.ID, err)
|
||||
}
|
||||
|
||||
// Insert labels.
|
||||
// Keep ON DUPLICATE as a defensive guard: if source labels contain duplicates
|
||||
// or this flow is later changed to skip the pre-delete step, import remains idempotent.
|
||||
for _, label := range issue.Labels {
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO labels (issue_id, label)
|
||||
VALUES (?, ?)
|
||||
ON DUPLICATE KEY UPDATE label = VALUES(label)
|
||||
`, issue.ID, label); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to insert label %q for issue %s: %v\n", label, issue.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
imported++
|
||||
}
|
||||
|
||||
if !jsonOutput && total > 100 {
|
||||
fmt.Printf(" Importing issues: %d/%d\n", total, total)
|
||||
}
|
||||
|
||||
issueIDs := orderedIssueIDs(data)
|
||||
|
||||
// Reconcile relations for deterministic and idempotent re-imports.
|
||||
// This is source-authoritative for migrated issue IDs: the target relation set
|
||||
// is replaced with what is present in the source snapshot.
|
||||
for _, issueID := range issueIDs {
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM dependencies WHERE issue_id = ?`, issueID); err != nil {
|
||||
return imported, skipped, fmt.Errorf("failed to reset dependencies for issue %s: %w", issueID, err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM events WHERE issue_id = ?`, issueID); err != nil {
|
||||
return imported, skipped, fmt.Errorf("failed to reset events for issue %s: %w", issueID, err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM comments WHERE issue_id = ?`, issueID); err != nil {
|
||||
return imported, skipped, fmt.Errorf("failed to reset comments for issue %s: %w", issueID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Import dependencies
|
||||
migratePrintProgress("Importing dependencies...")
|
||||
for _, issue := range data.issues {
|
||||
for _, dep := range issue.Dependencies {
|
||||
metadataStr := normalizeDependencyMetadata(dep.Metadata)
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO dependencies (issue_id, depends_on_id, type, created_by, created_at, metadata, thread_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
type = VALUES(type),
|
||||
created_by = VALUES(created_by),
|
||||
created_at = VALUES(created_at),
|
||||
metadata = VALUES(metadata),
|
||||
thread_id = VALUES(thread_id)
|
||||
`, dep.IssueID, dep.DependsOnID, dep.Type, dep.CreatedBy, dep.CreatedAt, metadataStr, dep.ThreadID); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to insert dependency %s -> %s: %v\n", dep.IssueID, dep.DependsOnID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Import events (includes comments)
|
||||
migratePrintProgress("Importing events...")
|
||||
eventCount := 0
|
||||
eventSkipped := 0
|
||||
for _, issueID := range issueIDs {
|
||||
events := data.eventsMap[issueID]
|
||||
for _, event := range events {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO events (issue_id, event_type, actor, old_value, new_value, comment, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`, issueID, event.EventType, event.Actor,
|
||||
nullableStringPtr(event.OldValue), nullableStringPtr(event.NewValue),
|
||||
nullableStringPtr(event.Comment), event.CreatedAt)
|
||||
if err == nil {
|
||||
eventCount++
|
||||
} else {
|
||||
eventSkipped++
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to insert event for issue %s: %v\n", issueID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !jsonOutput {
|
||||
if eventSkipped > 0 {
|
||||
fmt.Printf(" Imported %d events (%d skipped)\n", eventCount, eventSkipped)
|
||||
} else {
|
||||
fmt.Printf(" Imported %d events\n", eventCount)
|
||||
}
|
||||
}
|
||||
|
||||
// Import comments from legacy comments table (if available).
|
||||
migratePrintProgress("Importing comments...")
|
||||
commentCount := 0
|
||||
commentSkipped := 0
|
||||
for _, issueID := range issueIDs {
|
||||
comments := data.commentsMap[issueID]
|
||||
for _, comment := range comments {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO comments (issue_id, author, text, created_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`, issueID, comment.Author, comment.Text, comment.CreatedAt)
|
||||
if err == nil {
|
||||
commentCount++
|
||||
} else {
|
||||
commentSkipped++
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to insert comment for issue %s: %v\n", issueID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !jsonOutput {
|
||||
if commentSkipped > 0 {
|
||||
fmt.Printf(" Imported %d comments (%d skipped)\n", commentCount, commentSkipped)
|
||||
} else {
|
||||
fmt.Printf(" Imported %d comments\n", commentCount)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return imported, skipped, fmt.Errorf("failed to commit: %w", err)
|
||||
}
|
||||
|
||||
return imported, skipped, nil
|
||||
}
|
||||
|
||||
// Migration output helpers
|
||||
|
||||
func migratePrintProgress(message string) {
|
||||
if !jsonOutput {
|
||||
fmt.Printf("%s\n", message)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions for nullable values
|
||||
|
||||
func nullableString(s string) interface{} {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func nullableStringPtr(s *string) interface{} {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
func nullableIntPtr(i *int) interface{} {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
return *i
|
||||
}
|
||||
|
||||
func nullableInt(i int) interface{} {
|
||||
if i == 0 {
|
||||
return nil
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func nullableFloat32Ptr(f *float32) interface{} {
|
||||
if f == nil {
|
||||
return nil
|
||||
}
|
||||
return *f
|
||||
}
|
||||
|
||||
func normalizeDependencyMetadata(raw string) string {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" {
|
||||
return "{}"
|
||||
}
|
||||
if json.Valid([]byte(trimmed)) {
|
||||
return trimmed
|
||||
}
|
||||
encoded, err := json.Marshal(trimmed)
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(encoded)
|
||||
}
|
||||
|
||||
func sqliteOptionalTextExpr(columns map[string]bool, column string, fallback string) string {
|
||||
if columns != nil && columns[column] {
|
||||
return fmt.Sprintf("COALESCE(%s, %s)", column, fallback)
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func normalizedJSONBytes(raw string) json.RawMessage {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" {
|
||||
return nil
|
||||
}
|
||||
if json.Valid([]byte(trimmed)) {
|
||||
return json.RawMessage(trimmed)
|
||||
}
|
||||
encoded, err := json.Marshal(trimmed)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return encoded
|
||||
}
|
||||
|
||||
func orderedIssueIDs(data *migrationData) []string {
|
||||
seen := make(map[string]struct{}, len(data.issues))
|
||||
ids := make([]string, 0, len(data.issues))
|
||||
|
||||
for _, issue := range data.issues {
|
||||
if issue == nil || issue.ID == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[issue.ID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[issue.ID] = struct{}{}
|
||||
ids = append(ids, issue.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// formatJSONArray formats a string slice as JSON (matches Dolt schema expectation)
|
||||
func formatJSONArray(arr []string) string {
|
||||
if len(arr) == 0 {
|
||||
return ""
|
||||
}
|
||||
data, err := json.Marshal(arr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
|
@ -1,479 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// TestMigrateImport_ColumnParityWithInsertIssue verifies that the migration
|
||||
// INSERT in importToDolt covers the same columns as the canonical insertIssue
|
||||
// helper in internal/storage/dolt/issues.go. This catches silent data loss
|
||||
// when new columns are added to the schema but not to the migration path.
|
||||
func TestMigrateImport_ColumnParityWithInsertIssue(t *testing.T) {
|
||||
root := moduleRoot(t)
|
||||
migrateColumns := extractInsertColumns(t, filepath.Join(root, "cmd/bd/migrate_import.go"))
|
||||
canonicalColumns := extractInsertColumns(t, filepath.Join(root, "internal/storage/dolt/issues.go"))
|
||||
|
||||
if len(migrateColumns) == 0 {
|
||||
t.Fatal("failed to extract columns from migrate_import.go")
|
||||
}
|
||||
if len(canonicalColumns) == 0 {
|
||||
t.Fatal("failed to extract columns from issues.go")
|
||||
}
|
||||
|
||||
// Sort both for comparison
|
||||
sort.Strings(migrateColumns)
|
||||
sort.Strings(canonicalColumns)
|
||||
|
||||
// Find columns in canonical but missing from migrate
|
||||
missing := []string{}
|
||||
migrateSet := make(map[string]bool)
|
||||
for _, col := range migrateColumns {
|
||||
migrateSet[col] = true
|
||||
}
|
||||
for _, col := range canonicalColumns {
|
||||
if !migrateSet[col] {
|
||||
missing = append(missing, col)
|
||||
}
|
||||
}
|
||||
|
||||
if len(missing) > 0 {
|
||||
t.Errorf("migrate_import.go INSERT is missing columns present in insertIssue: %v\n"+
|
||||
"migrate has %d columns, canonical has %d columns",
|
||||
missing, len(migrateColumns), len(canonicalColumns))
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateImport_DependencyInsertPlaceholderParity verifies that the
|
||||
// dependency migration INSERT keeps SQL arity correct when columns change.
|
||||
func TestMigrateImport_DependencyInsertPlaceholderParity(t *testing.T) {
|
||||
root := moduleRoot(t)
|
||||
path := filepath.Join(root, "cmd/bd/migrate_import.go")
|
||||
data, err := os.ReadFile(path) // #nosec G304 - test reads known source files
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read %s: %v", path, err)
|
||||
}
|
||||
|
||||
// Capture INSERT INTO dependencies (...) VALUES (...) in importToDolt.
|
||||
re := regexp.MustCompile(`(?s)INSERT INTO dependencies\s*\(\s*(.*?)\)\s*VALUES\s*\(\s*(.*?)\s*\)\s*ON DUPLICATE KEY UPDATE`)
|
||||
matches := re.FindSubmatch(data)
|
||||
if len(matches) < 3 {
|
||||
t.Fatal("failed to locate dependencies INSERT statement in migrate_import.go")
|
||||
}
|
||||
|
||||
columnList := strings.TrimSpace(string(matches[1]))
|
||||
valuesList := strings.TrimSpace(string(matches[2]))
|
||||
|
||||
columns := splitColumns(columnList)
|
||||
placeholders := regexp.MustCompile(`\?`).FindAllString(valuesList, -1)
|
||||
|
||||
if len(columns) != len(placeholders) {
|
||||
t.Fatalf("dependencies INSERT arity mismatch: %d columns, %d placeholders\ncolumns=%q\nvalues=%q",
|
||||
len(columns), len(placeholders), columnList, valuesList)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateImport_IssueInsertPlaceholderParity verifies that issue INSERT
|
||||
// values keep placeholder count in sync with the column list.
|
||||
func TestMigrateImport_IssueInsertPlaceholderParity(t *testing.T) {
|
||||
root := moduleRoot(t)
|
||||
path := filepath.Join(root, "cmd/bd/migrate_import.go")
|
||||
data, err := os.ReadFile(path) // #nosec G304 - test reads known source files
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read %s: %v", path, err)
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`(?s)INSERT INTO issues\s*\(\s*(.*?)\)\s*VALUES\s*\(\s*(.*?)\s*\)\s*ON DUPLICATE KEY UPDATE`)
|
||||
matches := re.FindSubmatch(data)
|
||||
if len(matches) < 3 {
|
||||
t.Fatal("failed to locate issues INSERT statement in migrate_import.go")
|
||||
}
|
||||
|
||||
columnList := strings.TrimSpace(string(matches[1]))
|
||||
valuesList := strings.TrimSpace(string(matches[2]))
|
||||
|
||||
columns := splitColumns(columnList)
|
||||
placeholders := regexp.MustCompile(`\?`).FindAllString(valuesList, -1)
|
||||
|
||||
if len(columns) != len(placeholders) {
|
||||
t.Fatalf("issues INSERT arity mismatch: %d columns, %d placeholders\ncolumns=%q\nvalues=%q",
|
||||
len(columns), len(placeholders), columnList, valuesList)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDependencyMetadata_JSONSafety(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
want string
|
||||
wantQuoted bool
|
||||
}{
|
||||
{name: "empty", in: "", want: "{}", wantQuoted: false},
|
||||
{name: "whitespace", in: " ", want: "{}", wantQuoted: false},
|
||||
{name: "valid object", in: `{"k":"v"}`, want: `{"k":"v"}`, wantQuoted: false},
|
||||
{name: "valid array", in: `[1,2,3]`, want: `[1,2,3]`, wantQuoted: false},
|
||||
{name: "plain text", in: "legacy-metadata", want: `"legacy-metadata"`, wantQuoted: true},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := normalizeDependencyMetadata(tc.in)
|
||||
if got != tc.want {
|
||||
t.Fatalf("normalizeDependencyMetadata(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
if !json.Valid([]byte(got)) {
|
||||
t.Fatalf("normalizeDependencyMetadata(%q) produced invalid JSON: %q", tc.in, got)
|
||||
}
|
||||
if tc.wantQuoted && !(strings.HasPrefix(got, `"`) && strings.HasSuffix(got, `"`)) {
|
||||
t.Fatalf("expected quoted JSON string for %q, got %q", tc.in, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedIssueIDs_ExcludesOrphanMapKeys(t *testing.T) {
|
||||
data := &migrationData{
|
||||
issues: []*types.Issue{
|
||||
{ID: "bd-2"},
|
||||
{ID: "bd-1"},
|
||||
{ID: "bd-1"}, // duplicate should be de-duped
|
||||
},
|
||||
eventsMap: map[string][]*types.Event{
|
||||
"bd-2": {{EventType: types.EventType("commented")}},
|
||||
"orphan-evt": {{EventType: types.EventType("commented")}},
|
||||
},
|
||||
commentsMap: map[string][]*types.Comment{
|
||||
"bd-1": {{IssueID: "bd-1", Text: "ok"}},
|
||||
"orphan-cmt": {{IssueID: "orphan-cmt", Text: "orphan"}},
|
||||
},
|
||||
}
|
||||
|
||||
ids := orderedIssueIDs(data)
|
||||
if len(ids) != 2 {
|
||||
t.Fatalf("orderedIssueIDs length = %d, want 2", len(ids))
|
||||
}
|
||||
if ids[0] != "bd-2" || ids[1] != "bd-1" {
|
||||
t.Fatalf("orderedIssueIDs = %v, want [bd-2 bd-1]", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateImport_ReimportReconcilesIssueAndRelations(t *testing.T) {
|
||||
store := newTestStore(t, filepath.Join(t.TempDir(), ".beads", "beads.db"))
|
||||
ctx := context.Background()
|
||||
issueID := "test-migrate-reimport-1"
|
||||
ts := time.Date(2026, 2, 25, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
dataV1 := &migrationData{
|
||||
issues: []*types.Issue{
|
||||
{
|
||||
ID: issueID,
|
||||
Title: "title-v1",
|
||||
Description: "desc-v1",
|
||||
Design: "",
|
||||
AcceptanceCriteria: "",
|
||||
Notes: "",
|
||||
Status: types.Status("open"),
|
||||
Priority: 2,
|
||||
IssueType: types.IssueType("task"),
|
||||
CreatedAt: ts,
|
||||
CreatedBy: "tester",
|
||||
Owner: "tester",
|
||||
UpdatedAt: ts,
|
||||
Labels: []string{"alpha"},
|
||||
Dependencies: []*types.Dependency{
|
||||
{
|
||||
IssueID: issueID,
|
||||
DependsOnID: "external:rig:dep-v1",
|
||||
Type: types.DependencyType("blocks"),
|
||||
CreatedBy: "tester",
|
||||
CreatedAt: ts,
|
||||
Metadata: `{"v":1}`,
|
||||
ThreadID: "thread-v1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
eventsMap: map[string][]*types.Event{
|
||||
issueID: {
|
||||
{
|
||||
EventType: types.EventType("commented"),
|
||||
Actor: "actor-v1",
|
||||
Comment: strPtr("event-v1"),
|
||||
CreatedAt: ts,
|
||||
},
|
||||
},
|
||||
"orphan-evt": {
|
||||
{
|
||||
EventType: types.EventType("commented"),
|
||||
Actor: "orphan",
|
||||
CreatedAt: ts,
|
||||
},
|
||||
},
|
||||
},
|
||||
commentsMap: map[string][]*types.Comment{
|
||||
issueID: {
|
||||
{
|
||||
IssueID: issueID,
|
||||
Author: "author-v1",
|
||||
Text: "comment-v1",
|
||||
CreatedAt: ts,
|
||||
},
|
||||
},
|
||||
"orphan-cmt": {
|
||||
{
|
||||
IssueID: "orphan-cmt",
|
||||
Author: "ghost",
|
||||
Text: "ghost-comment",
|
||||
CreatedAt: ts,
|
||||
},
|
||||
},
|
||||
},
|
||||
config: map[string]string{
|
||||
"issue_prefix": "test",
|
||||
},
|
||||
}
|
||||
|
||||
imported, skipped, err := importToDolt(ctx, store, dataV1)
|
||||
if err != nil {
|
||||
t.Fatalf("first import failed: %v", err)
|
||||
}
|
||||
if imported != 1 || skipped != 0 {
|
||||
t.Fatalf("first import counts mismatch: imported=%d skipped=%d", imported, skipped)
|
||||
}
|
||||
|
||||
dataV2 := &migrationData{
|
||||
issues: []*types.Issue{
|
||||
{
|
||||
ID: issueID,
|
||||
Title: "title-v2",
|
||||
Description: "desc-v2",
|
||||
Design: "",
|
||||
AcceptanceCriteria: "",
|
||||
Notes: "",
|
||||
Status: types.Status("in_progress"),
|
||||
Priority: 1,
|
||||
IssueType: types.IssueType("task"),
|
||||
CreatedAt: ts,
|
||||
CreatedBy: "tester",
|
||||
Owner: "tester",
|
||||
UpdatedAt: ts.Add(1 * time.Hour),
|
||||
Labels: []string{"beta"},
|
||||
Dependencies: []*types.Dependency{
|
||||
{
|
||||
IssueID: issueID,
|
||||
DependsOnID: "external:rig:dep-v2",
|
||||
Type: types.DependencyType("blocks"),
|
||||
CreatedBy: "tester",
|
||||
CreatedAt: ts.Add(1 * time.Hour),
|
||||
Metadata: `{"v":2}`,
|
||||
ThreadID: "thread-v2",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
eventsMap: map[string][]*types.Event{
|
||||
issueID: {
|
||||
{
|
||||
EventType: types.EventType("commented"),
|
||||
Actor: "actor-v2",
|
||||
Comment: strPtr("event-v2"),
|
||||
CreatedAt: ts.Add(1 * time.Hour),
|
||||
},
|
||||
},
|
||||
},
|
||||
commentsMap: map[string][]*types.Comment{
|
||||
issueID: {
|
||||
{
|
||||
IssueID: issueID,
|
||||
Author: "author-v2",
|
||||
Text: "comment-v2",
|
||||
CreatedAt: ts.Add(1 * time.Hour),
|
||||
},
|
||||
},
|
||||
},
|
||||
config: map[string]string{
|
||||
"issue_prefix": "test",
|
||||
},
|
||||
}
|
||||
|
||||
imported, skipped, err = importToDolt(ctx, store, dataV2)
|
||||
if err != nil {
|
||||
t.Fatalf("second import failed: %v", err)
|
||||
}
|
||||
if imported != 1 || skipped != 0 {
|
||||
t.Fatalf("second import counts mismatch: imported=%d skipped=%d", imported, skipped)
|
||||
}
|
||||
|
||||
db := store.UnderlyingDB()
|
||||
|
||||
var title, status string
|
||||
if err := db.QueryRowContext(ctx, "SELECT title, status FROM issues WHERE id = ?", issueID).Scan(&title, &status); err != nil {
|
||||
t.Fatalf("failed to read issue: %v", err)
|
||||
}
|
||||
if title != "title-v2" || status != "in_progress" {
|
||||
t.Fatalf("issue not updated on re-import: title=%q status=%q", title, status)
|
||||
}
|
||||
|
||||
if got := countRowsByIssue(t, ctx, db, "labels", issueID); got != 1 {
|
||||
t.Fatalf("labels count = %d, want 1", got)
|
||||
}
|
||||
if got := countRowsByIssue(t, ctx, db, "dependencies", issueID); got != 1 {
|
||||
t.Fatalf("dependencies count = %d, want 1", got)
|
||||
}
|
||||
if got := countRowsByIssue(t, ctx, db, "events", issueID); got != 1 {
|
||||
t.Fatalf("events count = %d, want 1", got)
|
||||
}
|
||||
if got := countRowsByIssue(t, ctx, db, "comments", issueID); got != 1 {
|
||||
t.Fatalf("comments count = %d, want 1", got)
|
||||
}
|
||||
|
||||
var label string
|
||||
if err := db.QueryRowContext(ctx, "SELECT label FROM labels WHERE issue_id = ?", issueID).Scan(&label); err != nil {
|
||||
t.Fatalf("failed to read label: %v", err)
|
||||
}
|
||||
if label != "beta" {
|
||||
t.Fatalf("label mismatch after re-import: got %q want %q", label, "beta")
|
||||
}
|
||||
|
||||
var dependsOnID, depMetadata, depThreadID string
|
||||
if err := db.QueryRowContext(ctx, "SELECT depends_on_id, metadata, thread_id FROM dependencies WHERE issue_id = ?", issueID).
|
||||
Scan(&dependsOnID, &depMetadata, &depThreadID); err != nil {
|
||||
t.Fatalf("failed to read dependency: %v", err)
|
||||
}
|
||||
if dependsOnID != "external:rig:dep-v2" || depThreadID != "thread-v2" {
|
||||
t.Fatalf("dependency mismatch after re-import: depends_on=%q thread_id=%q", dependsOnID, depThreadID)
|
||||
}
|
||||
var depMetaObj map[string]int
|
||||
if err := json.Unmarshal([]byte(depMetadata), &depMetaObj); err != nil {
|
||||
t.Fatalf("dependency metadata is not valid JSON: %q (%v)", depMetadata, err)
|
||||
}
|
||||
if depMetaObj["v"] != 2 {
|
||||
t.Fatalf("dependency metadata mismatch: %+v", depMetaObj)
|
||||
}
|
||||
|
||||
var eventActor, eventComment string
|
||||
if err := db.QueryRowContext(ctx, "SELECT actor, comment FROM events WHERE issue_id = ?", issueID).
|
||||
Scan(&eventActor, &eventComment); err != nil {
|
||||
t.Fatalf("failed to read event: %v", err)
|
||||
}
|
||||
if eventActor != "actor-v2" || eventComment != "event-v2" {
|
||||
t.Fatalf("event mismatch after re-import: actor=%q comment=%q", eventActor, eventComment)
|
||||
}
|
||||
|
||||
var commentAuthor, commentText string
|
||||
if err := db.QueryRowContext(ctx, "SELECT author, text FROM comments WHERE issue_id = ?", issueID).
|
||||
Scan(&commentAuthor, &commentText); err != nil {
|
||||
t.Fatalf("failed to read comment: %v", err)
|
||||
}
|
||||
if commentAuthor != "author-v2" || commentText != "comment-v2" {
|
||||
t.Fatalf("comment mismatch after re-import: author=%q text=%q", commentAuthor, commentText)
|
||||
}
|
||||
|
||||
if got := countRowsByIssue(t, ctx, db, "events", "orphan-evt"); got != 0 {
|
||||
t.Fatalf("orphan events should not be imported, got %d rows", got)
|
||||
}
|
||||
if got := countRowsByIssue(t, ctx, db, "comments", "orphan-cmt"); got != 0 {
|
||||
t.Fatalf("orphan comments should not be imported, got %d rows", got)
|
||||
}
|
||||
}
|
||||
|
||||
// moduleRoot finds the Go module root by walking up from the test file.
|
||||
func moduleRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, filename, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("cannot determine test file path")
|
||||
}
|
||||
dir := filepath.Dir(filename)
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
||||
return dir
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
t.Fatal("cannot find module root (no go.mod)")
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
|
||||
// extractInsertColumns reads a Go source file and extracts column names from
|
||||
// the first INSERT INTO issues (...) statement.
|
||||
func extractInsertColumns(t *testing.T, path string) []string {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path) // #nosec G304 - test reads known source files
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read %s: %v", path, err)
|
||||
}
|
||||
|
||||
// Match INSERT INTO issues ( ... ) — capture the column list
|
||||
re := regexp.MustCompile(`(?s)INSERT INTO issues\s*\(\s*(.*?)\)\s*VALUES`)
|
||||
matches := re.FindSubmatch(data)
|
||||
if len(matches) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
raw := string(matches[1])
|
||||
// Split on commas and clean up
|
||||
parts := strings.Split(raw, ",")
|
||||
columns := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
col := strings.TrimSpace(p)
|
||||
col = strings.Trim(col, "`")
|
||||
if col != "" {
|
||||
columns = append(columns, col)
|
||||
}
|
||||
}
|
||||
return columns
|
||||
}
|
||||
|
||||
func splitColumns(raw string) []string {
|
||||
parts := strings.Split(raw, ",")
|
||||
columns := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
col := strings.TrimSpace(strings.Trim(p, "`"))
|
||||
if col != "" {
|
||||
columns = append(columns, col)
|
||||
}
|
||||
}
|
||||
return columns
|
||||
}
|
||||
|
||||
func countRowsByIssue(t *testing.T, ctx context.Context, db *sql.DB, table, issueID string) int {
|
||||
t.Helper()
|
||||
|
||||
var query string
|
||||
switch table {
|
||||
case "labels":
|
||||
query = "SELECT COUNT(*) FROM labels WHERE issue_id = ?"
|
||||
case "dependencies":
|
||||
query = "SELECT COUNT(*) FROM dependencies WHERE issue_id = ?"
|
||||
case "events":
|
||||
query = "SELECT COUNT(*) FROM events WHERE issue_id = ?"
|
||||
case "comments":
|
||||
query = "SELECT COUNT(*) FROM comments WHERE issue_id = ?"
|
||||
default:
|
||||
t.Fatalf("unsupported table in test helper: %s", table)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := db.QueryRowContext(ctx, query, issueID).Scan(&count); err != nil {
|
||||
t.Fatalf("failed to count rows for table=%s issue_id=%s: %v", table, issueID, err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func strPtr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
|
@ -1,428 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
)
|
||||
|
||||
// backupSQLite copies the SQLite database to a timestamped backup file in the
|
||||
// same directory. The original file is preserved. Returns the backup path.
|
||||
func backupSQLite(sqlitePath string) (string, error) {
|
||||
dir := filepath.Dir(sqlitePath)
|
||||
base := filepath.Base(sqlitePath)
|
||||
ext := filepath.Ext(base)
|
||||
name := base[:len(base)-len(ext)]
|
||||
|
||||
timestamp := time.Now().Format("20060102-150405")
|
||||
backupName := fmt.Sprintf("%s.backup-pre-dolt-%s%s", name, timestamp, ext)
|
||||
backupPath := filepath.Join(dir, backupName)
|
||||
|
||||
// If backup already exists (same second), add counter suffix.
|
||||
if _, err := os.Stat(backupPath); err == nil {
|
||||
for i := 1; i <= 100; i++ {
|
||||
backupName = fmt.Sprintf("%s.backup-pre-dolt-%s-%d%s", name, timestamp, i, ext)
|
||||
backupPath = filepath.Join(dir, backupName)
|
||||
if _, err := os.Stat(backupPath); err != nil {
|
||||
break // File doesn't exist (or stat error) — use this name
|
||||
}
|
||||
if i == 100 {
|
||||
return "", fmt.Errorf("too many backup files for timestamp %s", timestamp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
src, err := os.Open(sqlitePath) // #nosec G304 - user-provided path
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("opening sqlite database for backup: %w", err)
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
dst, err := os.OpenFile(backupPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) // #nosec G304 - derived from user path; O_EXCL prevents TOCTOU
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("creating backup file: %w", err)
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, src); err != nil {
|
||||
return "", fmt.Errorf("copying sqlite database: %w", err)
|
||||
}
|
||||
|
||||
return backupPath, nil
|
||||
}
|
||||
|
||||
// doltSystemDatabases are databases that Dolt/MySQL always creates.
|
||||
// These are not user databases and should be ignored when checking
|
||||
// for cross-project conflicts.
|
||||
var doltSystemDatabases = map[string]bool{
|
||||
"information_schema": true,
|
||||
"mysql": true,
|
||||
"performance_schema": true,
|
||||
"sys": true,
|
||||
}
|
||||
|
||||
// verifyServerTarget checks whether a Dolt server on the given port is an
|
||||
// appropriate target for the expected database. It queries SHOW DATABASES
|
||||
// and verifies the expected database name against what the server hosts.
|
||||
//
|
||||
// Returns nil if:
|
||||
// - No server is listening (connection refused — will be started later)
|
||||
// - Server is running and already hosts the expected database (idempotent)
|
||||
// - Server is running with only system databases (fresh server)
|
||||
// - Server is running with other user databases (shared server model —
|
||||
// logs the database list for diagnostics)
|
||||
//
|
||||
// Returns error if:
|
||||
// - Server is unreachable (timeout) or unqueryable (not a Dolt server)
|
||||
// - Port is non-zero but expectedDBName is empty
|
||||
func verifyServerTarget(expectedDBName string, port int, user string, password string) error {
|
||||
if port == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if expectedDBName == "" {
|
||||
return fmt.Errorf("empty database name — cannot verify server target")
|
||||
}
|
||||
|
||||
host := "127.0.0.1"
|
||||
addr := net.JoinHostPort(host, fmt.Sprintf("%d", port))
|
||||
|
||||
// Check if anything is listening on the port
|
||||
conn, err := net.DialTimeout("tcp", addr, 2*time.Second)
|
||||
if err != nil {
|
||||
// Connection refused = no server running = safe to proceed.
|
||||
// But timeouts or other errors = unknown state = warn and abort.
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
if sysErr, ok := opErr.Err.(*os.SyscallError); ok {
|
||||
// On POSIX the syscall is "connect"; on Windows it's "connectex".
|
||||
if sysErr.Syscall == "connect" || sysErr.Syscall == "connectex" {
|
||||
// ECONNREFUSED — no server, safe
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot verify server on port %d (unknown error, not safe to proceed): %w", port, err)
|
||||
}
|
||||
_ = conn.Close()
|
||||
|
||||
// Server is listening. Query SHOW DATABASES to verify target.
|
||||
if user == "" {
|
||||
user = "root"
|
||||
}
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/", user, password, addr)
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("connecting to server on port %d: %w", port, err)
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetConnMaxLifetime(2 * time.Second)
|
||||
|
||||
rows, err := db.Query("SHOW DATABASES")
|
||||
if err != nil {
|
||||
// Can't list databases — might be non-MySQL service or auth issue.
|
||||
// Treat as unsafe since we can't verify.
|
||||
return fmt.Errorf("cannot query databases on port %d (may not be a Dolt server): %w", port, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var userDatabases []string
|
||||
found := false
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
continue
|
||||
}
|
||||
if name == expectedDBName {
|
||||
found = true
|
||||
}
|
||||
if !doltSystemDatabases[name] {
|
||||
userDatabases = append(userDatabases, name)
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
// Our database already exists on this server — verified target match
|
||||
debug.Logf("verifyServerTarget: database %q found on port %d (idempotent)", expectedDBName, port)
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(userDatabases) == 0 {
|
||||
// Only system databases — fresh/clean server, safe to create our database
|
||||
debug.Logf("verifyServerTarget: fresh server on port %d (no user databases), will create %q", port, expectedDBName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Server has user databases but NOT ours. In the shared server model
|
||||
// (Gas Town), this is expected — one Dolt server hosts many project
|
||||
// databases. Log the database list for diagnostics in case migration
|
||||
// writes to the wrong server.
|
||||
debug.Logf("verifyServerTarget: server on port %d has databases %v but not %q — "+
|
||||
"will create database (shared server model)", port, userDatabases, expectedDBName)
|
||||
fmt.Fprintf(os.Stderr, " Note: Dolt server on port %d already hosts %d database(s); will create %q\n",
|
||||
port, len(userDatabases), expectedDBName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyMigrationCounts compares source (SQLite) row counts against target
|
||||
// (Dolt) row counts. Dolt counts must be >= source counts since there may be
|
||||
// pre-existing issues in the Dolt database.
|
||||
func verifyMigrationCounts(sourceIssueCount, sourceDepsCount, doltIssueCount, doltDepsCount int) error {
|
||||
var errs []string
|
||||
if doltIssueCount < sourceIssueCount {
|
||||
errs = append(errs, fmt.Sprintf(
|
||||
"issue count mismatch: source=%d, dolt=%d", sourceIssueCount, doltIssueCount,
|
||||
))
|
||||
}
|
||||
if doltDepsCount < sourceDepsCount {
|
||||
errs = append(errs, fmt.Sprintf(
|
||||
"dependency count mismatch: source=%d, dolt=%d", sourceDepsCount, doltDepsCount,
|
||||
))
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("migration verification failed: %s", strings.Join(errs, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyMigrationData connects to the Dolt server independently (not via the
|
||||
// DoltStore) and verifies that the migrated data matches the source. This
|
||||
// catches issues that count-only verification misses: wrong server target,
|
||||
// corrupted imports, partial writes.
|
||||
//
|
||||
// Checks performed:
|
||||
// 1. Issue count matches source
|
||||
// 2. Dependency count matches source
|
||||
// 3. Spot-check: first and last issue IDs and titles match
|
||||
func verifyMigrationData(sourceData *migrationData, dbName string, host string, port int, user string, password string) error {
|
||||
if sourceData.issueCount == 0 {
|
||||
// Empty database — nothing to verify
|
||||
return nil
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", user, password, net.JoinHostPort(host, fmt.Sprintf("%d", port)), dbName)
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("connecting to Dolt for verification: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetConnMaxLifetime(5 * time.Second)
|
||||
|
||||
// 1. Verify issue count
|
||||
var doltIssueCount int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM issues").Scan(&doltIssueCount); err != nil {
|
||||
return fmt.Errorf("querying Dolt issue count: %w", err)
|
||||
}
|
||||
if doltIssueCount < sourceData.issueCount {
|
||||
return fmt.Errorf("issue count mismatch: source=%d, dolt=%d", sourceData.issueCount, doltIssueCount)
|
||||
}
|
||||
|
||||
// 2. Verify dependency count
|
||||
var doltDepsCount int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM dependencies").Scan(&doltDepsCount); err != nil {
|
||||
// dependencies table might not exist in very old schemas
|
||||
debug.Logf("verifyMigrationData: dependencies count query failed (non-fatal): %v", err)
|
||||
} else {
|
||||
sourceDepsCount := 0
|
||||
for _, deps := range sourceData.depsMap {
|
||||
sourceDepsCount += len(deps)
|
||||
}
|
||||
if doltDepsCount < sourceDepsCount {
|
||||
return fmt.Errorf("dependency count mismatch: source=%d, dolt=%d", sourceDepsCount, doltDepsCount)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Spot-check: verify first and last issue by ID and title
|
||||
if len(sourceData.issues) > 0 {
|
||||
firstSource := sourceData.issues[0]
|
||||
lastSource := sourceData.issues[len(sourceData.issues)-1]
|
||||
|
||||
// Check first issue
|
||||
var doltTitle string
|
||||
err := db.QueryRow("SELECT title FROM issues WHERE id = ?", firstSource.ID).Scan(&doltTitle)
|
||||
if err != nil {
|
||||
return fmt.Errorf("spot-check failed: first issue %q not found in Dolt: %w", firstSource.ID, err)
|
||||
}
|
||||
if doltTitle != firstSource.Title {
|
||||
return fmt.Errorf("spot-check failed: first issue %q title mismatch: source=%q, dolt=%q",
|
||||
firstSource.ID, firstSource.Title, doltTitle)
|
||||
}
|
||||
|
||||
// Check last issue (only if different from first)
|
||||
if lastSource.ID != firstSource.ID {
|
||||
err = db.QueryRow("SELECT title FROM issues WHERE id = ?", lastSource.ID).Scan(&doltTitle)
|
||||
if err != nil {
|
||||
return fmt.Errorf("spot-check failed: last issue %q not found in Dolt: %w", lastSource.ID, err)
|
||||
}
|
||||
if doltTitle != lastSource.Title {
|
||||
return fmt.Errorf("spot-check failed: last issue %q title mismatch: source=%q, dolt=%q",
|
||||
lastSource.ID, lastSource.Title, doltTitle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug.Logf("verifyMigrationData: verified %d issues, spot-checked first/last titles", doltIssueCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
// migrationParams holds the configuration needed for the shared migration
|
||||
// safety phases (verify → import → commit → verify → finalize).
|
||||
type migrationParams struct {
|
||||
beadsDir string
|
||||
sqlitePath string
|
||||
backupPath string
|
||||
data *migrationData
|
||||
doltCfg *dolt.Config
|
||||
dbName string
|
||||
// Server connection info for independent verification
|
||||
serverHost string
|
||||
serverPort int
|
||||
serverUser string
|
||||
serverPassword string
|
||||
}
|
||||
|
||||
// runMigrationPhases runs the common post-extraction migration phases shared
|
||||
// by both the CGO (migrate_auto.go) and shim (migrate_shim.go) paths:
|
||||
//
|
||||
// 1. Verify server target (ensure we're writing to the right Dolt server)
|
||||
// 2. Create Dolt store and import data
|
||||
// 3. Set sync mode and commit
|
||||
// 4. Verify migration data (independent re-query with spot-checks)
|
||||
// 5. Finalize (update metadata, rename SQLite)
|
||||
//
|
||||
// On failure, performs rollback: removes dolt dir and restores original config.
|
||||
// Returns (imported, skipped) counts on success, or error on failure.
|
||||
func runMigrationPhases(ctx context.Context, params *migrationParams) (imported int, skipped int, err error) {
|
||||
beadsDir := params.beadsDir
|
||||
sqlitePath := params.sqlitePath
|
||||
backupPath := params.backupPath
|
||||
data := params.data
|
||||
doltCfg := params.doltCfg
|
||||
dbName := params.dbName
|
||||
|
||||
// Verify server target — don't write to the wrong Dolt server
|
||||
if err := verifyServerTarget(dbName, params.serverPort, params.serverUser, params.serverPassword); err != nil {
|
||||
return 0, 0, fmt.Errorf("server check failed: %w\n\nTo fix:\n"+
|
||||
" 1. Stop the other project's Dolt server\n"+
|
||||
" 2. Or set BEADS_DOLT_SERVER_PORT to a unique port for this project\n"+
|
||||
" Your SQLite database is intact. Backup at: %s", err, backupPath)
|
||||
}
|
||||
|
||||
// Save original config for rollback
|
||||
originalCfg, _ := configfile.Load(beadsDir)
|
||||
|
||||
// Create Dolt store and import
|
||||
doltPath := doltCfg.Path
|
||||
fmt.Fprintf(os.Stderr, "Creating Dolt database...\n")
|
||||
doltStore, err := dolt.New(ctx, doltCfg)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("dolt init failed: %w\n"+
|
||||
"Hint: ensure the Dolt server is running, then retry any bd command\n"+
|
||||
" Your SQLite database is intact. Backup at: %s", err, backupPath)
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Importing data...\n")
|
||||
imported, skipped, importErr := importToDolt(ctx, doltStore, data)
|
||||
if importErr != nil {
|
||||
_ = doltStore.Close()
|
||||
_ = os.RemoveAll(doltPath) // Safe: doltPath was just created by us
|
||||
return 0, 0, fmt.Errorf("import failed: %w\n"+
|
||||
" Your SQLite database is intact. Backup at: %s", importErr, backupPath)
|
||||
}
|
||||
|
||||
// Set sync mode in Dolt config table
|
||||
if err := doltStore.SetConfig(ctx, "sync.mode", "dolt-native"); err != nil {
|
||||
debug.Logf("migration: failed to set sync.mode: %v", err)
|
||||
}
|
||||
|
||||
// Commit the migration
|
||||
commitMsg := fmt.Sprintf("Auto-migrate from SQLite: %d issues imported", imported)
|
||||
if err := doltStore.Commit(ctx, commitMsg); err != nil {
|
||||
debug.Logf("migration: failed to create Dolt commit: %v", err)
|
||||
}
|
||||
|
||||
_ = doltStore.Close()
|
||||
|
||||
// Verify migration data by independently querying the Dolt server.
|
||||
// This catches issues that the import return values alone cannot:
|
||||
// wrong server target, partial writes, data corruption.
|
||||
if err := verifyMigrationData(data, dbName, params.serverHost, params.serverPort, params.serverUser, params.serverPassword); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: migration verification failed: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, " Your SQLite database is intact. Backup at: %s\n", backupPath)
|
||||
// Rollback: remove dolt dir, restore metadata
|
||||
_ = os.RemoveAll(doltPath)
|
||||
if originalCfg != nil {
|
||||
if rbErr := rollbackMetadata(beadsDir, originalCfg); rbErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: metadata rollback also failed: %v\n", rbErr)
|
||||
}
|
||||
}
|
||||
return 0, 0, fmt.Errorf("verification failed: %w", err)
|
||||
}
|
||||
|
||||
// Finalize — update metadata and rename SQLite (atomic cutover)
|
||||
if err := finalizeMigration(beadsDir, sqlitePath, dbName); err != nil {
|
||||
return imported, skipped, fmt.Errorf("finalization failed: %w\n"+
|
||||
" Data is in Dolt but metadata may be inconsistent.\n"+
|
||||
" Run 'bd doctor --fix' to repair.", err)
|
||||
}
|
||||
|
||||
return imported, skipped, nil
|
||||
}
|
||||
|
||||
// finalizeMigration updates metadata and config to reflect the completed
|
||||
// migration, then renames the SQLite file. This is the ONLY function that
|
||||
// modifies metadata and should be called last after verification succeeds.
|
||||
func finalizeMigration(beadsDir string, sqlitePath string, dbName string) error {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading metadata: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
|
||||
cfg.Backend = configfile.BackendDolt
|
||||
cfg.Database = "dolt"
|
||||
cfg.DoltDatabase = dbName
|
||||
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
return fmt.Errorf("saving metadata: %w", err)
|
||||
}
|
||||
|
||||
// Write sync.mode to config.yaml so future runs know we migrated.
|
||||
// This is best-effort: config package may not be initialized during
|
||||
// auto-migration (which runs before full CLI setup). The metadata.json
|
||||
// Backend field is the authoritative source of truth.
|
||||
if err := config.SaveConfigValue("sync.mode", string(config.SyncModeDoltNative), beadsDir); err != nil {
|
||||
// Non-fatal — metadata.json is already updated
|
||||
debug.Logf("finalizeMigration: config.yaml sync.mode write skipped: %v", err)
|
||||
}
|
||||
|
||||
// Rename SQLite file to mark it as migrated.
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if err := os.Rename(sqlitePath, migratedPath); err != nil {
|
||||
return fmt.Errorf("renaming sqlite database: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// rollbackMetadata restores metadata.json to the original configuration.
|
||||
// Called when migration fails after metadata was partially modified.
|
||||
func rollbackMetadata(beadsDir string, originalCfg *configfile.Config) error {
|
||||
if originalCfg == nil {
|
||||
return fmt.Errorf("no original config to restore")
|
||||
}
|
||||
return originalCfg.Save(beadsDir)
|
||||
}
|
||||
|
|
@ -1,521 +0,0 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
"github.com/steveyegge/beads/cmd/bd/doctor"
|
||||
"github.com/steveyegge/beads/cmd/bd/doctor/fix"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// Integration tests for the SQLite→Dolt migration safety features.
|
||||
// These test the full lifecycle using the test Dolt server infrastructure.
|
||||
// Fixes GH#2016, GH#2086.
|
||||
|
||||
// TestMigrationSafety_FullLifecycle tests the complete migration flow:
|
||||
// SQLite → backup → extract → import → verify counts → finalize → bd works.
|
||||
func TestMigrationSafety_FullLifecycle(t *testing.T) {
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Setup: legacy metadata (no backend field) + SQLite with data
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "", // legacy — triggers auto-migration
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "lifecycle", 5)
|
||||
|
||||
// Act: run migration
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify 1: backup was created
|
||||
backupFound := false
|
||||
entries, _ := os.ReadDir(beadsDir)
|
||||
for _, e := range entries {
|
||||
if strings.Contains(e.Name(), "backup-pre-dolt-") {
|
||||
backupFound = true
|
||||
// Verify backup has content (not empty)
|
||||
info, _ := e.Info()
|
||||
if info.Size() == 0 {
|
||||
t.Error("backup file is empty")
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !backupFound {
|
||||
t.Error("no backup file created during migration")
|
||||
}
|
||||
|
||||
// Verify 2: beads.db renamed to .migrated
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed to .migrated")
|
||||
}
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
t.Errorf("beads.db.migrated should exist: %v", err)
|
||||
}
|
||||
|
||||
// Verify 3: metadata.json updated correctly
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load updated config: %v", err)
|
||||
}
|
||||
if updatedCfg.Backend != configfile.BackendDolt {
|
||||
t.Errorf("backend should be 'dolt', got %q", updatedCfg.Backend)
|
||||
}
|
||||
if updatedCfg.Database != "dolt" {
|
||||
t.Errorf("database should be 'dolt', got %q", updatedCfg.Database)
|
||||
}
|
||||
if updatedCfg.DoltDatabase != "lifecycle" {
|
||||
t.Errorf("dolt_database should be 'lifecycle', got %q", updatedCfg.DoltDatabase)
|
||||
}
|
||||
|
||||
// Verify 4: server config preserved through migration
|
||||
if updatedCfg.DoltServerHost != "127.0.0.1" {
|
||||
t.Errorf("DoltServerHost should be preserved, got %q", updatedCfg.DoltServerHost)
|
||||
}
|
||||
if updatedCfg.DoltServerPort != testDoltServerPort {
|
||||
t.Errorf("DoltServerPort should be preserved (%d), got %d", testDoltServerPort, updatedCfg.DoltServerPort)
|
||||
}
|
||||
|
||||
// Clean up test database
|
||||
dropTestDatabase("lifecycle", testDoltServerPort)
|
||||
}
|
||||
|
||||
// TestMigrationSafety_FailedMigrationRecovery tests that when migration fails
|
||||
// (corrupted SQLite), the database is left in a recoverable state.
|
||||
func TestMigrationSafety_FailedMigrationRecovery(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Setup: legacy metadata + corrupted SQLite file
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "", // legacy
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("this is not a sqlite database but has enough bytes"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Act: attempt migration (should fail during extract)
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify 1: backup was created BEFORE the failure
|
||||
backupFound := false
|
||||
entries, _ := os.ReadDir(beadsDir)
|
||||
for _, e := range entries {
|
||||
if strings.Contains(e.Name(), "backup-pre-dolt-") {
|
||||
backupFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !backupFound {
|
||||
t.Error("backup should be created even when migration fails")
|
||||
}
|
||||
|
||||
// Verify 2: SQLite file is intact (NOT renamed)
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("beads.db should still exist after failed migration")
|
||||
}
|
||||
|
||||
// Verify 3: metadata.json is unchanged (backend NOT set to dolt)
|
||||
loadedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loadedCfg.Backend != "" {
|
||||
t.Errorf("backend should be unchanged (''), got %q after failed migration", loadedCfg.Backend)
|
||||
}
|
||||
|
||||
// Verify 4: no dolt/ directory created
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt")); !os.IsNotExist(err) {
|
||||
t.Error("dolt/ should not exist after failed migration")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationSafety_DoctorDetectsAndFixesBrokenState tests the full doctor
|
||||
// cycle: detect broken migration → fix → metadata restored.
|
||||
func TestMigrationSafety_DoctorDetectsAndFixesBrokenState(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Setup: simulate a broken migration state
|
||||
// metadata says dolt but no dolt/ dir and SQLite still exists
|
||||
cfg := &configfile.Config{
|
||||
Database: "dolt",
|
||||
Backend: configfile.BackendDolt,
|
||||
DoltDatabase: "broken-project",
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create a SQLite DB with real data (the data that would be lost without recovery)
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "broken", 3)
|
||||
|
||||
// Act 1: Doctor should detect broken state
|
||||
check := doctor.CheckBrokenMigrationState(tmpDir)
|
||||
if check.Status != doctor.StatusError {
|
||||
t.Errorf("expected error status, got %q: %s", check.Status, check.Message)
|
||||
}
|
||||
if check.Fix == "" {
|
||||
t.Error("expected fix suggestion for broken migration state")
|
||||
}
|
||||
|
||||
// Act 2: Apply fix directly via the fix package
|
||||
if err := fix.BrokenMigrationState(tmpDir); err != nil {
|
||||
t.Fatalf("BrokenMigrationState fix failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify: metadata restored to sqlite
|
||||
fixedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fixedCfg.GetBackend() != configfile.BackendSQLite {
|
||||
t.Errorf("backend should be restored to 'sqlite', got %q", fixedCfg.GetBackend())
|
||||
}
|
||||
if fixedCfg.Database != "beads.db" {
|
||||
t.Errorf("database should be restored to 'beads.db', got %q", fixedCfg.Database)
|
||||
}
|
||||
|
||||
// Verify: SQLite data still intact
|
||||
count, err := countSQLiteIssues(sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to count SQLite issues after fix: %v", err)
|
||||
}
|
||||
if count != 3 {
|
||||
t.Errorf("SQLite should still have 3 issues after fix, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationSafety_DoctorFixesBeadsDBMigrated tests that doctor --fix can
|
||||
// recover from a broken state where beads.db was renamed to .migrated
|
||||
// but the Dolt migration didn't actually complete.
|
||||
func TestMigrationSafety_DoctorFixesBeadsDBMigrated(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Setup: metadata says dolt, beads.db was renamed to .migrated, no dolt/ dir
|
||||
cfg := &configfile.Config{
|
||||
Database: "dolt",
|
||||
Backend: configfile.BackendDolt,
|
||||
DoltDatabase: "stale-project",
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// beads.db.migrated exists (renamed during failed migration), but NO beads.db
|
||||
migratedPath := filepath.Join(beadsDir, "beads.db.migrated")
|
||||
createTestSQLiteDB(t, migratedPath, "stale", 7)
|
||||
|
||||
// Act: Apply fix directly
|
||||
if err := fix.BrokenMigrationState(tmpDir); err != nil {
|
||||
t.Fatalf("BrokenMigrationState fix failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify: beads.db.migrated was renamed back to beads.db
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Errorf("beads.db should exist after fix (renamed from .migrated): %v", err)
|
||||
}
|
||||
|
||||
// Verify: metadata restored
|
||||
fixedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fixedCfg.GetBackend() != configfile.BackendSQLite {
|
||||
t.Errorf("backend should be 'sqlite', got %q", fixedCfg.GetBackend())
|
||||
}
|
||||
|
||||
// Verify: data intact (7 issues)
|
||||
count, err := countSQLiteIssues(sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to count issues: %v", err)
|
||||
}
|
||||
if count != 7 {
|
||||
t.Errorf("expected 7 issues after recovery, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationSafety_IdempotentRetry tests that running migration twice
|
||||
// doesn't duplicate data or lose the backup.
|
||||
func TestMigrationSafety_IdempotentRetry(t *testing.T) {
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Setup: legacy metadata + SQLite with data
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "idempotent", 4)
|
||||
|
||||
// Act 1: first migration
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify first migration succeeded
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Fatal("beads.db should be renamed after first migration")
|
||||
}
|
||||
cfg1, _ := configfile.Load(beadsDir)
|
||||
if cfg1.Backend != configfile.BackendDolt {
|
||||
t.Fatal("backend should be dolt after first migration")
|
||||
}
|
||||
|
||||
// Act 2: run again — should be a no-op (backend already dolt)
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify: metadata unchanged
|
||||
cfg2, _ := configfile.Load(beadsDir)
|
||||
if cfg2.Backend != configfile.BackendDolt {
|
||||
t.Error("backend should still be dolt after second run")
|
||||
}
|
||||
if cfg2.DoltDatabase != "idempotent" {
|
||||
t.Errorf("dolt_database should still be 'idempotent', got %q", cfg2.DoltDatabase)
|
||||
}
|
||||
|
||||
// Verify: backup still exists
|
||||
backupCount := 0
|
||||
entries, _ := os.ReadDir(beadsDir)
|
||||
for _, e := range entries {
|
||||
if strings.Contains(e.Name(), "backup-pre-dolt-") {
|
||||
backupCount++
|
||||
}
|
||||
}
|
||||
if backupCount != 1 {
|
||||
t.Errorf("expected exactly 1 backup file, got %d", backupCount)
|
||||
}
|
||||
|
||||
// Clean up
|
||||
dropTestDatabase("idempotent", testDoltServerPort)
|
||||
}
|
||||
|
||||
// TestMigrationSafety_SQLiteGuardPreventsAutoMigration tests that setting
|
||||
// backend=sqlite in metadata.json prevents auto-migration.
|
||||
func TestMigrationSafety_SQLiteGuardPreventsAutoMigration(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Setup: explicit sqlite backend + SQLite with data
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: configfile.BackendSQLite,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "guarded", 3)
|
||||
|
||||
// Act: attempt auto-migration
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Verify: SQLite is untouched
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("beads.db should still exist when backend=sqlite")
|
||||
}
|
||||
|
||||
// Verify: no backup created (migration didn't even start)
|
||||
entries, _ := os.ReadDir(beadsDir)
|
||||
for _, e := range entries {
|
||||
if strings.Contains(e.Name(), "backup-pre-dolt-") {
|
||||
t.Error("no backup should be created when backend=sqlite — migration should be skipped entirely")
|
||||
}
|
||||
}
|
||||
|
||||
// Verify: metadata unchanged
|
||||
loadedCfg, _ := configfile.Load(beadsDir)
|
||||
if loadedCfg.GetBackend() != configfile.BackendSQLite {
|
||||
t.Errorf("backend should still be 'sqlite', got %q", loadedCfg.GetBackend())
|
||||
}
|
||||
|
||||
// Verify: no dolt/ directory
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt")); !os.IsNotExist(err) {
|
||||
t.Error("dolt/ should not exist when backend=sqlite")
|
||||
}
|
||||
|
||||
// Verify: data intact
|
||||
count, err := countSQLiteIssues(sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 3 {
|
||||
t.Errorf("expected 3 issues intact, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationSafety_BackupContentMatchesSource verifies that the backup
|
||||
// created during migration is a byte-for-byte copy of the original SQLite.
|
||||
func TestMigrationSafety_BackupContentMatchesSource(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "backup-verify", 3)
|
||||
|
||||
// Read original content before migration touches anything
|
||||
originalContent, err := os.ReadFile(sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create backup using our safety function
|
||||
backupPath, err := backupSQLite(sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify: backup is byte-for-byte identical
|
||||
backupContent, err := os.ReadFile(backupPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(originalContent) != len(backupContent) {
|
||||
t.Errorf("backup size (%d) != original size (%d)", len(backupContent), len(originalContent))
|
||||
}
|
||||
|
||||
for i := range originalContent {
|
||||
if originalContent[i] != backupContent[i] {
|
||||
t.Errorf("backup differs at byte %d", i)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Verify: original still exists
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("original should still exist after backup")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrationSafety_MetadataJSONIsValidAfterMigration verifies the
|
||||
// migrated metadata.json is well-formed JSON with all expected fields.
|
||||
func TestMigrationSafety_MetadataJSONIsValidAfterMigration(t *testing.T) {
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
Backend: "",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "jsonvalid", 2)
|
||||
|
||||
doAutoMigrateSQLiteToDolt(beadsDir)
|
||||
|
||||
// Read and validate metadata.json
|
||||
data, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Must be valid JSON
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
t.Errorf("metadata.json is not valid JSON: %v\nContent: %s", err, data)
|
||||
}
|
||||
|
||||
// Must have required fields
|
||||
for _, key := range []string{"database", "backend", "dolt_database"} {
|
||||
if _, ok := result[key]; !ok {
|
||||
t.Errorf("metadata.json missing required field %q", key)
|
||||
}
|
||||
}
|
||||
|
||||
// backend must be "dolt"
|
||||
if result["backend"] != "dolt" {
|
||||
t.Errorf("backend = %v, want 'dolt'", result["backend"])
|
||||
}
|
||||
|
||||
// dolt_database must match prefix
|
||||
if result["dolt_database"] != "jsonvalid" {
|
||||
t.Errorf("dolt_database = %v, want 'jsonvalid'", result["dolt_database"])
|
||||
}
|
||||
|
||||
dropTestDatabase("jsonvalid", testDoltServerPort)
|
||||
}
|
||||
|
||||
// countSQLiteIssues opens a SQLite database and counts the issues.
|
||||
func countSQLiteIssues(dbPath string) (int, error) {
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var count int
|
||||
err = db.QueryRow("SELECT COUNT(*) FROM issues").Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
|
@ -1,599 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// --- backupSQLite tests ---
|
||||
|
||||
func TestBackupSQLite_CounterOverflow(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "beads.db")
|
||||
if err := os.WriteFile(dbPath, []byte("data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create the base backup and all counter variants 1-100
|
||||
// to exhaust the counter loop
|
||||
first, err := backupSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("first backup failed: %v", err)
|
||||
}
|
||||
// Extract timestamp from first backup name
|
||||
base := filepath.Base(first)
|
||||
// Pattern: beads.backup-pre-dolt-YYYYMMDD-HHMMSS.db
|
||||
parts := strings.SplitN(base, "backup-pre-dolt-", 2)
|
||||
if len(parts) != 2 {
|
||||
t.Fatalf("unexpected backup name: %s", base)
|
||||
}
|
||||
timestamp := strings.TrimSuffix(parts[1], ".db")
|
||||
|
||||
// Create counter files 1-100
|
||||
for i := 1; i <= 100; i++ {
|
||||
name := fmt.Sprintf("beads.backup-pre-dolt-%s-%d.db", timestamp, i)
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte("x"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Next backup attempt should fail with "too many backup files"
|
||||
_, err = backupSQLite(dbPath)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when all counter slots are taken")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "too many backup files") {
|
||||
t.Errorf("expected 'too many backup files' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupSQLite_NoExtension(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "beadsdb") // no extension
|
||||
if err := os.WriteFile(dbPath, []byte("data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
backupPath, err := backupSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("backupSQLite failed: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(filepath.Base(backupPath), "backup-pre-dolt-") {
|
||||
t.Errorf("backup name %q missing expected pattern", filepath.Base(backupPath))
|
||||
}
|
||||
|
||||
got, _ := os.ReadFile(backupPath)
|
||||
if string(got) != "data" {
|
||||
t.Errorf("backup content = %q, want %q", got, "data")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupSQLite_CreatesBackup(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "beads.db")
|
||||
content := []byte("sqlite-test-data")
|
||||
if err := os.WriteFile(dbPath, content, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
backupPath, err := backupSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("backupSQLite failed: %v", err)
|
||||
}
|
||||
|
||||
// Original still exists.
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
t.Fatalf("original file missing after backup: %v", err)
|
||||
}
|
||||
|
||||
// Backup exists and has correct content.
|
||||
got, err := os.ReadFile(backupPath)
|
||||
if err != nil {
|
||||
t.Fatalf("reading backup: %v", err)
|
||||
}
|
||||
if string(got) != string(content) {
|
||||
t.Errorf("backup content = %q, want %q", got, content)
|
||||
}
|
||||
|
||||
// Backup name contains expected pattern.
|
||||
if !strings.Contains(filepath.Base(backupPath), "backup-pre-dolt-") {
|
||||
t.Errorf("backup name %q missing expected pattern", filepath.Base(backupPath))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupSQLite_NonexistentFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "nonexistent.db")
|
||||
|
||||
_, err := backupSQLite(dbPath)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent file, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackupSQLite_BackupAlreadyExists(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "beads.db")
|
||||
if err := os.WriteFile(dbPath, []byte("data"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create first backup.
|
||||
first, err := backupSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("first backup failed: %v", err)
|
||||
}
|
||||
|
||||
// Create second backup (same second — should get counter suffix or different timestamp).
|
||||
second, err := backupSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("second backup failed: %v", err)
|
||||
}
|
||||
|
||||
if first == second {
|
||||
t.Errorf("two backups have identical paths: %s", first)
|
||||
}
|
||||
|
||||
// Both files should exist.
|
||||
for _, p := range []string{first, second} {
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
t.Errorf("backup missing: %s", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- verifyServerTarget tests ---
|
||||
|
||||
func TestVerifyServerTarget_NoServerRunning(t *testing.T) {
|
||||
// Use a dynamically allocated port guaranteed to be free
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to allocate port: %v", err)
|
||||
}
|
||||
port := listener.Addr().(*net.TCPAddr).Port
|
||||
listener.Close() // Close immediately — port is now free but known
|
||||
|
||||
err = verifyServerTarget("beads", port, "root", "")
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil for no server on free port %d, got: %v", port, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyServerTarget_PortZero(t *testing.T) {
|
||||
err := verifyServerTarget("beads", 0, "root", "")
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil for port 0, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyServerTarget_EmptyDBName(t *testing.T) {
|
||||
err := verifyServerTarget("", 3307, "root", "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty database name, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "empty database name") {
|
||||
t.Errorf("expected 'empty database name' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyServerTarget_NonMySQLServer(t *testing.T) {
|
||||
// Start a TCP listener that accepts connections but doesn't speak MySQL
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to start test listener: %v", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
port := listener.Addr().(*net.TCPAddr).Port
|
||||
|
||||
// Accept connections in background (just close them immediately)
|
||||
go func() {
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
// verifyServerTarget should return an error (can't query non-MySQL server)
|
||||
err = verifyServerTarget("beads", port, "root", "")
|
||||
if err == nil {
|
||||
t.Error("expected error when connecting to non-MySQL server, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// --- verifyMigrationData tests ---
|
||||
|
||||
func TestVerifyMigrationData_EmptySource(t *testing.T) {
|
||||
// Empty source data should pass without connecting to anything
|
||||
data := &migrationData{issueCount: 0}
|
||||
err := verifyMigrationData(data, "beads", "127.0.0.1", 0, "root", "")
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil for empty source, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMigrationData_ConnectionFailure(t *testing.T) {
|
||||
// Use a port that nothing is listening on
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to allocate port: %v", err)
|
||||
}
|
||||
port := listener.Addr().(*net.TCPAddr).Port
|
||||
listener.Close()
|
||||
|
||||
data := &migrationData{
|
||||
issueCount: 5,
|
||||
issues: make([]*types.Issue, 5),
|
||||
}
|
||||
err = verifyMigrationData(data, "beads", "127.0.0.1", port, "root", "")
|
||||
if err == nil {
|
||||
t.Fatal("expected error when Dolt server is unreachable, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// --- verifyMigrationCounts tests ---
|
||||
|
||||
func TestVerifyMigrationCounts_DepsOnlyMismatch(t *testing.T) {
|
||||
err := verifyMigrationCounts(10, 5, 10, 3)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when dolt has fewer deps, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "dependency count mismatch") {
|
||||
t.Errorf("error should mention dependency count mismatch, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMigrationCounts_BothMismatch(t *testing.T) {
|
||||
err := verifyMigrationCounts(10, 5, 8, 3)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when both counts mismatch, got nil")
|
||||
}
|
||||
// Should report both errors
|
||||
if !strings.Contains(err.Error(), "issue count") {
|
||||
t.Errorf("error should mention issue count, got: %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "dependency count") {
|
||||
t.Errorf("error should mention dependency count, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMigrationCounts_Match(t *testing.T) {
|
||||
err := verifyMigrationCounts(10, 5, 10, 5)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error for matching counts, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMigrationCounts_DoltHasMore(t *testing.T) {
|
||||
err := verifyMigrationCounts(10, 5, 15, 8)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error when dolt has more, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMigrationCounts_DoltHasLess(t *testing.T) {
|
||||
err := verifyMigrationCounts(10, 5, 8, 5)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when dolt has fewer issues, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "issue count mismatch") {
|
||||
t.Errorf("error should mention issue count mismatch, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyMigrationCounts_ZeroSource(t *testing.T) {
|
||||
err := verifyMigrationCounts(0, 0, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error for zero source, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- finalizeMigration tests ---
|
||||
|
||||
// setupBeadsDir creates a temp directory with a .beads subdirectory containing
|
||||
// a default metadata.json and an empty config.yaml. It also initializes
|
||||
// the config package's viper instance via BEADS_DIR env var.
|
||||
func setupBeadsDir(t *testing.T) (tmpDir, beadsDir, sqlitePath string) {
|
||||
t.Helper()
|
||||
|
||||
tmpDir = t.TempDir()
|
||||
beadsDir = filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create metadata.json with sqlite backend.
|
||||
cfg := configfile.DefaultConfig()
|
||||
cfg.Backend = configfile.BackendSQLite
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create config.yaml (empty, required by SaveConfigValue).
|
||||
configYaml := filepath.Join(beadsDir, "config.yaml")
|
||||
if err := os.WriteFile(configYaml, []byte(""), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create fake sqlite file.
|
||||
sqlitePath = filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("fake-sqlite"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Initialize config package pointing at our temp beads dir.
|
||||
t.Setenv("BEADS_DIR", beadsDir)
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
if err := config.Initialize(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return tmpDir, beadsDir, sqlitePath
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_UpdatesMetadata(t *testing.T) {
|
||||
_, beadsDir, sqlitePath := setupBeadsDir(t)
|
||||
|
||||
if err := finalizeMigration(beadsDir, sqlitePath, "testdb"); err != nil {
|
||||
t.Fatalf("finalizeMigration failed: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("loading metadata after migration: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Backend != configfile.BackendDolt {
|
||||
t.Errorf("backend = %q, want %q", cfg.Backend, configfile.BackendDolt)
|
||||
}
|
||||
if cfg.Database != "dolt" {
|
||||
t.Errorf("database = %q, want %q", cfg.Database, "dolt")
|
||||
}
|
||||
if cfg.DoltDatabase != "testdb" {
|
||||
t.Errorf("dolt_database = %q, want %q", cfg.DoltDatabase, "testdb")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_RenamesSQLite(t *testing.T) {
|
||||
_, beadsDir, sqlitePath := setupBeadsDir(t)
|
||||
|
||||
if err := finalizeMigration(beadsDir, sqlitePath, "testdb"); err != nil {
|
||||
t.Fatalf("finalizeMigration failed: %v", err)
|
||||
}
|
||||
|
||||
// Original should be gone.
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("sqlite file should have been renamed")
|
||||
}
|
||||
|
||||
// .migrated should exist.
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
t.Errorf("migrated file missing: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_WritesConfigYaml(t *testing.T) {
|
||||
_, beadsDir, sqlitePath := setupBeadsDir(t)
|
||||
|
||||
if err := finalizeMigration(beadsDir, sqlitePath, "testdb"); err != nil {
|
||||
t.Fatalf("finalizeMigration failed: %v", err)
|
||||
}
|
||||
|
||||
configYaml := filepath.Join(beadsDir, "config.yaml")
|
||||
data, err := os.ReadFile(configYaml)
|
||||
if err != nil {
|
||||
t.Fatalf("reading config.yaml: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(string(data), "sync") {
|
||||
t.Errorf("config.yaml should contain sync.mode key, got: %s", data)
|
||||
}
|
||||
if !strings.Contains(string(data), "dolt-native") {
|
||||
t.Errorf("config.yaml should contain dolt-native value for sync.mode, got: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_WritesDoltNativeSyncMode(t *testing.T) {
|
||||
_, beadsDir, sqlitePath := setupBeadsDir(t)
|
||||
|
||||
if err := finalizeMigration(beadsDir, sqlitePath, "testdb"); err != nil {
|
||||
t.Fatalf("finalizeMigration failed: %v", err)
|
||||
}
|
||||
|
||||
configYaml := filepath.Join(beadsDir, "config.yaml")
|
||||
data, err := os.ReadFile(configYaml)
|
||||
if err != nil {
|
||||
t.Fatalf("reading config.yaml: %v", err)
|
||||
}
|
||||
|
||||
// Must write "dolt-native", NOT "dolt" (which is not a valid sync.mode)
|
||||
if !strings.Contains(string(data), "dolt-native") {
|
||||
t.Errorf("config.yaml should contain sync.mode=dolt-native, got: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_PreservesServerConfig(t *testing.T) {
|
||||
_, beadsDir, sqlitePath := setupBeadsDir(t)
|
||||
|
||||
// Set server config fields before finalization
|
||||
cfg, _ := configfile.Load(beadsDir)
|
||||
cfg.DoltMode = configfile.DoltModeServer
|
||||
cfg.DoltServerHost = "192.168.1.50"
|
||||
cfg.DoltServerPort = 13306
|
||||
cfg.DoltServerUser = "beads_admin"
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := finalizeMigration(beadsDir, sqlitePath, "testdb"); err != nil {
|
||||
t.Fatalf("finalizeMigration failed: %v", err)
|
||||
}
|
||||
|
||||
loaded, loadErr := configfile.Load(beadsDir)
|
||||
if loadErr != nil {
|
||||
t.Fatalf("failed to load config: %v", loadErr)
|
||||
}
|
||||
if loaded.DoltMode != configfile.DoltModeServer {
|
||||
t.Errorf("DoltMode should be preserved, got %q", loaded.DoltMode)
|
||||
}
|
||||
if loaded.DoltServerHost != "192.168.1.50" {
|
||||
t.Errorf("DoltServerHost should be preserved, got %q", loaded.DoltServerHost)
|
||||
}
|
||||
if loaded.DoltServerPort != 13306 {
|
||||
t.Errorf("DoltServerPort should be preserved, got %d", loaded.DoltServerPort)
|
||||
}
|
||||
if loaded.DoltServerUser != "beads_admin" {
|
||||
t.Errorf("DoltServerUser should be preserved, got %q", loaded.DoltServerUser)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_NonexistentSQLite(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := configfile.DefaultConfig()
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// config.yaml needed by SaveConfigValue
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(""), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("BEADS_DIR", beadsDir)
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
if err := config.Initialize(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// SQLite file doesn't exist — rename should fail
|
||||
err := finalizeMigration(beadsDir, filepath.Join(beadsDir, "nonexistent.db"), "testdb")
|
||||
if err == nil {
|
||||
t.Error("expected error when SQLite file doesn't exist for rename")
|
||||
}
|
||||
|
||||
// But metadata should still be updated (rename is the last step)
|
||||
loaded, loadErr := configfile.Load(beadsDir)
|
||||
if loadErr != nil {
|
||||
t.Fatalf("failed to load config after partial finalize: %v", loadErr)
|
||||
}
|
||||
if loaded.Backend != configfile.BackendDolt {
|
||||
t.Errorf("metadata should be updated even when rename fails, got backend=%q", loaded.Backend)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinalizeMigration_NoMetadata(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// No metadata.json — should use default config
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("fake"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(""), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("BEADS_DIR", beadsDir)
|
||||
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
||||
if err := config.Initialize(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err := finalizeMigration(beadsDir, sqlitePath, "testdb")
|
||||
if err != nil {
|
||||
t.Fatalf("finalizeMigration with no existing metadata should work: %v", err)
|
||||
}
|
||||
|
||||
loaded, loadErr := configfile.Load(beadsDir)
|
||||
if loadErr != nil {
|
||||
t.Fatalf("failed to load config: %v", loadErr)
|
||||
}
|
||||
if loaded.Backend != configfile.BackendDolt {
|
||||
t.Errorf("backend should be 'dolt', got %q", loaded.Backend)
|
||||
}
|
||||
}
|
||||
|
||||
// --- rollbackMetadata tests ---
|
||||
|
||||
func TestRollbackMetadata_NilConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err := rollbackMetadata(beadsDir, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil config, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no original config") {
|
||||
t.Errorf("expected 'no original config' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollbackMetadata_RestoresOriginal(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Save original config.
|
||||
original := configfile.DefaultConfig()
|
||||
original.Backend = configfile.BackendSQLite
|
||||
original.Database = "beads.db"
|
||||
if err := original.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Modify metadata (simulating partial migration).
|
||||
modified := configfile.DefaultConfig()
|
||||
modified.Backend = configfile.BackendDolt
|
||||
modified.Database = "dolt"
|
||||
if err := modified.Save(beadsDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify it was modified.
|
||||
check, _ := configfile.Load(beadsDir)
|
||||
if check.Backend != configfile.BackendDolt {
|
||||
t.Fatal("setup: metadata should be modified")
|
||||
}
|
||||
|
||||
// Rollback.
|
||||
if err := rollbackMetadata(beadsDir, original); err != nil {
|
||||
t.Fatalf("rollbackMetadata failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify restored.
|
||||
restored, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("loading after rollback: %v", err)
|
||||
}
|
||||
if restored.Backend != configfile.BackendSQLite {
|
||||
t.Errorf("backend = %q, want %q", restored.Backend, configfile.BackendSQLite)
|
||||
}
|
||||
if restored.Database != "beads.db" {
|
||||
t.Errorf("database = %q, want %q", restored.Database, "beads.db")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,728 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// shimMigrateSQLiteToDolt performs automatic SQLite→Dolt migration using the
|
||||
// system sqlite3 CLI to export data as JSON, avoiding any CGO dependency.
|
||||
// This is the v1.0.0 upgrade path for users on SQLite who upgrade to a
|
||||
// Dolt-only bd binary.
|
||||
//
|
||||
// Steps:
|
||||
// 1. Detect beads.db (SQLite) in .beads/ with no Dolt database present
|
||||
// 2. Export all tables to JSON via the system sqlite3 CLI
|
||||
// 3. Create a new Dolt database
|
||||
// 4. Import all data into Dolt
|
||||
// 5. Rename beads.db to beads.db.migrated
|
||||
func shimMigrateSQLiteToDolt() {
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return
|
||||
}
|
||||
doShimMigrate(beadsDir)
|
||||
}
|
||||
|
||||
// doShimMigrate performs the actual migration for the given .beads directory.
|
||||
func doShimMigrate(beadsDir string) {
|
||||
// Check for SQLite database
|
||||
sqlitePath := findSQLiteDB(beadsDir)
|
||||
if sqlitePath == "" {
|
||||
return // No SQLite database, nothing to migrate
|
||||
}
|
||||
|
||||
// Skip backup/migrated files
|
||||
base := filepath.Base(sqlitePath)
|
||||
if strings.Contains(base, ".backup") || strings.Contains(base, ".migrated") {
|
||||
return
|
||||
}
|
||||
|
||||
// Guard: if the file is empty (0 bytes), it's not a real SQLite database.
|
||||
// This happens when a process creates beads.db but crashes before writing.
|
||||
// Remove the empty file to prevent repeated failed migration attempts.
|
||||
if info, err := os.Stat(sqlitePath); err == nil && info.Size() == 0 {
|
||||
debug.Logf("shim-migrate: removing empty %s (not a valid database)", base)
|
||||
_ = os.Remove(sqlitePath)
|
||||
return
|
||||
}
|
||||
|
||||
// Guard: if metadata.json explicitly indicates SQLite backend, the user has
|
||||
// opted to keep SQLite. Do NOT auto-migrate. Fixes GH#2016.
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
// Use GetBackend() for SQLite check (normalizes case, checks env var)
|
||||
if cfg.GetBackend() == configfile.BackendSQLite {
|
||||
debug.Logf("shim-migrate: skipping — backend explicitly set to sqlite")
|
||||
return
|
||||
}
|
||||
// Use raw field for Dolt check: only match when metadata.json was
|
||||
// explicitly written with "dolt" (by a prior migration). Empty/missing
|
||||
// Backend field means legacy pre-Dolt config that needs migration.
|
||||
if strings.EqualFold(cfg.Backend, configfile.BackendDolt) {
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
if err := os.Rename(sqlitePath, migratedPath); err == nil {
|
||||
debug.Logf("shim-migrate: renamed stale %s (backend already dolt)", base)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Check if Dolt already exists — if so, SQLite is leftover from a prior migration
|
||||
doltPath := filepath.Join(beadsDir, "dolt")
|
||||
if _, err := os.Stat(doltPath); err == nil {
|
||||
// Dolt exists alongside SQLite. Rename the leftover SQLite file.
|
||||
migratedPath := sqlitePath + ".migrated"
|
||||
if _, err := os.Stat(migratedPath); err != nil {
|
||||
// No .migrated file yet — rename now
|
||||
if err := os.Rename(sqlitePath, migratedPath); err == nil {
|
||||
debug.Logf("shim-migrate: renamed leftover %s to %s", filepath.Base(sqlitePath), filepath.Base(migratedPath))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Verify sqlite3 CLI is available
|
||||
sqlite3Path, err := exec.LookPath("sqlite3")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration requires the sqlite3 CLI tool\n")
|
||||
fmt.Fprintf(os.Stderr, "Hint: install sqlite3 and retry, or run 'bd migrate --to-dolt' with a CGO-enabled build\n")
|
||||
return
|
||||
}
|
||||
debug.Logf("shim-migrate: using sqlite3 at %s", sqlite3Path)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Phase 1: Backup — ALWAYS backup before touching anything
|
||||
fmt.Fprintf(os.Stderr, "Backing up SQLite database...\n")
|
||||
backupPath, backupErr := backupSQLite(sqlitePath)
|
||||
if backupErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration aborted (backup failed): %v\n", backupErr)
|
||||
fmt.Fprintf(os.Stderr, "Hint: check disk space and permissions, then retry\n")
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, " Backup saved to %s\n", filepath.Base(backupPath))
|
||||
|
||||
// Extract data from SQLite via CLI
|
||||
fmt.Fprintf(os.Stderr, "Extracting data from SQLite (via sqlite3 CLI)...\n")
|
||||
data, err := extractViaSQLiteCLI(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration failed (extract): %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Hint: run 'bd migrate --to-dolt' manually, or remove %s to skip\n", base)
|
||||
fmt.Fprintf(os.Stderr, " Your backup is at: %s\n", backupPath)
|
||||
return
|
||||
}
|
||||
|
||||
if data.issueCount == 0 {
|
||||
debug.Logf("shim-migrate: SQLite database is empty, migrating empty database")
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, " Found %d issues\n", data.issueCount)
|
||||
|
||||
// Determine database name from prefix
|
||||
dbName := "beads"
|
||||
if data.prefix != "" {
|
||||
dbName = data.prefix
|
||||
}
|
||||
|
||||
// Resolve server connection settings
|
||||
resolvedHost := "127.0.0.1"
|
||||
resolvedPort := 0
|
||||
resolvedUser := "root"
|
||||
resolvedPassword := ""
|
||||
resolvedTLS := false
|
||||
autoStart := os.Getenv("GT_ROOT") == "" && os.Getenv("BEADS_DOLT_AUTO_START") != "0"
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
resolvedHost = cfg.GetDoltServerHost()
|
||||
resolvedPort = doltserver.DefaultConfig(beadsDir).Port
|
||||
resolvedUser = cfg.GetDoltServerUser()
|
||||
resolvedPassword = cfg.GetDoltServerPassword()
|
||||
resolvedTLS = cfg.GetDoltServerTLS()
|
||||
}
|
||||
|
||||
// Run shared migration phases: verify → import → commit → verify data → finalize
|
||||
doltPath = filepath.Join(beadsDir, "dolt")
|
||||
params := &migrationParams{
|
||||
beadsDir: beadsDir,
|
||||
sqlitePath: sqlitePath,
|
||||
backupPath: backupPath,
|
||||
data: data,
|
||||
dbName: dbName,
|
||||
serverHost: resolvedHost,
|
||||
serverPort: resolvedPort,
|
||||
serverUser: resolvedUser,
|
||||
serverPassword: resolvedPassword,
|
||||
doltCfg: &dolt.Config{
|
||||
Path: doltPath,
|
||||
Database: dbName,
|
||||
CreateIfMissing: true, // migration creates a new Dolt database
|
||||
ServerHost: resolvedHost,
|
||||
ServerPort: resolvedPort,
|
||||
ServerUser: resolvedUser,
|
||||
ServerPassword: resolvedPassword,
|
||||
ServerTLS: resolvedTLS,
|
||||
AutoStart: autoStart,
|
||||
},
|
||||
}
|
||||
|
||||
imported, skipped, migErr := runMigrationPhases(ctx, params)
|
||||
if migErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: SQLite auto-migration failed: %v\n", migErr)
|
||||
return
|
||||
}
|
||||
|
||||
if skipped > 0 {
|
||||
fmt.Fprintf(os.Stderr, "Migrated %d issues from SQLite to Dolt (%d skipped)\n", imported, skipped)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Migrated %d issues from SQLite to Dolt ✓\n", imported)
|
||||
}
|
||||
}
|
||||
|
||||
// extractViaSQLiteCLI extracts all data from a SQLite database by shelling
|
||||
// out to the system sqlite3 CLI. Each table is queried with .mode json and
|
||||
// the resulting JSON array is parsed into Go structs.
|
||||
func extractViaSQLiteCLI(_ context.Context, dbPath string) (*migrationData, error) {
|
||||
// Verify the file looks like a real SQLite database (check magic bytes)
|
||||
if err := verifySQLiteFile(dbPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Extract config
|
||||
configMap, err := queryJSON(dbPath, "SELECT key, value FROM config")
|
||||
if err != nil {
|
||||
// Config table might not exist in very old databases
|
||||
debug.Logf("shim-migrate: config query failed (non-fatal): %v", err)
|
||||
configMap = nil
|
||||
}
|
||||
|
||||
config := make(map[string]string)
|
||||
prefix := ""
|
||||
for _, row := range configMap {
|
||||
k, _ := row["key"].(string)
|
||||
v, _ := row["value"].(string)
|
||||
if k != "" {
|
||||
config[k] = v
|
||||
}
|
||||
if k == "issue_prefix" {
|
||||
prefix = v
|
||||
}
|
||||
}
|
||||
|
||||
issueCols := queryColumnSet(dbPath, "issues")
|
||||
depCols := queryColumnSet(dbPath, "dependencies")
|
||||
|
||||
// All columns that may be missing in older beads database schemas.
|
||||
// Uses sqliteOptionalTextExpr which checks PRAGMA table_info before querying.
|
||||
// Fixes "no such column" errors for pre-v0.49 databases (GH#2016).
|
||||
opt := func(col, fallback string) string {
|
||||
return sqliteOptionalTextExpr(issueCols, col, fallback)
|
||||
}
|
||||
|
||||
// Extract issues
|
||||
issueQuery := fmt.Sprintf(`
|
||||
SELECT id, %s as content_hash,
|
||||
%s as title, %s as description,
|
||||
%s as design, %s as acceptance_criteria,
|
||||
%s as notes,
|
||||
%s as status, %s as priority,
|
||||
%s as issue_type,
|
||||
%s as assignee, %s as estimated_minutes,
|
||||
%s as created_at, %s as created_by,
|
||||
%s as owner,
|
||||
%s as updated_at, %s as closed_at, %s as external_ref, %s as spec_id,
|
||||
%s as compaction_level,
|
||||
%s as compacted_at, %s as compacted_at_commit,
|
||||
%s as original_size,
|
||||
%s as sender, %s as ephemeral, %s as wisp_type,
|
||||
%s as pinned,
|
||||
%s as is_template, %s as crystallizes,
|
||||
%s as mol_type, %s as work_type,
|
||||
%s as quality_score,
|
||||
%s as source_system, %s as source_repo,
|
||||
%s as close_reason, %s as closed_by_session,
|
||||
%s as event_kind, %s as actor,
|
||||
%s as target, %s as payload,
|
||||
%s as await_type, %s as await_id,
|
||||
%s as timeout_ns, %s as waiters,
|
||||
%s as hook_bead, %s as role_bead,
|
||||
%s as agent_state,
|
||||
%s as last_activity, %s as role_type,
|
||||
%s as rig,
|
||||
%s as due_at, %s as defer_until,
|
||||
%s as metadata
|
||||
FROM issues
|
||||
ORDER BY created_at, id
|
||||
`,
|
||||
opt("content_hash", "''"),
|
||||
opt("title", "''"), opt("description", "''"),
|
||||
opt("design", "''"), opt("acceptance_criteria", "''"),
|
||||
opt("notes", "''"),
|
||||
opt("status", "''"), opt("priority", "0"),
|
||||
opt("issue_type", "''"),
|
||||
opt("assignee", "''"), opt("estimated_minutes", "NULL"),
|
||||
opt("created_at", "''"), opt("created_by", "''"),
|
||||
opt("owner", "''"),
|
||||
opt("updated_at", "''"), opt("closed_at", "NULL"), opt("external_ref", "NULL"), opt("spec_id", "''"),
|
||||
opt("compaction_level", "0"),
|
||||
opt("compacted_at", "''"), opt("compacted_at_commit", "NULL"),
|
||||
opt("original_size", "0"),
|
||||
opt("sender", "''"), opt("ephemeral", "0"), opt("wisp_type", "''"),
|
||||
opt("pinned", "0"),
|
||||
opt("is_template", "0"), opt("crystallizes", "0"),
|
||||
opt("mol_type", "''"), opt("work_type", "''"),
|
||||
opt("quality_score", "NULL"),
|
||||
opt("source_system", "''"), opt("source_repo", "''"),
|
||||
opt("close_reason", "''"), opt("closed_by_session", "''"),
|
||||
opt("event_kind", "''"), opt("actor", "''"),
|
||||
opt("target", "''"), opt("payload", "''"),
|
||||
opt("await_type", "''"), opt("await_id", "''"),
|
||||
opt("timeout_ns", "0"), opt("waiters", "''"),
|
||||
opt("hook_bead", "''"), opt("role_bead", "''"),
|
||||
opt("agent_state", "''"),
|
||||
opt("last_activity", "''"), opt("role_type", "''"),
|
||||
opt("rig", "''"),
|
||||
opt("due_at", "''"), opt("defer_until", "''"),
|
||||
opt("metadata", "'{}'"),
|
||||
)
|
||||
issueRows, err := queryJSON(dbPath, issueQuery)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query issues: %w", err)
|
||||
}
|
||||
|
||||
issues := make([]*types.Issue, 0, len(issueRows))
|
||||
for _, row := range issueRows {
|
||||
issue := parseIssueRow(row)
|
||||
issues = append(issues, issue)
|
||||
}
|
||||
|
||||
// Extract labels
|
||||
labelsMap := make(map[string][]string)
|
||||
labelRows, err := queryJSON(dbPath, "SELECT issue_id, label FROM labels ORDER BY issue_id, label")
|
||||
if err == nil {
|
||||
for _, row := range labelRows {
|
||||
issueID, _ := row["issue_id"].(string)
|
||||
label, _ := row["label"].(string)
|
||||
if issueID != "" && label != "" {
|
||||
labelsMap[issueID] = append(labelsMap[issueID], label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract dependencies
|
||||
depsMap := make(map[string][]*types.Dependency)
|
||||
depOpt := func(col, fallback string) string {
|
||||
return sqliteOptionalTextExpr(depCols, col, fallback)
|
||||
}
|
||||
depQuery := fmt.Sprintf(`
|
||||
SELECT issue_id, depends_on_id, COALESCE(type,'') as type, %s as created_by, COALESCE(created_at,'') as created_at,
|
||||
%s as metadata, %s as thread_id
|
||||
FROM dependencies
|
||||
ORDER BY created_at, issue_id, depends_on_id
|
||||
`, depOpt("created_by", "''"), depOpt("metadata", "'{}'"), depOpt("thread_id", "''"))
|
||||
depRows, err := queryJSON(dbPath, depQuery)
|
||||
if err == nil {
|
||||
for _, row := range depRows {
|
||||
dep := &types.Dependency{
|
||||
IssueID: jsonStr(row, "issue_id"),
|
||||
DependsOnID: jsonStr(row, "depends_on_id"),
|
||||
Type: types.DependencyType(jsonStr(row, "type")),
|
||||
CreatedBy: jsonStr(row, "created_by"),
|
||||
CreatedAt: jsonTime(row, "created_at"),
|
||||
Metadata: strings.TrimSpace(jsonFlexibleText(row, "metadata")),
|
||||
ThreadID: jsonStr(row, "thread_id"),
|
||||
}
|
||||
if dep.Metadata == "" {
|
||||
dep.Metadata = "{}"
|
||||
}
|
||||
if dep.IssueID != "" {
|
||||
depsMap[dep.IssueID] = append(depsMap[dep.IssueID], dep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract events
|
||||
eventsMap := make(map[string][]*types.Event)
|
||||
eventRows, err := queryJSON(dbPath, `
|
||||
SELECT issue_id, COALESCE(event_type,'') as event_type, COALESCE(actor,'') as actor, old_value, new_value, comment, COALESCE(created_at,'') as created_at
|
||||
FROM events
|
||||
ORDER BY created_at, rowid
|
||||
`)
|
||||
if err == nil {
|
||||
for _, row := range eventRows {
|
||||
issueID := jsonStr(row, "issue_id")
|
||||
event := &types.Event{
|
||||
EventType: types.EventType(jsonStr(row, "event_type")),
|
||||
Actor: jsonStr(row, "actor"),
|
||||
CreatedAt: jsonTime(row, "created_at"),
|
||||
}
|
||||
if v := jsonNullableStr(row, "old_value"); v != nil {
|
||||
event.OldValue = v
|
||||
}
|
||||
if v := jsonNullableStr(row, "new_value"); v != nil {
|
||||
event.NewValue = v
|
||||
}
|
||||
if v := jsonNullableStr(row, "comment"); v != nil {
|
||||
event.Comment = v
|
||||
}
|
||||
if issueID != "" {
|
||||
eventsMap[issueID] = append(eventsMap[issueID], event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract comments (legacy table may be absent).
|
||||
commentsMap := make(map[string][]*types.Comment)
|
||||
commentRows, err := queryJSON(dbPath, `
|
||||
SELECT issue_id, COALESCE(author,'') as author, COALESCE(text,'') as text, COALESCE(created_at,'') as created_at
|
||||
FROM comments
|
||||
ORDER BY created_at, rowid
|
||||
`)
|
||||
if err == nil {
|
||||
for _, row := range commentRows {
|
||||
issueID := jsonStr(row, "issue_id")
|
||||
if issueID == "" {
|
||||
continue
|
||||
}
|
||||
commentsMap[issueID] = append(commentsMap[issueID], &types.Comment{
|
||||
IssueID: issueID,
|
||||
Author: jsonStr(row, "author"),
|
||||
Text: jsonStr(row, "text"),
|
||||
CreatedAt: jsonTime(row, "created_at"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Assign labels and dependencies to issues
|
||||
for _, issue := range issues {
|
||||
if labels, ok := labelsMap[issue.ID]; ok {
|
||||
issue.Labels = labels
|
||||
}
|
||||
if deps, ok := depsMap[issue.ID]; ok {
|
||||
issue.Dependencies = deps
|
||||
}
|
||||
}
|
||||
|
||||
return &migrationData{
|
||||
issues: issues,
|
||||
labelsMap: labelsMap,
|
||||
depsMap: depsMap,
|
||||
eventsMap: eventsMap,
|
||||
commentsMap: commentsMap,
|
||||
config: config,
|
||||
prefix: prefix,
|
||||
issueCount: len(issues),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func queryColumnSet(dbPath, table string) map[string]bool {
|
||||
rows, err := queryJSON(dbPath, fmt.Sprintf("PRAGMA table_info(%s)", table))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
columns := make(map[string]bool, len(rows))
|
||||
for _, row := range rows {
|
||||
name := jsonStr(row, "name")
|
||||
if name != "" {
|
||||
columns[name] = true
|
||||
}
|
||||
}
|
||||
return columns
|
||||
}
|
||||
|
||||
// queryJSON runs a SQL query against a SQLite database using the sqlite3 CLI
|
||||
// with JSON output mode. Returns a slice of maps representing each row.
|
||||
func queryJSON(dbPath, query string) ([]map[string]interface{}, error) {
|
||||
// Build sqlite3 command: .mode json + query
|
||||
input := fmt.Sprintf(".mode json\n%s\n", strings.TrimSpace(query))
|
||||
|
||||
cmd := exec.Command("sqlite3", "-readonly", dbPath)
|
||||
cmd.Stdin = strings.NewReader(input)
|
||||
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
return nil, fmt.Errorf("sqlite3 query failed: %s", strings.TrimSpace(string(exitErr.Stderr)))
|
||||
}
|
||||
return nil, fmt.Errorf("sqlite3 query failed: %w", err)
|
||||
}
|
||||
|
||||
// Empty result
|
||||
output := strings.TrimSpace(string(out))
|
||||
if output == "" || output == "[]" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var rows []map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(output), &rows); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse sqlite3 JSON output: %w", err)
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// verifySQLiteFile checks that a file starts with the SQLite magic bytes.
|
||||
func verifySQLiteFile(path string) error {
|
||||
f, err := os.Open(path) //nolint:gosec // path is constructed internally, not from user input
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot open %s: %w", filepath.Base(path), err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
magic := make([]byte, 16)
|
||||
n, err := f.Read(magic)
|
||||
if err != nil || n < 16 {
|
||||
return fmt.Errorf("file too small to be a SQLite database")
|
||||
}
|
||||
|
||||
if string(magic[:15]) != "SQLite format 3" {
|
||||
return fmt.Errorf("file is not a SQLite database (bad magic bytes)")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseIssueRow converts a JSON row map into a types.Issue.
|
||||
func parseIssueRow(row map[string]interface{}) *types.Issue {
|
||||
issue := &types.Issue{
|
||||
ID: jsonStr(row, "id"),
|
||||
ContentHash: jsonStr(row, "content_hash"),
|
||||
Title: jsonStr(row, "title"),
|
||||
Description: jsonStr(row, "description"),
|
||||
Design: jsonStr(row, "design"),
|
||||
AcceptanceCriteria: jsonStr(row, "acceptance_criteria"),
|
||||
Notes: jsonStr(row, "notes"),
|
||||
Status: types.Status(jsonStr(row, "status")),
|
||||
Priority: jsonInt(row, "priority"),
|
||||
IssueType: types.IssueType(jsonStr(row, "issue_type")),
|
||||
Assignee: jsonStr(row, "assignee"),
|
||||
CreatedAt: jsonTime(row, "created_at"),
|
||||
CreatedBy: jsonStr(row, "created_by"),
|
||||
Owner: jsonStr(row, "owner"),
|
||||
UpdatedAt: jsonTime(row, "updated_at"),
|
||||
SpecID: jsonStr(row, "spec_id"),
|
||||
CompactionLevel: jsonInt(row, "compaction_level"),
|
||||
OriginalSize: jsonInt(row, "original_size"),
|
||||
Sender: jsonStr(row, "sender"),
|
||||
Ephemeral: jsonBool(row, "ephemeral"),
|
||||
WispType: types.WispType(jsonStr(row, "wisp_type")),
|
||||
Pinned: jsonBool(row, "pinned"),
|
||||
IsTemplate: jsonBool(row, "is_template"),
|
||||
Crystallizes: jsonBool(row, "crystallizes"),
|
||||
MolType: types.MolType(jsonStr(row, "mol_type")),
|
||||
WorkType: types.WorkType(jsonStr(row, "work_type")),
|
||||
SourceSystem: jsonStr(row, "source_system"),
|
||||
SourceRepo: jsonStr(row, "source_repo"),
|
||||
CloseReason: jsonStr(row, "close_reason"),
|
||||
ClosedBySession: jsonStr(row, "closed_by_session"),
|
||||
EventKind: jsonStr(row, "event_kind"),
|
||||
Actor: jsonStr(row, "actor"),
|
||||
Target: jsonStr(row, "target"),
|
||||
Payload: jsonStr(row, "payload"),
|
||||
AwaitType: jsonStr(row, "await_type"),
|
||||
AwaitID: jsonStr(row, "await_id"),
|
||||
HookBead: jsonStr(row, "hook_bead"),
|
||||
RoleBead: jsonStr(row, "role_bead"),
|
||||
AgentState: types.AgentState(jsonStr(row, "agent_state")),
|
||||
RoleType: jsonStr(row, "role_type"),
|
||||
Rig: jsonStr(row, "rig"),
|
||||
}
|
||||
|
||||
// Nullable fields
|
||||
if v := jsonNullableInt(row, "estimated_minutes"); v != nil {
|
||||
issue.EstimatedMinutes = v
|
||||
}
|
||||
if v := jsonNullableStr(row, "external_ref"); v != nil {
|
||||
issue.ExternalRef = v
|
||||
}
|
||||
if v := jsonNullableStr(row, "compacted_at_commit"); v != nil {
|
||||
issue.CompactedAtCommit = v
|
||||
}
|
||||
if v := jsonNullableFloat32(row, "quality_score"); v != nil {
|
||||
issue.QualityScore = v
|
||||
}
|
||||
issue.Metadata = normalizedJSONBytes(jsonFlexibleText(row, "metadata"))
|
||||
|
||||
// Time fields
|
||||
issue.ClosedAt = parseNullTime(jsonStr(row, "closed_at"))
|
||||
issue.CompactedAt = parseNullTime(jsonStr(row, "compacted_at"))
|
||||
issue.LastActivity = parseNullTime(jsonStr(row, "last_activity"))
|
||||
issue.DueAt = parseNullTime(jsonStr(row, "due_at"))
|
||||
issue.DeferUntil = parseNullTime(jsonStr(row, "defer_until"))
|
||||
|
||||
// Timeout duration
|
||||
issue.Timeout = time.Duration(jsonInt64(row, "timeout_ns"))
|
||||
|
||||
// Waiters
|
||||
waitersJSON := jsonStr(row, "waiters")
|
||||
if waitersJSON != "" {
|
||||
_ = json.Unmarshal([]byte(waitersJSON), &issue.Waiters)
|
||||
}
|
||||
|
||||
return issue
|
||||
}
|
||||
|
||||
// JSON row accessor helpers
|
||||
|
||||
func jsonFlexibleText(row map[string]interface{}, key string) string {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return ""
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
return val
|
||||
case []byte:
|
||||
return string(val)
|
||||
default:
|
||||
data, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
}
|
||||
|
||||
func jsonStr(row map[string]interface{}, key string) string {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return ""
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
return val
|
||||
case float64:
|
||||
// JSON numbers come as float64
|
||||
if val == float64(int64(val)) {
|
||||
return strconv.FormatInt(int64(val), 10)
|
||||
}
|
||||
return strconv.FormatFloat(val, 'f', -1, 64)
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
func jsonNullableStr(row map[string]interface{}, key string) *string {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return nil
|
||||
}
|
||||
s := fmt.Sprintf("%v", v)
|
||||
return &s
|
||||
}
|
||||
|
||||
func jsonInt(row map[string]interface{}, key string) int {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return 0
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
return int(val)
|
||||
case string:
|
||||
i, _ := strconv.Atoi(val)
|
||||
return i
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func jsonInt64(row map[string]interface{}, key string) int64 {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return 0
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
return int64(val)
|
||||
case string:
|
||||
i, _ := strconv.ParseInt(val, 10, 64)
|
||||
return i
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func jsonBool(row map[string]interface{}, key string) bool {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return false
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
return val
|
||||
case float64:
|
||||
return val != 0
|
||||
case string:
|
||||
return val == "1" || val == "true"
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func jsonNullableInt(row map[string]interface{}, key string) *int {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return nil
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
i := int(val)
|
||||
return &i
|
||||
case string:
|
||||
i, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &i
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func jsonTime(row map[string]interface{}, key string) time.Time {
|
||||
s := jsonStr(row, key)
|
||||
if s == "" {
|
||||
return time.Time{}
|
||||
}
|
||||
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, "2006-01-02T15:04:05.999999999Z07:00", "2006-01-02 15:04:05"} {
|
||||
if t, err := time.Parse(layout, s); err == nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func jsonNullableFloat32(row map[string]interface{}, key string) *float32 {
|
||||
v, ok := row[key]
|
||||
if !ok || v == nil {
|
||||
return nil
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case float64:
|
||||
f := float32(val)
|
||||
return &f
|
||||
case string:
|
||||
f, err := strconv.ParseFloat(val, 32)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
f32 := float32(f)
|
||||
return &f32
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
@ -1,525 +0,0 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
// TestShimExtract_NoSQLite verifies the shim is a no-op when no SQLite DB exists.
|
||||
func TestShimExtract_NoSQLite(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
doShimMigrate(beadsDir)
|
||||
// Should return without doing anything — no panic, no error
|
||||
}
|
||||
|
||||
// TestShimExtract_DoltAlreadyExists verifies leftover SQLite is renamed when Dolt exists.
|
||||
func TestShimExtract_DoltAlreadyExists(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("fake"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doShimMigrate(beadsDir)
|
||||
|
||||
// beads.db should be renamed to beads.db.migrated
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed")
|
||||
}
|
||||
if _, err := os.Stat(sqlitePath + ".migrated"); err != nil {
|
||||
t.Errorf("beads.db.migrated should exist: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShimExtract_CorruptedFile verifies graceful handling of a non-SQLite file.
|
||||
func TestShimExtract_CorruptedFile(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
if err := os.WriteFile(sqlitePath, []byte("this is not a sqlite database at all"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
doShimMigrate(beadsDir)
|
||||
|
||||
// beads.db should still exist (migration failed gracefully)
|
||||
if _, err := os.Stat(sqlitePath); err != nil {
|
||||
t.Error("beads.db should still exist after failed migration")
|
||||
}
|
||||
// dolt/ should not exist
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt")); !os.IsNotExist(err) {
|
||||
t.Error("dolt/ should not exist after failed migration")
|
||||
}
|
||||
}
|
||||
|
||||
// TestShimExtract_QueryJSON verifies the sqlite3 CLI JSON extraction works.
|
||||
func TestShimExtract_QueryJSON(t *testing.T) {
|
||||
// Create a real SQLite database using the CGO driver (for test setup)
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "shim", 3)
|
||||
|
||||
// Test queryJSON
|
||||
rows, err := queryJSON(sqlitePath, "SELECT key, value FROM config")
|
||||
if err != nil {
|
||||
t.Fatalf("queryJSON failed: %v", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, row := range rows {
|
||||
k, _ := row["key"].(string)
|
||||
v, _ := row["value"].(string)
|
||||
if k == "issue_prefix" && v == "shim" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("expected config row with key=issue_prefix, value=shim; got %v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShimExtract_ExtractViaSQLiteCLI verifies full extraction from SQLite via CLI.
|
||||
func TestShimExtract_ExtractViaSQLiteCLI(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "ext2", 5)
|
||||
|
||||
ctx := t.Context()
|
||||
data, err := extractViaSQLiteCLI(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractViaSQLiteCLI failed: %v", err)
|
||||
}
|
||||
|
||||
if data.prefix != "ext2" {
|
||||
t.Errorf("expected prefix 'ext2', got %q", data.prefix)
|
||||
}
|
||||
if data.issueCount != 5 {
|
||||
t.Errorf("expected 5 issues, got %d", data.issueCount)
|
||||
}
|
||||
if len(data.issues) != 5 {
|
||||
t.Errorf("expected 5 issues in slice, got %d", len(data.issues))
|
||||
}
|
||||
|
||||
// Verify labels were loaded
|
||||
hasLabels := false
|
||||
for _, issue := range data.issues {
|
||||
if len(issue.Labels) > 0 {
|
||||
hasLabels = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasLabels {
|
||||
t.Error("expected at least one issue to have labels")
|
||||
}
|
||||
|
||||
// Verify config was loaded
|
||||
if data.config["issue_prefix"] != "ext2" {
|
||||
t.Errorf("config should contain issue_prefix=ext2, got %v", data.config)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShimExtract_FullMigration does an end-to-end shim migration with a real Dolt server.
|
||||
func TestShimExtract_FullMigration(t *testing.T) {
|
||||
if testDoltServerPort == 0 {
|
||||
t.Skip("Dolt test server not available, skipping")
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Write metadata.json with server config so migration can connect.
|
||||
// Backend is intentionally left empty (not "sqlite") because doShimMigrate
|
||||
// treats explicit "sqlite" as "user opted to keep SQLite" and skips migration.
|
||||
// Empty backend represents a legacy pre-Dolt config that needs migration.
|
||||
cfg := &configfile.Config{
|
||||
Database: "beads.db",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: testDoltServerPort,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("failed to write test metadata.json: %v", err)
|
||||
}
|
||||
|
||||
// Create SQLite database with test data (using CGO driver for setup)
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "shimmig", 3)
|
||||
|
||||
// Run shim migration
|
||||
doShimMigrate(beadsDir)
|
||||
|
||||
// Verify: beads.db renamed
|
||||
if _, err := os.Stat(sqlitePath); !os.IsNotExist(err) {
|
||||
t.Error("beads.db should have been renamed to .migrated")
|
||||
}
|
||||
if _, err := os.Stat(sqlitePath + ".migrated"); err != nil {
|
||||
t.Errorf("beads.db.migrated should exist: %v", err)
|
||||
}
|
||||
|
||||
// Verify: metadata.json updated
|
||||
updatedCfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load updated config: %v", err)
|
||||
}
|
||||
if updatedCfg.Backend != configfile.BackendDolt {
|
||||
t.Errorf("backend should be 'dolt', got %q", updatedCfg.Backend)
|
||||
}
|
||||
if updatedCfg.DoltDatabase != "shimmig" {
|
||||
t.Errorf("dolt_database should be 'shimmig', got %q", updatedCfg.DoltDatabase)
|
||||
}
|
||||
|
||||
// Verify: config.yaml has sync.mode
|
||||
configYaml := filepath.Join(beadsDir, "config.yaml")
|
||||
if data, err := os.ReadFile(configYaml); err == nil {
|
||||
if !strings.Contains(string(data), "dolt-native") {
|
||||
t.Error("config.yaml should contain sync.mode = dolt-native")
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up Dolt test database
|
||||
dropTestDatabase("shimmig", testDoltServerPort)
|
||||
}
|
||||
|
||||
// TestShimExtract_VerifySQLiteFile checks magic byte validation.
|
||||
func TestShimExtract_VerifySQLiteFile(t *testing.T) {
|
||||
// Valid SQLite file
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "verify", 1)
|
||||
|
||||
if err := verifySQLiteFile(sqlitePath); err != nil {
|
||||
t.Errorf("verifySQLiteFile should succeed for valid DB: %v", err)
|
||||
}
|
||||
|
||||
// Invalid file
|
||||
badPath := filepath.Join(beadsDir, "bad.db")
|
||||
if err := os.WriteFile(badPath, []byte("not a database file at all!!!"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := verifySQLiteFile(badPath); err == nil {
|
||||
t.Error("verifySQLiteFile should fail for non-SQLite file")
|
||||
}
|
||||
|
||||
// Too-small file
|
||||
tinyPath := filepath.Join(beadsDir, "tiny.db")
|
||||
if err := os.WriteFile(tinyPath, []byte("hi"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := verifySQLiteFile(tinyPath); err == nil {
|
||||
t.Error("verifySQLiteFile should fail for tiny file")
|
||||
}
|
||||
}
|
||||
|
||||
// TestShimExtract_ParityWithCGO verifies that the shim extraction produces
|
||||
// the same data as the CGO extractFromSQLite for the same database.
|
||||
func TestShimExtract_ParityWithCGO(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
createTestSQLiteDB(t, sqlitePath, "parity", 5)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Extract via CGO
|
||||
cgoData, err := extractFromSQLite(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractFromSQLite failed: %v", err)
|
||||
}
|
||||
|
||||
// Extract via shim
|
||||
shimData, err := extractViaSQLiteCLI(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractViaSQLiteCLI failed: %v", err)
|
||||
}
|
||||
|
||||
// Compare counts
|
||||
if cgoData.issueCount != shimData.issueCount {
|
||||
t.Errorf("issue count mismatch: CGO=%d, shim=%d", cgoData.issueCount, shimData.issueCount)
|
||||
}
|
||||
if cgoData.prefix != shimData.prefix {
|
||||
t.Errorf("prefix mismatch: CGO=%q, shim=%q", cgoData.prefix, shimData.prefix)
|
||||
}
|
||||
if len(cgoData.labelsMap) != len(shimData.labelsMap) {
|
||||
t.Errorf("labels map size mismatch: CGO=%d, shim=%d", len(cgoData.labelsMap), len(shimData.labelsMap))
|
||||
}
|
||||
if len(cgoData.config) != len(shimData.config) {
|
||||
t.Errorf("config map size mismatch: CGO=%d, shim=%d", len(cgoData.config), len(shimData.config))
|
||||
}
|
||||
|
||||
// Compare individual issues
|
||||
cgoIssues := make(map[string]string)
|
||||
for _, issue := range cgoData.issues {
|
||||
cgoIssues[issue.ID] = issue.Title
|
||||
}
|
||||
for _, issue := range shimData.issues {
|
||||
expected, ok := cgoIssues[issue.ID]
|
||||
if !ok {
|
||||
t.Errorf("shim has issue %s not found in CGO extraction", issue.ID)
|
||||
continue
|
||||
}
|
||||
if issue.Title != expected {
|
||||
t.Errorf("title mismatch for %s: CGO=%q, shim=%q", issue.ID, expected, issue.Title)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestShimExtract_OptionalFieldsAndOrdering(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sqlitePath := filepath.Join(beadsDir, "beads.db")
|
||||
db, err := sql.Open("sqlite3", sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open sqlite db: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
for _, stmt := range []string{
|
||||
`CREATE TABLE config (key TEXT PRIMARY KEY, value TEXT)`,
|
||||
`CREATE TABLE issues (
|
||||
id TEXT PRIMARY KEY,
|
||||
content_hash TEXT DEFAULT '',
|
||||
title TEXT DEFAULT '',
|
||||
description TEXT DEFAULT '',
|
||||
design TEXT DEFAULT '',
|
||||
acceptance_criteria TEXT DEFAULT '',
|
||||
notes TEXT DEFAULT '',
|
||||
status TEXT DEFAULT 'open',
|
||||
priority INTEGER DEFAULT 2,
|
||||
issue_type TEXT DEFAULT 'task',
|
||||
assignee TEXT DEFAULT '',
|
||||
estimated_minutes INTEGER,
|
||||
created_at TEXT DEFAULT '',
|
||||
created_by TEXT DEFAULT '',
|
||||
owner TEXT DEFAULT '',
|
||||
updated_at TEXT DEFAULT '',
|
||||
closed_at TEXT,
|
||||
external_ref TEXT,
|
||||
spec_id TEXT DEFAULT '',
|
||||
compaction_level INTEGER DEFAULT 0,
|
||||
compacted_at TEXT DEFAULT '',
|
||||
compacted_at_commit TEXT,
|
||||
original_size INTEGER DEFAULT 0,
|
||||
sender TEXT DEFAULT '',
|
||||
ephemeral INTEGER DEFAULT 0,
|
||||
wisp_type TEXT DEFAULT '',
|
||||
pinned INTEGER DEFAULT 0,
|
||||
is_template INTEGER DEFAULT 0,
|
||||
crystallizes INTEGER DEFAULT 0,
|
||||
mol_type TEXT DEFAULT '',
|
||||
work_type TEXT DEFAULT '',
|
||||
quality_score REAL,
|
||||
source_system TEXT DEFAULT '',
|
||||
source_repo TEXT DEFAULT '',
|
||||
close_reason TEXT DEFAULT '',
|
||||
closed_by_session TEXT DEFAULT '',
|
||||
event_kind TEXT DEFAULT '',
|
||||
actor TEXT DEFAULT '',
|
||||
target TEXT DEFAULT '',
|
||||
payload TEXT DEFAULT '',
|
||||
await_type TEXT DEFAULT '',
|
||||
await_id TEXT DEFAULT '',
|
||||
timeout_ns INTEGER DEFAULT 0,
|
||||
waiters TEXT DEFAULT '',
|
||||
hook_bead TEXT DEFAULT '',
|
||||
role_bead TEXT DEFAULT '',
|
||||
agent_state TEXT DEFAULT '',
|
||||
last_activity TEXT DEFAULT '',
|
||||
role_type TEXT DEFAULT '',
|
||||
rig TEXT DEFAULT '',
|
||||
due_at TEXT DEFAULT '',
|
||||
defer_until TEXT DEFAULT '',
|
||||
metadata TEXT DEFAULT '{}'
|
||||
)`,
|
||||
`CREATE TABLE labels (issue_id TEXT, label TEXT)`,
|
||||
`CREATE TABLE dependencies (
|
||||
issue_id TEXT,
|
||||
depends_on_id TEXT,
|
||||
type TEXT DEFAULT '',
|
||||
created_by TEXT DEFAULT '',
|
||||
created_at TEXT DEFAULT '',
|
||||
metadata TEXT DEFAULT '{}',
|
||||
thread_id TEXT DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE events (
|
||||
issue_id TEXT,
|
||||
event_type TEXT DEFAULT '',
|
||||
actor TEXT DEFAULT '',
|
||||
old_value TEXT,
|
||||
new_value TEXT,
|
||||
comment TEXT,
|
||||
created_at TEXT DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE comments (
|
||||
issue_id TEXT,
|
||||
author TEXT,
|
||||
text TEXT,
|
||||
created_at TEXT DEFAULT ''
|
||||
)`,
|
||||
} {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("failed to create test schema: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := db.Exec(`INSERT INTO config (key, value) VALUES ('issue_prefix', 'opt')`); err != nil {
|
||||
t.Fatalf("failed to insert config: %v", err)
|
||||
}
|
||||
|
||||
ts := time.Date(2026, 2, 1, 10, 0, 0, 0, time.UTC).Format(time.RFC3339)
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO issues (
|
||||
id, title, created_at, updated_at, status, priority, issue_type,
|
||||
spec_id, wisp_type, closed_by_session, metadata
|
||||
) VALUES
|
||||
('opt-b', 'B issue', ?, ?, 'open', 2, 'task', 'spec-b', 'ping', '', '{"k":"b"}'),
|
||||
('opt-a', 'A issue', ?, ?, 'open', 2, 'task', 'spec-a', 'heartbeat', 'sess-123', '{"k":"a"}')
|
||||
`, ts, ts, ts, ts); err != nil {
|
||||
t.Fatalf("failed to insert issues: %v", err)
|
||||
}
|
||||
|
||||
if _, err := db.Exec(`INSERT INTO labels (issue_id, label) VALUES ('opt-a', 'alpha')`); err != nil {
|
||||
t.Fatalf("failed to insert label: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO dependencies (issue_id, depends_on_id, type, created_by, created_at, metadata, thread_id)
|
||||
VALUES ('opt-a', 'external:rig:42', 'blocks', 'tester', ?, '{"edge":"external"}', 'thread-1')
|
||||
`, ts); err != nil {
|
||||
t.Fatalf("failed to insert dependency: %v", err)
|
||||
}
|
||||
|
||||
// Same created_at timestamp + insertion order should remain stable via ORDER BY created_at,rowid
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO events (issue_id, event_type, actor, comment, created_at)
|
||||
VALUES
|
||||
('opt-a', 'commented', 'actor-b', 'second logical', ?),
|
||||
('opt-a', 'commented', 'actor-a', 'first logical', ?)
|
||||
`, ts, ts); err != nil {
|
||||
t.Fatalf("failed to insert events: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
INSERT INTO comments (issue_id, author, text, created_at)
|
||||
VALUES
|
||||
('opt-a', 'author-b', 'second', ?),
|
||||
('opt-a', 'author-a', 'first', ?)
|
||||
`, ts, ts); err != nil {
|
||||
t.Fatalf("failed to insert comments: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
cgoData, err := extractFromSQLite(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractFromSQLite failed: %v", err)
|
||||
}
|
||||
shimData, err := extractViaSQLiteCLI(ctx, sqlitePath)
|
||||
if err != nil {
|
||||
t.Fatalf("extractViaSQLiteCLI failed: %v", err)
|
||||
}
|
||||
|
||||
if len(cgoData.issues) != 2 || len(shimData.issues) != 2 {
|
||||
t.Fatalf("unexpected issue counts cgo=%d shim=%d", len(cgoData.issues), len(shimData.issues))
|
||||
}
|
||||
if cgoData.issues[0].ID != "opt-a" || shimData.issues[0].ID != "opt-a" {
|
||||
t.Fatalf("issues are not ordered deterministically by id: cgo=%s shim=%s", cgoData.issues[0].ID, shimData.issues[0].ID)
|
||||
}
|
||||
|
||||
var cgoIssueA, shimIssueA *types.Issue
|
||||
for _, issue := range cgoData.issues {
|
||||
if issue.ID == "opt-a" {
|
||||
cgoIssueA = issue
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, issue := range shimData.issues {
|
||||
if issue.ID == "opt-a" {
|
||||
shimIssueA = issue
|
||||
break
|
||||
}
|
||||
}
|
||||
if cgoIssueA == nil || shimIssueA == nil {
|
||||
t.Fatal("opt-a issue not found in extracted data")
|
||||
}
|
||||
if cgoIssueA.SpecID != "spec-a" || shimIssueA.SpecID != "spec-a" {
|
||||
t.Fatalf("spec_id not preserved: cgo=%q shim=%q", cgoIssueA.SpecID, shimIssueA.SpecID)
|
||||
}
|
||||
if cgoIssueA.WispType != "heartbeat" || shimIssueA.WispType != "heartbeat" {
|
||||
t.Fatalf("wisp_type not preserved: cgo=%q shim=%q", cgoIssueA.WispType, shimIssueA.WispType)
|
||||
}
|
||||
if cgoIssueA.ClosedBySession != "sess-123" || shimIssueA.ClosedBySession != "sess-123" {
|
||||
t.Fatalf("closed_by_session not preserved: cgo=%q shim=%q", cgoIssueA.ClosedBySession, shimIssueA.ClosedBySession)
|
||||
}
|
||||
if !json.Valid(cgoIssueA.Metadata) || !json.Valid(shimIssueA.Metadata) {
|
||||
t.Fatal("metadata is not valid json")
|
||||
}
|
||||
|
||||
cgoDeps := cgoData.depsMap["opt-a"]
|
||||
shimDeps := shimData.depsMap["opt-a"]
|
||||
if len(cgoDeps) != 1 || len(shimDeps) != 1 {
|
||||
t.Fatalf("dependency count mismatch: cgo=%d shim=%d", len(cgoDeps), len(shimDeps))
|
||||
}
|
||||
if cgoDeps[0].ThreadID != "thread-1" || shimDeps[0].ThreadID != "thread-1" {
|
||||
t.Fatalf("thread_id not preserved: cgo=%q shim=%q", cgoDeps[0].ThreadID, shimDeps[0].ThreadID)
|
||||
}
|
||||
if cgoDeps[0].Metadata != `{"edge":"external"}` || shimDeps[0].Metadata != `{"edge":"external"}` {
|
||||
t.Fatalf("dependency metadata not preserved: cgo=%q shim=%q", cgoDeps[0].Metadata, shimDeps[0].Metadata)
|
||||
}
|
||||
|
||||
cgoEvents := cgoData.eventsMap["opt-a"]
|
||||
shimEvents := shimData.eventsMap["opt-a"]
|
||||
if len(cgoEvents) != 2 || len(shimEvents) != 2 {
|
||||
t.Fatalf("event count mismatch: cgo=%d shim=%d", len(cgoEvents), len(shimEvents))
|
||||
}
|
||||
if cgoEvents[0].Actor != "actor-b" || shimEvents[0].Actor != "actor-b" {
|
||||
t.Fatalf("event ordering mismatch: cgo_first=%q shim_first=%q", cgoEvents[0].Actor, shimEvents[0].Actor)
|
||||
}
|
||||
|
||||
cgoComments := cgoData.commentsMap["opt-a"]
|
||||
shimComments := shimData.commentsMap["opt-a"]
|
||||
if len(cgoComments) != 2 || len(shimComments) != 2 {
|
||||
t.Fatalf("comment count mismatch: cgo=%d shim=%d", len(cgoComments), len(shimComments))
|
||||
}
|
||||
if cgoComments[0].Author != "author-b" || shimComments[0].Author != "author-b" {
|
||||
t.Fatalf("comment ordering mismatch: cgo_first=%q shim_first=%q", cgoComments[0].Author, shimComments[0].Author)
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ const ConfigFileName = "metadata.json"
|
|||
|
||||
type Config struct {
|
||||
Database string `json:"database"`
|
||||
Backend string `json:"backend,omitempty"` // "dolt" (default) or "sqlite"
|
||||
Backend string `json:"backend,omitempty"` // Deprecated: always "dolt". Kept for JSON compat.
|
||||
|
||||
// Deletions configuration
|
||||
DeletionsRetentionDays int `json:"deletions_retention_days,omitempty"` // 0 means use default (3 days)
|
||||
|
|
@ -114,28 +114,17 @@ func (c *Config) DatabasePath(beadsDir string) string {
|
|||
// Check for custom dolt data directory (absolute path on a faster filesystem).
|
||||
// This is useful on WSL where .beads/ lives on NTFS (slow 9P mount) but
|
||||
// dolt data can be placed on native ext4 for 5-10x I/O speedup.
|
||||
if c.GetBackend() != BackendSQLite {
|
||||
if customDir := c.GetDoltDataDir(); customDir != "" {
|
||||
if filepath.IsAbs(customDir) {
|
||||
return customDir
|
||||
}
|
||||
return filepath.Join(beadsDir, customDir)
|
||||
if customDir := c.GetDoltDataDir(); customDir != "" {
|
||||
if filepath.IsAbs(customDir) {
|
||||
return customDir
|
||||
}
|
||||
return filepath.Join(beadsDir, customDir)
|
||||
}
|
||||
|
||||
if filepath.IsAbs(c.Database) {
|
||||
return c.Database
|
||||
}
|
||||
// For SQLite backend, return the SQLite database path.
|
||||
// Fixes GH#2016: SQLite users must not be forced to Dolt.
|
||||
if c.GetBackend() == BackendSQLite {
|
||||
db := c.Database
|
||||
if db == "" {
|
||||
db = "beads.db"
|
||||
}
|
||||
return filepath.Join(beadsDir, db)
|
||||
}
|
||||
// For Dolt backend, always use "dolt" as the directory name.
|
||||
// Always use "dolt" as the directory name.
|
||||
// Stale values like "town", "wyvern", "beads_rig" caused split-brain (see DOLT-HEALTH-P0.md).
|
||||
return filepath.Join(beadsDir, "dolt")
|
||||
}
|
||||
|
|
@ -162,8 +151,7 @@ func (c *Config) GetStaleClosedIssuesDays() int {
|
|||
|
||||
// Backend constants
|
||||
const (
|
||||
BackendDolt = "dolt"
|
||||
BackendSQLite = "sqlite"
|
||||
BackendDolt = "dolt"
|
||||
)
|
||||
|
||||
// BackendCapabilities describes behavioral constraints for a storage backend.
|
||||
|
|
@ -181,21 +169,10 @@ type BackendCapabilities struct {
|
|||
}
|
||||
|
||||
// CapabilitiesForBackend returns capabilities for a backend string.
|
||||
// Unknown backends are treated conservatively as single-process-only.
|
||||
//
|
||||
// Note: For Dolt, this returns SingleProcessOnly=true by default.
|
||||
// Use Config.GetCapabilities() when you have the full config to properly
|
||||
// handle server mode (which supports multi-process access).
|
||||
func CapabilitiesForBackend(backend string) BackendCapabilities {
|
||||
switch strings.TrimSpace(strings.ToLower(backend)) {
|
||||
case BackendSQLite:
|
||||
return BackendCapabilities{SingleProcessOnly: true}
|
||||
case "", BackendDolt:
|
||||
// Default to single-process-only; server mode overrides via Config.GetCapabilities().
|
||||
return BackendCapabilities{SingleProcessOnly: true}
|
||||
default:
|
||||
return BackendCapabilities{SingleProcessOnly: true}
|
||||
}
|
||||
// Dolt is the only supported backend. Returns SingleProcessOnly=true by default;
|
||||
// use Config.GetCapabilities() to properly handle server mode.
|
||||
func CapabilitiesForBackend(_ string) BackendCapabilities {
|
||||
return BackendCapabilities{SingleProcessOnly: true}
|
||||
}
|
||||
|
||||
// GetCapabilities returns the backend capabilities for this config.
|
||||
|
|
@ -210,28 +187,9 @@ func (c *Config) GetCapabilities() BackendCapabilities {
|
|||
return CapabilitiesForBackend(backend)
|
||||
}
|
||||
|
||||
// GetBackend returns the configured backend type.
|
||||
// Priority: BEADS_BACKEND env var > Backend field in metadata.json > default (dolt).
|
||||
// Fixes GH#2016: existing SQLite databases must be respected.
|
||||
// GetBackend returns the backend type. Always returns "dolt".
|
||||
func (c *Config) GetBackend() string {
|
||||
// Env var override — escape hatch for corrupted configs
|
||||
if env := os.Getenv("BEADS_BACKEND"); env != "" {
|
||||
switch strings.ToLower(env) {
|
||||
case BackendSQLite:
|
||||
return BackendSQLite
|
||||
case BackendDolt:
|
||||
return BackendDolt
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Warning: BEADS_BACKEND=%q is not a recognized backend (use 'sqlite' or 'dolt'), ignoring\n", env)
|
||||
}
|
||||
}
|
||||
// Normalize config field (handles hand-edited metadata.json with "SQLite", "SQLITE", etc.)
|
||||
switch strings.ToLower(c.Backend) {
|
||||
case BackendSQLite:
|
||||
return BackendSQLite
|
||||
default:
|
||||
return BackendDolt
|
||||
}
|
||||
return BackendDolt
|
||||
}
|
||||
|
||||
// Dolt mode constants
|
||||
|
|
|
|||
|
|
@ -351,228 +351,49 @@ func TestIsDoltServerModeEnvVar(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// TestGetBackendRespectsConfig tests that GetBackend returns the configured
|
||||
// backend from metadata.json instead of always returning "dolt".
|
||||
// Fixes GH#2016: v0.55.4 forces Dolt backend on existing SQLite databases.
|
||||
func TestGetBackendRespectsConfig(t *testing.T) {
|
||||
// TestGetBackendAlwaysDolt tests that GetBackend always returns "dolt".
|
||||
func TestGetBackendAlwaysDolt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *Config
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "explicit sqlite backend is respected",
|
||||
cfg: &Config{Backend: BackendSQLite},
|
||||
want: BackendSQLite,
|
||||
},
|
||||
{
|
||||
name: "explicit dolt backend is respected",
|
||||
cfg: &Config{Backend: BackendDolt},
|
||||
want: BackendDolt,
|
||||
},
|
||||
{
|
||||
name: "empty backend defaults to dolt for new installs",
|
||||
cfg: &Config{Backend: ""},
|
||||
want: BackendDolt,
|
||||
},
|
||||
{
|
||||
name: "legacy config with no backend field defaults to dolt",
|
||||
cfg: &Config{},
|
||||
want: BackendDolt,
|
||||
},
|
||||
{
|
||||
name: "uppercase SQLite is normalized",
|
||||
cfg: &Config{Backend: "SQLite"},
|
||||
want: BackendSQLite,
|
||||
},
|
||||
{
|
||||
name: "mixed case SQLITE is normalized",
|
||||
cfg: &Config{Backend: "SQLITE"},
|
||||
want: BackendSQLite,
|
||||
},
|
||||
{
|
||||
name: "uppercase DOLT is normalized",
|
||||
cfg: &Config{Backend: "DOLT"},
|
||||
want: BackendDolt,
|
||||
},
|
||||
{
|
||||
name: "unknown backend defaults to dolt",
|
||||
cfg: &Config{Backend: "postgres"},
|
||||
want: BackendDolt,
|
||||
},
|
||||
{name: "explicit dolt", cfg: &Config{Backend: BackendDolt}},
|
||||
{name: "empty backend", cfg: &Config{Backend: ""}},
|
||||
{name: "legacy config", cfg: &Config{}},
|
||||
{name: "stale sqlite value", cfg: &Config{Backend: "sqlite"}},
|
||||
{name: "unknown backend", cfg: &Config{Backend: "postgres"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.cfg.GetBackend()
|
||||
if got != tt.want {
|
||||
t.Errorf("GetBackend() = %q, want %q", got, tt.want)
|
||||
if got := tt.cfg.GetBackend(); got != BackendDolt {
|
||||
t.Errorf("GetBackend() = %q, want %q", got, BackendDolt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabasePathRespectsBackend tests that DatabasePath returns the SQLite
|
||||
// path when backend is "sqlite" and the Dolt path when backend is "dolt".
|
||||
// Fixes GH#2016.
|
||||
func TestDatabasePathRespectsBackend(t *testing.T) {
|
||||
// TestDatabasePathAlwaysDolt tests that DatabasePath always returns the dolt path.
|
||||
func TestDatabasePathAlwaysDolt(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
t.Run("sqlite backend returns sqlite path", func(t *testing.T) {
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendSQLite}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "beads.db")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("dolt backend returns dolt path", func(t *testing.T) {
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("sqlite backend with empty database uses default beads.db", func(t *testing.T) {
|
||||
cfg := &Config{Database: "", Backend: BackendSQLite}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "beads.db")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestIsDoltServerModeWithSQLiteBackend verifies that SQLite backend configs
|
||||
// never report as Dolt server mode, even with env var override.
|
||||
// Fixes GH#2016.
|
||||
func TestIsDoltServerModeWithSQLiteBackend(t *testing.T) {
|
||||
t.Run("sqlite backend is never server mode", func(t *testing.T) {
|
||||
cfg := &Config{Backend: BackendSQLite, DoltMode: DoltModeServer}
|
||||
if cfg.IsDoltServerMode() {
|
||||
t.Error("IsDoltServerMode() = true for sqlite backend, want false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("sqlite backend ignores BEADS_DOLT_SERVER_MODE env var", func(t *testing.T) {
|
||||
t.Setenv("BEADS_DOLT_SERVER_MODE", "1")
|
||||
cfg := &Config{Backend: BackendSQLite}
|
||||
if cfg.IsDoltServerMode() {
|
||||
t.Error("IsDoltServerMode() = true for sqlite backend with env var, want false")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestGetBackendRoundtrip tests that backend field survives save/load.
|
||||
// Fixes GH#2016.
|
||||
func TestGetBackendRoundtrip(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
Database: "beads.db",
|
||||
Backend: BackendSQLite,
|
||||
}
|
||||
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Save() failed: %v", err)
|
||||
}
|
||||
|
||||
loaded, err := Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() failed: %v", err)
|
||||
}
|
||||
|
||||
if loaded.GetBackend() != BackendSQLite {
|
||||
t.Errorf("GetBackend() after roundtrip = %q, want %q", loaded.GetBackend(), BackendSQLite)
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCapabilitiesForBackendSQLite tests that CapabilitiesForBackend handles
|
||||
// the "sqlite" backend string. Fixes GH#2016.
|
||||
func TestCapabilitiesForBackendSQLite(t *testing.T) {
|
||||
caps := CapabilitiesForBackend(BackendSQLite)
|
||||
// TestCapabilitiesForBackend tests that CapabilitiesForBackend returns
|
||||
// single-process-only by default.
|
||||
func TestCapabilitiesForBackend(t *testing.T) {
|
||||
caps := CapabilitiesForBackend("anything")
|
||||
if !caps.SingleProcessOnly {
|
||||
t.Error("CapabilitiesForBackend(sqlite).SingleProcessOnly = false, want true")
|
||||
t.Error("CapabilitiesForBackend().SingleProcessOnly = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetBackendEnvVarOverride tests that BEADS_BACKEND env var overrides
|
||||
// metadata.json, providing an escape hatch for corrupted configs.
|
||||
// Fixes GH#2016.
|
||||
func TestGetBackendEnvVarOverride(t *testing.T) {
|
||||
t.Run("env var forces sqlite", func(t *testing.T) {
|
||||
t.Setenv("BEADS_BACKEND", "sqlite")
|
||||
cfg := &Config{Backend: BackendDolt}
|
||||
if got := cfg.GetBackend(); got != BackendSQLite {
|
||||
t.Errorf("GetBackend() = %q with BEADS_BACKEND=sqlite, want %q", got, BackendSQLite)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("env var forces dolt", func(t *testing.T) {
|
||||
t.Setenv("BEADS_BACKEND", "dolt")
|
||||
cfg := &Config{Backend: BackendSQLite}
|
||||
if got := cfg.GetBackend(); got != BackendDolt {
|
||||
t.Errorf("GetBackend() = %q with BEADS_BACKEND=dolt, want %q", got, BackendDolt)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty env var uses config", func(t *testing.T) {
|
||||
cfg := &Config{Backend: BackendSQLite}
|
||||
if got := cfg.GetBackend(); got != BackendSQLite {
|
||||
t.Errorf("GetBackend() = %q without env var, want %q", got, BackendSQLite)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("uppercase SQLITE env var is normalized", func(t *testing.T) {
|
||||
t.Setenv("BEADS_BACKEND", "SQLITE")
|
||||
cfg := &Config{Backend: BackendDolt}
|
||||
if got := cfg.GetBackend(); got != BackendSQLite {
|
||||
t.Errorf("GetBackend() = %q with BEADS_BACKEND=SQLITE, want %q", got, BackendSQLite)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unknown env var value falls through to config", func(t *testing.T) {
|
||||
t.Setenv("BEADS_BACKEND", "postgres")
|
||||
cfg := &Config{Backend: BackendSQLite}
|
||||
if got := cfg.GetBackend(); got != BackendSQLite {
|
||||
t.Errorf("GetBackend() = %q with unknown env var, want %q (from config)", got, BackendSQLite)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestDatabasePathEnvVarOverride tests that DatabasePath respects the
|
||||
// BEADS_BACKEND env var, since DatabasePath calls GetBackend() internally.
|
||||
func TestDatabasePathEnvVarOverride(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
t.Run("env var sqlite overrides dolt config for database path", func(t *testing.T) {
|
||||
t.Setenv("BEADS_BACKEND", "sqlite")
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "beads.db")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q with BEADS_BACKEND=sqlite, want %q", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("env var dolt overrides sqlite config for database path", func(t *testing.T) {
|
||||
t.Setenv("BEADS_BACKEND", "dolt")
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendSQLite}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q with BEADS_BACKEND=dolt, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestGetCapabilities tests that GetCapabilities properly handles server mode
|
||||
func TestGetCapabilities(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
|
@ -595,11 +416,6 @@ func TestGetCapabilities(t *testing.T) {
|
|||
cfg: &Config{Backend: BackendDolt, DoltMode: DoltModeServer},
|
||||
wantSingleProc: false,
|
||||
},
|
||||
{
|
||||
name: "sqlite is single-process",
|
||||
cfg: &Config{Backend: BackendSQLite},
|
||||
wantSingleProc: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue