-
Notifications
You must be signed in to change notification settings - Fork 228
/
driver.go
537 lines (491 loc) · 14.9 KB
/
driver.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"hash/fnv"
"net/url"
"strconv"
"time"
"ariga.io/atlas/sql/internal/sqlx"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlclient"
)
type (
// Driver represents a PostgreSQL driver for introspecting database schemas,
// generating diff between schema elements and apply migrations changes.
Driver struct {
*conn
schema.Differ
schema.Inspector
migrate.PlanApplier
}
// database connection and its information.
conn struct {
schema.ExecQuerier
// The schema in the `search_path` parameter (if given).
schema string
// System variables that are set on `Open`.
version int
crdb bool
}
)
var _ interface {
migrate.Snapshoter
migrate.StmtScanner
migrate.CleanChecker
schema.TypeParseFormatter
} = (*Driver)(nil)
// DriverName holds the name used for registration.
const DriverName = "postgres"
func init() {
sqlclient.Register(
DriverName,
sqlclient.OpenerFunc(opener),
sqlclient.RegisterDriverOpener(Open),
sqlclient.RegisterFlavours("postgresql"),
sqlclient.RegisterCodec(MarshalHCL, EvalHCL),
sqlclient.RegisterURLParser(parser{}),
)
}
func opener(_ context.Context, u *url.URL) (*sqlclient.Client, error) {
ur := parser{}.ParseURL(u)
db, err := sql.Open(DriverName, ur.DSN)
if err != nil {
return nil, err
}
drv, err := Open(db)
if err != nil {
if cerr := db.Close(); cerr != nil {
err = fmt.Errorf("%w: %v", err, cerr)
}
return nil, err
}
switch drv := drv.(type) {
case *Driver:
drv.schema = ur.Schema
case noLockDriver:
drv.noLocker.(*Driver).schema = ur.Schema
}
return &sqlclient.Client{
Name: DriverName,
DB: db,
URL: ur,
Driver: drv,
}, nil
}
// Open opens a new PostgreSQL driver.
func Open(db schema.ExecQuerier) (migrate.Driver, error) {
c := &conn{ExecQuerier: db}
rows, err := db.QueryContext(context.Background(), paramsQuery)
if err != nil {
return nil, fmt.Errorf("postgres: scanning system variables: %w", err)
}
params, err := sqlx.ScanStrings(rows)
if err != nil {
return nil, fmt.Errorf("postgres: failed scanning rows: %w", err)
}
if len(params) != 1 && len(params) != 2 {
return nil, fmt.Errorf("postgres: unexpected number of rows: %d", len(params))
}
if c.version, err = strconv.Atoi(params[0]); err != nil {
return nil, fmt.Errorf("postgres: malformed version: %s: %w", params[0], err)
}
if c.version < 10_00_00 {
return nil, fmt.Errorf("postgres: unsupported postgres version: %d", c.version)
}
// Means we are connected to CockroachDB because we have a result for name='crdb_version'. see `paramsQuery`.
if c.crdb = len(params) == 2; c.crdb {
return noLockDriver{
&Driver{
conn: c,
Differ: &sqlx.Diff{DiffDriver: &crdbDiff{diff{c}}},
Inspector: &crdbInspect{inspect{c}},
PlanApplier: &planApply{c},
},
}, nil
}
return &Driver{
conn: c,
Differ: &sqlx.Diff{DiffDriver: &diff{c}},
Inspector: &inspect{c},
PlanApplier: &planApply{c},
}, nil
}
func (d *Driver) dev() *sqlx.DevDriver {
return &sqlx.DevDriver{
Driver: d,
PatchObject: func(s *schema.Schema, o schema.Object) {
if e, ok := o.(*schema.EnumType); ok {
e.Schema = s
}
},
}
}
// NormalizeRealm returns the normal representation of the given database.
func (d *Driver) NormalizeRealm(ctx context.Context, r *schema.Realm) (*schema.Realm, error) {
return d.dev().NormalizeRealm(ctx, r)
}
// NormalizeSchema returns the normal representation of the given database.
func (d *Driver) NormalizeSchema(ctx context.Context, s *schema.Schema) (*schema.Schema, error) {
return d.dev().NormalizeSchema(ctx, s)
}
// Lock implements the schema.Locker interface.
func (d *Driver) Lock(ctx context.Context, name string, timeout time.Duration) (schema.UnlockFunc, error) {
conn, err := sqlx.SingleConn(ctx, d.ExecQuerier)
if err != nil {
return nil, err
}
h := fnv.New32()
h.Write([]byte(name))
id := h.Sum32()
if err := acquire(ctx, conn, id, timeout); err != nil {
conn.Close()
return nil, err
}
return func() error {
defer conn.Close()
rows, err := conn.QueryContext(ctx, "SELECT pg_advisory_unlock($1)", id)
if err != nil {
return err
}
switch released, err := sqlx.ScanNullBool(rows); {
case err != nil:
return err
case !released.Valid || !released.Bool:
return fmt.Errorf("sql/postgres: failed releasing lock %d", id)
}
return nil
}, nil
}
// Snapshot implements migrate.Snapshoter.
func (d *Driver) Snapshot(ctx context.Context) (migrate.RestoreFunc, error) {
// Postgres will only then be considered bound to a schema if the `search_path` was given.
// In all other cases, the connection is considered bound to the realm.
if d.schema != "" {
s, err := d.InspectSchema(ctx, d.schema, nil)
if err != nil {
return nil, err
}
if len(s.Tables) > 0 {
return nil, &migrate.NotCleanError{
State: schema.NewRealm(s),
Reason: fmt.Sprintf("found table %q in connected schema", s.Tables[0].Name),
}
}
return d.SchemaRestoreFunc(s), nil
}
// Not bound to a schema.
realm, err := d.InspectRealm(ctx, nil)
if err != nil {
return nil, err
}
restore := d.RealmRestoreFunc(realm)
// Postgres is considered clean, if there are no schemas or the public schema has no tables.
if len(realm.Schemas) == 0 {
return restore, nil
}
if s, ok := realm.Schema("public"); len(realm.Schemas) == 1 && ok {
if len(s.Tables) > 0 {
return nil, &migrate.NotCleanError{
State: realm,
Reason: fmt.Sprintf("found table %q in schema %q", s.Tables[0].Name, s.Name),
}
}
return restore, nil
}
return nil, &migrate.NotCleanError{
State: realm,
Reason: fmt.Sprintf("found schema %q", realm.Schemas[0].Name),
}
}
// SchemaRestoreFunc returns a function that restores the given schema to its desired state.
func (d *Driver) SchemaRestoreFunc(desired *schema.Schema) migrate.RestoreFunc {
return func(ctx context.Context) error {
current, err := d.InspectSchema(ctx, desired.Name, nil)
if err != nil {
return err
}
changes, err := d.SchemaDiff(current, desired)
if err != nil {
return err
}
return d.ApplyChanges(ctx, withCascade(changes))
}
}
// RealmRestoreFunc returns a function that restores the given realm to its desired state.
func (d *Driver) RealmRestoreFunc(desired *schema.Realm) migrate.RestoreFunc {
return func(ctx context.Context) error {
current, err := d.InspectRealm(ctx, nil)
if err != nil {
return err
}
changes, err := d.RealmDiff(current, desired)
if err != nil {
return err
}
return d.ApplyChanges(ctx, withCascade(changes))
}
}
func withCascade(changes schema.Changes) schema.Changes {
for _, c := range changes {
switch c := c.(type) {
case *schema.DropTable:
c.Extra = append(c.Extra, &schema.IfExists{}, &Cascade{})
case *schema.DropView:
c.Extra = append(c.Extra, &schema.IfExists{}, &Cascade{})
case *schema.DropProc:
c.Extra = append(c.Extra, &schema.IfExists{}, &Cascade{})
case *schema.DropFunc:
c.Extra = append(c.Extra, &schema.IfExists{}, &Cascade{})
case *schema.DropObject:
c.Extra = append(c.Extra, &schema.IfExists{}, &Cascade{})
}
}
return changes
}
// CheckClean implements migrate.CleanChecker.
func (d *Driver) CheckClean(ctx context.Context, revT *migrate.TableIdent) error {
if revT == nil { // accept nil values
revT = &migrate.TableIdent{}
}
if d.schema != "" {
switch s, err := d.InspectSchema(ctx, d.schema, nil); {
case err != nil:
return err
case len(s.Tables) == 0, (revT.Schema == "" || s.Name == revT.Schema) && len(s.Tables) == 1 && s.Tables[0].Name == revT.Name:
return nil
default:
return &migrate.NotCleanError{State: schema.NewRealm(s), Reason: fmt.Sprintf("found table %q in schema %q", s.Tables[0].Name, s.Name)}
}
}
r, err := d.InspectRealm(ctx, nil)
if err != nil {
return err
}
for _, s := range r.Schemas {
switch {
case len(s.Tables) == 0 && s.Name == "public":
case len(s.Tables) == 0 || s.Name != revT.Schema:
return &migrate.NotCleanError{State: r, Reason: fmt.Sprintf("found schema %q", s.Name)}
case len(s.Tables) > 1:
return &migrate.NotCleanError{State: r, Reason: fmt.Sprintf("found %d tables in schema %q", len(s.Tables), s.Name)}
case len(s.Tables) == 1 && s.Tables[0].Name != revT.Name:
return &migrate.NotCleanError{State: r, Reason: fmt.Sprintf("found table %q in schema %q", s.Tables[0].Name, s.Name)}
}
}
return nil
}
// Version returns the version of the connected database.
func (d *Driver) Version() string {
return strconv.Itoa(d.conn.version)
}
// FormatType converts schema type to its column form in the database.
func (*Driver) FormatType(t schema.Type) (string, error) {
return FormatType(t)
}
// ParseType returns the schema.Type value represented by the given string.
func (*Driver) ParseType(s string) (schema.Type, error) {
return ParseType(s)
}
// StmtBuilder is a helper method used to build statements with PostgreSQL formatting.
func (*Driver) StmtBuilder(opts migrate.PlanOptions) *sqlx.Builder {
return &sqlx.Builder{
QuoteOpening: '"',
QuoteClosing: '"',
Schema: opts.SchemaQualifier,
Indent: opts.Indent,
}
}
// ScanStmts implements migrate.StmtScanner.
func (*Driver) ScanStmts(input string) ([]*migrate.Stmt, error) {
return (&migrate.Scanner{
ScannerOptions: migrate.ScannerOptions{
MatchBegin: true,
MatchBeginAtomic: true,
MatchDollarQuote: true,
},
}).Scan(input)
}
func acquire(ctx context.Context, conn schema.ExecQuerier, id uint32, timeout time.Duration) error {
switch {
// With timeout (context-based).
case timeout > 0:
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
fallthrough
// Infinite timeout.
case timeout < 0:
rows, err := conn.QueryContext(ctx, "SELECT pg_advisory_lock($1)", id)
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
err = schema.ErrLocked
}
if err != nil {
return err
}
return rows.Close()
// No timeout.
default:
rows, err := conn.QueryContext(ctx, "SELECT pg_try_advisory_lock($1)", id)
if err != nil {
return err
}
acquired, err := sqlx.ScanNullBool(rows)
if err != nil {
return err
}
if !acquired.Bool {
return schema.ErrLocked
}
return nil
}
}
// supportsIndexInclude reports if the server supports the INCLUDE clause.
func (c *conn) supportsIndexInclude() bool {
return c.version >= 11_00_00
}
// supportsIndexNullsDistinct reports if the server supports the NULLS [NOT] DISTINCT clause.
func (c *conn) supportsIndexNullsDistinct() bool {
return c.version >= 15_00_00
}
type parser struct{}
// ParseURL implements the sqlclient.URLParser interface.
func (parser) ParseURL(u *url.URL) *sqlclient.URL {
return &sqlclient.URL{URL: u, DSN: u.String(), Schema: u.Query().Get("search_path")}
}
// ChangeSchema implements the sqlclient.SchemaChanger interface.
func (parser) ChangeSchema(u *url.URL, s string) *url.URL {
nu := *u
q := nu.Query()
q.Set("search_path", s)
nu.RawQuery = q.Encode()
return &nu
}
// Standard column types (and their aliases) as defined in
// PostgreSQL codebase/website.
const (
TypeBit = "bit"
TypeBitVar = "bit varying"
TypeBoolean = "boolean"
TypeBool = "bool" // boolean.
TypeBytea = "bytea"
TypeCharacter = "character"
TypeChar = "char" // character
TypeCharVar = "character varying"
TypeVarChar = "varchar" // character varying
TypeText = "text"
typeName = "name" // internal type for object names
TypeSmallInt = "smallint"
TypeInteger = "integer"
TypeBigInt = "bigint"
TypeInt = "int" // integer.
TypeInt2 = "int2" // smallint.
TypeInt4 = "int4" // integer.
TypeInt8 = "int8" // bigint.
TypeCIDR = "cidr"
TypeInet = "inet"
TypeMACAddr = "macaddr"
TypeMACAddr8 = "macaddr8"
TypeCircle = "circle"
TypeLine = "line"
TypeLseg = "lseg"
TypeBox = "box"
TypePath = "path"
TypePolygon = "polygon"
TypePoint = "point"
TypeDate = "date"
TypeTime = "time" // time without time zone
TypeTimeTZ = "timetz" // time with time zone
TypeTimeWTZ = "time with time zone"
TypeTimeWOTZ = "time without time zone"
TypeTimestamp = "timestamp" // timestamp without time zone
TypeTimestampTZ = "timestamptz"
TypeTimestampWTZ = "timestamp with time zone"
TypeTimestampWOTZ = "timestamp without time zone"
TypeDouble = "double precision"
TypeReal = "real"
TypeFloat8 = "float8" // double precision
TypeFloat4 = "float4" // real
TypeFloat = "float" // float(p).
TypeNumeric = "numeric"
TypeDecimal = "decimal" // numeric
TypeSmallSerial = "smallserial" // smallint with auto_increment.
TypeSerial = "serial" // integer with auto_increment.
TypeBigSerial = "bigserial" // bigint with auto_increment.
TypeSerial2 = "serial2" // smallserial
TypeSerial4 = "serial4" // serial
TypeSerial8 = "serial8" // bigserial
TypeArray = "array"
TypeXML = "xml"
TypeJSON = "json"
TypeJSONB = "jsonb"
TypeUUID = "uuid"
TypeMoney = "money"
TypeInterval = "interval"
TypeTSQuery = "tsquery"
TypeTSVector = "tsvector"
TypeUserDefined = "user-defined"
TypeInt4Range = "int4range"
TypeInt4MultiRange = "int4multirange"
TypeInt8Range = "int8range"
TypeInt8MultiRange = "int8multirange"
TypeNumRange = "numrange"
TypeNumMultiRange = "nummultirange"
TypeTSRange = "tsrange"
TypeTSMultiRange = "tsmultirange"
TypeTSTZRange = "tstzrange"
TypeTSTZMultiRange = "tstzmultirange"
TypeDateRange = "daterange"
TypeDateMultiRange = "datemultirange"
// PostgreSQL internal object types and their aliases.
typeOID = "oid"
typeRegClass = "regclass"
typeRegCollation = "regcollation"
typeRegConfig = "regconfig"
typeRegDictionary = "regdictionary"
typeRegNamespace = "regnamespace"
typeRegOper = "regoper"
typeRegOperator = "regoperator"
typeRegProc = "regproc"
typeRegProcedure = "regprocedure"
typeRegRole = "regrole"
typeRegType = "regtype"
// PostgreSQL of supported pseudo-types.
typeAny = "any"
typeAnyElement = "anyelement"
typeAnyArray = "anyarray"
typeAnyNonArray = "anynonarray"
typeAnyEnum = "anyenum"
typeInternal = "internal"
typeRecord = "record"
typeTrigger = "trigger"
typeVoid = "void"
typeUnknown = "unknown"
)
// List of supported index types.
const (
IndexTypeBTree = "BTREE"
IndexTypeBRIN = "BRIN"
IndexTypeHash = "HASH"
IndexTypeGIN = "GIN"
IndexTypeGiST = "GIST"
IndexTypeSPGiST = "SPGIST"
defaultPagePerRange = 128
)
// List of "GENERATED" types.
const (
GeneratedTypeAlways = "ALWAYS"
GeneratedTypeByDefault = "BY_DEFAULT" // BY DEFAULT.
)
// List of PARTITION KEY types.
const (
PartitionTypeRange = "RANGE"
PartitionTypeList = "LIST"
PartitionTypeHash = "HASH"
)