-
Notifications
You must be signed in to change notification settings - Fork 323
Add support for execution of multiple statements in one migration file #276
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It didn't execute multiple queries before? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, unfortunately not. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use gofmt.