fix(database): generate db url by url.URL for schema upgrade (#8852)

Closes #7948

Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
He Weiwei 2019-08-28 16:59:22 +08:00 committed by GitHub
parent 94138137d5
commit 2c1c816941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,7 @@ package dao
import (
"fmt"
"net/url"
"os"
"github.com/astaxie/beego/orm"
@ -91,14 +92,21 @@ func (p *pgsql) Register(alias ...string) error {
// UpgradeSchema calls migrate tool to upgrade schema to the latest based on the SQL scripts.
func (p *pgsql) UpgradeSchema() error {
dbURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", p.usr, p.pwd, p.host, p.port, p.database, p.sslmode)
dbURL := url.URL{
Scheme: "postgres",
User: url.UserPassword(p.usr, p.pwd),
Host: fmt.Sprintf("%s:%s", p.host, p.port),
Path: p.database,
RawQuery: fmt.Sprintf("sslmode=%s", p.sslmode),
}
// For UT
path := os.Getenv("POSTGRES_MIGRATION_SCRIPTS_PATH")
if len(path) == 0 {
path = defaultMigrationPath
}
srcURL := fmt.Sprintf("file://%s", path)
m, err := migrate.New(srcURL, dbURL)
m, err := migrate.New(srcURL, dbURL.String())
if err != nil {
return err
}