Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Clickhouse schema migrations in cluster mode #1180

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions database/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
type Config struct {
DatabaseName string
ClusterName string
IsDistributed bool
MigrationsTable string
MigrationsTableEngine string
MultiStatementEnabled bool
Expand Down Expand Up @@ -99,6 +100,7 @@ func (ch *ClickHouse) Open(dsn string) (database.Driver, error) {
MigrationsTableEngine: migrationsTableEngine,
DatabaseName: purl.Query().Get("database"),
ClusterName: purl.Query().Get("x-cluster-name"),
IsDistributed: purl.Query().Get("x-distributed") == "true",
MultiStatementEnabled: purl.Query().Get("x-multi-statement") == "true",
MultiStatementMaxSize: multiStatementMaxSize,
},
Expand Down Expand Up @@ -130,7 +132,13 @@ func (ch *ClickHouse) init() error {
ch.config.MigrationsTableEngine = DefaultMigrationsTableEngine
}

return ch.ensureVersionTable()
if err := ch.ensureVersionTable(); err != nil {
return err
}
if ch.config.IsDistributed {
return ch.ensureDistributedTable()
}
return nil
}

func (ch *ClickHouse) Run(r io.Reader) error {
Expand Down Expand Up @@ -192,7 +200,7 @@ func (ch *ClickHouse) SetVersion(version int, dirty bool) error {
return err
}

query := "INSERT INTO " + ch.config.MigrationsTable + " (version, dirty, sequence) VALUES (?, ?, ?)"
query := "INSERT INTO " + ch.config.DatabaseName + "." + 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)}
}
Expand Down Expand Up @@ -233,19 +241,30 @@ func (ch *ClickHouse) ensureVersionTable() (err error) {

// if not, create the empty migration table
if len(ch.config.ClusterName) > 0 {
query = fmt.Sprintf(`
CREATE TABLE %s ON CLUSTER %s (

if ch.config.IsDistributed { // for sharded clusters we want to create a distributed table
baseTableName := ch.config.MigrationsTable + "_base"
query = fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s.%s ON CLUSTER %s (
version Int64,
dirty UInt8,
sequence UInt64
) Engine=%s Primary Key (sequence)`, ch.config.DatabaseName, baseTableName, ch.config.ClusterName, ch.config.MigrationsTableEngine)
} else {
query = fmt.Sprintf(`
CREATE TABLE %s.%s ON CLUSTER %s (
version Int64,
dirty UInt8,
sequence UInt64
) Engine=%s`, ch.config.MigrationsTable, ch.config.ClusterName, ch.config.MigrationsTableEngine)
) Engine=%s`, ch.config.DatabaseName, ch.config.MigrationsTable, ch.config.ClusterName, ch.config.MigrationsTableEngine)
}
} else {
query = fmt.Sprintf(`
CREATE TABLE %s (
CREATE TABLE %s.%s (
version Int64,
dirty UInt8,
sequence UInt64
) Engine=%s`, ch.config.MigrationsTable, ch.config.MigrationsTableEngine)
) Engine=%s`, ch.config.DatabaseName, ch.config.MigrationsTable, ch.config.MigrationsTableEngine)
}

if strings.HasSuffix(ch.config.MigrationsTableEngine, "Tree") {
Expand All @@ -258,6 +277,17 @@ func (ch *ClickHouse) ensureVersionTable() (err error) {
return nil
}

func (ch *ClickHouse) ensureDistributedTable() error {
baseTableName := ch.config.MigrationsTable + "_base"
query := fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s.%s ON CLUSTER %s AS %s.%s Engine=Distributed(%s, %s, %s, sequence)`, ch.config.DatabaseName, ch.config.MigrationsTable, ch.config.ClusterName, ch.config.DatabaseName, baseTableName, ch.config.ClusterName, ch.config.DatabaseName, baseTableName)

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

func (ch *ClickHouse) Drop() (err error) {
query := "SHOW TABLES FROM " + quoteIdentifier(ch.config.DatabaseName)
tables, err := ch.conn.Query(query)
Expand Down
Loading