From acf5f3e3433070fa2ff8ae88a58cdb6befcbbfbd Mon Sep 17 00:00:00 2001 From: colindickson Date: Mon, 27 Nov 2023 10:37:20 -0500 Subject: [PATCH] clichouse create user query update --- db/dialect_clickhouse.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/db/dialect_clickhouse.go b/db/dialect_clickhouse.go index c50d2db..fc71684 100644 --- a/db/dialect_clickhouse.go +++ b/db/dialect_clickhouse.go @@ -133,22 +133,27 @@ func (d clickhouseDialect) OnlyInserts() bool { func (d clickhouseDialect) CreateUser(tx Tx, ctx context.Context, l *Loader, username string, password string, _database string, readOnly bool) error { user, pass := EscapeIdentifier(username), EscapeIdentifier(password) - var q string + + createUserQ := fmt.Sprintf("CREATE USER %s IDENTIFIED BY '%s';", user, pass) + _, err := tx.ExecContext(ctx, createUserQ) + if err != nil { + return fmt.Errorf("executing query %q: %w", createUserQ, err) + } + + var grantQ string if readOnly { - q = fmt.Sprintf(` - CREATE USER %s IDENTIFIED BY '%s'; + grantQ = fmt.Sprintf(` GRANT SELECT ON *.* TO %s; - `, user, pass, user) + `, user) } else { - q = fmt.Sprintf(` - CREATE USER %s IDENTIFIED BY '%s'; + grantQ = fmt.Sprintf(` GRANT ALL ON *.* TO %s; - `, user, pass, user) + `, user) } - _, err := tx.ExecContext(ctx, q) + _, err = tx.ExecContext(ctx, grantQ) if err != nil { - return fmt.Errorf("executing query %q: %w", q, err) + return fmt.Errorf("executing query %q: %w", grantQ, err) } return nil