sonda/internal/storage/upgrade_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

161 lines
5.5 KiB
Go

package storage
import (
"context"
"io/fs"
"strconv"
"testing"
"testing/fstest"
"git.kmalloc.org/squid/sonda/migrations"
)
// upTo returns the migrations of driver numbered up to n, to build a database
// as an older version of Sonda would have left it.
func upTo(t *testing.T, driver string, n int) fs.FS {
t.Helper()
full, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
entries, err := fs.ReadDir(full, ".")
if err != nil {
t.Fatal(err)
}
out := fstest.MapFS{}
for _, e := range entries {
num, err := strconv.Atoi(e.Name()[:4])
if err != nil {
t.Fatal(err)
}
if num <= n {
data, _ := fs.ReadFile(full, e.Name())
out[e.Name()] = &fstest.MapFile{Data: data}
}
}
return out
}
// Upgrading a database written before libraries existed: each distinct root
// becomes a library and every file keeps its id, track and missing flag.
func TestMigration0005UpgradesExistingData(t *testing.T) {
for _, driver := range availableDrivers() {
t.Run(driver, func(t *testing.T) {
ctx := context.Background()
db := openBackend(t, driver)
if err := Migrate(ctx, db, upTo(t, driver, 4)); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('a'), ('b'), ('c')"); err != nil {
t.Fatal(err)
}
rows := `INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix, missing_at) VALUES
(1, '/music/a', 'x.mp3', 'mp3', 10, 100, NULL),
(2, '/music/b', 'x.mp3', 'mp3', 20, 200, NULL),
(3, '/music/a', 'y.mp3', 'mp3', 30, 300, 5)`
if _, err := db.Exec(rows); err != nil {
t.Fatal(err)
}
full, _ := migrations.For(driver)
if err := Migrate(ctx, db, full); err != nil {
t.Fatalf("upgrade: %v", err)
}
if n := count(t, db, "SELECT COUNT(*) FROM libraries"); n != 2 {
t.Errorf("libraries = %d, want 2 (one per distinct root)", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM libraries WHERE name = path"); n != 2 {
t.Error("a migrated library should be named after its path")
}
if n := count(t, db, "SELECT COUNT(*) FROM audio_files"); n != 3 {
t.Errorf("audio files = %d, want 3: none may be lost", n)
}
if n := count(t, db, `SELECT COUNT(*) FROM audio_files f JOIN libraries l ON l.id = f.library_id WHERE l.path = '/music/a'`); n != 2 {
t.Errorf("files linked to /music/a = %d, want 2", n)
}
if n := count(t, db, `SELECT COUNT(*) FROM audio_files f JOIN libraries l ON l.id = f.library_id WHERE l.path = '/music/b' AND f.track_id = 2 AND f.size_bytes = 20`); n != 1 {
t.Error("the file of /music/b was not linked correctly")
}
if n := count(t, db, "SELECT COUNT(*) FROM audio_files WHERE id = 3 AND track_id = 3 AND missing_at = 5"); n != 1 {
t.Error("id, track or missing flag changed during the upgrade")
}
if _, err := db.Exec("SELECT root FROM audio_files"); err == nil {
t.Error("the root column still exists")
}
// The new uniqueness rule is in place.
var lib int64
if err := db.QueryRow("SELECT library_id FROM audio_files WHERE id = 1").Scan(&lib); err != nil {
t.Fatal(err)
}
dup := `INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix) VALUES (1, ?, 'x.mp3', 'mp3', 1, 1)`
if _, err := db.Exec(dup, lib); err == nil {
t.Error("duplicate (library_id, rel_path) was accepted after the upgrade")
}
})
}
}
func TestMigration0005OnAnEmptyDatabase(t *testing.T) {
for _, driver := range availableDrivers() {
t.Run(driver, func(t *testing.T) {
ctx := context.Background()
db := openBackend(t, driver)
if err := Migrate(ctx, db, upTo(t, driver, 4)); err != nil {
t.Fatal(err)
}
full, _ := migrations.For(driver)
if err := Migrate(ctx, db, full); err != nil {
t.Fatal(err)
}
if n := count(t, db, "SELECT COUNT(*) FROM libraries"); n != 0 {
t.Errorf("libraries = %d, want 0", n)
}
})
}
}
// On Linux /Music and /music are different directories. Both can hold rows,
// and library names must be unique ignoring case, so the migration has to
// give them different names instead of failing.
func TestMigration0005RootsDifferingOnlyInCase(t *testing.T) {
for _, driver := range availableDrivers() {
t.Run(driver, func(t *testing.T) {
ctx := context.Background()
db := openBackend(t, driver)
if err := Migrate(ctx, db, upTo(t, driver, 4)); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('a'), ('b'), ('c')"); err != nil {
t.Fatal(err)
}
rows := `INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix) VALUES
(1, '/Music', 'x.mp3', 'mp3', 10, 100),
(2, '/music', 'x.mp3', 'mp3', 20, 200),
(3, '/MUSIC', 'x.mp3', 'mp3', 30, 300)`
if _, err := db.Exec(rows); err != nil {
t.Fatal(err)
}
full, _ := migrations.For(driver)
if err := Migrate(ctx, db, full); err != nil {
t.Fatalf("the upgrade must not fail on roots that differ only in case: %v", err)
}
if n := count(t, db, "SELECT COUNT(*) FROM libraries"); n != 3 {
t.Errorf("libraries = %d, want 3", n)
}
if n := count(t, db, "SELECT COUNT(DISTINCT lower(name)) FROM libraries"); n != 3 {
t.Error("migrated names are not unique ignoring case")
}
// Every file ends up in the library of its own path.
if n := count(t, db, `SELECT COUNT(*) FROM audio_files f JOIN libraries l ON l.id = f.library_id
WHERE (l.path = '/Music' AND f.size_bytes = 10) OR (l.path = '/music' AND f.size_bytes = 20)
OR (l.path = '/MUSIC' AND f.size_bytes = 30)`); n != 3 {
t.Errorf("correctly linked files = %d, want 3", n)
}
})
}
}