~egtann/migrate

f65681a14a1955d08414b47e479bcbbe357fd6be — Evan Tann a month ago c755baa
fix v1 cold start bug
6 files changed, 57 insertions(+), 17 deletions(-)

M cmd/migrate/main.go
M migrate.go
M mysql/mysql.go
M postgres/postgres.go
M sqlite/sqlite.go
M store.go
M cmd/migrate/main.go => cmd/migrate/main.go +1 -1
@@ 39,7 39,7 @@ func run() error {
	flag.Parse()

	if *version {
		fmt.Println("v1.0.0rc2")
		fmt.Println("v1.0.0rc3")
		return nil
	}


M migrate.go => migrate.go +2 -1
@@ 62,7 62,7 @@ func New(
	if err = db.CreateMetaCheckpointsIfNotExists(); err != nil {
		return nil, errors.Wrap(err, "create meta checkpoints table")
	}
	curVersion, err := db.CreateMetaVersionIfNotExists()
	curVersion, err := db.CreateMetaVersionIfNotExists(version)
	if err != nil {
		return nil, errors.Wrap(err, "create meta version table")
	}


@@ 80,6 80,7 @@ func New(
		if err = db.UpgradeToV1(tmpMigrations); err != nil {
			return nil, errors.Wrap(err, "upgrade to v1")
		}
		curVersion = 1
	}

	// If skip, then we record the migrations but do not perform them. This

M mysql/mysql.go => mysql/mysql.go +19 -6
@@ 42,20 42,33 @@ func New(
	return db, nil
}

func (db *DB) CreateMetaVersionIfNotExists() (int, error) {
	q := `CREATE TABLE IF NOT EXISTS metaversion (
func (db *DB) CreateMetaVersionIfNotExists(schemaVersion int) (int, error) {
	created := true
	q := `CREATE TABLE metaversion (
		version INTEGER NOT NULL
	)`
	if _, err := db.Exec(q); err != nil {
		return 0, errors.Wrap(err, "create metaversion table")
	_, err := db.Exec(q)
	if err != nil {
		// Check if the table already existed
		if !strings.Contains(err.Error(), "Error 1050:") {
			return 0, errors.Wrap(err, "create metaversion table")
		}
		created = false
	}

	var version int
	q = `SELECT version FROM metaversion`
	err := db.Get(&version, q)
	err = db.Get(&version, q)
	switch {
	case err == sql.ErrNoRows:
		return 0, nil
		if !created {
			schemaVersion = 0
		}
		q = `INSERT INTO metaversion (version) VALUES (?)`
		if _, err := db.Exec(q, schemaVersion); err != nil {
			return 0, errors.Wrap(err, "insert version")
		}
		return schemaVersion, nil
	case err != nil:
		return 0, errors.Wrap(err, "get version")
	}

M postgres/postgres.go => postgres/postgres.go +17 -4
@@ 3,6 3,7 @@ package postgres
import (
	"database/sql"
	"fmt"
	"strings"

	"egt.run/migrate"
	"github.com/jmoiron/sqlx"


@@ 112,12 113,17 @@ func (db *DB) DeleteMetaCheckpoints() error {
	return err
}

func (db *DB) CreateMetaVersionIfNotExists() (int, error) {
	q := `CREATE TABLE IF NOT EXISTS metaversion (
func (db *DB) CreateMetaVersionIfNotExists(schemaVersion int) (int, error) {
	created := true
	q := `CREATE TABLE metaversion (
		version INTEGER NOT NULL
	)`
	if _, err := db.Exec(q); err != nil {
		return 0, errors.Wrap(err, "create metaversion table")
		// Check if the table already existed
		if !strings.Contains(err.Error(), "already exists") {
			return 0, errors.Wrap(err, "create metaversion table")
		}
		created = false
	}

	var version int


@@ 125,7 131,14 @@ func (db *DB) CreateMetaVersionIfNotExists() (int, error) {
	err := db.Get(&version, q)
	switch {
	case err == sql.ErrNoRows:
		return 0, nil
		if !created {
			schemaVersion = 0
		}
		q = `INSERT INTO metaversion (version) VALUES ($1)`
		if _, err := db.Exec(q, schemaVersion); err != nil {
			return 0, errors.Wrap(err, "insert version")
		}
		return schemaVersion, nil
	case err != nil:
		return 0, errors.Wrap(err, "get version")
	}

M sqlite/sqlite.go => sqlite/sqlite.go +17 -4
@@ 2,6 2,7 @@ package sqlite

import (
	"database/sql"
	"strings"

	"egt.run/migrate"
	"github.com/jmoiron/sqlx"


@@ 95,12 96,17 @@ func (db *DB) DeleteMetaCheckpoints() error {
	return err
}

func (db *DB) CreateMetaVersionIfNotExists() (int, error) {
	q := `CREATE TABLE IF NOT EXISTS metaversion (
func (db *DB) CreateMetaVersionIfNotExists(schemaVersion int) (int, error) {
	created := true
	q := `CREATE TABLE metaversion (
		version INTEGER NOT NULL
	)`
	if _, err := db.Exec(q); err != nil {
		return 0, errors.Wrap(err, "create metaversion table")
		// Check if the table already existed
		if !strings.Contains(err.Error(), "already exists") {
			return 0, errors.Wrap(err, "create metaversion table")
		}
		created = false
	}

	var version int


@@ 108,7 114,14 @@ func (db *DB) CreateMetaVersionIfNotExists() (int, error) {
	err := db.Get(&version, q)
	switch {
	case err == sql.ErrNoRows:
		return 0, nil
		if !created {
			schemaVersion = 0
		}
		q = `INSERT INTO metaversion (version) VALUES ($1)`
		if _, err := db.Exec(q, schemaVersion); err != nil {
			return 0, errors.Wrap(err, "insert version")
		}
		return schemaVersion, nil
	case err != nil:
		return 0, errors.Wrap(err, "get version")
	}

M store.go => store.go +1 -1
@@ 9,7 9,7 @@ type Store interface {
	Exec(string, ...interface{}) (sql.Result, error)

	// CreateMetaversionIfNotExists and report the current version.
	CreateMetaVersionIfNotExists() (int, error)
	CreateMetaVersionIfNotExists(schemaVersion int) (int, error)
	CreateMetaIfNotExists() error
	CreateMetaCheckpointsIfNotExists() error