(postgresVersion string)
| 56 | } |
| 57 | |
| 58 | func PostgresDB(postgresVersion string) *PostgresInstance { |
| 59 | ctx := context.Background() |
| 60 | |
| 61 | image := fmt.Sprintf("postgres:%s-alpine", postgresVersion) |
| 62 | |
| 63 | postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 64 | ContainerRequest: testcontainers.ContainerRequest{ |
| 65 | Image: image, |
| 66 | ExposedPorts: []string{"5432/tcp"}, |
| 67 | Env: map[string]string{"POSTGRES_USER": "postgres", "POSTGRES_PASSWORD": "postgres", "POSTGRES_DB": "permify"}, |
| 68 | Cmd: []string{"postgres", "-c", "track_commit_timestamp=on"}, |
| 69 | WaitingFor: wait.ForAll( |
| 70 | wait.ForLog("database system is ready to accept connections"), |
| 71 | wait.ForListeningPort("5432/tcp"), |
| 72 | ), |
| 73 | }, |
| 74 | Started: true, |
| 75 | }) |
| 76 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 77 | |
| 78 | var db *PQDatabase.Postgres |
| 79 | |
| 80 | cleanup := func() { |
| 81 | if db != nil { |
| 82 | _ = db.Close() |
| 83 | } |
| 84 | if postgres != nil { |
| 85 | terminateCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 86 | defer cancel() |
| 87 | _ = postgres.Terminate(terminateCtx) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | expectNoError := func(err error) { |
| 92 | if err != nil { |
| 93 | cleanup() |
| 94 | } |
| 95 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 96 | } |
| 97 | |
| 98 | host, err := postgres.Host(ctx) |
| 99 | expectNoError(err) |
| 100 | |
| 101 | port, err := postgres.MappedPort(ctx, "5432") |
| 102 | expectNoError(err) |
| 103 | |
| 104 | dbAddr := fmt.Sprintf("%s:%s", host, port.Port()) |
| 105 | postgresDSN := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", "postgres", "postgres", dbAddr, "permify") |
| 106 | |
| 107 | cfg := config.Database{ |
| 108 | Engine: "postgres", |
| 109 | URI: postgresDSN, |
| 110 | AutoMigrate: true, |
| 111 | MaxOpenConnections: 20, |
| 112 | MaxIdleConnections: 1, |
| 113 | MaxConnectionLifetime: 300, |
| 114 | MaxConnectionIdleTime: 60, |
| 115 | } |
no test coverage detected