diff --git a/pkg/model/clickhouse/cluster.go b/pkg/model/clickhouse/cluster.go index f67057818..88789927e 100644 --- a/pkg/model/clickhouse/cluster.go +++ b/pkg/model/clickhouse/cluster.go @@ -167,6 +167,12 @@ func (c *Cluster) exec(ctx context.Context, host string, queries []string, _opts sqlAttach := strings.ReplaceAll(sql, "CREATE TABLE", "ATTACH TABLE") err = conn.Exec(ctx, sqlAttach, opts) } + if err != nil && strings.Contains(err.Error(), "Code: 117") && strings.Contains(sql, "CREATE TABLE") { + // WARNING: error message or code may change in newer ClickHouse versions + c.l.V(1).M(host).F().Info("Directory for table already exists. Trying ATTACH TABLE instead") + sqlAttach := strings.ReplaceAll(sql, "CREATE TABLE", "ATTACH TABLE") + err = conn.Exec(ctx, sqlAttach, opts) + } if err == nil || strings.Contains(err.Error(), "ALREADY_EXISTS") { queries[i] = "" // Query is executed or object already exists, removing from the list } else {