-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from choonkeat/cassandra
add Cassandra support
- Loading branch information
Showing
28 changed files
with
407 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cid.txt | ||
./dbmigrate | ||
tests/db/migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
language: go | ||
go: | ||
- "1.11.x" | ||
- "1.14.x" | ||
services: | ||
- docker | ||
script: | ||
- env GO111MODULE=on make test | ||
- env GO111MODULE=on make test BUILD_TARGET="./cmd/dbmigrate" DATABASE_DRIVERS=sqlite3 | ||
- make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
// by default, Makefile `make build` compiles without this file | ||
// if sqlite3 is required, | ||
// env CGO_ENABLED=1 make build BUILD_TARGET="./cmd/dbmigrate" | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"net/url" | ||
|
||
_ "github.com/MichaelS11/go-cql-driver" | ||
"github.com/choonkeat/dbmigrate" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func init() { | ||
dbmigrate.Register("cql", dbmigrate.Adapter{ | ||
CreateVersionsTable: `CREATE TABLE IF NOT EXISTS dbmigrate_versions (version text, PRIMARY KEY (version));`, | ||
SelectExistingVersions: `SELECT version FROM dbmigrate_versions`, | ||
InsertNewVersion: `INSERT INTO dbmigrate_versions (version) VALUES (?)`, | ||
DeleteOldVersion: `DELETE FROM dbmigrate_versions WHERE version = ?`, | ||
PingQuery: `SELECT gossip_generation FROM system.local`, | ||
BaseDatabaseURL: func(databaseURL string) (string, string, error) { | ||
u, err := url.Parse(databaseURL) | ||
if err != nil { | ||
return "", "", errors.Wrapf(err, "invalid cassandra dsn") | ||
} | ||
q := u.Query() | ||
dbName := q.Get("keyspace") | ||
q.Set("keyspace", "system") // default connection | ||
u.RawQuery = q.Encode() | ||
return u.String(), dbName, nil | ||
}, | ||
BeginTx: func(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (dbmigrate.ExecCommitRollbacker, error) { | ||
return &noTx{db: db}, nil | ||
}, | ||
}) | ||
} | ||
|
||
// Implements dbmigrate.ExecCommitRollbacker | ||
type noTx struct { | ||
db *sql.DB | ||
} | ||
|
||
func (tx *noTx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { | ||
return tx.db.ExecContext(ctx, query, args...) | ||
} | ||
|
||
func (tx *noTx) Commit() error { | ||
return nil | ||
} | ||
|
||
func (tx *noTx) Rollback() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.