Skip to content

Commit f695d99

Browse files
fix(container): test failing due to lack of init scheme initialization
1 parent ada5ecd commit f695d99

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

internal/testutils/container.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,37 @@ func SetupTestPostgres(t *testing.T) string {
4646
return connStr
4747
}
4848

49-
// SetupTestDB creates a fresh Postgres container, applies migrations, and returns the connection pool.
49+
// SetupTestDB creates a fresh Postgres container, initializes schema.sql, and returns the connection pool.
5050
func SetupTestDB(t *testing.T) *pgxpool.Pool {
5151
ctx := context.Background()
52-
connStr := SetupTestPostgres(t)
53-
54-
// Get repo root for migrations/
5552
_, filename, _, _ := runtime.Caller(0)
5653
projectRoot := filepath.Join(filepath.Dir(filename), "../..")
57-
migrationsPath := filepath.Join(projectRoot, "migrations")
54+
schemaPath := filepath.Join(projectRoot, "schema.sql")
55+
56+
pgContainer, err := postgres.Run(ctx,
57+
"postgres:16-alpine",
58+
postgres.WithInitScripts(schemaPath),
59+
postgres.WithDatabase("test_db"),
60+
postgres.WithUsername("test"),
61+
postgres.WithPassword("test"),
62+
testcontainers.WithWaitStrategy(
63+
wait.ForLog("database system is ready to accept connections").
64+
WithOccurrence(2).
65+
WithStartupTimeout(30*time.Second)),
66+
)
67+
if err != nil {
68+
t.Fatalf("failed to start postgres container: %v", err)
69+
}
70+
71+
t.Cleanup(func() {
72+
if err := pgContainer.Terminate(ctx); err != nil {
73+
t.Fatalf("failed to terminate container: %v", err)
74+
}
75+
})
5876

59-
if err := database.RunMigrations(ctx, connStr, migrationsPath); err != nil {
60-
t.Fatalf("failed to run migrations: %v", err)
77+
connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
78+
if err != nil {
79+
t.Fatalf("failed to get connection string: %v", err)
6180
}
6281

6382
pool, err := database.NewPool(ctx, connStr)

tests/benchmarks/suite_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ func TestMain(m *testing.M) {
4343

4444
_, filename, _, _ := runtime.Caller(0)
4545
projectRoot := filepath.Join(filepath.Dir(filename), "../..")
46-
migrationsPath := filepath.Join(projectRoot, "migrations")
46+
schemaPath := filepath.Join(projectRoot, "schema.sql")
4747

48-
log.Printf("Using migrations from: %s", migrationsPath)
48+
log.Printf("Using schema from: %s", schemaPath)
4949

5050
pgContainer, err := postgres.Run(ctx,
5151
"postgres:16-alpine",
52+
postgres.WithInitScripts(schemaPath),
5253
postgres.WithDatabase("bench_db"),
5354
postgres.WithUsername("bench"),
5455
postgres.WithPassword("bench"),
@@ -76,10 +77,6 @@ func TestMain(m *testing.M) {
7677
log.Fatalf("failed to get connection string: %v", err)
7778
}
7879

79-
if err := database.RunMigrations(ctx, connStr, migrationsPath); err != nil {
80-
log.Fatalf("failed to run migrations: %v", err)
81-
}
82-
8380
benchDB, err = database.NewPool(ctx, connStr)
8481
if err != nil {
8582
log.Fatalf("failed to connect to database: %v", err)

0 commit comments

Comments
 (0)