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

*: read downstream table before CREATE TABLE to fix IF NOT EXISTS #1915

Merged
merged 7 commits into from
Jul 26, 2021
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
1 change: 1 addition & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,7 @@ func (l *Loader) cleanDumpFiles() {
var lastErr error
for f := range files {
if strings.HasSuffix(f, ".sql") {
// TODO: table structure files are not used now, but we plan to used them in future so not delete them
if strings.HasSuffix(f, "-schema-create.sql") || strings.HasSuffix(f, "-schema.sql") {
continue
}
Expand Down
13 changes: 8 additions & 5 deletions syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1393,11 +1393,7 @@ func (s *Syncer) Run(ctx context.Context) (err error) {
if s.cfg.Mode == config.ModeAll && fresh {
delLoadTask = true
flushCheckpoint = true
err = s.loadTableStructureFromDump(ctx)
if err != nil {
tctx.L().Warn("error happened when load table structure from dump files", zap.Error(err))
cleanDumpFile = false
}
// TODO: loadTableStructureFromDump in future
} else {
cleanDumpFile = false
}
Expand Down Expand Up @@ -2525,6 +2521,7 @@ func (s *Syncer) trackDDL(usedSchema string, sql string, tableNames [][]*filter.
shouldSchemaExist bool
shouldTableExistNum int // tableNames[:shouldTableExistNum] should exist
shouldRefTableExistNum int // tableNames[1:shouldTableExistNum] should exist, since first one is "caller table"
tryFetchDownstreamTable bool // to make sure if not exists will execute correctly
)

switch node := stmt.(type) {
Expand All @@ -2548,6 +2545,7 @@ func (s *Syncer) trackDDL(usedSchema string, sql string, tableNames [][]*filter.
shouldSchemaExist = true
// for CREATE TABLE LIKE/AS, the reference tables should exist
shouldRefTableExistNum = len(srcTables)
tryFetchDownstreamTable = true
case *ast.DropTableStmt:
shouldExecDDLOnSchemaTracker = true
if err := s.checkpoint.DeleteTablePoint(ec.tctx, srcTable.Schema, srcTable.Name); err != nil {
Expand Down Expand Up @@ -2602,6 +2600,11 @@ func (s *Syncer) trackDDL(usedSchema string, sql string, tableNames [][]*filter.
}
}

if tryFetchDownstreamTable {
// ignore table not exists error, just try to fetch table from downstream.
_, _ = s.getTable(ec.tctx, srcTables[0].Schema, srcTables[0].Name, targetTables[0].Schema, targetTables[0].Name)
}

if shouldExecDDLOnSchemaTracker {
if err := s.schemaTracker.Exec(ec.tctx.Ctx, usedSchema, sql); err != nil {
ec.tctx.L().Error("cannot track DDL", zap.String("schema", usedSchema), zap.String("statement", sql), log.WrapStringerField("location", ec.currentLocation), log.ShortError(err))
Expand Down
6 changes: 3 additions & 3 deletions tests/shardddl1_1/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ function DM_027_CASE() {
run_sql_source1 "insert into ${shardddl1}.${tb2} values (4)"
run_sql_source1 "insert into ${shardddl1}.${tb3} values (5,6)"
# we now haven't checked table struct when create sharding table
# there should be a error message like "Unknown column 'val' in 'field list'", "unknown column val"
# but different TiDB version output different message. so we only roughly match here
# and we'll attach IF NOT EXISTS to every CREATE TABLE and fetch downstream table first, so downstream table strcuture
Copy link
Collaborator Author

@lance6716 lance6716 Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will cause CREATE TABLE in upstream becomes ineffective when downstream has already contained that table

When downstream table has same column with upstream one, this PR imtroduced more correctness for genearting WHERE
When downstream table has less column, this PR changes error message from "unknown column" to "column value mismatch"
When downstream table has more column, this PR will break the synchronizing with error "Column count doesn't match value count"

# is in use indeed. This leads to the error below.
run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
"query-status test" \
"nknown column" 1 # ignore case for first letter
"Column count doesn't match value count: 1 (columns) vs 2 (values)" 1
}

function DM_027() {
Expand Down