/
postgres.go
94 lines (74 loc) · 1.95 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package containers
import (
"context"
"database/sql"
"strings"
"testing"
"time"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jmoiron/sqlx"
"github.com/ory/dockertest/v3"
)
type Postgres struct {
resource *dockertest.Resource
}
func (p *Postgres) SQLX(t *testing.T) *sqlx.DB {
t.Helper()
db, err := sqlx.Open("postgres", strings.Join([]string{
"host=localhost",
"port=" + p.resource.GetPort("5432/tcp"),
"user=postgres",
"password=postgres",
"dbname=postgres",
"sslmode=disable",
}, " "))
if err != nil {
t.Fatal("failed to connect to postgres", err)
}
return db
}
func (p *Postgres) DB(t *testing.T) *sql.DB {
t.Helper()
return p.SQLX(t).DB
}
func (p *Postgres) Migrate(t *testing.T, fileDir string) *migrate.Migrate {
t.Helper()
driver, err := postgres.WithInstance(p.DB(t), &postgres.Config{})
if err != nil {
t.Fatal("failed to create migration driver", err)
}
m, err := migrate.NewWithDatabaseInstance("file://"+fileDir, "postgres", driver)
if err != nil {
t.Fatal("failed to create migration instance", err)
}
return m
}
func NewPostgres(t *testing.T) *Postgres {
t.Helper()
pool, err := dockertest.NewPool("")
if err != nil {
t.Fatalf("failed to construct docker pool: %v", err)
}
if err := pool.Client.Ping(); err != nil {
t.Fatalf("failed to connect to docker: %v", err)
}
resource, err := pool.Run("postgres", "latest", []string{
"POSTGRES_DB=postgres",
"POSTGRES_USER=postgres",
"POSTGRES_PASSWORD=postgres",
})
if err != nil {
t.Fatalf("failed to start postgres container: %v", err)
}
r := &Postgres{resource: resource}
if err := pool.Retry(func() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
return r.SQLX(t).PingContext(ctx)
}); err != nil {
t.Fatalf("failed to connect to postgres container: %v", err)
}
return r
}