Skip to content

Commit

Permalink
fix: escape cluster name
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 7, 2023
1 parent 2d9abe8 commit faaf5d3
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions ch/chschema/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (f Formatter) AppendIdent(b []byte, ident string) []byte {
return AppendIdent(b, ident)
}

func (f Formatter) AppendFQN(b []byte, ident string) []byte {
return AppendFQN(b, ident)
}

func (f Formatter) WithArg(arg NamedArgAppender) Formatter {
return Formatter{
args: f.args.WithArg(arg),
Expand Down
2 changes: 1 addition & 1 deletion ch/chschema/sqlfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type FQN string
var _ QueryAppender = (*FQN)(nil)

func (s FQN) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendIdent(b, string(s)), nil
return fmter.AppendFQN(b, string(s)), nil
}

func AppendFQN(b []byte, field string) []byte {
Expand Down
4 changes: 2 additions & 2 deletions ch/query_table_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (q *CreateTableQuery) IfNotExists() *CreateTableQuery {
return q
}

func (q *CreateTableQuery) OnCluster(query string, args ...any) *CreateTableQuery {
q.onCluster = chschema.SafeQuery(query, args)
func (q *CreateTableQuery) OnCluster(cluster string) *CreateTableQuery {
q.onCluster = chschema.UnsafeIdent(cluster)
return q
}

Expand Down
4 changes: 2 additions & 2 deletions ch/query_table_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (q *DropTableQuery) IfExists() *DropTableQuery {
return q
}

func (q *DropTableQuery) OnCluster(query string, args ...any) *DropTableQuery {
q.onCluster = chschema.SafeQuery(query, args)
func (q *DropTableQuery) OnCluster(cluster string) *DropTableQuery {
q.onCluster = chschema.UnsafeIdent(cluster)
return q
}

Expand Down
10 changes: 5 additions & 5 deletions ch/query_view_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type CreateViewQuery struct {
materialized bool
ifNotExists bool
view chschema.QueryWithArgs
cluster chschema.QueryWithArgs
onCluster chschema.QueryWithArgs
to chschema.QueryWithArgs
where whereQuery
group []chschema.QueryWithArgs
Expand Down Expand Up @@ -53,12 +53,12 @@ func (q *CreateViewQuery) ViewExpr(query string, args ...any) *CreateViewQuery {
}

func (q *CreateViewQuery) OnCluster(cluster string) *CreateViewQuery {
q.cluster = chschema.UnsafeIdent(cluster)
q.onCluster = chschema.UnsafeIdent(cluster)
return q
}

func (q *CreateViewQuery) OnClusterExpr(query string, args ...any) *CreateViewQuery {
q.cluster = chschema.SafeQuery(query, args)
q.onCluster = chschema.SafeQuery(query, args)
return q
}

Expand Down Expand Up @@ -197,9 +197,9 @@ func (q *CreateViewQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []b
return nil, err
}

if !q.cluster.IsEmpty() {
if !q.onCluster.IsEmpty() {
b = append(b, " ON CLUSTER "...)
b, err = q.cluster.AppendQuery(fmter, b)
b, err = q.onCluster.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions ch/query_view_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
type DropViewQuery struct {
baseQuery

ifExists bool
view chschema.QueryWithArgs
cluster chschema.QueryWithArgs
ifExists bool
view chschema.QueryWithArgs
onCluster chschema.QueryWithArgs
}

var _ Query = (*DropViewQuery)(nil)
Expand Down Expand Up @@ -54,12 +54,12 @@ func (q *DropViewQuery) ViewExpr(query string, args ...any) *DropViewQuery {
}

func (q *DropViewQuery) OnCluster(cluster string) *DropViewQuery {
q.cluster = chschema.UnsafeIdent(cluster)
q.onCluster = chschema.UnsafeIdent(cluster)
return q
}

func (q *DropViewQuery) OnClusterExpr(query string, args ...any) *DropViewQuery {
q.cluster = chschema.SafeQuery(query, args)
q.onCluster = chschema.SafeQuery(query, args)
return q
}

Expand All @@ -84,9 +84,9 @@ func (q *DropViewQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []byt
return nil, err
}

if !q.cluster.IsEmpty() {
if !q.onCluster.IsEmpty() {
b = append(b, " ON CLUSTER "...)
b, err = q.cluster.AppendQuery(fmter, b)
b, err = q.onCluster.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion chmigrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func WithOnCluster(cluster string) MigratorOption {
}

// WithMarkAppliedOnSuccess sets the migrator to only mark migrations as applied/unapplied
// when their up/down is successful
// when their up/down is successful.
func WithMarkAppliedOnSuccess(enabled bool) MigratorOption {
return func(m *Migrator) {
m.markAppliedOnSuccess = enabled
Expand Down

0 comments on commit faaf5d3

Please sign in to comment.