-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
59 lines (49 loc) · 1.07 KB
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package pgtest
import (
"context"
"fmt"
"github.com/charlieparkes/go-fixtures/v2"
)
type Postgres struct {
fixture
f *fixtures.Fixtures
networkName string
}
func NewPostgres(ctx context.Context, opts ...Opt) (*Postgres, error) {
p := &Postgres{}
p.f = fixtures.NewFixtures()
for _, opt := range opts {
opt(p)
}
// Docker
if err := p.f.Add(ctx, fixtures.NewDocker(
fixtures.DockerNetworkName(p.networkName),
)); err != nil {
return nil, fmt.Errorf("failed to setup docker: %w", err)
}
p.docker = p.f.Docker()
// Postgres
if err := p.f.Add(ctx, &p.fixture); err != nil {
return nil, fmt.Errorf("failed to setup postgres: %w", err)
}
// Healthcheck
if err := p.Ping(ctx); err != nil {
return nil, fmt.Errorf("failed to ping postgres: %w", err)
}
return p, nil
}
func (p *Postgres) TearDown(ctx context.Context) error {
return p.f.TearDown(ctx)
}
func (p *Postgres) RecoverTearDown(ctx context.Context) {
if p == nil {
return
}
p.f.RecoverTearDown(ctx)
}
func Must(p *Postgres, err error) *Postgres {
if err != nil {
panic(err)
}
return p
}