-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.go
275 lines (249 loc) · 7.03 KB
/
migrate.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
package migrate
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/rs/zerolog/log"
"golang.org/x/xerrors"
)
// Migration describes a PostgreSQL database migration.
// At least one of SQL and Hash must be set.
// If both are set, Hash must be the SHA-256 of SQL.
type Migration struct {
Name string
SQL string // unavailable for applied migrations
Hash string // internally computed if unset
RequiredSHA string
OutsideTx bool
}
func (m *Migration) hash() string {
if m.SQL == "" {
return m.Hash
}
h := sha256.Sum256([]byte(withoutWhiteSpace(m.SQL)))
return hex.EncodeToString(h[:])
}
// withoutWhiteSpace removes all whitespace, interior and exterior so that
// migration specifications can be tweaked for readability without changing the
// semantics.
func withoutWhiteSpace(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func (m Migration) String() string {
return fmt.Sprintf("%s - %s", m.Name, m.hash()[:5])
}
// Error records an error
// and the migration that caused it.
// Index is the index of Mig in
// the given list of migrations,
// or the index where it would have
// been if it's not in the list.
type Error struct {
Mig Migration
Index int
Err error
}
func (e *Error) Error() string {
return e.Mig.String() + " at " + strconv.Itoa(e.Index) + ": " + e.Err.Error()
}
// Run runs all unapplied migrations in m.
func Run(ctx context.Context, db beginner, m []Migration) error {
for {
// keep going until there are no more to run (or an error)
ran, err := run1(ctx, db, m)
if !ran {
return err
}
}
}
type beginner interface {
execer
BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
}
type execer interface {
ExecContext(context.Context, string, ...any) (sql.Result, error)
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...any) *sql.Row
}
// run1 runs a single unapplied migration.
// It returns whether it ran successfully
// along with any error.
//
// Note that a return value of false, nil
// means that there were no unapplied migrations to run.
func run1(ctx context.Context, db beginner, migrations []Migration) (ran bool, err error) {
// Begin a SQL transaction for all changes to the migrations
// table. We use it to acquire an exclusive lock on the
// table to ensure we're the only process migrating this
// database.
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return false, xerrors.Errorf("begin tx: %w", err)
}
defer tx.Rollback()
// Acquire a tx-level lock. 4 is an arbitrary bigint that should
// uniquely identify this lock.
_, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(4)`)
if err != nil {
return false, xerrors.Errorf("advisory lock: %w", err)
}
// Create the migrations table if not yet created.
const q = `
CREATE SEQUENCE IF NOT EXISTS migration_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
CREATE TABLE IF NOT EXISTS migrations (
filename text NOT NULL,
hash text NOT NULL,
applied_at timestamp with time zone DEFAULT now() NOT NULL,
index int DEFAULT nextval('migration_seq') NOT NULL,
PRIMARY KEY(filename)
);
`
_, err = tx.ExecContext(ctx, q)
if err != nil {
return false, xerrors.Errorf("creating migration table: %w", err)
}
// Query migrations for the set of unapplied transactions.
unapplied, err := FilterApplied(tx, migrations)
if err != nil {
return false, err
}
if len(unapplied) == 0 {
return false, nil // all up to date!
}
m := unapplied[0]
// Some migrations contain SQL that PostgreSQL does not support
// within a transaction, so we flag those and run them outside
// of the migration transaction.
//
// This means it is possible that the migration can be applied
// successfully, but then we fail to insert a row in the
// migrations table. We'll need to keep an eye on any error
// messages from migratedb, to avoid attempting to apply
// the same migration again.
if m.OutsideTx {
_, err = db.ExecContext(ctx, m.SQL)
} else {
_, err = tx.ExecContext(ctx, m.SQL)
}
if err != nil {
return false, xerrors.Errorf("migration %s: %w", m.Name, err)
}
err = insertAppliedMigration(ctx, tx, m)
if err != nil {
return false, err
}
err = tx.Commit()
if err == nil {
log.Ctx(ctx).Info().Str("migration", m.Name).Msg("success")
} else {
log.Ctx(ctx).Error().Err(err).Str("migration", m.Name).Msg("failed")
}
return err == nil, err
}
// GetApplied returns the list of currently-applied migrations.
func GetApplied(ctx context.Context, db execer) ([]Migration, error) {
const q1 = `
SELECT count(*) FROM pg_tables
WHERE schemaname=current_schema() AND tablename='migrations'
`
var n int
err := db.QueryRowContext(ctx, q1).Scan(&n)
if err != nil {
return nil, xerrors.Errorf("checking for migrations table: %w", err)
}
if n == 0 {
return nil, nil
}
const q2 = `
SELECT filename, hash
FROM migrations
ORDER BY index
`
var a []Migration
rows, err := db.QueryContext(ctx, q2)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
m := Migration{}
err := rows.Scan(&m.Name, &m.Hash)
if err != nil {
return nil, err
}
a = append(a, m)
}
err = rows.Err()
if err != nil {
return nil, err
}
return a, nil
}
// FilterApplied returns the slice of ms containing
// all migrations in ms that haven't yet been applied.
func FilterApplied(db execer, ms []Migration) ([]Migration, error) {
applied, err := GetApplied(context.TODO(), db)
if err != nil {
return nil, err
}
for i, app := range applied {
if i >= len(ms) {
return nil, &Error{Mig: app, Index: i, Err: errors.New("applied but not requested")}
}
m := ms[i]
if app.hash() != m.hash() {
return nil, &Error{Mig: app, Index: i, Err: errors.New("hash mismatch")}
}
}
return ms[len(applied):], nil
}
func insertAppliedMigration(ctx context.Context, db *sql.Tx, m Migration) error {
const q = `
INSERT INTO migrations (filename, hash, applied_at)
VALUES($1, $2, NOW())
`
_, err := db.ExecContext(ctx, q, m.Name, m.hash())
if err != nil {
return xerrors.Errorf("recording applied migration: %w", err)
}
return nil
}
var validNameRegex = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\.\d\.[a-z][a-z0-9_-]+\.sql$`)
// Validity returns an error if the provided list of migrations
// isn't valid. The required properties are:
// names are well formed
// names are in order
// keys (YYYY-MM-DD.N) are not duplicated
func Validity(migrations []Migration) error {
list := make([]Migration, len(migrations))
copy(list, migrations)
for i, m := range list {
if !validNameRegex.MatchString(m.Name) {
return fmt.Errorf("bad name: %s", m.Name)
}
if i > 0 && list[i-1].Name >= m.Name {
return errors.New("out of order: " + m.Name)
}
}
// Fail if we have more than one of any index
// on the same day. YYYY-MM-DD.N
a := make([]string, len(list))
for i, m := range list {
a[i] = m.Name[:12]
if i > 0 && a[i-1] == a[i] {
return fmt.Errorf("duplicate indexes %s %s", list[i-1].Name, m.Name)
}
}
return nil
}