You've already forked go-clickhouse
mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-14 23:44:59 +02:00
chore: improve auto-create database
This commit is contained in:
@ -29,6 +29,7 @@ type Config struct {
|
|||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
Database string
|
Database string
|
||||||
|
Cluster string
|
||||||
|
|
||||||
DialTimeout time.Duration
|
DialTimeout time.Duration
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
@ -151,6 +152,12 @@ func WithDatabase(database string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithCluster(cluster string) Option {
|
||||||
|
return func(db *DB) {
|
||||||
|
db.conf.Cluster = cluster
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithDialTimeout configures dial timeout for establishing new connections.
|
// WithDialTimeout configures dial timeout for establishing new connections.
|
||||||
// Default is 5 seconds.
|
// Default is 5 seconds.
|
||||||
func WithDialTimeout(timeout time.Duration) Option {
|
func WithDialTimeout(timeout time.Duration) Option {
|
||||||
|
7
ch/db.go
7
ch/db.go
@ -130,10 +130,15 @@ func (db *DB) autoCreateDatabase() {
|
|||||||
conf := db.conf.clone()
|
conf := db.conf.clone()
|
||||||
conf.Database = ""
|
conf.Database = ""
|
||||||
|
|
||||||
|
query := "CREATE DATABASE IF NOT EXISTS ?"
|
||||||
|
if conf.Cluster != "" {
|
||||||
|
query += " ON CLUSTER ?"
|
||||||
|
}
|
||||||
|
|
||||||
tmp := newDB(conf)
|
tmp := newDB(conf)
|
||||||
defer tmp.Close()
|
defer tmp.Close()
|
||||||
|
|
||||||
if _, err := tmp.Exec("CREATE DATABASE IF NOT EXISTS ?", Ident(db.conf.Database)); err != nil {
|
if _, err := tmp.Exec(query, Ident(db.conf.Database), Ident(db.conf.Cluster)); err != nil {
|
||||||
internal.Logger.Printf("create database %q failed: %s", db.conf.Database, err)
|
internal.Logger.Printf("create database %q failed: %s", db.conf.Database, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user