Skip to content

Commit

Permalink
migrate: refactor examples to use the new FromFS function
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Apr 30, 2021
1 parent fe1498f commit d123214
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
13 changes: 13 additions & 0 deletions migrate/example/cql/embed.go
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
23 changes: 12 additions & 11 deletions migrate/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -21,40 +22,40 @@ 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)
}
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)
}
}

0 comments on commit d123214

Please sign in to comment.