feat: add export and backup commands

- nib export: dump all entities to JSON (stdout or --output file)
- nib backup: atomic SQLite backup via VACUUM INTO (WAL-safe)
- Store.Backup() method on db layer
- Tests for both commands
This commit is contained in:
2026-05-20 20:54:44 -04:00
parent 33f6d99ba7
commit 2152baeb4f
4 changed files with 229 additions and 0 deletions
+65
View File
@@ -3,6 +3,8 @@ package cmd
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
@@ -219,3 +221,66 @@ func TestRunLsEmpty(t *testing.T) {
t.Fatalf("runLs empty: %v", err)
}
}
func TestRunExport(t *testing.T) {
store := testStore(t)
seedEntity(t, store, "export me", db.GlyphNote)
seedEntity(t, store, "export me too", db.GlyphTodo)
store.Close()
outFile := filepath.Join(t.TempDir(), "export.json")
exportOutput = outFile
err := runExport(newCmd(), nil)
if err != nil {
t.Fatalf("runExport: %v", err)
}
data, err := os.ReadFile(outFile)
if err != nil {
t.Fatalf("read export: %v", err)
}
var entities []exportEntity
if err := json.Unmarshal(data, &entities); err != nil {
t.Fatalf("unmarshal export: %v", err)
}
if len(entities) != 2 {
t.Fatalf("expected 2 entities, got %d", len(entities))
}
}
func TestRunBackup(t *testing.T) {
store := testStore(t)
seedEntity(t, store, "backup me", db.GlyphNote)
store.Close()
dst := filepath.Join(t.TempDir(), "backup.db")
err := runBackup(newCmd(), []string{dst})
if err != nil {
t.Fatalf("runBackup: %v", err)
}
info, err := os.Stat(dst)
if err != nil {
t.Fatalf("backup file missing: %v", err)
}
if info.Size() == 0 {
t.Fatal("backup file is empty")
}
backed, err := db.Open(dst)
if err != nil {
t.Fatalf("open backup: %v", err)
}
defer backed.Close()
entities, err := backed.List(context.Background(), db.DefaultListParams())
if err != nil {
t.Fatalf("list backup: %v", err)
}
if len(entities) != 1 {
t.Fatalf("expected 1 entity in backup, got %d", len(entities))
}
}