feat(db): add embedded auto-migrating schema system

Replace 4 independent schema management code paths (Docker shell
script, setup wizard pg_restore, test helpers, manual psql) with a
single migration runner embedded in the server binary.

The new server/migrations/ package uses Go embed to bundle all SQL
schemas. On startup, Migrate() creates a schema_version tracking
table, detects existing databases (auto-marks baseline as applied),
and runs pending migrations in transactions.

Key changes:
- Consolidated init.sql + 9.2-update + 33 patches into 0001_init.sql
- Setup wizard simplified to single "Apply schema" checkbox
- Test helpers use migrations.Migrate() instead of pg_restore
- Docker no longer needs schema volume mounts or init script
- Seed data (shops, events, gacha) embedded and applied via API
- Future migrations just add 0002_*.sql files — no manual steps
This commit is contained in:
Houmgaor
2026-02-23 21:19:21 +01:00
parent 6a7db47723
commit 27fb0faa1e
62 changed files with 4736 additions and 932 deletions

View File

@@ -130,44 +130,6 @@ func TestClientModes(t *testing.T) {
}
}
func TestApplySQLFiles(t *testing.T) {
// This test doesn't need a real database — we test the file reading/sorting logic
// by verifying it returns errors when the directory doesn't exist.
_, err := applySQLFiles(nil, "/nonexistent/path")
if err == nil {
t.Error("expected error for nonexistent directory")
}
}
func TestApplySQLFilesOrdering(t *testing.T) {
// Verify that collectSQLFiles returns files in sorted order and skips non-.sql files.
dir := t.TempDir()
files := []string{"03_c.sql", "01_a.sql", "02_b.sql"}
for _, f := range files {
if err := os.WriteFile(filepath.Join(dir, f), []byte("-- "+f), 0644); err != nil {
t.Fatal(err)
}
}
// Non-SQL file should be skipped
if err := os.WriteFile(filepath.Join(dir, "readme.txt"), []byte("not sql"), 0644); err != nil {
t.Fatal(err)
}
collected, err := collectSQLFiles(dir)
if err != nil {
t.Fatalf("collectSQLFiles failed: %v", err)
}
if len(collected) != 3 {
t.Fatalf("got %d files, want 3", len(collected))
}
expected := []string{"01_a.sql", "02_b.sql", "03_c.sql"}
for i, f := range collected {
if f != expected[i] {
t.Errorf("file[%d] = %q, want %q", i, f, expected[i])
}
}
}
func TestWriteConfig(t *testing.T) {
dir := t.TempDir()
origDir, _ := os.Getwd()