diff --git a/pkg/drivers/mysql/mysql.go b/pkg/drivers/mysql/mysql.go index f1ce870b..a67c6f75 100644 --- a/pkg/drivers/mysql/mysql.go +++ b/pkg/drivers/mysql/mysql.go @@ -52,7 +52,7 @@ var ( // with each other for a give value of KINE_SCHEMA_MIGRATION env var ``, } - createDB = "CREATE DATABASE IF NOT EXISTS " + createDB = "CREATE DATABASE IF NOT EXISTS `%s`;" ) func New(ctx context.Context, dataSourceName string, tlsInfo tls.Config, connPoolConfig generic.ConnectionPoolConfig, metricsRegisterer prometheus.Registerer) (server.Backend, error) { @@ -185,7 +185,9 @@ func createDBIfNotExist(dataSourceName string) error { } if !exists { - if _, err = db.Exec(createDB + dbName); err != nil { + stmt := fmt.Sprintf(createDB, dbName) + logrus.Tracef("SETUP EXEC : %v", util.Stripped(stmt)) + if _, err = db.Exec(stmt); err != nil { if mysqlError, ok := err.(*mysql.MySQLError); !ok || mysqlError.Number != 1049 { return err } @@ -194,7 +196,7 @@ func createDBIfNotExist(dataSourceName string) error { if err != nil { return err } - if _, err = db.Exec(createDB + dbName); err != nil { + if _, err = db.Exec(stmt); err != nil { return err } } diff --git a/pkg/drivers/pgsql/pgsql.go b/pkg/drivers/pgsql/pgsql.go index e339e846..28ddc43f 100644 --- a/pkg/drivers/pgsql/pgsql.go +++ b/pkg/drivers/pgsql/pgsql.go @@ -56,7 +56,7 @@ var ( // queries use the index. `ALTER TABLE kine ALTER COLUMN name SET DATA TYPE TEXT COLLATE "C" USING name::TEXT COLLATE "C"`, } - createDB = "CREATE DATABASE " + createDB = `CREATE DATABASE "%s";` ) func New(ctx context.Context, dataSourceName string, tlsInfo tls.Config, connPoolConfig generic.ConnectionPoolConfig, metricsRegisterer prometheus.Registerer) (server.Backend, error) { @@ -228,9 +228,8 @@ func createDBIfNotExist(dataSourceName string) error { logrus.Warnf("failed to check existence of database %s, going to attempt create: %v", dbName, err) } - stmt := createDB + dbName + ";" - if !exists { + stmt := fmt.Sprintf(createDB, dbName) logrus.Tracef("SETUP EXEC : %v", util.Stripped(stmt)) if _, err = db.Exec(stmt); err != nil { logrus.Warnf("failed to create database %s: %v", dbName, err) @@ -265,6 +264,10 @@ func prepareDSN(dataSourceName string, tlsInfo tls.Config) (string, error) { u.Path = "/kubernetes" } + // makes quoting database and schema reference the same unquoted identifier + // See: https://www.postgresql.org/docs/12/sql-syntax-lexical.html#:~:text=unquoted%20names%20are%20always%20folded%20to%20lower%20case + u.Path = strings.ToLower(u.Path) + queryMap, err := url.ParseQuery(u.RawQuery) if err != nil { return "", err