@@ -2,13 +2,15 @@ package cassandra
22
33import (
44 "fmt"
5+ "github.com/gocql/gocql"
6+ "github.com/mattes/migrate/database"
57 "io"
68 "io/ioutil"
79 nurl "net/url"
8- "github.com/gocql/gocql"
9- "time"
10- "github.com/mattes/migrate/database"
10+ "regexp"
1111 "strconv"
12+ "strings"
13+ "time"
1214)
1315
1416func init () {
@@ -20,8 +22,8 @@ var DefaultMigrationsTable = "schema_migrations"
2022var dbLocked = false
2123
2224var (
23- ErrNilConfig = fmt .Errorf ("no config" )
24- ErrNoKeyspace = fmt .Errorf ("no keyspace provided" )
25+ ErrNilConfig = fmt .Errorf ("no config" )
26+ ErrNoKeyspace = fmt .Errorf ("no keyspace provided" )
2527 ErrDatabaseDirty = fmt .Errorf ("database is dirty" )
2628)
2729
@@ -35,7 +37,7 @@ type Cassandra struct {
3537 isLocked bool
3638
3739 // Open and WithInstance need to guarantee that config is never nil
38- config * Config
40+ config * Config
3941}
4042
4143func (p * Cassandra ) Open (url string ) (database.Driver , error ) {
@@ -59,11 +61,23 @@ func (p *Cassandra) Open(url string) (database.Driver, error) {
5961 MigrationsTable : migrationsTable ,
6062 }
6163
64+ username := u .User .Username ()
65+ if len (username ) == 0 {
66+ username = "cassandra"
67+ }
68+ password , _ := u .User .Password ()
69+ if len (password ) == 0 {
70+ password = "cassandra"
71+ }
72+
6273 cluster := gocql .NewCluster (u .Host )
6374 cluster .Keyspace = u .Path [1 :len (u .Path )]
6475 cluster .Consistency = gocql .All
6576 cluster .Timeout = 1 * time .Minute
66-
77+ cluster .Authenticator = gocql.PasswordAuthenticator {
78+ Username : username ,
79+ Password : password ,
80+ }
6781
6882 // Retrieve query string configuration
6983 if len (u .Query ().Get ("consistency" )) > 0 {
@@ -111,7 +125,7 @@ func (p *Cassandra) Close() error {
111125}
112126
113127func (p * Cassandra ) Lock () error {
114- if ( dbLocked ) {
128+ if dbLocked {
115129 return database .ErrLocked
116130 }
117131 dbLocked = true
@@ -130,9 +144,17 @@ func (p *Cassandra) Run(migration io.Reader) error {
130144 }
131145 // run migration
132146 query := string (migr [:])
133- if err := p .session .Query (query ).Exec (); err != nil {
134- // TODO: cast to Cassandra error and get line number
135- return database.Error {OrigErr : err , Err : "migration failed" , Query : migr }
147+ matches := regexp .MustCompile (`(?m:;$)` ).Split (query , - 1 )
148+ for _ , match := range matches {
149+ trimmedMatch := strings .Trim (match , " \t \r \n " )
150+ if len (trimmedMatch ) == 0 {
151+ continue
152+ }
153+
154+ if err := p .session .Query (trimmedMatch ).Exec (); err != nil {
155+ // TODO: cast to Cassandra error and get line number
156+ return database.Error {OrigErr : err , Err : "migration failed" , Query : migr }
157+ }
136158 }
137159
138160 return nil
@@ -153,7 +175,6 @@ func (p *Cassandra) SetVersion(version int, dirty bool) error {
153175 return nil
154176}
155177
156-
157178// Return current keyspace version
158179func (p * Cassandra ) Version () (version int , dirty bool , err error ) {
159180 query := `SELECT version, dirty FROM "` + p .config .MigrationsTable + `" LIMIT 1`
@@ -191,7 +212,6 @@ func (p *Cassandra) Drop() error {
191212 return nil
192213}
193214
194-
195215// Ensure version table exists
196216func (p * Cassandra ) ensureVersionTable () error {
197217 err := p .session .Query (fmt .Sprintf ("CREATE TABLE IF NOT EXISTS %s (version bigint, dirty boolean, PRIMARY KEY(version))" , p .config .MigrationsTable )).Exec ()
@@ -204,7 +224,6 @@ func (p *Cassandra) ensureVersionTable() error {
204224 return nil
205225}
206226
207-
208227// ParseConsistency wraps gocql.ParseConsistency
209228// to return an error instead of a panicking.
210229func parseConsistency (consistencyStr string ) (consistency gocql.Consistency , err error ) {
0 commit comments