mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-12 16:10:03 +01:00
parent
a45d03acdb
commit
6cd39e1fba
3 changed files with 29 additions and 13 deletions
|
|
@ -127,6 +127,12 @@ protocol://username:password@host:port/database_name?options
|
||||||
DATABASE_URL="mysql://username:password@127.0.0.1:3306/database_name"
|
DATABASE_URL="mysql://username:password@127.0.0.1:3306/database_name"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A socket parameter can be specified to connect through a unix socket file:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
DATABASE_URL="mysql://username:password@/database_name?socket=/var/run/mysqld/mysqld.sock"
|
||||||
|
```
|
||||||
|
|
||||||
**PostgreSQL**
|
**PostgreSQL**
|
||||||
|
|
||||||
When connecting to Postgres, you may need to add the `sslmode=disable` option to your connection string, as dbmate by default requires a TLS connection (some other frameworks/languages allow unencrypted connections by default).
|
When connecting to Postgres, you may need to add the `sslmode=disable` option to your connection string, as dbmate by default requires a TLS connection (some other frameworks/languages allow unencrypted connections by default).
|
||||||
|
|
|
||||||
|
|
@ -19,20 +19,20 @@ type MySQLDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeMySQLURL(u *url.URL) string {
|
func normalizeMySQLURL(u *url.URL) string {
|
||||||
// set default port
|
|
||||||
host := u.Host
|
|
||||||
|
|
||||||
if u.Port() == "" {
|
|
||||||
host = fmt.Sprintf("%s:3306", host)
|
|
||||||
}
|
|
||||||
|
|
||||||
// host format required by go-sql-driver/mysql
|
|
||||||
host = fmt.Sprintf("tcp(%s)", host)
|
|
||||||
|
|
||||||
query := u.Query()
|
query := u.Query()
|
||||||
query.Set("multiStatements", "true")
|
query.Set("multiStatements", "true")
|
||||||
|
|
||||||
queryString := query.Encode()
|
host := u.Host
|
||||||
|
protocol := "tcp"
|
||||||
|
|
||||||
|
if query.Get("socket") != "" {
|
||||||
|
protocol = "unix"
|
||||||
|
host = query.Get("socket")
|
||||||
|
query.Del("socket")
|
||||||
|
} else if u.Port() == "" {
|
||||||
|
// set default port
|
||||||
|
host = fmt.Sprintf("%s:3306", host)
|
||||||
|
}
|
||||||
|
|
||||||
// Get decoded user:pass
|
// Get decoded user:pass
|
||||||
userPassEncoded := u.User.String()
|
userPassEncoded := u.User.String()
|
||||||
|
|
@ -45,8 +45,9 @@ func normalizeMySQLURL(u *url.URL) string {
|
||||||
normalizedString = userPass + "@"
|
normalizedString = userPass + "@"
|
||||||
}
|
}
|
||||||
|
|
||||||
normalizedString = fmt.Sprintf("%s%s%s?%s", normalizedString,
|
// connection string format required by go-sql-driver/mysql
|
||||||
host, u.Path, queryString)
|
normalizedString = fmt.Sprintf("%s%s(%s)%s?%s", normalizedString,
|
||||||
|
protocol, host, u.Path, query.Encode())
|
||||||
|
|
||||||
return normalizedString
|
return normalizedString
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,15 @@ func TestNormalizeMySQLURLCustomSpecialChars(t *testing.T) {
|
||||||
require.Equal(t, "duhfsd7s:123!@123!@@tcp(host:123)/foo?flag=on&multiStatements=true", s)
|
require.Equal(t, "duhfsd7s:123!@123!@@tcp(host:123)/foo?flag=on&multiStatements=true", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNormalizeMySQLURLSocket(t *testing.T) {
|
||||||
|
u, err := url.Parse("mysql:///foo?socket=/var/run/mysqld/mysqld.sock&flag=on")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "", u.Host)
|
||||||
|
|
||||||
|
s := normalizeMySQLURL(u)
|
||||||
|
require.Equal(t, "unix(/var/run/mysqld/mysqld.sock)/foo?flag=on&multiStatements=true", s)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMySQLCreateDropDatabase(t *testing.T) {
|
func TestMySQLCreateDropDatabase(t *testing.T) {
|
||||||
drv := MySQLDriver{}
|
drv := MySQLDriver{}
|
||||||
u := mySQLTestURL(t)
|
u := mySQLTestURL(t)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue