-
Notifications
You must be signed in to change notification settings - Fork 402
/
create.go
67 lines (52 loc) · 1.61 KB
/
create.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package migrate
import (
"context"
"database/sql"
"errors"
"github.com/zeebo/errs"
"storj.io/storj/private/dbutil/txutil"
"storj.io/storj/private/tagsql"
)
// Error is the default migrate errs class.
var Error = errs.Class("migrate")
// Create with a previous schema check.
func Create(ctx context.Context, identifier string, db DBX) error {
// is this necessary? it's not immediately obvious why we roll back the transaction
// when the schemas match.
justRollbackPlease := errs.Class("only used to tell WithTx to do a rollback")
err := txutil.WithTx(ctx, db, nil, func(ctx context.Context, tx tagsql.Tx) (err error) {
schema := db.Schema()
_, err = tx.ExecContext(ctx, db.Rebind(`CREATE TABLE IF NOT EXISTS table_schemas (id text, schemaText text);`))
if err != nil {
return err
}
row := tx.QueryRow(ctx, db.Rebind(`SELECT schemaText FROM table_schemas WHERE id = ?;`), identifier)
var previousSchema string
err = row.Scan(&previousSchema)
// not created yet
if errors.Is(err, sql.ErrNoRows) {
_, err := tx.ExecContext(ctx, schema)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, db.Rebind(`INSERT INTO table_schemas(id, schemaText) VALUES (?, ?);`), identifier, schema)
if err != nil {
return err
}
return nil
}
if err != nil {
return err
}
if schema != previousSchema {
return Error.New("schema mismatch:\nold %v\nnew %v", previousSchema, schema)
}
return justRollbackPlease.New("")
})
if justRollbackPlease.Has(err) {
err = nil
}
return Error.Wrap(err)
}