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

fix(mysql): quote database name in CREATE DATABASE statement #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion pkg/drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
cryptotls "crypto/tls"
"database/sql"
"fmt"
"strings"

"github.com/go-sql-driver/mysql"
"github.com/k3s-io/kine/pkg/drivers/generic"
Expand Down Expand Up @@ -137,7 +138,7 @@ func createDBIfNotExist(dataSourceName string) error {
if err != nil {
return err
}
dbName := config.DBName
dbName := quoteIdentifier(config.DBName)

db, err := sql.Open("mysql", dataSourceName)
if err != nil {
Expand Down Expand Up @@ -188,3 +189,7 @@ func prepareDSN(dataSourceName string, tlsConfig *cryptotls.Config) (string, err

return parsedDSN, nil
}

func quoteIdentifier(id string) string {
return "`" + strings.ReplaceAll(id, "`", "``") + "`"
}
6 changes: 5 additions & 1 deletion pkg/drivers/pgsql/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func createDBIfNotExist(dataSourceName string) error {
return err
}

dbName := strings.SplitN(u.Path, "/", 2)[1]
dbName := quoteIdentifier(strings.SplitN(u.Path, "/", 2)[1])
db, err := sql.Open("postgres", dataSourceName)
if err != nil {
return err
Expand Down Expand Up @@ -208,3 +208,7 @@ func prepareDSN(dataSourceName string, tlsInfo tls.Config) (string, error) {
u.RawQuery = params.Encode()
return u.String(), nil
}

func quoteIdentifier(id string) string {
return pq.QuoteIdentifier(id)
}