diff --git a/database/cassandra/README.md b/database/cassandra/README.md index b44000d7b..c3d4387ad 100644 --- a/database/cassandra/README.md +++ b/database/cassandra/README.md @@ -26,6 +26,7 @@ system_schema table which comes with 3.X | `sslkey` | | Key file location. The file must contain PEM encoded data. | | `sslrootcert` | | The location of the root certificate file. The file must contain PEM encoded data. | | `sslmode` | | Whether or not to use SSL (disable\|require\|verify-ca\|verify-full) | +| `disable-host-lookup`| false | Disable initial host lookup. | `timeout` is parsed using [time.ParseDuration(s string)](https://golang.org/pkg/time/#ParseDuration) diff --git a/database/cassandra/cassandra.go b/database/cassandra/cassandra.go index 58713e72b..a639426d1 100644 --- a/database/cassandra/cassandra.go +++ b/database/cassandra/cassandra.go @@ -156,6 +156,14 @@ func (c *Cassandra) Open(url string) (database.Driver, error) { } } + if len(u.Query().Get("disable-host-lookup")) > 0 { + if flag, err := strconv.ParseBool(u.Query().Get("disable-host-lookup")); err != nil && flag { + cluster.DisableInitialHostLookup = true + } else if err != nil { + return nil, err + } + } + session, err := cluster.CreateSession() if err != nil { return nil, err @@ -228,16 +236,26 @@ func (c *Cassandra) Run(migration io.Reader) error { } func (c *Cassandra) SetVersion(version int, dirty bool) error { - query := `TRUNCATE "` + c.config.MigrationsTable + `"` - if err := c.session.Query(query).Exec(); err != nil { - return &database.Error{OrigErr: err, Query: []byte(query)} + // DELETE instead of TRUNCATE because AWS Keyspaces does not support it + // see: https://docs.aws.amazon.com/keyspaces/latest/devguide/cassandra-apis.html + squery := `SELECT version FROM "` + c.config.MigrationsTable + `"` + dquery := `DELETE FROM "` + c.config.MigrationsTable + `" WHERE version = ?` + iter := c.session.Query(squery).Iter() + var previous int + for iter.Scan(&previous) { + if err := c.session.Query(dquery, previous).Exec(); err != nil { + return &database.Error{OrigErr: err, Query: []byte(dquery)} + } + } + if err := iter.Close(); err != nil { + return &database.Error{OrigErr: err, Query: []byte(squery)} } // Also re-write the schema version for nil dirty versions to prevent // empty schema version for failed down migration on the first migration // See: https://github.com/golang-migrate/migrate/issues/330 if version >= 0 || (version == database.NilVersion && dirty) { - query = `INSERT INTO "` + c.config.MigrationsTable + `" (version, dirty) VALUES (?, ?)` + query := `INSERT INTO "` + c.config.MigrationsTable + `" (version, dirty) VALUES (?, ?)` if err := c.session.Query(query, version, dirty).Exec(); err != nil { return &database.Error{OrigErr: err, Query: []byte(query)} } diff --git a/go.mod b/go.mod index 641c17dd0..271d26a99 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/fsouza/fake-gcs-server v1.17.0 github.com/go-sql-driver/mysql v1.5.0 github.com/gobuffalo/here v0.6.0 - github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4 + github.com/gocql/gocql v0.0.0-20210515062232-b7ef815b4556 github.com/gofrs/uuid v4.0.0+incompatible // indirect github.com/google/flatbuffers v2.0.0+incompatible // indirect github.com/google/go-github/v35 v35.2.0 diff --git a/go.sum b/go.sum index 77f73feaf..2e236713f 100644 --- a/go.sum +++ b/go.sum @@ -217,6 +217,8 @@ github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/V github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4 h1:vF83LI8tAakwEwvWZtrIEx7pOySacl2TOxx6eXk4ePo= github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4/go.mod h1:4Fw1eo5iaEhDUs8XyuhSVCVy52Jq3L+/3GJgYkwc+/0= +github.com/gocql/gocql v0.0.0-20210515062232-b7ef815b4556 h1:N/MD/sr6o61X+iZBAT2qEUF023s4KbA8RWfKzl0L6MQ= +github.com/gocql/gocql v0.0.0-20210515062232-b7ef815b4556/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=