Feature/statistics api #26

Merged
Sansan merged 12 commits from feature/statistics-api into develop 2026-06-14 11:48:36 +02:00
2 changed files with 37 additions and 13 deletions
Showing only changes of commit 176848bb6d - Show all commits
+3 -1
View File
@@ -20,7 +20,9 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// TODO: Remove these global variables once test_helpers.go is fully migrated to use Database struct // TODO: DEPRECATED - Remove these global variables once all code is migrated to use Database struct
// Use database.go's Database struct instead. These globals remain for backward compatibility
// with legacy code paths. New code should use the Database struct from database.go.
var Dbpool *pgxpool.Pool var Dbpool *pgxpool.Pool
var Ctx = context.Background() var Ctx = context.Background()
+34 -12
View File
@@ -1,11 +1,15 @@
package db package db
import ( import (
"context"
"database/sql" "database/sql"
"fmt" "fmt"
"os" "os"
"testing" "testing"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -198,23 +202,41 @@ func createTestDB(t *testing.T, host, port, user, password, dbname string) {
} }
} }
// applyMigrations applies n migrations to the database // applyMigrations applies n migrations to the database using Go migrate library
// Note: This test requires the migrate CLI tool to be available,
// or use the Go migrate library directly for programmatic testing.
// For integration testing, set DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD env vars.
func applyMigrations(t *testing.T, host, port, user, password, dbname string, steps int) { func applyMigrations(t *testing.T, host, port, user, password, dbname string, steps int) {
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", migrationURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
host, port, user, password, dbname) user, password, host, port, dbname)
db, err := sql.Open("postgres", connStr) db, err := sql.Open("postgres", migrationURL)
require.NoError(t, err) require.NoError(t, err)
defer db.Close() defer db.Close()
// Verify connection works driver, err := postgres.WithInstance(db, &postgres.Config{})
err = db.Ping()
require.NoError(t, err) require.NoError(t, err)
t.Logf("✓ Connected to database: %s", dbname) m, err := migrate.NewWithDatabaseInstance(
t.Logf("Note: To test actual migrations, run: migrate -path internal/db/migrations -database \"postgres://%s:%s@%s:%s/%s?sslmode=disable\" up %d", "file://internal/db/migrations",
user, password, host, port, dbname, steps) "postgres", driver)
require.NoError(t, err)
// Get current version
version, _, err := m.Version()
require.NoError(t, err)
t.Logf("Current migration version: %d", version)
// Apply exactly 'steps' migrations
if steps > 0 {
err = m.Steps(steps)
if err != nil && err != migrate.ErrNoChange {
require.NoError(t, err)
}
} else if steps < 0 {
err = m.Steps(steps)
require.NoError(t, err)
}
// Get new version
newVersion, _, err := m.Version()
require.NoError(t, err)
t.Logf("Migration version after applying %d steps: %d", steps, newVersion)
} }