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

Commit

Permalink
restore: don't restore auto id if the table doesn't has it. (#420)
Browse files Browse the repository at this point in the history
* restore: don't restore auto id if the table doesn't has it.

* restore: fix some bugs

* backup: don't alloc auto ID if the table doesn't need it

* restore: remove `table.NeedRebaseAutoID`
  • Loading branch information
YuJuncen committed Jul 15, 2020
1 parent de452e1 commit 6390453
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func BuildBackupRangeAndSchema(
switch {
case tableInfo.IsSequence():
globalAutoID, err = seqAlloc.NextGlobalAutoID(tableInfo.ID)
case tableInfo.IsView():
// no auto ID for views.
case tableInfo.IsView() || !utils.NeedAutoID(tableInfo):
// no auto ID for views or table without either rowID nor auto_increment ID.
default:
globalAutoID, err = idAlloc.NextGlobalAutoID(tableInfo.ID)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error {
}
}
restoreMetaSQL = fmt.Sprintf(setValFormat, table.Info.AutoIncID)
err = db.se.Execute(ctx, restoreMetaSQL)
} else {
var alterAutoIncIDFormat string
switch {
Expand All @@ -163,9 +164,11 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error {
utils.EncloseName(table.Db.Name.O),
utils.EncloseName(table.Info.Name.O),
table.Info.AutoIncID)
if utils.NeedAutoID(table.Info) {
err = db.se.Execute(ctx, restoreMetaSQL)
}
}

err = db.se.Execute(ctx, restoreMetaSQL)
if err != nil {
log.Error("restore meta sql failed",
zap.String("query", restoreMetaSQL),
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func (tbl *Table) NoChecksum() bool {
return tbl.Crc64Xor == 0 && tbl.TotalKvs == 0 && tbl.TotalBytes == 0
}

// NeedAutoID checks whether the table needs backing up with an autoid.
func NeedAutoID(tblInfo *model.TableInfo) bool {
hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle
hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil
return hasRowID || hasAutoIncID
}

// Database wraps the schema and tables of a database.
type Database struct {
Info *model.DBInfo
Expand Down

0 comments on commit 6390453

Please sign in to comment.