Skip to content

Commit d123214

Browse files
committed
migrate: refactor examples to use the new FromFS function
1 parent fe1498f commit d123214

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Diff for: migrate/example/cql/embed.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (C) 2017 ScyllaDB
2+
// Use of this source code is governed by a ALv2-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build all integration
6+
7+
package cql
8+
9+
import "embed"
10+
11+
// Files contains *.cql schema migration files.
12+
//go:embed *.cql
13+
var Files embed.FS
File renamed without changes.

Diff for: migrate/example/example_test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/scylladb/gocqlx/v2"
1414
"github.com/scylladb/gocqlx/v2/gocqlxtest"
1515
"github.com/scylladb/gocqlx/v2/migrate"
16+
"github.com/scylladb/gocqlx/v2/migrate/example/cql"
1617
)
1718

1819
// Running examples locally:
@@ -21,40 +22,40 @@ import (
2122
func TestExample(t *testing.T) {
2223
const ks = "migrate_example"
2324

25+
// Create keyspace
2426
cluster := gocqlxtest.CreateCluster()
2527
cluster.Keyspace = ks
26-
2728
if err := gocqlxtest.CreateKeyspace(cluster, ks); err != nil {
2829
t.Fatal("CreateKeyspace:", err)
2930
}
31+
32+
// Create session using the keyspace
3033
session, err := gocqlx.WrapSession(cluster.CreateSession())
3134
if err != nil {
3235
t.Fatal("CreateSession:", err)
3336
}
3437
defer session.Close()
3538

3639
// Add callback prints
37-
printEvent := func(ctx context.Context, session gocqlx.Session, ev migrate.CallbackEvent, name string) error {
40+
log := func(ctx context.Context, session gocqlx.Session, ev migrate.CallbackEvent, name string) error {
3841
t.Log(ev, name)
3942
return nil
4043
}
41-
4244
reg := migrate.CallbackRegister{}
43-
reg.Add(migrate.BeforeMigration, "m1.cql", printEvent)
44-
reg.Add(migrate.AfterMigration, "m1.cql", printEvent)
45-
reg.Add(migrate.CallComment, "1", printEvent)
46-
reg.Add(migrate.CallComment, "2", printEvent)
47-
reg.Add(migrate.CallComment, "3", printEvent)
48-
45+
reg.Add(migrate.BeforeMigration, "m1.cql", log)
46+
reg.Add(migrate.AfterMigration, "m1.cql", log)
47+
reg.Add(migrate.CallComment, "1", log)
48+
reg.Add(migrate.CallComment, "2", log)
49+
reg.Add(migrate.CallComment, "3", log)
4950
migrate.Callback = reg.Callback
5051

5152
// First run prints data
52-
if err := migrate.Migrate(context.Background(), session, "migrations"); err != nil {
53+
if err := migrate.FromFS(context.Background(), session, cql.Files); err != nil {
5354
t.Fatal("Migrate:", err)
5455
}
5556

5657
// Second run skips the processed files
57-
if err := migrate.Migrate(context.Background(), session, "migrations"); err != nil {
58+
if err := migrate.FromFS(context.Background(), session, cql.Files); err != nil {
5859
t.Fatal("Migrate:", err)
5960
}
6061
}

0 commit comments

Comments
 (0)