migrate: refactor examples to use the new FromFS function
This commit is contained in:
committed by
Michal Jan Matczuk
parent
fe1498fe13
commit
d123214401
13
migrate/example/cql/embed.go
Normal file
13
migrate/example/cql/embed.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright (C) 2017 ScyllaDB
|
||||
// Use of this source code is governed by a ALv2-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build all integration
|
||||
|
||||
package cql
|
||||
|
||||
import "embed"
|
||||
|
||||
// Files contains *.cql schema migration files.
|
||||
//go:embed *.cql
|
||||
var Files embed.FS
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/scylladb/gocqlx/v2"
|
||||
"github.com/scylladb/gocqlx/v2/gocqlxtest"
|
||||
"github.com/scylladb/gocqlx/v2/migrate"
|
||||
"github.com/scylladb/gocqlx/v2/migrate/example/cql"
|
||||
)
|
||||
|
||||
// Running examples locally:
|
||||
@@ -21,12 +22,14 @@ import (
|
||||
func TestExample(t *testing.T) {
|
||||
const ks = "migrate_example"
|
||||
|
||||
// Create keyspace
|
||||
cluster := gocqlxtest.CreateCluster()
|
||||
cluster.Keyspace = ks
|
||||
|
||||
if err := gocqlxtest.CreateKeyspace(cluster, ks); err != nil {
|
||||
t.Fatal("CreateKeyspace:", err)
|
||||
}
|
||||
|
||||
// Create session using the keyspace
|
||||
session, err := gocqlx.WrapSession(cluster.CreateSession())
|
||||
if err != nil {
|
||||
t.Fatal("CreateSession:", err)
|
||||
@@ -34,27 +37,25 @@ func TestExample(t *testing.T) {
|
||||
defer session.Close()
|
||||
|
||||
// Add callback prints
|
||||
printEvent := func(ctx context.Context, session gocqlx.Session, ev migrate.CallbackEvent, name string) error {
|
||||
log := func(ctx context.Context, session gocqlx.Session, ev migrate.CallbackEvent, name string) error {
|
||||
t.Log(ev, name)
|
||||
return nil
|
||||
}
|
||||
|
||||
reg := migrate.CallbackRegister{}
|
||||
reg.Add(migrate.BeforeMigration, "m1.cql", printEvent)
|
||||
reg.Add(migrate.AfterMigration, "m1.cql", printEvent)
|
||||
reg.Add(migrate.CallComment, "1", printEvent)
|
||||
reg.Add(migrate.CallComment, "2", printEvent)
|
||||
reg.Add(migrate.CallComment, "3", printEvent)
|
||||
|
||||
reg.Add(migrate.BeforeMigration, "m1.cql", log)
|
||||
reg.Add(migrate.AfterMigration, "m1.cql", log)
|
||||
reg.Add(migrate.CallComment, "1", log)
|
||||
reg.Add(migrate.CallComment, "2", log)
|
||||
reg.Add(migrate.CallComment, "3", log)
|
||||
migrate.Callback = reg.Callback
|
||||
|
||||
// First run prints data
|
||||
if err := migrate.Migrate(context.Background(), session, "migrations"); err != nil {
|
||||
if err := migrate.FromFS(context.Background(), session, cql.Files); err != nil {
|
||||
t.Fatal("Migrate:", err)
|
||||
}
|
||||
|
||||
// Second run skips the processed files
|
||||
if err := migrate.Migrate(context.Background(), session, "migrations"); err != nil {
|
||||
if err := migrate.FromFS(context.Background(), session, cql.Files); err != nil {
|
||||
t.Fatal("Migrate:", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user