GetPostgresDSN starts a PostgreSQL container (if not already running) and creates a fresh database for this test.
(t *testing.T)
| 190 | |
| 191 | // GetPostgresDSN starts a PostgreSQL container (if not already running) and creates a fresh database for this test. |
| 192 | func GetPostgresDSN(t *testing.T) string { |
| 193 | skipIfContainerProviderUnavailable(t) |
| 194 | |
| 195 | ctx := context.Background() |
| 196 | |
| 197 | postgresOnce.Do(func() { |
| 198 | nw, err := requireTestNetwork(ctx) |
| 199 | if err != nil { |
| 200 | t.Fatalf("failed to create test network: %v", err) |
| 201 | } |
| 202 | |
| 203 | container, err := postgres.Run(ctx, |
| 204 | "postgres:18", |
| 205 | postgres.WithDatabase("init_db"), |
| 206 | postgres.WithUsername(testUser), |
| 207 | postgres.WithPassword(testPassword), |
| 208 | testcontainers.WithWaitStrategy( |
| 209 | wait.ForAll( |
| 210 | wait.ForLog("database system is ready to accept connections").WithOccurrence(2), |
| 211 | wait.ForListeningPort("5432/tcp"), |
| 212 | ).WithDeadline(120*time.Second), |
| 213 | ), |
| 214 | network.WithNetwork([]string{postgresNetworkAlias}, nw), |
| 215 | ) |
| 216 | if err != nil { |
| 217 | t.Fatalf("failed to start PostgreSQL container: %v", err) |
| 218 | } |
| 219 | postgresContainer.Store(container) |
| 220 | |
| 221 | dsn, err := container.ConnectionString(ctx, "sslmode=disable") |
| 222 | if err != nil { |
| 223 | t.Fatalf("failed to get PostgreSQL connection string: %v", err) |
| 224 | } |
| 225 | |
| 226 | if err := waitForDB("postgres", dsn, 30*time.Second); err != nil { |
| 227 | t.Fatalf("PostgreSQL not ready for connections: %v", err) |
| 228 | } |
| 229 | |
| 230 | postgresBaseDSN.Store(dsn) |
| 231 | }) |
| 232 | |
| 233 | dsn, ok := postgresBaseDSN.Load().(string) |
| 234 | if !ok || dsn == "" { |
| 235 | t.Fatal("PostgreSQL container failed to start in a previous test") |
| 236 | } |
| 237 | |
| 238 | // Serialize database creation to avoid "table already exists" race conditions |
| 239 | dbCreationMutex.Lock() |
| 240 | defer dbCreationMutex.Unlock() |
| 241 | |
| 242 | // Create a fresh database for this test |
| 243 | dbName := fmt.Sprintf("memos_test_%d", dbCounter.Add(1)) |
| 244 | db, err := sql.Open("postgres", dsn) |
| 245 | if err != nil { |
| 246 | t.Fatalf("failed to connect to PostgreSQL: %v", err) |
| 247 | } |
| 248 | defer db.Close() |
| 249 |