Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
sycner: automatically use upstream sql_mode (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
GMHDBJD authored Nov 26, 2020
1 parent b975729 commit e51b4fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 23 additions & 2 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (s *Syncer) Init(ctx context.Context) (err error) {
return err
}

err = s.createDBs()
err = s.createDBs(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -2342,7 +2342,7 @@ func (s *Syncer) printStatus(ctx context.Context) {
}
}

func (s *Syncer) createDBs() error {
func (s *Syncer) createDBs(ctx context.Context) error {
var err error
dbCfg := s.cfg.From
dbCfg.RawDBCfg = config.DefaultRawDBConfig().SetReadTimeout(maxDMLConnectionTimeout)
Expand All @@ -2351,6 +2351,27 @@ func (s *Syncer) createDBs() error {
return err
}

hasSQLMode := false
// get sql_mode from upstream db
if s.cfg.To.Session == nil {
s.cfg.To.Session = make(map[string]string)
} else {
for k := range s.cfg.To.Session {
if strings.ToLower(k) == "sql_mode" {
hasSQLMode = true
break
}
}
}
if !hasSQLMode {
sqlMode, err2 := utils.GetGlobalVariable(ctx, s.fromDB.BaseDB.DB, "sql_mode")
if err2 != nil {
s.tctx.L().Warn("cannot get sql_mode from upstream database", log.ShortError(err2))
} else {
s.cfg.To.Session["sql_mode"] = sqlMode
}
}

dbCfg = s.cfg.To
dbCfg.RawDBCfg = config.DefaultRawDBConfig().
SetReadTimeout(maxDMLConnectionTimeout).
Expand Down
4 changes: 4 additions & 0 deletions tests/all_mode/data/db2.increment.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
use all_mode;
delete from t2 where name = 'Sansa';


-- test sql_mode=NO_AUTO_VALUE_ON_ZERO
insert into t2 (id, name) values (0,'haha')

0 comments on commit e51b4fe

Please sign in to comment.