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

restore: don't restore auto id if the table doesn't has it. #420

Merged
merged 4 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 table.NeedRebaseAutoID() {
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 additions & 0 deletions pkg/utils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ 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
}

// NeedRebaseAutoID checks whether the table need to rebase its autoid.
func (tbl *Table) NeedRebaseAutoID() bool {
return NeedAutoID(tbl.Info)
}

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