diff --git a/lightning/backend/session.go b/lightning/backend/session.go index 3087229c0..95138be2c 100644 --- a/lightning/backend/session.go +++ b/lightning/backend/session.go @@ -18,12 +18,14 @@ import ( "errors" "fmt" "strconv" + "time" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/util/timeutil" "github.com/pingcap/tidb-lightning/lightning/common" ) @@ -194,12 +196,31 @@ func newSession(options *SessionOptions) *session { vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode() vars.StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode() vars.StmtCtx.IgnoreZeroInDate = !sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode() + var tz *time.Location if options.SysVars != nil { + var tidbTimeZone, tidbSystemTimeZone string for k, v := range options.SysVars { vars.SetSystemVar(k, v) + if k == "time_zone" { + tidbTimeZone = v + } + if k == "system_time_zone" { + tidbSystemTimeZone = v + } } + // if we get system zone from TiDB server, we can set it for lightning session. + if tidbTimeZone == "SYSTEM" && tidbSystemTimeZone != "" { + loc, err := timeutil.LoadLocation(tidbSystemTimeZone) + if err != nil { + loc = time.Local + } + tz = loc + } + } + if tz == nil { + tz = vars.Location() } - vars.StmtCtx.TimeZone = vars.Location() + vars.StmtCtx.TimeZone = tz vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)) vars.TxnCtx = nil diff --git a/lightning/restore/tidb.go b/lightning/restore/tidb.go index 33a1773eb..f4a9d62f5 100644 --- a/lightning/restore/tidb.go +++ b/lightning/restore/tidb.go @@ -50,6 +50,7 @@ var ( "max_allowed_packet": "67108864", "div_precision_increment": "4", "time_zone": "SYSTEM", + "system_time_zone": "", "lc_time_names": "en_US", "default_week_format": "0", "block_encryption_mode": "aes-128-ecb", diff --git a/lightning/restore/tidb_test.go b/lightning/restore/tidb_test.go index 664e47067..adefec2f7 100644 --- a/lightning/restore/tidb_test.go +++ b/lightning/restore/tidb_test.go @@ -416,6 +416,7 @@ func (s *tidbSuite) TestObtainRowFormatVersionSucceed(c *C) { c.Assert(sysVars, DeepEquals, map[string]string{ "tidb_row_format_version": "2", "max_allowed_packet": "1073741824", + "system_time_zone": "", "div_precision_increment": "10", "time_zone": "-08:00", "lc_time_names": "ja_JP", @@ -442,6 +443,7 @@ func (s *tidbSuite) TestObtainRowFormatVersionFailure(c *C) { c.Assert(sysVars, DeepEquals, map[string]string{ "tidb_row_format_version": "1", "max_allowed_packet": "67108864", + "system_time_zone": "", "div_precision_increment": "4", "time_zone": "+00:00", "lc_time_names": "en_US",