mirror of https://github.com/status-im/migrate.git
Merge pull request #76 from agquick/master
Support for multiple statements in Cassandra cql migration files
This commit is contained in:
commit
fad64ed381
|
@ -3,6 +3,9 @@
|
||||||
* Drop command will not work on Cassandra 2.X because it rely on
|
* Drop command will not work on Cassandra 2.X because it rely on
|
||||||
system_schema table which comes with 3.X
|
system_schema table which comes with 3.X
|
||||||
* Other commands should work properly but are **not tested**
|
* Other commands should work properly but are **not tested**
|
||||||
|
* The Cassandra driver (gocql) does not natively support executing multipe statements in a single query. To allow for multiple statements in a single migration, you can use the `x-multi-statement` param. There are two important caveats:
|
||||||
|
* This mode splits the migration text into separately-executed statements by a semi-colon `;`. Thus `x-multi-statement` cannot be used when a statement in the migration contains a string with a semi-colon.
|
||||||
|
* The queries are not executed in any sort of transaction/batch, meaning you are responsible for fixing partial migrations.
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
@ -12,6 +15,7 @@ system_schema table which comes with 3.X
|
||||||
| URL Query | Default value | Description |
|
| URL Query | Default value | Description |
|
||||||
|------------|-------------|-----------|
|
|------------|-------------|-----------|
|
||||||
| `x-migrations-table` | schema_migrations | Name of the migrations table |
|
| `x-migrations-table` | schema_migrations | Name of the migrations table |
|
||||||
|
| `x-multi-statement` | false | Enable multiple statements to be ran in a single migration (See note above) |
|
||||||
| `port` | 9042 | The port to bind to |
|
| `port` | 9042 | The port to bind to |
|
||||||
| `consistency` | ALL | Migration consistency
|
| `consistency` | ALL | Migration consistency
|
||||||
| `protocol` | | Cassandra protocol version (3 or 4)
|
| `protocol` | | Cassandra protocol version (3 or 4)
|
||||||
|
|
|
@ -31,6 +31,7 @@ var (
|
||||||
type Config struct {
|
type Config struct {
|
||||||
MigrationsTable string
|
MigrationsTable string
|
||||||
KeyspaceName string
|
KeyspaceName string
|
||||||
|
MultiStatementEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Cassandra struct {
|
type Cassandra struct {
|
||||||
|
@ -127,6 +128,7 @@ func (c *Cassandra) Open(url string) (database.Driver, error) {
|
||||||
return WithInstance(session, &Config{
|
return WithInstance(session, &Config{
|
||||||
KeyspaceName: strings.TrimPrefix(u.Path, "/"),
|
KeyspaceName: strings.TrimPrefix(u.Path, "/"),
|
||||||
MigrationsTable: u.Query().Get("x-migrations-table"),
|
MigrationsTable: u.Query().Get("x-migrations-table"),
|
||||||
|
MultiStatementEnabled: u.Query().Get("x-multi-statement") == "true",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,11 +157,26 @@ func (c *Cassandra) Run(migration io.Reader) error {
|
||||||
}
|
}
|
||||||
// run migration
|
// run migration
|
||||||
query := string(migr[:])
|
query := string(migr[:])
|
||||||
|
|
||||||
|
if c.config.MultiStatementEnabled {
|
||||||
|
// split query by semi-colon
|
||||||
|
queries := strings.Split(query, ";")
|
||||||
|
|
||||||
|
for _, q := range(queries) {
|
||||||
|
tq := strings.TrimSpace(q)
|
||||||
|
if (tq == "") { continue }
|
||||||
|
if err := c.session.Query(tq).Exec(); err != nil {
|
||||||
|
// TODO: cast to Cassandra error and get line number
|
||||||
|
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if err := c.session.Query(query).Exec(); err != nil {
|
if err := c.session.Query(query).Exec(); err != nil {
|
||||||
// TODO: cast to Cassandra error and get line number
|
// TODO: cast to Cassandra error and get line number
|
||||||
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
|
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue