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

Commit

Permalink
Support multiple statements migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
syndbg committed Nov 15, 2017
1 parent 69472d5 commit 2195521
Showing 1 changed file with 15 additions and 5 deletions.
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"

"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)
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

0 comments on commit 2195521

Please sign in to comment.