Files
Alphaeus Mote d3756bce80 feat(db): store the database under data/db and auto-migrate existing files
The SQLite database now lives at data/db/orchestrad.db instead of the data
root. db.New creates the db/ directory (SQLite will not) and, on first run,
moves a legacy data/orchestrad.db plus its -wal/-shm sidecars into db/ so
existing installations keep their data. Verified live: an existing demo
database migrated into data/db and all data (connections, rules, schedules)
was retained. Test covers the move + idempotency.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-02 20:41:34 -04:00

43 lines
1.2 KiB
Go

package db
import (
"os"
"path/filepath"
"testing"
"github.com/Grace-Solutions/OrchestrAD/internal/logging"
)
func TestMigrateLegacyDBLocation(t *testing.T) {
data := t.TempDir()
newPath := filepath.Join(data, "db", "orchestrad.db")
if err := os.MkdirAll(filepath.Dir(newPath), 0755); err != nil {
t.Fatal(err)
}
// Seed a legacy database + WAL/SHM sidecars in the data root.
for _, suffix := range []string{"", "-wal", "-shm"} {
if err := os.WriteFile(filepath.Join(data, "orchestrad.db"+suffix), []byte("x"+suffix), 0644); err != nil {
t.Fatal(err)
}
}
migrateLegacyDBLocation(newPath, logging.Default())
// Files moved into db/, originals gone.
for _, suffix := range []string{"", "-wal", "-shm"} {
if _, err := os.Stat(newPath + suffix); err != nil {
t.Errorf("expected %s in db/, got error: %v", newPath+suffix, err)
}
if _, err := os.Stat(filepath.Join(data, "orchestrad.db"+suffix)); !os.IsNotExist(err) {
t.Errorf("legacy file orchestrad.db%s should be gone", suffix)
}
}
// Second run is a no-op (new file already exists).
migrateLegacyDBLocation(newPath, logging.Default())
if _, err := os.Stat(newPath); err != nil {
t.Errorf("new db should still exist after second run: %v", err)
}
}