Merge branch 'main' of https://github.com/steveyegge/beads
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -12,23 +12,34 @@ import (
|
|||||||
"github.com/steveyegge/beads/internal/types"
|
"github.com/steveyegge/beads/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// isJSONLNewer checks if JSONL file is newer than database file.
|
||||||
|
// Returns true if JSONL is newer, false otherwise.
|
||||||
|
func isJSONLNewer(jsonlPath string) bool {
|
||||||
|
jsonlInfo, jsonlStatErr := os.Stat(jsonlPath)
|
||||||
|
if jsonlStatErr != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
beadsDir := filepath.Dir(jsonlPath)
|
||||||
|
dbPath := filepath.Join(beadsDir, "beads.db")
|
||||||
|
dbInfo, dbStatErr := os.Stat(dbPath)
|
||||||
|
if dbStatErr != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonlInfo.ModTime().After(dbInfo.ModTime())
|
||||||
|
}
|
||||||
|
|
||||||
// validatePreExport performs integrity checks before exporting database to JSONL.
|
// validatePreExport performs integrity checks before exporting database to JSONL.
|
||||||
// Returns error if critical issues found that would cause data loss.
|
// Returns error if critical issues found that would cause data loss.
|
||||||
func validatePreExport(ctx context.Context, store storage.Storage, jsonlPath string) error {
|
func validatePreExport(ctx context.Context, store storage.Storage, jsonlPath string) error {
|
||||||
// Check if JSONL is newer than database - if so, must import first
|
// Check if JSONL is newer than database - if so, must import first
|
||||||
jsonlInfo, jsonlStatErr := os.Stat(jsonlPath)
|
if isJSONLNewer(jsonlPath) {
|
||||||
if jsonlStatErr == nil {
|
return fmt.Errorf("refusing to export: JSONL is newer than database (import first to avoid data loss)")
|
||||||
beadsDir := filepath.Dir(jsonlPath)
|
|
||||||
dbPath := filepath.Join(beadsDir, "beads.db")
|
|
||||||
dbInfo, dbStatErr := os.Stat(dbPath)
|
|
||||||
if dbStatErr == nil {
|
|
||||||
// If JSONL is newer, refuse export - caller must import first
|
|
||||||
if jsonlInfo.ModTime().After(dbInfo.ModTime()) {
|
|
||||||
return fmt.Errorf("refusing to export: JSONL is newer than database (import first to avoid data loss)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jsonlInfo, jsonlStatErr := os.Stat(jsonlPath)
|
||||||
|
|
||||||
// Get database issue count (fast path with COUNT(*) if available)
|
// Get database issue count (fast path with COUNT(*) if available)
|
||||||
dbCount, err := countDBIssuesFast(ctx, store)
|
dbCount, err := countDBIssuesFast(ctx, store)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -126,6 +126,16 @@ Use --merge to merge the sync branch back to main branch.`,
|
|||||||
if dryRun {
|
if dryRun {
|
||||||
fmt.Println("→ [DRY RUN] Would export pending changes to JSONL")
|
fmt.Println("→ [DRY RUN] Would export pending changes to JSONL")
|
||||||
} else {
|
} else {
|
||||||
|
// Smart conflict resolution: if JSONL is newer, auto-import first
|
||||||
|
if isJSONLNewer(jsonlPath) {
|
||||||
|
fmt.Println("→ JSONL is newer than database, importing first...")
|
||||||
|
if err := importFromJSONL(ctx, jsonlPath, renameOnImport); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Error auto-importing: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("✓ Auto-import complete")
|
||||||
|
}
|
||||||
|
|
||||||
// Pre-export integrity checks
|
// Pre-export integrity checks
|
||||||
if err := ensureStoreActive(); err == nil && store != nil {
|
if err := ensureStoreActive(); err == nil && store != nil {
|
||||||
if err := validatePreExport(ctx, store, jsonlPath); err != nil {
|
if err := validatePreExport(ctx, store, jsonlPath); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user