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

fix(database/clickhouse): properly escape database and table name #1004

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
8 changes: 4 additions & 4 deletions database/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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.MigrationsTable + "` (version, dirty, sequence) VALUES (?, ?, ?)"
Copy link
Member

@dhui dhui Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, there are still strings that would break this. e.g. if the table or db contained a `
See also: golang/go#18478

EDIT:
You'll see that some db drivers implement this themselves

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just merged another PR (#857) that fixes this

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 @@ -220,7 +220,7 @@ func (ch *ClickHouse) ensureVersionTable() (err error) {

var (
table string
query = "SHOW TABLES FROM " + ch.config.DatabaseName + " LIKE '" + ch.config.MigrationsTable + "'"
query = "SHOW TABLES FROM `" + ch.config.DatabaseName + "` LIKE '" + ch.config.MigrationsTable + "'"
)
// check if migration table exists
if err := ch.conn.QueryRow(query).Scan(&table); err != nil {
Expand Down Expand Up @@ -259,7 +259,7 @@ func (ch *ClickHouse) ensureVersionTable() (err error) {
}

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

if err != nil {
Expand All @@ -277,7 +277,7 @@ func (ch *ClickHouse) Drop() (err error) {
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 {
return &database.Error{OrigErr: err, Query: []byte(query)}
Expand Down
Loading