Skip to content

Commit

Permalink
feat: support dbName with operators in name (minus, ...) (#322)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
lucasfcnunes authored Sep 23, 2024
1 parent 47d7636 commit a799fda
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions pkg/drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/drivers/pgsql/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a799fda

Please sign in to comment.