Feature/statistics api #26
@@ -20,7 +20,9 @@ import (
|
||||
"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 Ctx = context.Background()
|
||||
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"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/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
|
||||
// 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.
|
||||
// applyMigrations applies n migrations to the database using Go migrate library
|
||||
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",
|
||||
host, port, user, password, dbname)
|
||||
migrationURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
user, password, host, port, dbname)
|
||||
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
db, err := sql.Open("postgres", migrationURL)
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
// Verify connection works
|
||||
err = db.Ping()
|
||||
driver, err := postgres.WithInstance(db, &postgres.Config{})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Logf("✓ Connected to database: %s", dbname)
|
||||
t.Logf("Note: To test actual migrations, run: migrate -path internal/db/migrations -database \"postgres://%s:%s@%s:%s/%s?sslmode=disable\" up %d",
|
||||
user, password, host, port, dbname, steps)
|
||||
m, err := migrate.NewWithDatabaseInstance(
|
||||
"file://internal/db/migrations",
|
||||
"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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user