2019-02-12 16:57:26 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
2019-12-19 09:14:56 +00:00
|
|
|
"context"
|
2019-02-12 16:57:26 +00:00
|
|
|
"database/sql"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2020-01-17 18:22:44 +00:00
|
|
|
|
2020-01-30 19:38:25 +00:00
|
|
|
"storj.io/storj/private/dbutil/txutil"
|
2020-01-17 18:22:44 +00:00
|
|
|
"storj.io/storj/private/tagsql"
|
2019-02-12 16:57:26 +00:00
|
|
|
)
|
|
|
|
|
2019-11-02 20:09:07 +00:00
|
|
|
var (
|
|
|
|
// ErrValidateVersionQuery is when there is an error querying version table
|
|
|
|
ErrValidateVersionQuery = errs.Class("validate db version query error")
|
|
|
|
// ErrValidateVersionMismatch is when the migration version does not match the current database version
|
|
|
|
ErrValidateVersionMismatch = errs.Class("validate db version mismatch error")
|
2019-11-19 20:52:57 +00:00
|
|
|
// ErrValidateMinVersion is when the migration version does not match the current database version
|
|
|
|
ErrValidateMinVersion = errs.Class("validate minimum version error")
|
2019-11-02 20:09:07 +00:00
|
|
|
)
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Scenarios it doesn't handle properly.
|
|
|
|
|
|
|
|
1. Rollback to initial state on multi-step migration.
|
|
|
|
|
|
|
|
Let's say there's a scenario where we run migration steps:
|
|
|
|
1. update a table schema
|
|
|
|
2. move files
|
|
|
|
3. update a table schema
|
|
|
|
4. update a table schema, which fails
|
|
|
|
|
|
|
|
In this case there's no easy way to rollback the moving of files.
|
|
|
|
|
|
|
|
2. Undoing migrations.
|
|
|
|
|
|
|
|
Intentionally left out, because we do not gain that much from currently.
|
|
|
|
|
|
|
|
3. Snapshotting the whole state.
|
|
|
|
|
|
|
|
This probably should be done by the user of this library, when there's disk-space available.
|
|
|
|
|
|
|
|
4. Figuring out what the exact executed steps are.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Migration describes a migration steps
|
|
|
|
type Migration struct {
|
|
|
|
Table string
|
|
|
|
Steps []*Step
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step describes a single step in migration.
|
|
|
|
type Step struct {
|
2020-01-30 19:38:25 +00:00
|
|
|
DB tagsql.DB // The DB to execute this step on
|
2019-02-12 16:57:26 +00:00
|
|
|
Description string
|
|
|
|
Version int // Versions should start at 0
|
|
|
|
Action Action
|
|
|
|
}
|
|
|
|
|
|
|
|
// Action is something that needs to be done
|
|
|
|
type Action interface {
|
2020-01-30 19:38:25 +00:00
|
|
|
Run(ctx context.Context, log *zap.Logger, db tagsql.DB, tx tagsql.Tx) error
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 21:55:21 +00:00
|
|
|
// TargetVersion returns migration with steps upto specified version
|
|
|
|
func (migration *Migration) TargetVersion(version int) *Migration {
|
|
|
|
m := *migration
|
|
|
|
m.Steps = nil
|
|
|
|
for _, step := range migration.Steps {
|
|
|
|
if step.Version <= version {
|
|
|
|
m.Steps = append(m.Steps, step)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
// ValidTableName checks whether the specified table name is valid
|
|
|
|
func (migration *Migration) ValidTableName() error {
|
|
|
|
matched, err := regexp.MatchString(`^[a-z_]+$`, migration.Table)
|
|
|
|
if !matched || err != nil {
|
|
|
|
return Error.New("invalid table name: %v", migration.Table)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:09:07 +00:00
|
|
|
// ValidateSteps checks that the version for each migration step increments in order
|
2019-02-12 16:57:26 +00:00
|
|
|
func (migration *Migration) ValidateSteps() error {
|
|
|
|
sorted := sort.SliceIsSorted(migration.Steps, func(i, j int) bool {
|
|
|
|
return migration.Steps[i].Version <= migration.Steps[j].Version
|
|
|
|
})
|
|
|
|
if !sorted {
|
|
|
|
return Error.New("steps have incorrect order")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:09:07 +00:00
|
|
|
// ValidateVersions checks that the version of the migration matches the state of the database
|
2020-01-13 13:44:55 +00:00
|
|
|
func (migration *Migration) ValidateVersions(ctx context.Context, log *zap.Logger) error {
|
2019-11-02 20:09:07 +00:00
|
|
|
for _, step := range migration.Steps {
|
2020-01-13 13:44:55 +00:00
|
|
|
dbVersion, err := migration.getLatestVersion(ctx, log, step.DB)
|
2019-11-02 20:09:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrValidateVersionQuery.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if step.Version > dbVersion {
|
|
|
|
return ErrValidateVersionMismatch.New("expected %d <= %d", step.Version, dbVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(migration.Steps) > 0 {
|
|
|
|
last := migration.Steps[len(migration.Steps)-1]
|
|
|
|
log.Debug("Database version is up to date", zap.Int("version", last.Version))
|
|
|
|
} else {
|
|
|
|
log.Debug("No Versions")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
// Run runs the migration steps
|
2020-01-13 13:44:55 +00:00
|
|
|
func (migration *Migration) Run(ctx context.Context, log *zap.Logger) error {
|
2019-02-12 16:57:26 +00:00
|
|
|
err := migration.ValidTableName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = migration.ValidateSteps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:34:54 +00:00
|
|
|
initialSetup := false
|
|
|
|
for i, step := range migration.Steps {
|
2019-12-19 09:14:56 +00:00
|
|
|
step := step
|
2019-09-10 20:42:23 +01:00
|
|
|
if step.DB == nil {
|
|
|
|
return Error.New("step.DB is nil for step %d", step.Version)
|
|
|
|
}
|
2019-02-12 16:57:26 +00:00
|
|
|
|
2020-01-13 13:44:55 +00:00
|
|
|
err = migration.ensureVersionTable(ctx, log, step.DB)
|
2019-09-10 20:42:23 +01:00
|
|
|
if err != nil {
|
2020-01-14 11:41:23 +00:00
|
|
|
return Error.New("creating version table failed: %w", err)
|
2019-09-10 20:42:23 +01:00
|
|
|
}
|
2019-02-12 16:57:26 +00:00
|
|
|
|
2020-01-13 13:44:55 +00:00
|
|
|
version, err := migration.getLatestVersion(ctx, log, step.DB)
|
2019-09-10 20:42:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2020-01-06 12:34:54 +00:00
|
|
|
if i == 0 && version < 0 {
|
|
|
|
initialSetup = true
|
|
|
|
}
|
2019-09-10 20:42:23 +01:00
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
if step.Version <= version {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-10 20:42:23 +01:00
|
|
|
stepLog := log.Named(strconv.Itoa(step.Version))
|
2020-01-06 12:34:54 +00:00
|
|
|
if !initialSetup {
|
|
|
|
stepLog.Info(step.Description)
|
|
|
|
}
|
2019-02-12 16:57:26 +00:00
|
|
|
|
2020-01-30 19:38:25 +00:00
|
|
|
err = txutil.WithTx(ctx, step.DB, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
2020-01-13 13:44:55 +00:00
|
|
|
err = step.Action.Run(ctx, stepLog, step.DB, tx)
|
2019-12-19 09:14:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:44:55 +00:00
|
|
|
err = migration.addVersion(ctx, tx, step.DB, step.Version)
|
2019-12-19 09:14:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-02-12 16:57:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 14:34:48 +01:00
|
|
|
if len(migration.Steps) > 0 {
|
|
|
|
last := migration.Steps[len(migration.Steps)-1]
|
2020-01-06 12:34:54 +00:00
|
|
|
if initialSetup {
|
|
|
|
log.Info("Database Created", zap.Int("version", last.Version))
|
|
|
|
} else {
|
|
|
|
log.Info("Database Version", zap.Int("version", last.Version))
|
|
|
|
}
|
2019-09-11 14:34:48 +01:00
|
|
|
} else {
|
|
|
|
log.Info("No Versions")
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createVersionTable creates a new version table
|
2020-01-30 19:38:25 +00:00
|
|
|
func (migration *Migration) ensureVersionTable(ctx context.Context, log *zap.Logger, db tagsql.DB) error {
|
|
|
|
err := txutil.WithTx(ctx, db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
2020-01-17 18:22:44 +00:00
|
|
|
_, err := tx.Exec(ctx, rebind(db, `CREATE TABLE IF NOT EXISTS `+migration.Table+` (version int, commited_at text)`)) //nolint:misspell
|
2019-12-19 09:14:56 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return Error.Wrap(err)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getLatestVersion finds the latest version table
|
2020-01-30 19:38:25 +00:00
|
|
|
func (migration *Migration) getLatestVersion(ctx context.Context, log *zap.Logger, db tagsql.DB) (int, error) {
|
2019-02-12 16:57:26 +00:00
|
|
|
var version sql.NullInt64
|
2020-01-30 19:38:25 +00:00
|
|
|
err := txutil.WithTx(ctx, db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
2020-01-17 18:22:44 +00:00
|
|
|
err := tx.QueryRow(ctx, rebind(db, `SELECT MAX(version) FROM `+migration.Table)).Scan(&version)
|
2019-12-19 09:14:56 +00:00
|
|
|
if err == sql.ErrNoRows || !version.Valid {
|
|
|
|
version.Int64 = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2019-02-12 16:57:26 +00:00
|
|
|
|
2019-12-19 09:14:56 +00:00
|
|
|
return int(version.Int64), Error.Wrap(err)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addVersion adds information about a new migration
|
2020-01-30 19:38:25 +00:00
|
|
|
func (migration *Migration) addVersion(ctx context.Context, tx tagsql.Tx, db tagsql.DB, version int) error {
|
2020-01-17 18:22:44 +00:00
|
|
|
_, err := tx.Exec(ctx, rebind(db, `
|
2019-06-10 09:52:09 +01:00
|
|
|
INSERT INTO `+migration.Table+` (version, commited_at) VALUES (?, ?)`), //nolint:misspell
|
2019-02-12 16:57:26 +00:00
|
|
|
version, time.Now().String(),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:52:57 +00:00
|
|
|
// CurrentVersion finds the latest version for the db
|
2020-01-30 19:38:25 +00:00
|
|
|
func (migration *Migration) CurrentVersion(ctx context.Context, log *zap.Logger, db tagsql.DB) (int, error) {
|
2020-01-13 13:44:55 +00:00
|
|
|
err := migration.ensureVersionTable(ctx, log, db)
|
2019-11-19 20:52:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, Error.Wrap(err)
|
|
|
|
}
|
2020-01-13 13:44:55 +00:00
|
|
|
return migration.getLatestVersion(ctx, log, db)
|
2019-11-19 20:52:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
// SQL statements that are executed on the database
|
|
|
|
type SQL []string
|
|
|
|
|
|
|
|
// Run runs the SQL statements
|
2020-01-30 19:38:25 +00:00
|
|
|
func (sql SQL) Run(ctx context.Context, log *zap.Logger, db tagsql.DB, tx tagsql.Tx) (err error) {
|
2019-02-12 16:57:26 +00:00
|
|
|
for _, query := range sql {
|
2020-01-17 18:22:44 +00:00
|
|
|
_, err := tx.Exec(ctx, rebind(db, query))
|
2019-02-12 16:57:26 +00:00
|
|
|
if err != nil {
|
2020-01-17 00:30:39 +00:00
|
|
|
return errs.Wrap(err)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Func is an arbitrary operation
|
2020-01-30 19:38:25 +00:00
|
|
|
type Func func(ctx context.Context, log *zap.Logger, db tagsql.DB, tx tagsql.Tx) error
|
2019-02-12 16:57:26 +00:00
|
|
|
|
|
|
|
// Run runs the migration
|
2020-01-30 19:38:25 +00:00
|
|
|
func (fn Func) Run(ctx context.Context, log *zap.Logger, db tagsql.DB, tx tagsql.Tx) error {
|
2020-01-13 13:44:55 +00:00
|
|
|
return fn(ctx, log, db, tx)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|