File size: 11,705 Bytes
1e92f2d |
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 |
package migrate
import (
"bufio"
"bytes"
"context"
"embed"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
"github.com/target/goalert/lock"
"github.com/target/goalert/retry"
"github.com/target/goalert/util/log"
"github.com/target/goalert/util/sqlutil"
)
//go:embed migrations
var fs embed.FS
func migrationIDs() []string {
files, err := fs.ReadDir("migrations")
if err != nil {
panic(err)
}
ids := make([]string, 0, len(files))
for _, f := range files {
if f.IsDir() {
continue
}
ids = append(ids, strings.TrimPrefix(f.Name(), "migrations/"))
}
return ids
}
// LatestID will return the ID of the latest migration.
func LatestID() string {
ids := migrationIDs()
if len(ids) == 0 {
return ""
}
return ids[len(ids)-1]
}
// Names will return all AssetNames without the timestamps and extensions
func Names() []string {
uniq := make(map[string]struct{})
var names []string
for _, id := range migrationIDs() {
name := migrationName(id)
if _, ok := uniq[name]; ok {
panic("duplicate migration name " + name)
}
uniq[name] = struct{}{}
names = append(names, name)
}
return names
}
func migrationName(file string) string {
file = strings.TrimPrefix(file, "migrations/")
// trim the timestamp, including the trailing hyphen
// Example : 20170808110638-user-email.sql would become user-email.sql
file = file[15:]
file = strings.TrimSuffix(file, ".sql")
return file
}
func migrationID(name string) (int, string) {
for i, id := range migrationIDs() {
if migrationName(id) == name {
return i, id
}
}
return -1, ""
}
// VerifyIsLatest will verify the latest migration is the same as the latest available migration.
//
// This ensures the DB isn't newer than the currently running code.
func VerifyIsLatest(ctx context.Context, url string) error {
conn, err := getConn(ctx, url)
if err != nil {
return err
}
defer conn.Close(ctx)
var dbLatest string
err = conn.QueryRow(ctx, `select id from gorp_migrations order by id desc limit 1`).Scan(&dbLatest)
if err != nil {
return fmt.Errorf("read latest migration from DB: %w", err)
}
if dbLatest != LatestID() {
return errors.Errorf("latest migration in DB is '%s' but expected '%s'; local GoAlert version is likely older than the DB's latest migration (not allowed in SWO-mode)", dbLatest, LatestID())
}
return nil
}
// VerifyAll will verify all migrations have already been applied.
func VerifyAll(ctx context.Context, url string) error {
ids := migrationIDs()
targetIndex := len(ids) - 1
targetID := ids[targetIndex]
targetName := migrationName(targetID)
conn, err := getConn(ctx, url)
if err != nil {
return err
}
defer conn.Close(ctx)
var hasLatest bool
err = conn.QueryRow(ctx, `select true from gorp_migrations where id = $1`, targetID).Scan(&hasLatest)
if !errors.Is(err, pgx.ErrNoRows) && err != nil {
return err
}
if hasLatest {
return nil
}
return errors.Errorf("latest migration '%s' has not been applied", targetName)
}
// ApplyAll will atomically perform all UP migrations.
func ApplyAll(ctx context.Context, url string) (int, error) {
return Up(ctx, url, "")
}
func getConn(ctx context.Context, url string) (*pgx.Conn, error) {
var conn *pgx.Conn
err := retry.DoTemporaryError(func(int) error {
var err error
conn, err = pgx.Connect(ctx, url)
return err
},
retry.Limit(12),
retry.FibBackoff(time.Millisecond*100),
)
if err != nil {
return nil, err
}
_, err = conn.Exec(ctx, `set lock_timeout = 15000`)
if err != nil {
conn.Close(ctx)
return nil, errors.Wrap(err, "set lock timeout")
}
return conn, nil
}
func aquireLock(ctx context.Context, conn *pgx.Conn) error {
for {
_, err := conn.Exec(ctx, `select pg_advisory_lock($1)`, lock.GlobalMigrate)
if err == nil {
return nil
}
// 55P03 is lock_not_available
// https://www.postgresql.org/docs/9.6/static/errcodes-appendix.html
//
// If the lock gets a timeout, terminate stale backends and try again.
if e := sqlutil.MapError(err); e != nil && e.Code == "55P03" {
log.Log(ctx, errors.Wrap(err, "get migration lock (will retry)"))
_, err = conn.Exec(ctx, `
select pg_terminate_backend(l.pid)
from pg_locks l
join pg_stat_activity act on act.pid = l.pid and state = 'idle' and state_change < now() - '30 seconds'::interval
where locktype = 'advisory' and objid = $1 and granted
`, lock.GlobalMigrate)
if err != nil {
conn.Close(ctx)
return errors.Wrap(err, "terminate stale backends")
}
continue
}
}
}
func ensureTableQuery(ctx context.Context, conn *pgx.Conn, fn func() error) error {
err := fn()
if err == nil {
return nil
}
// 42P01 is undefined_table
// https://www.postgresql.org/docs/9.6/static/errcodes-appendix.html
if e := sqlutil.MapError(err); e != nil && e.Code == "42P01" {
// continue
} else {
return err
}
_, err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS gorp_migrations (
id text PRIMARY KEY,
applied_at timestamp with time zone
)
`)
if err != nil {
return err
}
return fn()
}
// Up will apply all migrations up to, and including, targetName.
// If targetName is empty, all available migrations are applied.
func Up(ctx context.Context, url, targetName string) (int, error) {
if targetName == "" {
names := Names()
targetName = names[len(names)-1]
}
targetIndex, targetID := migrationID(targetName)
if targetIndex == -1 {
return 0, errors.Errorf("unknown migration target name '%s'", targetName)
}
conn, err := getConn(ctx, url)
if err != nil {
return 0, err
}
defer conn.Close(ctx)
var hasLatest bool
err = ensureTableQuery(ctx, conn, func() error {
return conn.QueryRow(ctx, `select true from gorp_migrations where id = $1`, targetID).Scan(&hasLatest)
})
if err == nil && hasLatest {
return 0, nil
}
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return 0, err
}
migrations, err := parseMigrations()
if err != nil {
return 0, err
}
err = aquireLock(ctx, conn)
if err != nil {
return 0, err
}
rows, err := conn.Query(ctx, `select id from gorp_migrations order by id`)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return 0, err
}
defer rows.Close()
ids := migrationIDs()
i := -1
for rows.Next() {
i++
var id string
err = rows.Scan(&id)
if err != nil {
return 0, errors.Wrap(err, "scan applied migrations")
}
if ids[i] != id {
return 0, errors.Errorf("migration mismatch db has '%s' but expected '%s'", id, ids[i])
}
}
return performMigrations(ctx, conn, true, migrations[i+1:targetIndex+1])
}
// Down will roll back all migrations up to, but NOT including, targetName.
//
// If the DB contains unknown migrations, err is returned.
func Down(ctx context.Context, url, targetName string) (int, error) {
targetIndex, targetID := migrationID(targetName)
if targetIndex == -1 {
return 0, errors.Errorf("unknown migration target name '%s'", targetName)
}
conn, err := getConn(ctx, url)
if err != nil {
return 0, err
}
defer conn.Close(ctx)
var latest string
err = ensureTableQuery(ctx, conn, func() error {
return conn.QueryRow(ctx, `select id from gorp_migrations order by id desc limit 1`).Scan(&latest)
})
if err != nil {
return 0, err
}
if latest == targetID {
return 0, nil
}
migrations, err := parseMigrations()
if err != nil {
return 0, err
}
byID := make(map[string]migration)
for _, m := range migrations {
byID[m.ID] = m
}
err = aquireLock(ctx, conn)
if err != nil {
return 0, err
}
rows, err := conn.Query(ctx, `select id from gorp_migrations where id > $1 order by id desc`, targetID)
if err != nil {
return 0, err
}
defer rows.Close()
migrations = migrations[:0]
for rows.Next() {
var id string
err = rows.Scan(&id)
if err != nil {
return 0, err
}
m, ok := byID[id]
if !ok {
return 0, errors.Errorf("could not find db migration '%s' to roll back", id)
}
migrations = append(migrations, m)
}
return performMigrations(ctx, conn, false, migrations)
}
func readMigration(id string) ([]byte, error) {
data, err := fs.ReadFile("migrations/" + id)
if err != nil {
return nil, fmt.Errorf("read 'migrations/%s': %w", id, err)
}
return data, nil
}
// DumpMigrations will attempt to write all migration files to the specified directory
func DumpMigrations(dest string) error {
for _, id := range migrationIDs() {
fullPath := filepath.Join(dest, "migrations", id)
if err := os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
return err
}
data, err := readMigration(id)
if err != nil {
return err
}
err = os.WriteFile(fullPath, data, 0o644)
if err != nil {
return errors.Wrapf(err, "write to %s", fullPath)
}
}
return nil
}
type migration struct {
ID string
Name string
Up migrationStep
Down migrationStep
}
type migrationStep struct {
statements []string
disableTx bool
isUp bool
*migration
}
func parseMigrations() ([]migration, error) {
var migrations []migration
for _, id := range migrationIDs() {
var m migration
m.ID = id
m.Name = migrationName(id)
data, err := readMigration(id)
if err != nil {
return nil, err
}
var up, down strings.Builder
var isUp, isDown bool
r := bufio.NewScanner(bytes.NewReader(data))
for r.Scan() {
line := r.Text()
if strings.HasPrefix(line, "-- +migrate Up") {
isUp = true
isDown = false
m.Up.disableTx = strings.Contains(line, "notransaction")
continue
}
if strings.HasPrefix(line, "-- +migrate Down") {
isUp = false
isDown = true
m.Down.disableTx = strings.Contains(line, "notransaction")
continue
}
switch {
case isUp:
up.WriteString(line)
up.WriteString("\n")
case isDown:
down.WriteString(line)
down.WriteString("\n")
}
// ignore other lines
}
m.Up.statements = sqlutil.SplitQuery(up.String())
m.Up.isUp = true
m.Up.migration = &m
m.Down.statements = sqlutil.SplitQuery(down.String())
m.Down.migration = &m
migrations = append(migrations, m)
}
return migrations, nil
}
const (
deleteMigrationRecord = `delete from gorp_migrations where id = $1`
insertMigrationRecord = `insert into gorp_migrations (id, applied_at) values ($1, now())`
)
func (step migrationStep) doneStmt() string {
if step.isUp {
return insertMigrationRecord
}
return deleteMigrationRecord
}
func (step migrationStep) applyNoTx(ctx context.Context, c *pgx.Conn) error {
for i, stmt := range step.statements {
_, err := c.Exec(ctx, stmt)
if err != nil {
return errors.Wrapf(err, "statement #%d\n%s", i+1, stmt)
}
}
_, err := c.Exec(ctx, step.doneStmt(), step.ID)
if err != nil {
return errors.Wrap(err, "update gorp_migrations")
}
return nil
}
func (step migrationStep) apply(ctx context.Context, c *pgx.Conn) error {
if step.disableTx {
return step.applyNoTx(ctx, c)
}
tx, err := c.Begin(ctx)
if err != nil {
return errors.Wrap(err, "begin tx")
}
defer sqlutil.RollbackContext(ctx, "migrate: apply", tx)
// tx applies to the connection, so NoTx
// will execute correctly.
err = step.applyNoTx(ctx, c)
if err != nil {
return err
}
return errors.Wrap(tx.Commit(ctx), "commit")
}
func performMigrations(ctx context.Context, c *pgx.Conn, applyUp bool, migrations []migration) (int, error) {
typ := "DOWN"
if applyUp {
typ = "UP"
}
for i, m := range migrations {
step := m.Down
if applyUp {
step = m.Up
}
s := time.Now()
err := step.apply(ctx, c)
if err != nil {
return i, errors.Wrapf(err, "apply '%s'", m.Name)
}
log.Debugf(ctx, "Applied %s migration '%s' in %s", typ, m.Name, time.Since(s).Truncate(time.Millisecond))
}
return len(migrations), nil
}
|