-
Notifications
You must be signed in to change notification settings - Fork 275
/
postgres.go
425 lines (348 loc) · 10.3 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package postgres
import (
"bytes"
"database/sql"
"fmt"
"io"
"net/url"
"runtime"
"strings"
"github.com/amacneil/dbmate/v2/pkg/dbmate"
"github.com/amacneil/dbmate/v2/pkg/dbutil"
"github.com/lib/pq"
)
func init() {
dbmate.RegisterDriver(NewDriver, "postgres")
dbmate.RegisterDriver(NewDriver, "postgresql")
}
// Driver provides top level database functions
type Driver struct {
migrationsTableName string
databaseURL *url.URL
log io.Writer
}
// NewDriver initializes the driver
func NewDriver(config dbmate.DriverConfig) dbmate.Driver {
return &Driver{
migrationsTableName: config.MigrationsTableName,
databaseURL: config.DatabaseURL,
log: config.Log,
}
}
func connectionString(u *url.URL) string {
hostname := u.Hostname()
port := u.Port()
query := u.Query()
// support socket parameter for consistency with mysql
if query.Get("socket") != "" {
query.Set("host", query.Get("socket"))
query.Del("socket")
}
// default hostname
if hostname == "" && query.Get("host") == "" {
switch runtime.GOOS {
case "linux":
query.Set("host", "/var/run/postgresql")
case "darwin", "freebsd", "dragonfly", "openbsd", "netbsd":
query.Set("host", "/tmp")
default:
hostname = "localhost"
}
}
// host param overrides url hostname
if query.Get("host") != "" {
hostname = ""
}
// always specify a port
if query.Get("port") != "" {
port = query.Get("port")
query.Del("port")
}
if port == "" {
port = "5432"
}
// generate output URL
out, _ := url.Parse(u.String())
out.Host = fmt.Sprintf("%s:%s", hostname, port)
out.RawQuery = query.Encode()
return out.String()
}
func connectionArgsForDump(u *url.URL) []string {
u = dbutil.MustParseURL(connectionString(u))
// find schemas from search_path
query := u.Query()
schemas := strings.Split(query.Get("search_path"), ",")
query.Del("search_path")
u.RawQuery = query.Encode()
out := []string{}
for _, schema := range schemas {
schema = strings.TrimSpace(schema)
if schema != "" {
out = append(out, "--schema", schema)
}
}
out = append(out, u.String())
return out
}
// Open creates a new database connection
func (drv *Driver) Open() (*sql.DB, error) {
return sql.Open("postgres", connectionString(drv.databaseURL))
}
func (drv *Driver) openPostgresDB() (*sql.DB, error) {
// clone databaseURL
postgresURL, err := url.Parse(connectionString(drv.databaseURL))
if err != nil {
return nil, err
}
// connect to postgres database
postgresURL.Path = "postgres"
return sql.Open("postgres", postgresURL.String())
}
// CreateDatabase creates the specified database
func (drv *Driver) CreateDatabase() error {
name := dbutil.DatabaseName(drv.databaseURL)
fmt.Fprintf(drv.log, "Creating: %s\n", name)
db, err := drv.openPostgresDB()
if err != nil {
return err
}
defer dbutil.MustClose(db)
_, err = db.Exec(fmt.Sprintf("create database %s",
pq.QuoteIdentifier(name)))
return err
}
// DropDatabase drops the specified database (if it exists)
func (drv *Driver) DropDatabase() error {
name := dbutil.DatabaseName(drv.databaseURL)
fmt.Fprintf(drv.log, "Dropping: %s\n", name)
db, err := drv.openPostgresDB()
if err != nil {
return err
}
defer dbutil.MustClose(db)
_, err = db.Exec(fmt.Sprintf("drop database if exists %s",
pq.QuoteIdentifier(name)))
return err
}
func (drv *Driver) schemaMigrationsDump(db *sql.DB) ([]byte, error) {
migrationsTable, err := drv.quotedMigrationsTableName(db)
if err != nil {
return nil, err
}
// load applied migrations
migrations, err := dbutil.QueryColumn(db,
"select quote_literal(version) from "+migrationsTable+" order by version asc")
if err != nil {
return nil, err
}
// build migrations table data
var buf bytes.Buffer
buf.WriteString("\n--\n-- Dbmate schema migrations\n--\n\n")
if len(migrations) > 0 {
buf.WriteString("INSERT INTO " + migrationsTable + " (version) VALUES\n (" +
strings.Join(migrations, "),\n (") +
");\n")
}
return buf.Bytes(), nil
}
// DumpSchema returns the current database schema
func (drv *Driver) DumpSchema(db *sql.DB) ([]byte, error) {
// load schema
args := append([]string{"--format=plain", "--encoding=UTF8", "--schema-only",
"--no-privileges", "--no-owner"}, connectionArgsForDump(drv.databaseURL)...)
schema, err := dbutil.RunCommand("pg_dump", args...)
if err != nil {
return nil, err
}
migrations, err := drv.schemaMigrationsDump(db)
if err != nil {
return nil, err
}
schema = append(schema, migrations...)
return dbutil.TrimLeadingSQLComments(schema)
}
// DatabaseExists determines whether the database exists
func (drv *Driver) DatabaseExists() (bool, error) {
name := dbutil.DatabaseName(drv.databaseURL)
db, err := drv.openPostgresDB()
if err != nil {
return false, err
}
defer dbutil.MustClose(db)
exists := false
err = db.QueryRow("select true from pg_database where datname = $1", name).
Scan(&exists)
if err == sql.ErrNoRows {
return false, nil
}
return exists, err
}
// MigrationsTableExists checks if the schema_migrations table exists
func (drv *Driver) MigrationsTableExists(db *sql.DB) (bool, error) {
schema, migrationsTableNameParts, err := drv.migrationsTableNameParts(db)
if err != nil {
return false, err
}
migrationsTable := strings.Join(migrationsTableNameParts, ".")
exists := false
err = db.QueryRow("SELECT 1 FROM information_schema.tables "+
"WHERE table_schema = $1 "+
"AND table_name = $2",
schema, migrationsTable).
Scan(&exists)
if err == sql.ErrNoRows {
return false, nil
}
return exists, err
}
// CreateMigrationsTable creates the schema_migrations table
func (drv *Driver) CreateMigrationsTable(db *sql.DB) error {
schema, migrationsTable, err := drv.quotedMigrationsTableNameParts(db)
if err != nil {
return err
}
// first attempt at creating migrations table
createTableStmt := fmt.Sprintf(
"create table if not exists %s.%s (version varchar(128) primary key)",
schema, migrationsTable)
_, err = db.Exec(createTableStmt)
if err == nil {
// table exists or created successfully
return nil
}
// catch 'schema does not exist' error
pqErr, ok := err.(*pq.Error)
if !ok || pqErr.Code != "3F000" {
// unknown error
return err
}
// in theory we could attempt to create the schema every time, but we avoid that
// in case the user doesn't have permissions to create schemas
fmt.Fprintf(drv.log, "Creating schema: %s\n", schema)
_, err = db.Exec(fmt.Sprintf("create schema if not exists %s", schema))
if err != nil {
return err
}
// second and final attempt at creating migrations table
_, err = db.Exec(createTableStmt)
return err
}
// SelectMigrations returns a list of applied migrations
// with an optional limit (in descending order)
func (drv *Driver) SelectMigrations(db *sql.DB, limit int) (map[string]bool, error) {
migrationsTable, err := drv.quotedMigrationsTableName(db)
if err != nil {
return nil, err
}
query := "select version from " + migrationsTable + " order by version desc"
if limit >= 0 {
query = fmt.Sprintf("%s limit %d", query, limit)
}
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer dbutil.MustClose(rows)
migrations := map[string]bool{}
for rows.Next() {
var version string
if err := rows.Scan(&version); err != nil {
return nil, err
}
migrations[version] = true
}
if err = rows.Err(); err != nil {
return nil, err
}
return migrations, nil
}
// InsertMigration adds a new migration record
func (drv *Driver) InsertMigration(db dbutil.Transaction, version string) error {
migrationsTable, err := drv.quotedMigrationsTableName(db)
if err != nil {
return err
}
_, err = db.Exec("insert into "+migrationsTable+" (version) values ($1)", version)
return err
}
// DeleteMigration removes a migration record
func (drv *Driver) DeleteMigration(db dbutil.Transaction, version string) error {
migrationsTable, err := drv.quotedMigrationsTableName(db)
if err != nil {
return err
}
_, err = db.Exec("delete from "+migrationsTable+" where version = $1", version)
return err
}
// Ping verifies a connection to the database server. It does not verify whether the
// specified database exists.
func (drv *Driver) Ping() error {
// attempt connection to primary database, not "postgres" database
// to support servers with no "postgres" database
// (see https://github.com/amacneil/dbmate/issues/78)
db, err := drv.Open()
if err != nil {
return err
}
defer dbutil.MustClose(db)
err = db.Ping()
if err == nil {
return nil
}
// ignore 'database does not exist' error
pqErr, ok := err.(*pq.Error)
if ok && pqErr.Code == "3D000" {
return nil
}
return err
}
func (drv *Driver) quotedMigrationsTableName(db dbutil.Transaction) (string, error) {
schema, name, err := drv.quotedMigrationsTableNameParts(db)
if err != nil {
return "", err
}
return schema + "." + name, nil
}
func (drv *Driver) migrationsTableNameParts(db dbutil.Transaction) (string, []string, error) {
schema := ""
tableNameParts := strings.Split(drv.migrationsTableName, ".")
if len(tableNameParts) > 1 {
// schema specified as part of table name
schema, tableNameParts = tableNameParts[0], tableNameParts[1:]
}
if schema == "" {
// no schema specified with table name, try URL search path if available
searchPath := strings.Split(drv.databaseURL.Query().Get("search_path"), ",")
schema = strings.TrimSpace(searchPath[0])
}
var err error
if schema == "" {
// if no URL available, use current schema
// this is a hack because we don't always have the URL context available
schema, err = dbutil.QueryValue(db, "select current_schema()")
if err != nil {
return "", nil, err
}
}
// fall back to public schema as last resort
if schema == "" {
schema = "public"
}
return schema, tableNameParts, nil
}
func (drv *Driver) quotedMigrationsTableNameParts(db dbutil.Transaction) (string, string, error) {
schema, tableNameParts, err := drv.migrationsTableNameParts(db)
if err != nil {
return "", "", err
}
// quote all parts
// use server rather than client to do this to avoid unnecessary quotes
// (which would change schema.sql diff)
tableNameParts = append([]string{schema}, tableNameParts...)
quotedNameParts, err := dbutil.QueryColumn(db, "select quote_ident(unnest($1::text[]))", pq.Array(tableNameParts))
if err != nil {
return "", "", err
}
// if more than one part, we already have a schema
return quotedNameParts[0], strings.Join(quotedNameParts[1:], "."), nil
}