mirror of https://github.com/status-im/migrate.git
Merge pull request #82 from UnitedTraders/clickhouse-multiline
Add support for the multiline clickhouse queries
This commit is contained in:
commit
4750e57ce1
|
@ -1,6 +1,6 @@
|
||||||
# ClickHouse
|
# ClickHouse
|
||||||
|
|
||||||
`clickhouse://host:port?username=user&password=qwerty&database=clicks`
|
`clickhouse://host:port?username=user&password=qwerty&database=clicks&x-multi-statement=true`
|
||||||
|
|
||||||
| URL Query | Description |
|
| URL Query | Description |
|
||||||
|------------|-------------|
|
|------------|-------------|
|
||||||
|
@ -10,3 +10,10 @@
|
||||||
| `password` | The user's password |
|
| `password` | The user's password |
|
||||||
| `host` | The host to connect to. |
|
| `host` | The host to connect to. |
|
||||||
| `port` | The port to bind to. |
|
| `port` | The port to bind to. |
|
||||||
|
| `x-multi-statement` | false | Enable multiple statements to be ran in a single migration (See note below) |
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
* The Clickhouse driver 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.
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-migrate/migrate"
|
"github.com/golang-migrate/migrate"
|
||||||
|
@ -17,8 +18,9 @@ var DefaultMigrationsTable = "schema_migrations"
|
||||||
var ErrNilConfig = fmt.Errorf("no config")
|
var ErrNilConfig = fmt.Errorf("no config")
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DatabaseName string
|
DatabaseName string
|
||||||
MigrationsTable string
|
MigrationsTable string
|
||||||
|
MultiStatementEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -66,8 +68,9 @@ func (ch *ClickHouse) Open(dsn string) (database.Driver, error) {
|
||||||
ch = &ClickHouse{
|
ch = &ClickHouse{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
config: &Config{
|
config: &Config{
|
||||||
MigrationsTable: purl.Query().Get("x-migrations-table"),
|
MigrationsTable: purl.Query().Get("x-migrations-table"),
|
||||||
DatabaseName: purl.Query().Get("database"),
|
DatabaseName: purl.Query().Get("database"),
|
||||||
|
MultiStatementEnabled: purl.Query().Get("x-multi-statement") == "true",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +100,22 @@ func (ch *ClickHouse) Run(r io.Reader) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ch.config.MultiStatementEnabled {
|
||||||
|
// split query by semi-colon
|
||||||
|
queries := strings.Split(string(migration), ";")
|
||||||
|
for _, q := range queries {
|
||||||
|
tq := strings.TrimSpace(q)
|
||||||
|
if tq == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := ch.conn.Exec(string(q)); err != nil {
|
||||||
|
return database.Error{OrigErr: err, Err: "migration failed", Query: []byte(q)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := ch.conn.Exec(string(migration)); err != nil {
|
if _, err := ch.conn.Exec(string(migration)); err != nil {
|
||||||
return database.Error{OrigErr: err, Err: "migration failed", Query: migration}
|
return database.Error{OrigErr: err, Err: "migration failed", Query: migration}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue