@@ -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.
5050func 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 )
0 commit comments