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

Commit

Permalink
Add Cassandra Enhancements
Browse files Browse the repository at this point in the history
* Authentication functionality,
* Execution of multiple statements in one migration file
  • Loading branch information
syndbg committed Sep 15, 2017
1 parent 804f9c2 commit 6445880
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions database/cassandra/cassandra.go
Expand Up @@ -59,11 +59,23 @@ func (p *Cassandra) Open(url string) (database.Driver, error) {
MigrationsTable: migrationsTable,
}

username := u.User.Username()
if len(username) == 0 {
username = "cassandra"
}
password, _ := u.User.Password()
if len(password) == 0 {
password = "cassandra"
}

cluster := gocql.NewCluster(u.Host)
cluster.Keyspace = u.Path[1:len(u.Path)]
cluster.Consistency = gocql.All
cluster.Timeout = 1 * time.Minute

cluster.Authenticator = gocql.PasswordAuthenticator{
Username: username,
Password: password,
}

// Retrieve query string configuration
if len(u.Query().Get("consistency")) > 0 {
Expand Down Expand Up @@ -130,9 +142,17 @@ func (p *Cassandra) Run(migration io.Reader) error {
}
// 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 6445880

Please sign in to comment.