Files
silo-server/internal/userdb/displayprefs_migrate_test.go
T
QuickandClaude Fable 5 440a75b189 refactor(jellycompat): give DisplayPreferences its own table
The Jellyfin DisplayPreferences blobs rode the legacy user_settings
key/value table under synthetic jellycompat:* keys, which forced the
legacy settings API to carry a prefix carve-out in its otherwise-closed
unknown-key gate. They are the compat subsystem's storage, not user
settings: the contract neither validates nor resolves them.

Move them to a dedicated jellycompat_displayprefs table in both
backends, keyed by (prefs id, client) per user, with the blob stored as
opaque text served back byte-for-byte (deliberately not jsonb, which
would re-serialize it). The data-copy migrations — per-user SQLite V16
and a paired SQL + Go goose migration for Postgres — are transactional
and harmless to re-run, and both drive their key parsing and row
classification from the new internal/jellycompat/displayprefs package
so the backends cannot diverge, following the internal/settingsmigrate
precedent. A jellycompat:* row that does not parse as a DisplayPrefs
key (only ever writable through the removed carve-out) is recorded in
user_setting_migration_rejects rather than silently deleted.

With the last non-settings tenant gone, the jellycompatSettingPrefix
carve-out is deleted: the legacy settings endpoints now refuse
jellycompat:* keys like any other unknown key and never surface them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-28 13:32:41 +00:00

163 lines
5.4 KiB
Go

package userdb
import (
"database/sql"
"testing"
)
// seedLegacyDisplayPrefs writes the user_settings rows a pre-cutover install
// holds: handler-written DisplayPreferences blobs, a real user setting that
// must not move, and one jellycompat row only the legacy settings API's
// removed unknown-key carve-out could have produced.
func seedLegacyDisplayPrefs(t *testing.T, db *sql.DB) {
t.Helper()
for key, value := range map[string]string{
// Verbatim copy matters: unusual spacing and key order must survive.
"jellycompat:displayprefs:usersettings:emby": `{"SortBy":"SortName", "CustomPrefs":{"b":"2","a":"1"}}`,
"jellycompat:displayprefs:f137a2dd:": `{"SortBy":"DateCreated"}`,
"jellycompat:stray": "not a displayprefs blob",
"ui_theme": "cobalt-studio",
} {
if _, err := db.Exec(
`INSERT INTO user_settings (key, value) VALUES (?, ?)`, key, value); err != nil {
t.Fatalf("seeding user_settings %s: %v", key, err)
}
}
}
func runDisplayPrefsMove(t *testing.T, db *sql.DB) {
t.Helper()
tx, err := db.Begin()
if err != nil {
t.Fatalf("begin: %v", err)
}
if err := migrateToV16(tx); err != nil {
t.Fatalf("migrateToV16: %v", err)
}
if err := tx.Commit(); err != nil {
t.Fatalf("commit: %v", err)
}
}
// TestMigrateToV16MovesDisplayPrefs runs the real migration against a real
// database. The parsing rules are unit-tested in
// internal/jellycompat/displayprefs; what this covers is the wiring — blobs
// land verbatim in the dedicated table, user_settings comes out with no
// jellycompat tenants, and the one unparseable row is recorded rather than
// silently deleted.
func TestMigrateToV16MovesDisplayPrefs(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
if err := InitSchema(db); err != nil {
t.Fatalf("InitSchema: %v", err)
}
seedLegacyDisplayPrefs(t, db)
runDisplayPrefsMove(t, db)
t.Run("blobs move verbatim", func(t *testing.T) {
for _, want := range []struct{ prefsID, client, value string }{
{"usersettings", "emby", `{"SortBy":"SortName", "CustomPrefs":{"b":"2","a":"1"}}`},
{"f137a2dd", "", `{"SortBy":"DateCreated"}`},
} {
got, err := GetJellycompatDisplayPrefs(db, want.prefsID, want.client)
if err != nil {
t.Fatalf("reading %s/%s: %v", want.prefsID, want.client, err)
}
if got != want.value {
t.Errorf("%s/%s = %q, want the blob byte-for-byte", want.prefsID, want.client, got)
}
}
})
t.Run("user_settings keeps no jellycompat tenants", func(t *testing.T) {
var count int
if err := db.QueryRow(
`SELECT COUNT(*) FROM user_settings WHERE key LIKE 'jellycompat:%'`).Scan(&count); err != nil {
t.Fatalf("counting: %v", err)
}
if count != 0 {
t.Errorf("%d jellycompat rows still ride user_settings", count)
}
// The real settings stay put.
if theme, err := GetSetting(db, "ui_theme"); err != nil || theme != "cobalt-studio" {
t.Errorf("ui_theme = (%q, %v); the move touched a non-jellycompat row", theme, err)
}
})
t.Run("unparseable rows are recorded, not silently deleted", func(t *testing.T) {
var value, reason string
err := db.QueryRow(`
SELECT value, reason FROM user_setting_migration_rejects
WHERE source_table = 'user_settings' AND source_key = 'jellycompat:stray'`).
Scan(&value, &reason)
if err != nil {
t.Fatalf("the stray row was dropped rather than recorded: %v", err)
}
if value != "not a displayprefs blob" || reason == "" {
t.Errorf("reject = (%q, %q); the original value and a reason must survive", value, reason)
}
})
t.Run("a second run is a no-op", func(t *testing.T) {
countAll := func() (blobs, rejects int) {
t.Helper()
if err := db.QueryRow(`SELECT COUNT(*) FROM jellycompat_displayprefs`).Scan(&blobs); err != nil {
t.Fatalf("counting blobs: %v", err)
}
if err := db.QueryRow(`SELECT COUNT(*) FROM user_setting_migration_rejects`).Scan(&rejects); err != nil {
t.Fatalf("counting rejects: %v", err)
}
return blobs, rejects
}
blobsBefore, rejectsBefore := countAll()
runDisplayPrefsMove(t, db)
blobsAfter, rejectsAfter := countAll()
if blobsAfter != blobsBefore || rejectsAfter != rejectsBefore {
t.Errorf("re-run changed counts: blobs %d→%d, rejects %d→%d",
blobsBefore, blobsAfter, rejectsBefore, rejectsAfter)
}
})
}
// TestMigrateToV16IsAtomic: the move runs inside the caller's transaction, so
// a rollback must leave the legacy rows exactly where they were.
func TestMigrateToV16IsAtomic(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
if err := InitSchema(db); err != nil {
t.Fatalf("InitSchema: %v", err)
}
seedLegacyDisplayPrefs(t, db)
tx, err := db.Begin()
if err != nil {
t.Fatalf("begin: %v", err)
}
if err := migrateToV16(tx); err != nil {
t.Fatalf("migrateToV16: %v", err)
}
if err := tx.Rollback(); err != nil {
t.Fatalf("rollback: %v", err)
}
var legacy, moved int
if err := db.QueryRow(
`SELECT COUNT(*) FROM user_settings WHERE key LIKE 'jellycompat:%'`).Scan(&legacy); err != nil {
t.Fatalf("counting legacy rows: %v", err)
}
if err := db.QueryRow(`SELECT COUNT(*) FROM jellycompat_displayprefs`).Scan(&moved); err != nil {
t.Fatalf("counting moved rows: %v", err)
}
if legacy != 3 || moved != 0 {
t.Errorf("rollback left %d legacy rows (want 3) and %d moved rows (want 0)", legacy, moved)
}
}