Skip to content

Commit

Permalink
fix(mysql): quote database name in CREATE DATABASE statement
Browse files Browse the repository at this point in the history
In order to allow the full Unicode Basic Multilingual Plane in the
database name, the name is now quoted in the CREATE DATABASE statement.
  • Loading branch information
dakraus committed Nov 28, 2022
1 parent aade545 commit a3c375f
Showing 1 changed file with 6 additions and 1 deletion.
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, "`", "``") + "`"
}

0 comments on commit a3c375f

Please sign in to comment.