/
pg.go
251 lines (207 loc) · 5.38 KB
/
pg.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package pg
import (
"context"
"errors"
"log"
"net/url"
"os"
"strconv"
"strings"
"github.com/go-pg/pg/v10"
"github.com/golang-migrate/migrate"
"golang.org/x/xerrors"
)
// Database is a connection to a Postgres database.
type Database struct {
*pg.DB
version uint
migrationPath string
connectionString string
}
// NewDatabase creates a new database connection.
func NewDatabase(conf Config) (*Database, error) {
ops, err := parseURL(conf.ConnectionString)
if err != nil {
return nil, err
}
// useful when debugging long running queries
ops.ApplicationName = conf.PGApplicationName
db, err := connectPG(&conf.ConnectPGOptions, ops)
if err != nil {
return nil, err
}
// set query logger
if conf.SetInternalPGLogger {
pg.SetLogger(&logger{log.New(os.Stdout, conf.PGLoggerPrefix, log.LstdFlags)})
}
provider := &Database{
DB: db,
migrationPath: conf.MigrationPath,
connectionString: conf.ConnectionString,
}
return provider, nil
}
// Ping pings the database to ensure that we can connect to it.
func (d *Database) Ping(ctx context.Context) (err error) {
return d.DB.RunInTransaction(ctx, func(tx *pg.Tx) error {
i := 0
_, err = tx.QueryOne(pg.Scan(&i), "SELECT 1")
return err
})
}
// Close closes the database.
func (d *Database) Close() error {
return d.DB.Close()
}
// GetMigrationVersion returns what migration we are on.
func (d *Database) GetMigrationVersion(ctx context.Context, v *uint) error {
return d.DB.RunInTransaction(ctx, func(tx *pg.Tx) error {
if v == nil {
return errors.New("pg: migration version (v) is nil")
}
*v = d.version
return nil
})
}
// MigrateUp performs an up migration.
func (d *Database) MigrateUp(ctx context.Context, version *uint) error {
return d.DB.RunInTransaction(ctx, func(tx *pg.Tx) error {
if version == nil {
return errors.New("pg: migration version (v) is nil")
}
migrator, err := migrate.New("file://"+d.migrationPath, d.connectionString)
if err != nil {
return err
}
defer migrator.Close()
// get the version first
// ignore dirty, as Up will fail if dirty
v, _, err := migrator.Version()
if err != nil {
if err.Error() != migrate.ErrNilVersion.Error() {
return err
}
} else {
// set the current migration version
d.version = v
*version = v
}
// Migrate all the way up ...
if err := migrator.Up(); err != nil {
return err
}
// get new version
v, _, err = migrator.Version()
// should be version now
if err != nil {
return err
}
// set the current migration version
d.version = v
*version = v
// should be idempotent
srcErr, dbErr := migrator.Close()
if srcErr != nil {
return srcErr
} else if dbErr != nil {
return dbErr
}
return nil
})
}
// MigrateDown performs a down migration.
func (d *Database) MigrateDown(ctx context.Context) error {
return d.DB.RunInTransaction(ctx, func(tx *pg.Tx) error {
m, err := migrate.New("file://"+d.migrationPath, d.connectionString)
if err != nil {
return err
}
// cleanup after
defer m.Close()
// Migrate all the way down ...
return m.Down()
})
}
// TruncateAll truncates all tables other that schema_migrations.
func (d *Database) TruncateAll() error {
// query the DB for a list of all our tables
var tableInfo []struct {
Table string
}
query := `
SELECT table_name "table"
FROM information_schema.tables WHERE table_schema='public'
AND table_type='BASE TABLE' AND table_name!= 'schema_migrations';
`
if _, err := d.DB.Query(&tableInfo, query); err != nil {
return err
}
// run a transaction that truncates all our tables
return d.DB.RunInTransaction(context.Background(), func(tx *pg.Tx) error {
for _, info := range tableInfo {
if _, err := tx.Exec("TRUNCATE " + info.Table + " CASCADE;"); err != nil {
return err
}
}
return nil
})
}
func (d *Database) RunInTransaction(ctx context.Context, next func(ctx context.Context) error) error {
tx, err := d.DB.Begin()
if err != nil {
return err
}
return tx.RunInTransaction(ctx, func(tx *pg.Tx) error {
return convertError(next(ctx))
})
}
// ToURL constructs a Postgres querystring with sensible defaults.
func ToURL(port int, ssl bool, username, password, db, host string) string {
str := "postgres://"
if len(username) == 0 {
username = "postgres"
}
str += url.PathEscape(username)
if len(password) > 0 {
str = str + ":" + url.PathEscape(password)
}
if port == 0 {
port = 5432
}
if db == "" {
db = "postgres"
}
if host == "" {
host = "localhost"
}
mode := ""
if !ssl {
mode = "?sslmode=disable"
}
return str + "@" +
host + ":" +
strconv.Itoa(port) + "/" +
url.PathEscape(db) + mode
}
// parseURL is a wrapper around `pg.ParseURL`
// that undoes the logic in https://github.com/go-pg/pg/pull/458; which is
// to ensure that InsecureSkipVerify is false.
func parseURL(sURL string) (*pg.Options, error) {
options, err := pg.ParseURL(sURL)
if err != nil {
return nil, xerrors.Errorf("pg: %w", err)
}
if options.TLSConfig != nil {
// override https://github.com/go-pg/pg/pull/458
options.TLSConfig.InsecureSkipVerify = false
// TLSConfig now requires either InsecureSkipVerify = true or ServerName not empty
options.TLSConfig.ServerName = strings.Split(options.Addr, ":")[0]
}
return options, nil
}
type logger struct {
log *log.Logger
}
func (l *logger) Printf(ctx context.Context, format string, v ...interface{}) {
l.log.Printf(format, v...)
}