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

Add possiblity for cassandra migrations to have multiple query statem… #342

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions database/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
nurl "net/url"
"strconv"
"strings"
"regexp"
"time"

"github.com/gocql/gocql"
Expand Down Expand Up @@ -137,10 +139,26 @@ func (p *Cassandra) Run(migration io.Reader) error {
return err
}
// 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}
var isPreparedStatement = regexp.MustCompile(`(?mi)^BEGIN BATCH`)
var splitRegex = regexp.MustCompile(`(?m);\s*$`)

queries := string(migr[:])
requests := []string{queries}
if isPreparedStatement.MatchString(queries) != true {
requests = splitRegex.Split(queries, -1)
}

for i, query := range requests {
if strings.TrimSpace(query) == "" {
continue
}
if i < len(requests) - 1 {
query += ";"
}
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}
}
}

return nil
Expand Down