From 2195521b5609189062118a9203086a26f62f89f5 Mon Sep 17 00:00:00 2001 From: Anton Antonov Date: Wed, 15 Nov 2017 22:48:28 +0200 Subject: [PATCH 1/2] Support multiple statements migrations --- database/cassandra/cassandra.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/database/cassandra/cassandra.go b/database/cassandra/cassandra.go index 42563fdb..bcbfe606 100644 --- a/database/cassandra/cassandra.go +++ b/database/cassandra/cassandra.go @@ -4,9 +4,11 @@ import ( "fmt" "io" "io/ioutil" - nurl "net/url" + "regexp" "strconv" + "strings" "time" + nurl "net/url" "github.com/gocql/gocql" "github.com/mattes/migrate/database" @@ -136,11 +138,19 @@ func (p *Cassandra) Run(migration io.Reader) error { if err != nil { return err } - // run migration + // split multiple statements and run migration query := string(migr[:]) - if err := p.session.Query(query).Exec(); err != nil { - // TODO: cast to Cassandra error and get line number - return database.Error{OrigErr: err, Err: "migration failed", Query: migr} + matches := regexp.MustCompile(`(?m:;$)`).Split(query, -1) + for _, match := range matches { + trimmedMatch := strings.Trim(match, " \t\r\n") + if len(trimmedMatch) == 0 { + continue + } + + if err := p.session.Query(trimmedMatch).Exec(); err != nil { + // TODO: cast to Cassandra error and get line number + return database.Error{OrigErr: err, Err: "migration failed", Query: migr} + } } return nil From 69809373d355850b3bd3bbb7a6f73017d8dd58d6 Mon Sep 17 00:00:00 2001 From: Anton Antonov Date: Thu, 16 Nov 2017 00:35:51 +0200 Subject: [PATCH 2/2] Make Cassandra tests runnable locally (at least) Wasn't able to run them locally due to `gocql` not able to connect at all. Instead, force Cassandra container to broadcast to localhost and auth with default credentials. --- database/cassandra/cassandra_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/database/cassandra/cassandra_test.go b/database/cassandra/cassandra_test.go index 4ca764a0..ebbf13f6 100644 --- a/database/cassandra/cassandra_test.go +++ b/database/cassandra/cassandra_test.go @@ -11,7 +11,7 @@ import ( ) var versions = []mt.Version{ - {Image: "cassandra:3.0.10"}, + {Image: "cassandra:3.0.10", ENV: []string{"CASSANDRA_BROADCAST_ADDRESS=127.0.0.1"} }, {Image: "cassandra:3.0"}, } @@ -28,6 +28,7 @@ func isReady(i mt.Instance) bool { //cluster.ProtoVersion = 4 cluster.Consistency = gocql.All cluster.Timeout = 1 * time.Minute + cluster.Authenticator = gocql.PasswordAuthenticator{Username: "cassandra", Password: "cassandra"} p, err := cluster.CreateSession() if err != nil { return false @@ -43,7 +44,7 @@ func Test(t *testing.T) { p := &Cassandra{} portMap := i.NetworkSettings().Ports port, _ := strconv.Atoi(portMap["9042/tcp"][0].HostPort) - addr := fmt.Sprintf("cassandra://%v:%v/testks", i.Host(), port) + addr := fmt.Sprintf("cassandra://%v:%v/testks?username=cassandra&password=cassandra", i.Host(), port) d, err := p.Open(addr) if err != nil { t.Fatalf("%v", err)