Skip to content

Commit

Permalink
Add WithInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
kshvakov committed Jun 21, 2017
2 parents 1440a07 + 14636b7 commit b031b22
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli/build_clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package main

import (
_ "github.com/kshvakov/clickhouse"
_ "github.com/mattes/migrate/database/clickhouse"
_ "github.com/kshvakov/migrate/database/clickhouse"
)
6 changes: 4 additions & 2 deletions database/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Config struct {
func init() {
database.Register("clickhouse", &ClickHouse{})
}

func WithInstance(conn *sql.DB, config *Config) (database.Driver, error) {
if config == nil {
return nil, ErrNilConfig
Expand Down Expand Up @@ -60,7 +59,6 @@ func (ch *ClickHouse) Open(dsn string) (database.Driver, error) {
if err != nil {
return nil, err
}

q := migrate.FilterCustomQuery(purl)
q.Scheme = "tcp"
conn, err := sql.Open("clickhouse", q.String())
Expand Down Expand Up @@ -138,10 +136,12 @@ func (ch *ClickHouse) SetVersion(version int, dirty bool) error {
if err != nil {
return err
}

query := "INSERT INTO " + ch.config.MigrationsTable + " (version, dirty, sequence) VALUES (?, ?, ?)"
if _, err := tx.Exec(query, version, bool(dirty), time.Now().UnixNano()); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

return tx.Commit()
}

Expand Down Expand Up @@ -186,7 +186,9 @@ func (ch *ClickHouse) Drop() error {
if err := tables.Scan(&table); err != nil {
return err
}

query = "DROP TABLE IF EXISTS " + ch.config.DatabaseName + "." + table

if _, err := ch.conn.Exec(query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
Expand Down

0 comments on commit b031b22

Please sign in to comment.