Skip to content
This repository has been archived by the owner on Jun 28, 2018. It is now read-only.

Add support for execution of multiple statements in one migration file #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions database/cassandra/cassandra.go
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"io"
"io/ioutil"
nurl "net/url"
"regexp"
"strconv"
"strings"
"time"
nurl "net/url"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use gofmt.


"github.com/gocql/gocql"
"github.com/mattes/migrate/database"
Expand Down Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't execute multiple queries before?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just submitted #280 (but didn't know about this PR until now) which has an opt-in when it comes to multi-statement support to avoid the case of

INSERT INTO codesamples(code) VALUES ('import a;
import b;');

Might be worth considering.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, unfortunately not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a few unit tests to make sure that this works as expected?

Copy link
Author

@syndbg syndbg Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had troubles getting them running locally, but I got this working eventually. Still I can't find why they're considered a FAIL.

Atm the tests fail for me even before the changes.

I'll spend more time to see why they're failing before I start to add more tests (that are probably going to fail either way).

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
Expand Down
5 changes: 3 additions & 2 deletions database/cassandra/cassandra_test.go
Expand Up @@ -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"},
}

Expand All @@ -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
Expand All @@ -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)
Expand Down