2018-05-24 12:54:11 +02:00
|
|
|
|
// Copyright (C) 2017 ScyllaDB
|
|
|
|
|
|
// Use of this source code is governed by a ALv2-style
|
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
2021-11-20 21:23:18 +03:00
|
|
|
|
//go:build all || integration
|
2018-05-24 12:54:11 +02:00
|
|
|
|
// +build all integration
|
|
|
|
|
|
|
|
|
|
|
|
package migrate_test
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
2021-04-29 18:19:32 +02:00
|
|
|
|
"io/fs"
|
2018-05-24 12:54:11 +02:00
|
|
|
|
"strings"
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
"github.com/psanford/memfs"
|
2024-06-14 13:07:21 -04:00
|
|
|
|
|
2020-04-29 09:50:30 +02:00
|
|
|
|
"github.com/scylladb/gocqlx/v2"
|
2024-06-14 13:07:21 -04:00
|
|
|
|
"github.com/scylladb/gocqlx/v2/gocqlxtest"
|
2020-04-29 09:50:30 +02:00
|
|
|
|
"github.com/scylladb/gocqlx/v2/migrate"
|
2018-05-24 12:54:11 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var migrateSchema = `
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS gocqlx_test.migrate_table (
|
|
|
|
|
|
testint int,
|
|
|
|
|
|
testuuid timeuuid,
|
|
|
|
|
|
PRIMARY KEY(testint, testuuid)
|
|
|
|
|
|
)
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
var insertMigrate = `INSERT INTO gocqlx_test.migrate_table (testint, testuuid) VALUES (%d, now())`
|
|
|
|
|
|
|
2020-04-20 17:39:17 +02:00
|
|
|
|
func recreateTables(tb testing.TB, session gocqlx.Session) {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
2020-04-20 17:39:17 +02:00
|
|
|
|
if err := session.ExecStmt("DROP TABLE IF EXISTS gocqlx_test.gocqlx_migrate"); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
tb.Fatal(err)
|
|
|
|
|
|
}
|
2020-04-20 17:39:17 +02:00
|
|
|
|
if err := session.ExecStmt(migrateSchema); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
tb.Fatal(err)
|
|
|
|
|
|
}
|
2020-04-20 17:39:17 +02:00
|
|
|
|
if err := session.ExecStmt("TRUNCATE gocqlx_test.migrate_table"); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
tb.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestMigration(t *testing.T) {
|
2024-06-14 13:07:21 -04:00
|
|
|
|
session := gocqlxtest.CreateSession(t)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
defer session.Close()
|
|
|
|
|
|
recreateTables(t, session)
|
|
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("init", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
if err := migrate.FromFS(ctx, session, makeTestFS(2)); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if c := countMigrations(t, session); c != 2 {
|
|
|
|
|
|
t.Fatal("expected 2 migration got", c)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("update", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
if err := migrate.FromFS(ctx, session, makeTestFS(4)); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if c := countMigrations(t, session); c != 4 {
|
|
|
|
|
|
t.Fatal("expected 4 migration got", c)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("ahead", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
err := migrate.FromFS(ctx, session, makeTestFS(2))
|
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "ahead") {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal("expected error")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
t.Log(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("tempered with file", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
f := makeTestFS(4)
|
|
|
|
|
|
writeFile(f, 3, "SELECT * FROM bla;")
|
2018-05-24 12:54:11 +02:00
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
if err := migrate.FromFS(ctx, session, f); err == nil || !strings.Contains(err.Error(), "tempered") {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal("expected error")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
t.Log(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestMigrationNoSemicolon(t *testing.T) {
|
2024-06-14 13:07:21 -04:00
|
|
|
|
session := gocqlxtest.CreateSession(t)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
defer session.Close()
|
|
|
|
|
|
recreateTables(t, session)
|
|
|
|
|
|
|
2020-04-20 17:39:17 +02:00
|
|
|
|
if err := session.ExecStmt(migrateSchema); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
f := makeTestFS(0)
|
|
|
|
|
|
f.WriteFile("0.cql", []byte(fmt.Sprintf(insertMigrate, 0)+";"+fmt.Sprintf(insertMigrate, 1)), fs.ModePerm)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
if err := migrate.FromFS(ctx, session, f); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if c := countMigrations(t, session); c != 2 {
|
|
|
|
|
|
t.Fatal("expected 2 migration got", c)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 13:18:49 +01:00
|
|
|
|
func TestIsCallback(t *testing.T) {
|
|
|
|
|
|
table := []struct {
|
|
|
|
|
|
Name string
|
|
|
|
|
|
Stmt string
|
|
|
|
|
|
Cb string
|
|
|
|
|
|
}{
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CQL statement",
|
|
|
|
|
|
Stmt: "SELECT * from X;",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CQL comment",
|
|
|
|
|
|
Stmt: "-- Item",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CALL without space",
|
|
|
|
|
|
Stmt: "--CALL Foo;",
|
|
|
|
|
|
Cb: "Foo",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CALL with space",
|
|
|
|
|
|
Stmt: "-- CALL Foo;",
|
|
|
|
|
|
Cb: "Foo",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CALL with many spaces",
|
|
|
|
|
|
Stmt: "-- CALL Foo;",
|
|
|
|
|
|
Cb: "Foo",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CALL with many spaces 2",
|
|
|
|
|
|
Stmt: "-- CALL Foo;",
|
|
|
|
|
|
Cb: "Foo",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: "CALL with unicode",
|
|
|
|
|
|
Stmt: "-- CALL α;",
|
|
|
|
|
|
Cb: "α",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i := range table {
|
|
|
|
|
|
test := table[i]
|
|
|
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
|
|
|
|
|
if migrate.IsCallback(test.Stmt) != test.Cb {
|
|
|
|
|
|
t.Errorf("IsCallback(%s)=%s, expected %s", test.Stmt, migrate.IsCallback(test.Stmt), test.Cb)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-24 12:54:11 +02:00
|
|
|
|
func TestMigrationCallback(t *testing.T) {
|
|
|
|
|
|
var (
|
|
|
|
|
|
beforeCalled int
|
|
|
|
|
|
afterCalled int
|
2020-12-02 13:18:49 +01:00
|
|
|
|
inCalled int
|
2018-05-24 12:54:11 +02:00
|
|
|
|
)
|
2020-04-20 17:39:17 +02:00
|
|
|
|
migrate.Callback = func(ctx context.Context, session gocqlx.Session, ev migrate.CallbackEvent, name string) error {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
switch ev {
|
|
|
|
|
|
case migrate.BeforeMigration:
|
2024-06-14 13:07:21 -04:00
|
|
|
|
beforeCalled++
|
2018-05-24 12:54:11 +02:00
|
|
|
|
case migrate.AfterMigration:
|
2024-06-14 13:07:21 -04:00
|
|
|
|
afterCalled++
|
2020-12-02 13:18:49 +01:00
|
|
|
|
case migrate.CallComment:
|
2024-06-14 13:07:21 -04:00
|
|
|
|
inCalled++
|
2018-05-24 12:54:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
migrate.Callback = nil
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
reset := func() {
|
|
|
|
|
|
beforeCalled = 0
|
|
|
|
|
|
afterCalled = 0
|
2020-12-02 13:18:49 +01:00
|
|
|
|
inCalled = 0
|
2018-05-24 12:54:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 13:18:49 +01:00
|
|
|
|
assertCallbacks := func(t *testing.T, before, afer, in int) {
|
2024-06-14 13:07:21 -04:00
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
2020-12-02 13:18:49 +01:00
|
|
|
|
if beforeCalled != before {
|
|
|
|
|
|
t.Fatalf("expected %d before calls got %d", before, beforeCalled)
|
|
|
|
|
|
}
|
|
|
|
|
|
if afterCalled != afer {
|
|
|
|
|
|
t.Fatalf("expected %d after calls got %d", afer, afterCalled)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
}
|
2020-12-02 13:18:49 +01:00
|
|
|
|
if inCalled != in {
|
|
|
|
|
|
t.Fatalf("expected %d in calls got %d", in, inCalled)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-14 13:07:21 -04:00
|
|
|
|
session := gocqlxtest.CreateSession(t)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
defer session.Close()
|
|
|
|
|
|
recreateTables(t, session)
|
|
|
|
|
|
|
2020-04-20 17:39:17 +02:00
|
|
|
|
if err := session.ExecStmt(migrateSchema); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("init", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
f := makeTestFS(2)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
reset()
|
|
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
if err := migrate.FromFS(ctx, session, f); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
2020-12-02 13:18:49 +01:00
|
|
|
|
assertCallbacks(t, 2, 2, 0)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("no duplicate calls", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
f := makeTestFS(4)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
reset()
|
|
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
if err := migrate.FromFS(ctx, session, f); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
2020-12-02 13:18:49 +01:00
|
|
|
|
assertCallbacks(t, 2, 2, 0)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("in calls", func(t *testing.T) {
|
2021-04-29 18:19:32 +02:00
|
|
|
|
f := makeTestFS(4)
|
|
|
|
|
|
writeFile(f, 4, "\n-- CALL Foo;\n")
|
|
|
|
|
|
writeFile(f, 5, "\n-- CALL Bar;\n")
|
2020-12-02 13:18:49 +01:00
|
|
|
|
reset()
|
|
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
if err := migrate.FromFS(ctx, session, f); err != nil {
|
2020-12-02 13:18:49 +01:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
assertCallbacks(t, 2, 2, 2)
|
2018-05-24 12:54:11 +02:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-20 17:39:17 +02:00
|
|
|
|
func countMigrations(tb testing.TB, session gocqlx.Session) int {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
|
|
|
|
var v int
|
2020-04-20 17:39:17 +02:00
|
|
|
|
if err := session.Query("SELECT COUNT(*) FROM gocqlx_test.migrate_table", nil).Get(&v); err != nil {
|
2018-05-24 12:54:11 +02:00
|
|
|
|
tb.Fatal(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
func makeTestFS(n int) *memfs.FS {
|
|
|
|
|
|
f := memfs.New()
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
|
writeFile(f, i, fmt.Sprintf(insertMigrate, i)+";")
|
2018-05-24 12:54:11 +02:00
|
|
|
|
}
|
2021-04-29 18:19:32 +02:00
|
|
|
|
return f
|
2018-05-24 12:54:11 +02:00
|
|
|
|
}
|
2020-12-02 13:18:49 +01:00
|
|
|
|
|
2021-04-29 18:19:32 +02:00
|
|
|
|
func writeFile(f *memfs.FS, i int, text string) {
|
|
|
|
|
|
f.WriteFile(fmt.Sprint(i, ".cql"), []byte(text), fs.ModePerm)
|
2020-12-02 13:18:49 +01:00
|
|
|
|
}
|