2019-02-12 16:57:26 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package migrate_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/dbutil/pgutil/pgtest"
|
2019-12-04 03:36:21 +00:00
|
|
|
"storj.io/storj/private/dbutil/tempdb"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/migrate"
|
|
|
|
"storj.io/storj/private/testcontext"
|
2019-02-12 16:57:26 +00:00
|
|
|
)
|
|
|
|
|
2019-09-20 17:26:07 +01:00
|
|
|
func TestBasicMigrationSqliteNoRebind(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
basicMigration(t, db, db)
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
func TestBasicMigrationSqlite(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
basicMigration(t, db, &sqliteDB{DB: db})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicMigrationPostgres(t *testing.T) {
|
2019-04-26 14:39:11 +01:00
|
|
|
if *pgtest.ConnStr == "" {
|
|
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", pgtest.DefaultConnStr)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
2019-12-04 03:36:21 +00:00
|
|
|
testBasicMigrationGeneric(t, *pgtest.ConnStr)
|
|
|
|
}
|
2019-02-12 16:57:26 +00:00
|
|
|
|
2019-12-04 03:36:21 +00:00
|
|
|
func TestBasicMigrationCockroach(t *testing.T) {
|
|
|
|
if *pgtest.CrdbConnStr == "" {
|
|
|
|
t.Skipf("cockroach flag missing, example:\n-cockroach-test-db=%s", pgtest.DefaultCrdbConnStr)
|
|
|
|
}
|
|
|
|
testBasicMigrationGeneric(t, *pgtest.CrdbConnStr)
|
|
|
|
}
|
2019-02-21 09:28:10 +00:00
|
|
|
|
2019-12-04 03:36:21 +00:00
|
|
|
func testBasicMigrationGeneric(t *testing.T, connStr string) {
|
|
|
|
db, err := tempdb.OpenUnique(connStr, "create-")
|
2019-02-21 09:28:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-12 16:57:26 +00:00
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
2019-12-04 03:36:21 +00:00
|
|
|
basicMigration(t, db.DB, &postgresDB{DB: db.DB})
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func basicMigration(t *testing.T, db *sql.DB, testDB migrate.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
dbName := strings.ToLower(`versions_` + t.Name())
|
|
|
|
defer func() { assert.NoError(t, dropTables(db, dbName, "users")) }()
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(ctx.File("alpha.txt"), []byte("test"), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
m := migrate.Migration{
|
|
|
|
Table: dbName,
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
2019-09-10 20:42:23 +01:00
|
|
|
DB: testDB,
|
2019-02-12 16:57:26 +00:00
|
|
|
Description: "Initialize Table",
|
|
|
|
Version: 1,
|
|
|
|
Action: migrate.SQL{
|
|
|
|
`CREATE TABLE users (id int)`,
|
|
|
|
`INSERT INTO users (id) VALUES (1)`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-09-10 20:42:23 +01:00
|
|
|
DB: testDB,
|
2019-02-12 16:57:26 +00:00
|
|
|
Description: "Move files",
|
|
|
|
Version: 2,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, _ migrate.DB, tx *sql.Tx) error {
|
|
|
|
return os.Rename(ctx.File("alpha.txt"), ctx.File("beta.txt"))
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:52:57 +00:00
|
|
|
dbVersion, err := m.CurrentVersion(nil, testDB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, dbVersion, -1)
|
|
|
|
|
2019-09-10 20:42:23 +01:00
|
|
|
err = m.Run(zap.NewNop())
|
2019-02-12 16:57:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-11-19 20:52:57 +00:00
|
|
|
dbVersion, err = m.CurrentVersion(nil, testDB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, dbVersion, 2)
|
|
|
|
|
|
|
|
m2 := migrate.Migration{
|
|
|
|
Table: dbName,
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
|
|
|
DB: testDB,
|
|
|
|
Version: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dbVersion, err = m2.CurrentVersion(nil, testDB)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, dbVersion, 2)
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
var version int
|
|
|
|
err = db.QueryRow(`SELECT MAX(version) FROM ` + dbName).Scan(&version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, version)
|
|
|
|
|
|
|
|
var id int
|
|
|
|
err = db.QueryRow(`SELECT MAX(id) FROM users`).Scan(&id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, id)
|
|
|
|
|
|
|
|
// file not exists
|
|
|
|
_, err = os.Stat(ctx.File("alpha.txt"))
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// file exists
|
|
|
|
_, err = os.Stat(ctx.File("beta.txt"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
data, err := ioutil.ReadFile(ctx.File("beta.txt"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("test"), data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleMigrationSqlite(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
multipleMigration(t, db, &sqliteDB{DB: db})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleMigrationPostgres(t *testing.T) {
|
2019-04-26 14:39:11 +01:00
|
|
|
if *pgtest.ConnStr == "" {
|
|
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", pgtest.DefaultConnStr)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 14:39:11 +01:00
|
|
|
db, err := sql.Open("postgres", *pgtest.ConnStr)
|
2019-02-12 16:57:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
multipleMigration(t, db, &postgresDB{DB: db})
|
|
|
|
}
|
|
|
|
|
|
|
|
func multipleMigration(t *testing.T, db *sql.DB, testDB migrate.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
dbName := strings.ToLower(`versions_` + t.Name())
|
|
|
|
defer func() { assert.NoError(t, dropTables(db, dbName)) }()
|
|
|
|
|
|
|
|
steps := 0
|
|
|
|
m := migrate.Migration{
|
|
|
|
Table: dbName,
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
2019-09-10 20:42:23 +01:00
|
|
|
DB: testDB,
|
2019-02-12 16:57:26 +00:00
|
|
|
Description: "Step 1",
|
|
|
|
Version: 1,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, _ migrate.DB, tx *sql.Tx) error {
|
|
|
|
steps++
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
2019-09-10 20:42:23 +01:00
|
|
|
DB: testDB,
|
2019-02-12 16:57:26 +00:00
|
|
|
Description: "Step 2",
|
|
|
|
Version: 2,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, _ migrate.DB, tx *sql.Tx) error {
|
|
|
|
steps++
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-10 20:42:23 +01:00
|
|
|
err := m.Run(zap.NewNop())
|
2019-02-12 16:57:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, steps)
|
|
|
|
|
|
|
|
m.Steps = append(m.Steps, &migrate.Step{
|
2019-09-10 20:42:23 +01:00
|
|
|
DB: testDB,
|
2019-02-12 16:57:26 +00:00
|
|
|
Description: "Step 3",
|
|
|
|
Version: 3,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, _ migrate.DB, tx *sql.Tx) error {
|
|
|
|
steps++
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
})
|
2019-09-10 20:42:23 +01:00
|
|
|
err = m.Run(zap.NewNop())
|
2019-02-12 16:57:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var version int
|
|
|
|
err = db.QueryRow(`SELECT MAX(version) FROM ` + dbName).Scan(&version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, version)
|
|
|
|
|
|
|
|
assert.Equal(t, 3, steps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailedMigrationSqlite(t *testing.T) {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
failedMigration(t, db, &sqliteDB{DB: db})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailedMigrationPostgres(t *testing.T) {
|
2019-04-26 14:39:11 +01:00
|
|
|
if *pgtest.ConnStr == "" {
|
|
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", pgtest.DefaultConnStr)
|
2019-02-12 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 14:39:11 +01:00
|
|
|
db, err := sql.Open("postgres", *pgtest.ConnStr)
|
2019-02-12 16:57:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, db.Close()) }()
|
|
|
|
|
|
|
|
failedMigration(t, db, &postgresDB{DB: db})
|
|
|
|
}
|
|
|
|
|
|
|
|
func failedMigration(t *testing.T, db *sql.DB, testDB migrate.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
dbName := strings.ToLower(`versions_` + t.Name())
|
|
|
|
defer func() { assert.NoError(t, dropTables(db, dbName)) }()
|
|
|
|
|
|
|
|
m := migrate.Migration{
|
|
|
|
Table: dbName,
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
2019-09-10 20:42:23 +01:00
|
|
|
DB: testDB,
|
2019-02-12 16:57:26 +00:00
|
|
|
Description: "Step 1",
|
|
|
|
Version: 1,
|
|
|
|
Action: migrate.Func(func(log *zap.Logger, _ migrate.DB, tx *sql.Tx) error {
|
|
|
|
return fmt.Errorf("migration failed")
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-10 20:42:23 +01:00
|
|
|
err := m.Run(zap.NewNop())
|
2019-02-12 16:57:26 +00:00
|
|
|
require.Error(t, err, "migration failed")
|
|
|
|
|
|
|
|
var version sql.NullInt64
|
|
|
|
err = db.QueryRow(`SELECT MAX(version) FROM ` + dbName).Scan(&version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, false, version.Valid)
|
|
|
|
}
|
|
|
|
|
2019-02-19 09:39:04 +00:00
|
|
|
func TestTargetVersion(t *testing.T) {
|
|
|
|
m := migrate.Migration{
|
|
|
|
Table: "test",
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
|
|
|
Description: "Step 1",
|
|
|
|
Version: 1,
|
|
|
|
Action: migrate.SQL{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Step 2",
|
|
|
|
Version: 2,
|
|
|
|
Action: migrate.SQL{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Step 2.2",
|
|
|
|
Version: 2,
|
|
|
|
Action: migrate.SQL{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Step 3",
|
|
|
|
Version: 3,
|
|
|
|
Action: migrate.SQL{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testedMigration := m.TargetVersion(2)
|
|
|
|
assert.Equal(t, 3, len(testedMigration.Steps))
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:57:26 +00:00
|
|
|
func TestInvalidStepsOrder(t *testing.T) {
|
|
|
|
m := migrate.Migration{
|
|
|
|
Table: "test",
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
|
|
|
Version: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Version: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.ValidateSteps()
|
|
|
|
require.Error(t, err, "migrate: steps have incorrect order")
|
|
|
|
}
|
|
|
|
|
|
|
|
func dropTables(db *sql.DB, names ...string) error {
|
|
|
|
var errlist errs.Group
|
|
|
|
for _, name := range names {
|
|
|
|
_, err := db.Exec(`DROP TABLE ` + name)
|
|
|
|
errlist.Add(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errlist.Err()
|
|
|
|
}
|