generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.go
152 lines (125 loc) · 4.53 KB
/
db.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package testutils
import (
"context"
"fmt"
"strings"
"time"
_ "github.com/lib/pq"
_ "github.com/tursodatabase/libsql-client-go/libsql"
_ "modernc.org/sqlite"
"github.com/docker/go-connections/nat"
"entgo.io/ent/dialect"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
type TC struct {
Container testcontainers.Container
Dialect string
URI string
}
// TODO: troubleshoot support for cockroachdb; currently complains about dropping an index during tests
// func getCRDB(ctx context.Context, image string, opts ...testcontainers.ContainerCustomizer) (testcontainers.Container, string, error) {
// defaultImg := "docker.io/cockroachdb/cockroach"
// imgTag := "latest"
// if strings.Contains(image, ":") {
// p := strings.SplitN(image, ":", 2) //nolint:gomnd
// imgTag = p[1]
// }
// req := testcontainers.ContainerRequest{
// Image: fmt.Sprintf("%s:%s", defaultImg, imgTag),
// ExposedPorts: []string{"26257/tcp", "8080/tcp"},
// WaitingFor: wait.ForHTTP("/health").WithPort("8080"),
// Cmd: []string{"start-single-node", "--insecure"},
// }
// container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
// ContainerRequest: req,
// Started: true,
// })
// if err != nil {
// return nil, "", err
// }
// mappedPort, err := container.MappedPort(ctx, "26257")
// if err != nil {
// return nil, "", err
// }
// hostIP, err := container.Host(ctx)
// if err != nil {
// return nil, "", err
// }
// uri := fmt.Sprintf("postgres://root@%s:%s/defaultdb?sslmode=disable", hostIP, mappedPort.Port())
// return container, uri, nil
// }
func getPG(ctx context.Context, image string, opts ...testcontainers.ContainerCustomizer) (postgres.PostgresContainer, string, error) {
defaultImg := "docker.io/postgres"
imgTag := "alpine"
if strings.Contains(image, ":") {
p := strings.SplitN(image, ":", 2) //nolint:gomnd
imgTag = p[1]
}
dbURI := "postgres://postgres:postgres@%s:%s/postgres?sslmode=disable"
uriFunc := func(host string, port nat.Port) string {
return fmt.Sprintf(dbURI, host, port.Port())
}
opts = append(opts,
testcontainers.WithImage(fmt.Sprintf("%s:%s", defaultImg, imgTag)),
testcontainers.WithWaitStrategy(
wait.ForSQL(nat.Port("5432"), "postgres", uriFunc),
wait.ForExposedPort(),
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(30*time.Second)),
postgres.WithPassword("postgres"),
)
container, err := postgres.RunContainer(ctx, opts...)
if err != nil {
return postgres.PostgresContainer{}, "", err
}
mappedPort, err := container.MappedPort(ctx, "5432")
if err != nil {
return postgres.PostgresContainer{}, "", err
}
hostIP, err := container.Host(ctx)
if err != nil {
return postgres.PostgresContainer{}, "", err
}
uri := fmt.Sprintf(dbURI, hostIP, mappedPort.Port())
return *container, uri, nil
}
func getTestDB(ctx context.Context, u string) (TC, error) {
switch {
// case strings.HasPrefix(u, "cockroach"), strings.HasPrefix(u, "cockroachdb"), strings.HasPrefix(u, "crdb"):
// container, uri, err := getCRDB(ctx, u)
// return TC{Container: container, URI: uri, Dialect: dialect.Postgres}, err
case strings.HasPrefix(u, "postgres"):
container, uri, err := getPG(ctx, u)
return TC{Container: container, URI: uri, Dialect: dialect.Postgres}, err
default:
return TC{}, newURIError(u)
}
}
// GetTestURI returns the dialect, connection string and if used a testcontainer for database connectivity in tests
func GetTestURI(ctx context.Context, u string) *TC {
switch {
case u == "":
// return dialect.SQLite, "file::memory:?cache=shared"
return &TC{Dialect: "libsql", URI: "file::memory:?cache=shared"}
case strings.HasPrefix(u, "sqlite://"):
// return dialect.SQLite, strings.TrimPrefix(u, "sqlite://")
return &TC{Dialect: dialect.SQLite, URI: strings.TrimPrefix(u, "sqlite://")}
case strings.HasPrefix(u, "libsql://"):
// return dialect.SQLite, strings.TrimPrefix(u, "libsql://")
return &TC{Dialect: "libsql", URI: strings.TrimPrefix(u, "libsql://")}
case strings.HasPrefix(u, "postgres://"), strings.HasPrefix(u, "postgresql://"):
// return dialect.Postgres, u
return &TC{Dialect: dialect.Postgres, URI: u}
case strings.HasPrefix(u, "docker://"):
container, err := getTestDB(ctx, strings.TrimPrefix(u, "docker://"))
if err != nil {
panic(err)
}
return &container
default:
panic("invalid DB URI, uri: " + u)
}
}