Skip to content

Commit 8113d38

Browse files
author
Shlomi Noach
authored
Merge pull request #723 from github/no-auto-value-on-zero
Always use NO_AUTO_VALUE_ON_ZERO
2 parents bd47692 + 681458e commit 8113d38

File tree

2 files changed

+18
-59
lines changed

2 files changed

+18
-59
lines changed

go/logic/applier.go

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,12 @@ func (this *Applier) ApplyIterationInsertQuery() (chunkSize int64, rowsAffected
483483
}
484484
defer tx.Rollback()
485485
sessionQuery := fmt.Sprintf(`SET SESSION time_zone = '%s'`, this.migrationContext.ApplierTimeZone)
486+
sqlModeAddendum := `,NO_AUTO_VALUE_ON_ZERO`
486487
if !this.migrationContext.SkipStrictMode {
487-
sessionQuery += ", sql_mode = CONCAT(@@session.sql_mode, ',STRICT_ALL_TABLES')"
488+
sqlModeAddendum = fmt.Sprintf("%s,STRICT_ALL_TABLES", sqlModeAddendum)
488489
}
490+
sessionQuery = fmt.Sprintf("%s, sql_mode = CONCAT(@@session.sql_mode, ',%s')", sessionQuery, sqlModeAddendum)
491+
489492
if _, err := tx.Exec(sessionQuery); err != nil {
490493
return nil, err
491494
}
@@ -977,63 +980,6 @@ func (this *Applier) buildDMLEventQuery(dmlEvent *binlog.BinlogDMLEvent) (result
977980
return append(results, newDmlBuildResultError(fmt.Errorf("Unknown dml event type: %+v", dmlEvent.DML)))
978981
}
979982

980-
// ApplyDMLEventQuery writes an entry to the ghost table, in response to an intercepted
981-
// original-table binlog event
982-
func (this *Applier) ApplyDMLEventQuery(dmlEvent *binlog.BinlogDMLEvent) error {
983-
for _, buildResult := range this.buildDMLEventQuery(dmlEvent) {
984-
if buildResult.err != nil {
985-
return buildResult.err
986-
}
987-
// TODO The below is in preparation for transactional writes on the ghost tables.
988-
// Such writes would be, for example:
989-
// - prepended with sql_mode setup
990-
// - prepended with time zone setup
991-
// - prepended with SET SQL_LOG_BIN=0
992-
// - prepended with SET FK_CHECKS=0
993-
// etc.
994-
//
995-
// a known problem: https://github.com/golang/go/issues/9373 -- bitint unsigned values, not supported in database/sql
996-
// is solved by silently converting unsigned bigints to string values.
997-
//
998-
999-
err := func() error {
1000-
tx, err := this.db.Begin()
1001-
if err != nil {
1002-
return err
1003-
}
1004-
rollback := func(err error) error {
1005-
tx.Rollback()
1006-
return err
1007-
}
1008-
sessionQuery := fmt.Sprintf("SET SESSION time_zone = '+00:00'")
1009-
if !this.migrationContext.SkipStrictMode {
1010-
sessionQuery += ", sql_mode = CONCAT(@@session.sql_mode, ',STRICT_ALL_TABLES')"
1011-
}
1012-
if _, err := tx.Exec(sessionQuery); err != nil {
1013-
return rollback(err)
1014-
}
1015-
if _, err := tx.Exec(buildResult.query, buildResult.args...); err != nil {
1016-
return rollback(err)
1017-
}
1018-
if err := tx.Commit(); err != nil {
1019-
return err
1020-
}
1021-
return nil
1022-
}()
1023-
1024-
if err != nil {
1025-
err = fmt.Errorf("%s; query=%s; args=%+v", err.Error(), buildResult.query, buildResult.args)
1026-
return log.Errore(err)
1027-
}
1028-
// no error
1029-
atomic.AddInt64(&this.migrationContext.TotalDMLEventsApplied, 1)
1030-
if this.migrationContext.CountTableRows {
1031-
atomic.AddInt64(&this.migrationContext.RowsDeltaEstimate, buildResult.rowsDelta)
1032-
}
1033-
}
1034-
return nil
1035-
}
1036-
1037983
// ApplyDMLEventQueries applies multiple DML queries onto the _ghost_ table
1038984
func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent)) error {
1039985

@@ -1051,9 +997,13 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent))
1051997
}
1052998

1053999
sessionQuery := "SET SESSION time_zone = '+00:00'"
1000+
1001+
sqlModeAddendum := `,NO_AUTO_VALUE_ON_ZERO`
10541002
if !this.migrationContext.SkipStrictMode {
1055-
sessionQuery += ", sql_mode = CONCAT(@@session.sql_mode, ',STRICT_ALL_TABLES')"
1003+
sqlModeAddendum = fmt.Sprintf("%s,STRICT_ALL_TABLES", sqlModeAddendum)
10561004
}
1005+
sessionQuery = fmt.Sprintf("%s, sql_mode = CONCAT(@@session.sql_mode, ',%s')", sessionQuery, sqlModeAddendum)
1006+
10571007
if _, err := tx.Exec(sessionQuery); err != nil {
10581008
return rollback(err)
10591009
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
drop table if exists gh_ost_test;
2+
create table gh_ost_test (
3+
id int auto_increment,
4+
i int not null,
5+
primary key(id)
6+
) auto_increment=1;
7+
8+
set session sql_mode='NO_AUTO_VALUE_ON_ZERO';
9+
insert into gh_ost_test values (0, 23);

0 commit comments

Comments
 (0)