Add WithInstance

This commit is contained in:
kshvakov 2017-06-21 17:12:05 +03:00
commit b031b22c80
2 changed files with 5 additions and 3 deletions

View File

@ -4,5 +4,5 @@ package main
import ( import (
_ "github.com/kshvakov/clickhouse" _ "github.com/kshvakov/clickhouse"
_ "github.com/mattes/migrate/database/clickhouse" _ "github.com/kshvakov/migrate/database/clickhouse"
) )

View File

@ -24,7 +24,6 @@ type Config struct {
func init() { func init() {
database.Register("clickhouse", &ClickHouse{}) database.Register("clickhouse", &ClickHouse{})
} }
func WithInstance(conn *sql.DB, config *Config) (database.Driver, error) { func WithInstance(conn *sql.DB, config *Config) (database.Driver, error) {
if config == nil { if config == nil {
return nil, ErrNilConfig return nil, ErrNilConfig
@ -60,7 +59,6 @@ func (ch *ClickHouse) Open(dsn string) (database.Driver, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
q := migrate.FilterCustomQuery(purl) q := migrate.FilterCustomQuery(purl)
q.Scheme = "tcp" q.Scheme = "tcp"
conn, err := sql.Open("clickhouse", q.String()) conn, err := sql.Open("clickhouse", q.String())
@ -138,10 +136,12 @@ func (ch *ClickHouse) SetVersion(version int, dirty bool) error {
if err != nil { if err != nil {
return err return err
} }
query := "INSERT INTO " + ch.config.MigrationsTable + " (version, dirty, sequence) VALUES (?, ?, ?)" query := "INSERT INTO " + ch.config.MigrationsTable + " (version, dirty, sequence) VALUES (?, ?, ?)"
if _, err := tx.Exec(query, version, bool(dirty), time.Now().UnixNano()); err != nil { if _, err := tx.Exec(query, version, bool(dirty), time.Now().UnixNano()); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)} return &database.Error{OrigErr: err, Query: []byte(query)}
} }
return tx.Commit() return tx.Commit()
} }
@ -186,7 +186,9 @@ func (ch *ClickHouse) Drop() error {
if err := tables.Scan(&table); err != nil { if err := tables.Scan(&table); err != nil {
return err return err
} }
query = "DROP TABLE IF EXISTS " + ch.config.DatabaseName + "." + table query = "DROP TABLE IF EXISTS " + ch.config.DatabaseName + "." + table
if _, err := ch.conn.Exec(query); err != nil { if _, err := ch.conn.Exec(query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)} return &database.Error{OrigErr: err, Query: []byte(query)}
} }