migrate: Add support CQL comment callbacks
This patch adds a new migration event type CallComment that it triggered by adding `-- CALL <name>;` comment in a CQL file. Fixes #101
This commit is contained in:
committed by
Michal Jan Matczuk
parent
41e4a3fa11
commit
d25129e2fc
@@ -90,7 +90,7 @@ func TestMigration(t *testing.T) {
|
||||
dir := makeMigrationDir(t, 4)
|
||||
defer os.Remove(dir)
|
||||
|
||||
temperFile(t, dir, "3.cql")
|
||||
appendMigrationFile(t, dir, 3, "\nSELECT * FROM bla;\n")
|
||||
|
||||
if err := migrate.Migrate(ctx, session, dir); err == nil || !strings.Contains(err.Error(), "tempered") {
|
||||
t.Fatal("expected error")
|
||||
@@ -129,10 +129,62 @@ func TestMigrationNoSemicolon(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationCallback(t *testing.T) {
|
||||
var (
|
||||
beforeCalled int
|
||||
afterCalled int
|
||||
inCalled int
|
||||
)
|
||||
migrate.Callback = func(ctx context.Context, session gocqlx.Session, ev migrate.CallbackEvent, name string) error {
|
||||
switch ev {
|
||||
@@ -140,6 +192,8 @@ func TestMigrationCallback(t *testing.T) {
|
||||
beforeCalled += 1
|
||||
case migrate.AfterMigration:
|
||||
afterCalled += 1
|
||||
case migrate.CallComment:
|
||||
inCalled += 1
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -151,14 +205,18 @@ func TestMigrationCallback(t *testing.T) {
|
||||
reset := func() {
|
||||
beforeCalled = 0
|
||||
afterCalled = 0
|
||||
inCalled = 0
|
||||
}
|
||||
|
||||
assertCallbacks := func(t *testing.T, b, a int) {
|
||||
if beforeCalled != b {
|
||||
t.Fatalf("expected %d before calls got %d", b, beforeCalled)
|
||||
assertCallbacks := func(t *testing.T, before, afer, in int) {
|
||||
if beforeCalled != before {
|
||||
t.Fatalf("expected %d before calls got %d", before, beforeCalled)
|
||||
}
|
||||
if afterCalled != b {
|
||||
t.Fatalf("expected %d after calls got %d", a, afterCalled)
|
||||
if afterCalled != afer {
|
||||
t.Fatalf("expected %d after calls got %d", afer, afterCalled)
|
||||
}
|
||||
if inCalled != in {
|
||||
t.Fatalf("expected %d in calls got %d", in, inCalled)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +238,7 @@ func TestMigrationCallback(t *testing.T) {
|
||||
if err := migrate.Migrate(ctx, session, dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertCallbacks(t, 2, 2)
|
||||
assertCallbacks(t, 2, 2, 0)
|
||||
})
|
||||
|
||||
t.Run("no duplicate calls", func(t *testing.T) {
|
||||
@@ -191,7 +249,21 @@ func TestMigrationCallback(t *testing.T) {
|
||||
if err := migrate.Migrate(ctx, session, dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertCallbacks(t, 2, 2)
|
||||
assertCallbacks(t, 2, 2, 0)
|
||||
})
|
||||
|
||||
t.Run("in calls", func(t *testing.T) {
|
||||
dir := makeMigrationDir(t, 4)
|
||||
defer os.Remove(dir)
|
||||
reset()
|
||||
|
||||
appendMigrationFile(t, dir, 4, "\n-- CALL Foo;\n")
|
||||
appendMigrationFile(t, dir, 5, "\n-- CALL Bar;\n")
|
||||
|
||||
if err := migrate.Migrate(ctx, session, dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertCallbacks(t, 2, 2, 2)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -204,7 +276,7 @@ func makeMigrationDir(tb testing.TB, n int) (dir string) {
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
path := filepath.Join(dir, fmt.Sprint(i, ".cql"))
|
||||
path := migrateFilePath(dir, i)
|
||||
cql := []byte(fmt.Sprintf(insertMigrate, i) + ";")
|
||||
if err := ioutil.WriteFile(path, cql, os.ModePerm); err != nil {
|
||||
os.Remove(dir)
|
||||
@@ -225,10 +297,17 @@ func countMigrations(tb testing.TB, session gocqlx.Session) int {
|
||||
return v
|
||||
}
|
||||
|
||||
func temperFile(tb testing.TB, dir, name string) {
|
||||
tb.Helper()
|
||||
|
||||
if err := ioutil.WriteFile(filepath.Join(dir, name), []byte("SELECT * FROM bla;"), os.ModePerm); err != nil {
|
||||
func appendMigrationFile(tb testing.TB, dir string, i int, text string) {
|
||||
path := migrateFilePath(dir, i)
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
if _, err := f.WriteString(text); err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func migrateFilePath(dir string, i int) string {
|
||||
return filepath.Join(dir, fmt.Sprint(i, ".cql"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user