Skip to content

Commit dedc6b1

Browse files
committed
Fix --timezone flag
Set the timezone in the DSN, and add quoting. See go-sql-driver/mysql#405
1 parent 14a93d4 commit dedc6b1

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

main.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"fmt"
88
"log"
9+
"net/url"
910
"os"
1011
"time"
1112
)
@@ -71,6 +72,12 @@ func main() {
7172

7273
connstr := dbInfo.User + ":" + dbInfo.Password + "@tcp(" + dbInfo.HostPort +
7374
")/" + dbInfo.Database + "?strict=true&charset=utf8&parseTime=True&loc=UTC"
75+
if dbInfo.Timezone != "" {
76+
// Quoting with %27 is needed
77+
// https://github.com/go-sql-driver/mysql/issues/405
78+
connstr = connstr + "&time_zone=%27" + url.QueryEscape(dbInfo.Timezone) + "%27"
79+
}
80+
log.Println(connstr)
7481

7582
db, err := sql.Open("mysql", connstr)
7683
if err != nil {
@@ -79,12 +86,6 @@ func main() {
7986
}
8087
defer db.Close()
8188

82-
if dbInfo.Timezone != "" {
83-
if _, err := db.Exec("SET @time_zone = ?", dbInfo.Timezone); err != nil {
84-
log.Fatal(err)
85-
}
86-
}
87-
8889
if err = run(db); err != nil {
8990
log.Println(err)
9091
// As os.Exit shortcircuits defers, we have to close explicitely

0 commit comments

Comments
 (0)