feat: Remove global db.Dbpool with dependency injection (Phase 0)

- Add Database struct in internal/db/database.go with Pool, Ctx, and RunMigrations()
- Update server.go to use Database struct with NewServerInstance()
- Add backend.go with InitBackend(), BackendRepo(), BackendCtx(), BackendPool()
- Update music.go and sync.go to use BackendRepo() and BackendCtx() instead of db.Dbpool/db.Ctx
- Update token_handler.go to accept pool parameter
- Update routes.go to use s.db.Pool for middleware
- Update cmd/main.go to use NewServerInstance() and HTTPServer()
- Update test_helpers.go to initialize backend with test database
- Update test files to use backend.BackendPool() and backend.BackendCtx()

Benefits:
- Easier to mock database for unit tests
- Follows Go best practices (dependency injection)
- Better architecture with explicit dependencies
- RunMigrations() replaces old Migrate_db() function

Note: Global db.Dbpool and db.Ctx still exist in dbHelper.go for backward compatibility
with test_helpers.go, but production code no longer uses them.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-06-01 18:50:05 +02:00
parent 89e884fae9
commit 06cbad708d
11 changed files with 320 additions and 100 deletions
+15 -15
View File
@@ -75,8 +75,8 @@ func TestSyncPopulatesDatabase(t *testing.T) {
db.TestClearDatabase(t)
// Before sync - should have no games
repo := repository.New(db.Dbpool)
gamesBefore, err := repo.FindAllGames(db.Ctx)
repo := repository.New(backend.BackendPool())
gamesBefore, err := repo.FindAllGames(backend.BackendCtx())
assert.NoError(t, err)
beforeCount := len(gamesBefore)
t.Logf("Games before sync: %d", beforeCount)
@@ -92,7 +92,7 @@ func TestSyncPopulatesDatabase(t *testing.T) {
}
// After sync - should have games
gamesAfter, err := repo.FindAllGames(db.Ctx)
gamesAfter, err := repo.FindAllGames(backend.BackendCtx())
assert.NoError(t, err)
afterCount := len(gamesAfter)
t.Logf("Games after sync: %d", afterCount)
@@ -112,8 +112,8 @@ func TestSyncMakesDifference(t *testing.T) {
db.TestClearDatabase(t)
// Before sync - should have no games
repo := repository.New(db.Dbpool)
gamesBefore, err := repo.FindAllGames(db.Ctx)
repo := repository.New(backend.BackendPool())
gamesBefore, err := repo.FindAllGames(backend.BackendCtx())
assert.NoError(t, err)
assert.Equal(t, 0, len(gamesBefore), "Should have no games before sync")
@@ -127,7 +127,7 @@ func TestSyncMakesDifference(t *testing.T) {
}
// After sync - should have games
gamesAfter, err := repo.FindAllGames(db.Ctx)
gamesAfter, err := repo.FindAllGames(backend.BackendCtx())
assert.NoError(t, err)
assert.True(t, len(gamesAfter) > 0, "Should have games after sync")
}
@@ -199,8 +199,8 @@ func TestSyncGamesNewOnlyChanges(t *testing.T) {
}
// Get initial count
repo := repository.New(db.Dbpool)
gamesBefore, _ := repo.FindAllGames(db.Ctx)
repo := repository.New(backend.BackendPool())
gamesBefore, _ := repo.FindAllGames(backend.BackendCtx())
beforeCount := len(gamesBefore)
// Run incremental sync (should not change count if nothing changed)
@@ -211,7 +211,7 @@ func TestSyncGamesNewOnlyChanges(t *testing.T) {
time.Sleep(2 * time.Second)
// Count should be the same
gamesAfter, _ := repo.FindAllGames(db.Ctx)
gamesAfter, _ := repo.FindAllGames(backend.BackendCtx())
afterCount := len(gamesAfter)
// Note: This might not be exactly equal due to timing, but should be close
@@ -227,8 +227,8 @@ func TestResetGames(t *testing.T) {
e := StartTestServer(t)
// First ensure we have data
repo := repository.New(db.Dbpool)
gamesBefore, _ := repo.FindAllGames(db.Ctx)
repo := repository.New(backend.BackendPool())
gamesBefore, _ := repo.FindAllGames(backend.BackendCtx())
beforeCount := len(gamesBefore)
if beforeCount == 0 {
@@ -238,7 +238,7 @@ func TestResetGames(t *testing.T) {
t.Error("Sync did not complete within timeout")
return
}
gamesBefore, _ = repo.FindAllGames(db.Ctx)
gamesBefore, _ = repo.FindAllGames(backend.BackendCtx())
beforeCount = len(gamesBefore)
}
@@ -253,7 +253,7 @@ func TestResetGames(t *testing.T) {
// Note: reset might take a moment to propagate
time.Sleep(1 * time.Second)
gamesAfter, _ := repo.FindAllGames(db.Ctx)
gamesAfter, _ := repo.FindAllGames(backend.BackendCtx())
afterCount := len(gamesAfter)
t.Logf("Games after reset: %d", afterCount)
@@ -281,8 +281,8 @@ func TestSyncGamesNewFull(t *testing.T) {
}
// Verify database is populated
repo := repository.New(db.Dbpool)
games, err := repo.FindAllGames(db.Ctx)
repo := repository.New(backend.BackendPool())
games, err := repo.FindAllGames(backend.BackendCtx())
assert.NoError(t, err)
assert.True(t, len(games) > 0, "Database should be populated after full sync")
t.Logf("Full sync populated %d games", len(games))