Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dbName with operators in name (minus, ...) #322

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
schemaMigrations = []string{
`ALTER TABLE kine MODIFY COLUMN id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL UNIQUE, MODIFY COLUMN create_revision BIGINT UNSIGNED, MODIFY COLUMN prev_revision BIGINT UNSIGNED`,
}
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 @@ -179,7 +179,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 @@ -188,7 +190,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
10 changes: 7 additions & 3 deletions pkg/drivers/pgsql/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgsql
import (
"context"
"database/sql"
"fmt"
"net/url"
"os"
"regexp"
Expand Down Expand Up @@ -50,7 +51,7 @@ var (
schemaMigrations = []string{
`ALTER TABLE kine ALTER COLUMN id SET DATA TYPE BIGINT, ALTER COLUMN create_revision SET DATA TYPE BIGINT, ALTER COLUMN prev_revision SET DATA TYPE BIGINT; ALTER SEQUENCE kine_id_seq AS BIGINT`,
}
createDB = "CREATE DATABASE "
createDB = `CREATE DATABASE "%s";`
brandond marked this conversation as resolved.
Show resolved Hide resolved
)

func New(ctx context.Context, dataSourceName string, tlsInfo tls.Config, connPoolConfig generic.ConnectionPoolConfig, metricsRegisterer prometheus.Registerer) (server.Backend, error) {
Expand Down Expand Up @@ -165,9 +166,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 @@ -202,6 +202,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