MCPcopy Index your code
hub / github.com/bytebase/bytebase / getPgContainerWithImage

Function getPgContainerWithImage

backend/common/testcontainer/testcontainer.go:115–163  ·  view source on GitHub ↗
(ctx context.Context, image string)

Source from the content-addressed store, hash-verified

113}
114
115func getPgContainerWithImage(ctx context.Context, image string) (retC *Container, retErr error) {
116 req := testcontainers.ContainerRequest{
117 Image: image,
118 Env: map[string]string{
119 "LANG": "en_US.UTF-8",
120 "POSTGRES_PASSWORD": "root-password",
121 },
122 ExposedPorts: []string{"5432/tcp"},
123 WaitingFor: wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute),
124 }
125
126 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
127 ContainerRequest: req,
128 Started: true,
129 })
130 if err != nil {
131 return nil, err
132 }
133
134 host, err := c.Host(ctx)
135 if err != nil {
136 return nil, err
137 }
138 port, err := c.MappedPort(ctx, "5432/tcp")
139 if err != nil {
140 return nil, err
141 }
142
143 db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=postgres", host, port.Port()))
144 if err != nil {
145 return nil, err
146 }
147 defer func() {
148 if retErr != nil {
149 db.Close()
150 }
151 }()
152
153 if err := waitDBPing(ctx, db); err != nil {
154 return nil, err
155 }
156
157 return &Container{
158 container: c,
159 host: host,
160 port: port.Port(),
161 db: db,
162 }, nil
163}
164
165func waitDBPing(ctx context.Context, db *sql.DB) error {
166 started := time.Now()

Callers 2

GetPgContainerFunction · 0.85
GetPg17ContainerFunction · 0.85

Calls 4

waitDBPingFunction · 0.85
PortMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected