mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
* chore: Fix golangci-lint configuration and patch errors Due to misconfiguration of a linting rules directory, our linter has not been working properly. This change fixes the configuration issue, and all remaining linting errors. * Fix race in peer logging * Fix race and return * Lock on bufferred amount low * Fix mutex lock
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package postgres
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
"github.com/ory/dockertest/v3/docker"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// Open creates a new PostgreSQL server using a Docker container.
|
|
func Open() (string, func(), error) {
|
|
pool, err := dockertest.NewPool("")
|
|
if err != nil {
|
|
return "", nil, xerrors.Errorf("create pool: %w", err)
|
|
}
|
|
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
|
|
Repository: "postgres",
|
|
Tag: "11",
|
|
Env: []string{
|
|
"POSTGRES_PASSWORD=postgres",
|
|
"POSTGRES_USER=postgres",
|
|
"POSTGRES_DB=postgres",
|
|
"listen_addresses = '*'",
|
|
},
|
|
}, func(config *docker.HostConfig) {
|
|
// set AutoRemove to true so that stopped container goes away by itself
|
|
config.AutoRemove = true
|
|
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
|
|
})
|
|
if err != nil {
|
|
return "", nil, xerrors.Errorf("could not start resource: %w", err)
|
|
}
|
|
hostAndPort := resource.GetHostPort("5432/tcp")
|
|
dbURL := fmt.Sprintf("postgres://postgres:postgres@%s/postgres?sslmode=disable", hostAndPort)
|
|
|
|
// Docker should hard-kill the container after 120 seconds.
|
|
err = resource.Expire(120)
|
|
if err != nil {
|
|
return "", nil, xerrors.Errorf("could not expire resource: %w", err)
|
|
}
|
|
|
|
pool.MaxWait = 120 * time.Second
|
|
err = pool.Retry(func() error {
|
|
db, err := sql.Open("postgres", dbURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = db.Ping()
|
|
_ = db.Close()
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
return dbURL, func() {
|
|
_ = pool.Purge(resource)
|
|
}, nil
|
|
}
|