Skip to content

Commit a799fda

Browse files
authored
feat: support dbName with operators in name (minus, ...) (#322)
* feat: support `dbName` with operators in name (minus, ...) * fix: quoting makes identifiers case-sensitive * fix: missing fmt import Signed-off-by: Lucas Fernando Cardoso Nunes <[email protected]>
1 parent 47d7636 commit a799fda

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

pkg/drivers/mysql/mysql.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var (
5252
// with each other for a give value of KINE_SCHEMA_MIGRATION env var
5353
``,
5454
}
55-
createDB = "CREATE DATABASE IF NOT EXISTS "
55+
createDB = "CREATE DATABASE IF NOT EXISTS `%s`;"
5656
)
5757

5858
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 {
185185
}
186186

187187
if !exists {
188-
if _, err = db.Exec(createDB + dbName); err != nil {
188+
stmt := fmt.Sprintf(createDB, dbName)
189+
logrus.Tracef("SETUP EXEC : %v", util.Stripped(stmt))
190+
if _, err = db.Exec(stmt); err != nil {
189191
if mysqlError, ok := err.(*mysql.MySQLError); !ok || mysqlError.Number != 1049 {
190192
return err
191193
}
@@ -194,7 +196,7 @@ func createDBIfNotExist(dataSourceName string) error {
194196
if err != nil {
195197
return err
196198
}
197-
if _, err = db.Exec(createDB + dbName); err != nil {
199+
if _, err = db.Exec(stmt); err != nil {
198200
return err
199201
}
200202
}

pkg/drivers/pgsql/pgsql.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
// queries use the index.
5757
`ALTER TABLE kine ALTER COLUMN name SET DATA TYPE TEXT COLLATE "C" USING name::TEXT COLLATE "C"`,
5858
}
59-
createDB = "CREATE DATABASE "
59+
createDB = `CREATE DATABASE "%s";`
6060
)
6161

6262
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 {
228228
logrus.Warnf("failed to check existence of database %s, going to attempt create: %v", dbName, err)
229229
}
230230

231-
stmt := createDB + dbName + ";"
232-
233231
if !exists {
232+
stmt := fmt.Sprintf(createDB, dbName)
234233
logrus.Tracef("SETUP EXEC : %v", util.Stripped(stmt))
235234
if _, err = db.Exec(stmt); err != nil {
236235
logrus.Warnf("failed to create database %s: %v", dbName, err)
@@ -265,6 +264,10 @@ func prepareDSN(dataSourceName string, tlsInfo tls.Config) (string, error) {
265264
u.Path = "/kubernetes"
266265
}
267266

267+
// makes quoting database and schema reference the same unquoted identifier
268+
// See: https://www.postgresql.org/docs/12/sql-syntax-lexical.html#:~:text=unquoted%20names%20are%20always%20folded%20to%20lower%20case
269+
u.Path = strings.ToLower(u.Path)
270+
268271
queryMap, err := url.ParseQuery(u.RawQuery)
269272
if err != nil {
270273
return "", err

0 commit comments

Comments
 (0)