Fix bd-180: bd import now creates database if .beads/ directory exists

Previously, 'bd import' would fail if the database didn't exist, even when
importing into a directory with a .beads/ folder. This made it impossible
to recreate a database from JSONL after schema changes or database removal.

Changes:
- Allow import command to create database when .beads/ directory exists
- Prefer local .beads/vc.db over ancestor databases for import operations
- Enable the workflow: mv .beads/vc.db backup && bd import .beads/issues.jsonl

Fixes #180

Amp-Thread-ID: https://ampcode.com/threads/T-7842ea55-6ce4-4162-9ad8-299124a15946
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-20 23:36:40 -07:00
parent 50a0846763
commit f41cd8816e
2 changed files with 35 additions and 5 deletions

View File

@@ -96,15 +96,44 @@ var rootCmd = &cobra.Command{
// Initialize database path
if dbPath == "" {
cwd, err := os.Getwd()
localBeadsDir := ""
if err == nil {
localBeadsDir = filepath.Join(cwd, ".beads")
}
// Use public API to find database (same logic as extensions)
if foundDB := beads.FindDatabasePath(); foundDB != "" {
dbPath = foundDB
// Special case for import: if we found a database but there's a local .beads/
// directory without a database, prefer creating a local database
if cmd.Name() == "import" && localBeadsDir != "" {
if _, err := os.Stat(localBeadsDir); err == nil {
// Check if found database is NOT in the local .beads/ directory
if !strings.HasPrefix(dbPath, localBeadsDir+string(filepath.Separator)) {
// Use local .beads/vc.db instead for import
dbPath = filepath.Join(localBeadsDir, "vc.db")
}
}
}
} else {
// No database found - error out instead of falling back to ~/.beads
fmt.Fprintf(os.Stderr, "Error: no beads database found\n")
fmt.Fprintf(os.Stderr, "Hint: run 'bd init' to create a database in the current directory\n")
fmt.Fprintf(os.Stderr, " or set BEADS_DB environment variable to specify a database\n")
os.Exit(1)
// For import command, allow creating database if .beads/ directory exists
if cmd.Name() == "import" && localBeadsDir != "" {
if _, err := os.Stat(localBeadsDir); err == nil {
// .beads/ directory exists - set dbPath for import to create
dbPath = filepath.Join(localBeadsDir, "vc.db")
}
}
// If dbPath still not set, error out
if dbPath == "" {
// No database found - error out instead of falling back to ~/.beads
fmt.Fprintf(os.Stderr, "Error: no beads database found\n")
fmt.Fprintf(os.Stderr, "Hint: run 'bd init' to create a database in the current directory\n")
fmt.Fprintf(os.Stderr, " or set BEADS_DB environment variable to specify a database\n")
os.Exit(1)
}
}
}