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

Commit 6445880

Browse files
committed
Add Cassandra Enhancements
* Authentication functionality, * Execution of multiple statements in one migration file
1 parent 804f9c2 commit 6445880

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

database/cassandra/cassandra.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,23 @@ func (p *Cassandra) Open(url string) (database.Driver, error) {
5959
MigrationsTable: migrationsTable,
6060
}
6161

62+
username := u.User.Username()
63+
if len(username) == 0 {
64+
username = "cassandra"
65+
}
66+
password, _ := u.User.Password()
67+
if len(password) == 0 {
68+
password = "cassandra"
69+
}
70+
6271
cluster := gocql.NewCluster(u.Host)
6372
cluster.Keyspace = u.Path[1:len(u.Path)]
6473
cluster.Consistency = gocql.All
6574
cluster.Timeout = 1 * time.Minute
66-
75+
cluster.Authenticator = gocql.PasswordAuthenticator{
76+
Username: username,
77+
Password: password,
78+
}
6779

6880
// Retrieve query string configuration
6981
if len(u.Query().Get("consistency")) > 0 {
@@ -130,9 +142,17 @@ func (p *Cassandra) Run(migration io.Reader) error {
130142
}
131143
// run migration
132144
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}
145+
matches := regexp.MustCompile(`(?m:;$)`).Split(query, -1)
146+
for _, match := range matches {
147+
trimmedMatch := strings.Trim(match, " \t\r\n")
148+
if len(trimmedMatch) == 0 {
149+
continue
150+
}
151+
152+
if err := p.session.Query(trimmedMatch).Exec(); err != nil {
153+
// TODO: cast to Cassandra error and get line number
154+
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
155+
}
136156
}
137157

138158
return nil

0 commit comments

Comments
 (0)