Skip to content

Commit

Permalink
clichouse create user query update
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Nov 27, 2023
1 parent fad7d61 commit acf5f3e
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions db/dialect_clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit acf5f3e

Please sign in to comment.