github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/shardddl4_1/run.sh (about)

     1  #!/bin/bash
     2  
     3  set -eu
     4  
     5  cur=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
     6  source $cur/../_utils/test_prepare
     7  WORK_DIR=$TEST_DIR/$TEST_NAME
     8  source $cur/../_utils/shardddl_lib.sh
     9  
    10  function DM_131_CASE() {
    11  	# Test rollback NULL to NOT NULL.
    12  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
    13  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
    14  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
    15  
    16  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify b int not null;"
    17  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
    18  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
    19  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
    20  
    21  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify b int;"
    22  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
    23  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
    24  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
    25  
    26  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    27  
    28  	# Test rollback NOT NULL to NULL
    29  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify b int not null;"
    30  	run_sql_source2 "alter table ${shardddl1}.${tb1} modify b int not null;"
    31  	run_sql_source2 "alter table ${shardddl1}.${tb2} modify b int not null;"
    32  
    33  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
    34  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
    35  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
    36  
    37  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify b int;"
    38  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(13,13);"
    39  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(14,14);"
    40  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(15,15);"
    41  
    42  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify b int not null;"
    43  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(16,16);"
    44  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(17,17);"
    45  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(18,18);"
    46  
    47  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    48  }
    49  
    50  # Modify nullable and then rollback.
    51  function DM_131 {
    52  	# run_case 131 "double-source-pessimistic" \
    53  	# "run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    54  	#  run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    55  	#  run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
    56  	# "clean_table" "pessimistic"
    57  	run_case 131 "double-source-optimistic" \
    58  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    59       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    60       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
    61  		"clean_table" "optimistic"
    62  }
    63  
    64  function DM_132_CASE {
    65  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
    66  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
    67  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
    68  
    69  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(a, b);"
    70  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
    71  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
    72  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
    73  
    74  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(a, b);"
    75  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
    76  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
    77  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
    78  
    79  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop primary key, add primary key(a, b);"
    80  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
    81  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
    82  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
    83  
    84  	# FIXME: DM should report an error to user that data constraints become smaller and may not be able to rollback.
    85  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
    86  }
    87  
    88  # Expand the primary key field.
    89  function DM_132 {
    90  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int, b int, primary key(a) nonclustered);"
    91  	run_case 132 "double-source-pessimistic" \
    92  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    93           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    94           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
    95  		"clean_table" "pessimistic"
    96  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int, b int, primary key(a) nonclustered);"
    97  	run_case 132 "double-source-optimistic" \
    98  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
    99           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
   100           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
   101  		"clean_table" "optimistic"
   102  }
   103  
   104  function DM_133_CASE {
   105  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   106  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   107  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   108  
   109  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(a);"
   110  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   111  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   112  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   113  
   114  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(a);"
   115  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   116  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   117  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   118  
   119  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop primary key, add primary key(a);"
   120  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
   121  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
   122  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
   123  
   124  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   125  }
   126  
   127  # Shrink the primary key field.
   128  function DM_133 {
   129  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int, b int, primary key(a,b) nonclustered);"
   130  	run_case 133 "double-source-pessimistic" \
   131  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a,b));\"; \
   132           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a,b));\"; \
   133           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int, b int, primary key(a,b));\"" \
   134  		"clean_table" "pessimistic"
   135  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int, b int, primary key(a,b) nonclustered);"
   136  	run_case 133 "double-source-optimistic" \
   137  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a,b));\"; \
   138           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a,b));\"; \
   139           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int, b int, primary key(a,b));\"" \
   140  		"clean_table" "optimistic"
   141  }
   142  
   143  function DM_134_CASE {
   144  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   145  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   146  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   147  
   148  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(b);"
   149  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   150  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   151  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   152  
   153  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(b);"
   154  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   155  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   156  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   157  
   158  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop primary key, add primary key(b);"
   159  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
   160  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
   161  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
   162  
   163  	# FIXME: dm-master should give warnings to users that constraint is changed.
   164  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   165  }
   166  
   167  # Change the primary key field.
   168  function DM_134 {
   169  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int, b int, primary key(a) nonclustered);"
   170  	run_case 134 "double-source-pessimistic" \
   171  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a));\"; \
   172           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a));\"; \
   173           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int, b int, primary key(a));\"" \
   174  		"clean_table" "pessimistic"
   175  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int, b int, primary key(a) nonclustered);"
   176  	run_case 134 "double-source-optimistic" \
   177  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a));\"; \
   178           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int, b int, primary key(a));\"; \
   179           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int, b int, primary key(a));\"" \
   180  		"clean_table" "optimistic"
   181  }
   182  
   183  function DM_135_CASE {
   184  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   185  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   186  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   187  
   188  	if ! run_sql_source1 "alter table ${shardddl1}.${tb1} drop primary key, add primary key(b);" 2>&1 |
   189  		grep "Incorrect table definition; there can be only one auto column and it must be defined as a key" >/dev/null; then
   190  		echo "sql should be failed because there can be only one auto column and it must be defined as a key" >&2
   191  		return 255
   192  	fi
   193  }
   194  
   195  function DM_135() {
   196  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int auto_increment, b int, primary key(a) nonclustered);"
   197  	run_case 135 "double-source-pessimistic" \
   198  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int auto_increment primary key, b int);\"; \
   199           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int auto_increment primary key, b int);\"; \
   200           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int auto_increment primary key, b int);\"" \
   201  		"clean_table" "pessimistic"
   202  	run_sql_tidb "create database if not exists ${shardddl}; create table ${shardddl}.${tb} (a int auto_increment, b int, primary key(a) nonclustered);"
   203  	run_case 135 "double-source-optimistic" \
   204  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int auto_increment primary key, b int);\"; \
   205           run_sql_source2 \"create table ${shardddl1}.${tb1} (a int auto_increment primary key, b int);\"; \
   206           run_sql_source2 \"create table ${shardddl1}.${tb2} (a int auto_increment primary key, b int);\"" \
   207  		"clean_table" "optimistic"
   208  }
   209  
   210  function DM_136_CASE {
   211  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1);"
   212  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2,2);"
   213  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3,3);"
   214  
   215  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop index uk, add unique key uk(a, b);"
   216  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4);"
   217  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5);"
   218  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,6);"
   219  
   220  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop index uk, add unique key uk(a, b);"
   221  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7);"
   222  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   223  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9);"
   224  
   225  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop index uk, add unique key uk(a, b);"
   226  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10);"
   227  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11);"
   228  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12);"
   229  
   230  	# FIXME: DM should report an error to user that data constraints become smaller and may not be able to rollback.
   231  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   232  }
   233  
   234  # Expand the unique key field.
   235  function DM_136 {
   236  	# run_case 136 "double-source-pessimistic" \
   237  	#     "run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a));\"; \
   238  	#      run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a));\"; \
   239  	#      run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int, b int, unique key uk(a));\"" \
   240  	#     "clean_table" "pessimistic"
   241  
   242  	run_case 136 "double-source-optimistic" \
   243  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a));\"; \
   244           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a));\"; \
   245           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int, b int, unique key uk(a));\"" \
   246  		"clean_table" "optimistic"
   247  }
   248  
   249  function DM_137_CASE {
   250  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1);"
   251  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2,2);"
   252  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3,3);"
   253  
   254  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop index uk, add unique key uk(a);"
   255  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4);"
   256  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5);"
   257  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,6);"
   258  
   259  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop index uk, add unique key uk(a);"
   260  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7);"
   261  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   262  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9);"
   263  
   264  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop index uk, add unique key uk(a);"
   265  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10);"
   266  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11);"
   267  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12);"
   268  
   269  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   270  }
   271  
   272  # Shrink the unique key field.
   273  function DM_137 {
   274  	# run_case 137 "double-source-pessimistic" \
   275  	#     "run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a, b));\"; \
   276  	#      run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a, b));\"; \
   277  	#      run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int, b int, unique key uk(a, b));\"" \
   278  	#     "clean_table" "pessimistic"
   279  
   280  	run_case 137 "double-source-optimistic" \
   281  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a, b));\"; \
   282           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int, b int, unique key uk(a, b));\"; \
   283           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int, b int, unique key uk(a, b));\"" \
   284  		"clean_table" "optimistic"
   285  }
   286  
   287  function DM_138_CASE {
   288  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   289  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   290  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   291  
   292  	run_sql_source1 "alter table ${shardddl1}.${tb1} add unique key uk(a);"
   293  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   294  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   295  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   296  
   297  	run_sql_source2 "alter table ${shardddl1}.${tb1} add unique key uk(a);"
   298  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   299  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   300  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   301  
   302  	run_sql_source2 "alter table ${shardddl1}.${tb2} add unique key uk(a);"
   303  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
   304  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
   305  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
   306  
   307  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   308  }
   309  
   310  # Add the unique key.
   311  function DM_138 {
   312  	run_case 138 "double-source-pessimistic" \
   313  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   314           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   315           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int);\"" \
   316  		"clean_table" "pessimistic"
   317  
   318  	run_case 138 "double-source-optimistic" \
   319  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   320           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   321           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int);\"" \
   322  		"clean_table" "optimistic"
   323  }
   324  
   325  function DM_139_CASE {
   326  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   327  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   328  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   329  
   330  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop index uk;"
   331  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   332  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   333  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   334  
   335  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop index uk;"
   336  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   337  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   338  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   339  
   340  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop index uk;"
   341  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10);"
   342  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
   343  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
   344  
   345  	# FIXME: DM should report an error to user that this operation may not be able to rollback.
   346  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   347  }
   348  
   349  # Drop the unique key.
   350  function DM_139 {
   351  	run_case 139 "double-source-pessimistic" \
   352  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int, unique key uk(a));\"; \
   353           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int, unique key uk(a));\"; \
   354           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int, unique key uk(a));\"" \
   355  		"clean_table" "pessimistic"
   356  
   357  	run_case 139 "double-source-optimistic" \
   358  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int, unique key uk(a));\"; \
   359           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int, unique key uk(a));\"; \
   360           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int, unique key uk(a));\"" \
   361  		"clean_table" "optimistic"
   362  }
   363  
   364  function DM_142_CASE {
   365  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10),(11),(12),(13),(14),(15);"
   366  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(20),(21),(22),(23),(24),(25);"
   367  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(30),(31),(32),(33),(34),(35);"
   368  
   369  	run_sql_source1 "alter table ${shardddl1}.${tb1} add partition (partition p1 values less than (200));"
   370  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(110),(111),(112),(113),(114),(115);"
   371  	run_sql_source2 "alter table ${shardddl1}.${tb1} add partition (partition p1 values less than (200));"
   372  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(120),(121),(122),(123),(124),(125);"
   373  	run_sql_source2 "alter table ${shardddl1}.${tb2} add partition (partition p1 values less than (200));"
   374  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(130),(131),(132),(133),(134),(135);"
   375  
   376  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   377  }
   378  
   379  # Add new partition.
   380  function DM_142 {
   381  	run_case 142 "double-source-pessimistic" \
   382  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id)(partition p0 values less than (100));\"; \
   383           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id)(partition p0 values less than (100));\"; \
   384           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key) partition by range(id)(partition p0 values less than (100));\"" \
   385  		"clean_table" "pessimistic"
   386  
   387  	# Note: not support optimistic partition yet
   388  	#run_case 142 "double-source-optimistic" \
   389  	#	"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id)(partition p0 values less than (100));\"; \
   390  	#     run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id)(partition p0 values less than (100));\"; \
   391  	#     run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key) partition by range(id)(partition p0 values less than (100));\"" \
   392  	#	"clean_table" "optimistic"
   393  }
   394  
   395  function DM_143_CASE {
   396  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10),(11),(12),(13),(14),(15),(110),(111),(112),(113),(114),(115);"
   397  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(20),(21),(22),(23),(24),(25),(120),(121),(122),(123),(124),(125);"
   398  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(30),(31),(32),(33),(34),(35),(130),(131),(132),(133),(134),(135);"
   399  
   400  	run_sql_source1 "delete from ${shardddl1}.${tb1} where id >= 100;"
   401  	run_sql_source2 "delete from ${shardddl1}.${tb1} where id >= 100;"
   402  	run_sql_source2 "delete from ${shardddl1}.${tb2} where id >= 100;"
   403  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop partition p1;"
   404  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop partition p1;"
   405  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop partition p1;"
   406  
   407  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   408  }
   409  
   410  # Remove partition.
   411  function DM_143 {
   412  	run_case 143 "double-source-pessimistic" \
   413  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id) \
   414          (partition p0 values less than (100), partition p1 values less than (200));\"; \
   415           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id) \
   416           (partition p0 values less than (100), partition p1 values less than (200));\"; \
   417           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key) partition by range(id) \
   418           (partition p0 values less than (100), partition p1 values less than (200));\"" \
   419  		"clean_table" "pessimistic"
   420  	# optimistic sharding doesn't support partition
   421  	# run_case 143 "double-source-optimistic" \
   422  	# 	"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id) \
   423  	#     (partition p0 values less than (100), partition p1 values less than (200));\"; \
   424  	#      run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key) partition by range(id) \
   425  	#      (partition p0 values less than (100), partition p1 values less than (200));\"; \
   426  	#      run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key) partition by range(id) \
   427  	#      (partition p0 values less than (100), partition p1 values less than (200));\"" \
   428  	# 	"clean_table" "optimistic"
   429  }
   430  
   431  function DM_145_CASE {
   432  	shardmode=$1
   433  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(100),(101),(102),(103),(104),(105);"
   434  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(200),(201),(202),(203),(204),(205);"
   435  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(300),(301),(302),(303),(304),(305);"
   436  
   437  	run_sql_source1 "alter table ${shardddl1}.${tb1} engine=innodb;"
   438  	run_sql_source2 "alter table ${shardddl1}.${tb1} engine=innodb;"
   439  	run_sql_source2 "alter table ${shardddl1}.${tb2} engine=innodb;"
   440  
   441  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(400),(401),(402),(403),(404),(405);"
   442  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(500),(501),(502),(503),(504),(505);"
   443  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(600),(601),(602),(603),(604),(605);"
   444  
   445  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   446  }
   447  
   448  # Defragment.
   449  function DM_145 {
   450  	run_case 145 "double-source-pessimistic" "init_table 111 211 212" "clean_table" "pessimistic"
   451  	run_case 145 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   452  }
   453  
   454  function DM_146_CASE {
   455  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(100),(101),(102),(103),(104),(105);"
   456  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(200),(201),(202),(203),(204),(205);"
   457  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(300),(301),(302),(303),(304),(305);"
   458  
   459  	run_sql_source1 "alter table ${shardddl1}.${tb1} ROW_FORMAT=COMPACT;"
   460  
   461  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   462  		"query-status test" \
   463  		"This type of ALTER TABLE is currently unsupported" 1
   464  }
   465  
   466  # Modify row format.
   467  function DM_146 {
   468  	run_case 146 "double-source-pessimistic" "init_table 111 211 212" "clean_table" "pessimistic"
   469  	run_case 146 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   470  }
   471  
   472  function DM_147_CASE {
   473  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   474  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   475  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   476  
   477  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column b int, drop column c;"
   478  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   479  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   480  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   481  
   482  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column c int, drop column b;"
   483  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   484  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   485  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   486  
   487  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   488  		"query-status test" \
   489  		"because schema conflict detected" 1 \
   490  		"add column c that wasn't fully dropped in downstream" 1
   491  
   492  	# try to fix data
   493  	echo 'create table tbl(a int primary key, b int, c int) engine=innodb default charset=latin1 collate=latin1_bin;' >${WORK_DIR}/schema.sql
   494  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   495  		"binlog-schema update test ${shardddl1} ${tb1} ${WORK_DIR}/schema.sql -s mysql-replica-01" \
   496  		"\"result\": true" 2
   497  
   498  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   499  		"binlog replace test \"alter table ${shardddl1}.${tb1} drop column b\"" \
   500  		"\"result\": true" 2 \
   501  		"\"source 'mysql-replica-02' has no error\"" 1
   502  
   503  	run_sql_tidb "update ${shardddl}.${tb} set c=null where a=1;"
   504  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   505  }
   506  
   507  # Add and Drop multiple fields and then rollback.
   508  function DM_147 {
   509  	run_case 147 "double-source-optimistic" \
   510  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   511       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   512       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"" \
   513  		"clean_table" "optimistic"
   514  }
   515  
   516  function DM_148_CASE {
   517  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   518  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   519  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   520  
   521  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column b int after id, add column c int after b;"
   522  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4,4);"
   523  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   524  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   525  
   526  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column b int after id, add column c int after b;"
   527  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7,7);"
   528  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8,8);"
   529  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9);"
   530  
   531  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column b int after id, add column c int after b;"
   532  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10,10);"
   533  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11,11);"
   534  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12,12);"
   535  
   536  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   537  }
   538  
   539  # Add multiple fields in a specific order.
   540  function DM_148 {
   541  	run_case 148 "double-source-pessimistic" \
   542  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   543           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   544           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int);\"" \
   545  		"clean_table" "pessimistic"
   546  	run_case 148 "double-source-optimistic" \
   547  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   548           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   549           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int);\"" \
   550  		"clean_table" "optimistic"
   551  }
   552  
   553  function DM_149_CASE {
   554  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,\"aaaaaaa\");"
   555  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,\"bbbbbbb\");"
   556  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,\"ccccccc\");"
   557  
   558  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify column a varchar(20);"
   559  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,\"aaaaaaaaaaaaaa\");"
   560  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,\"bbbbbbb\");"
   561  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,\"ccccccc\");"
   562  
   563  	run_sql_source2 "alter table ${shardddl1}.${tb1} modify column a varchar(20);"
   564  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,\"aaaaaaaaaaaaaa\");"
   565  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,\"bbbbbbbbbbbbbb\");"
   566  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,\"ccccccc\");"
   567  
   568  	run_sql_source2 "alter table ${shardddl1}.${tb2} modify column a varchar(20);"
   569  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,\"aaaaaaaaaaaaaa\");"
   570  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,\"bbbbbbbbbbbbbb\");"
   571  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,\"cccccccccccccc\");"
   572  
   573  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   574  }
   575  
   576  # Increase field length.
   577  function DM_149 {
   578  	run_case 149 "double-source-pessimistic" \
   579  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(10));\"; \
   580           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(10));\"; \
   581           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a varchar(10));\"" \
   582  		"clean_table" "pessimistic"
   583  	run_case 149 "double-source-optimistic" \
   584  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(10));\"; \
   585           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(10));\"; \
   586           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a varchar(10));\"" \
   587  		"clean_table" "optimistic"
   588  }
   589  
   590  function DM_150_CASE {
   591  	shardmode=$1
   592  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,\"aaaaaaa\");"
   593  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,\"bbbbbbb\");"
   594  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,\"ccccccc\");"
   595  
   596  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify column a varchar(10);"
   597  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,\"aaaaaaa\");"
   598  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,\"bbbbbbb\");"
   599  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,\"ccccccc\");"
   600  
   601  	if [[ "$shardmode" == "pessimistic" ]]; then
   602  		# ddl: "modify column a varchar(10);" passes in worker1, but in pessimistic mode is still waiting for the other worker in the sharding group to be executed with the same ddl.
   603  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   604  			"query-status test" \
   605  			'"ALTER TABLE `shardddl`.`tb` MODIFY COLUMN `a` VARCHAR(10)"' 2
   606  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   607  			"show-ddl-locks" \
   608  			'ALTER TABLE `shardddl`.`tb` MODIFY COLUMN `a` VARCHAR(10)"' 1
   609  
   610  		# we alter database in source2 and the ddl lock will be resolved
   611  		run_sql_source2 "alter table ${shardddl1}.${tb1} modify column a varchar(10);"
   612  		run_sql_source2 "alter table ${shardddl1}.${tb2} modify column a varchar(10);"
   613  		check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   614  	else
   615  		# ddl: "modify column a varchar(10)" is passed in optimistic mode and will be executed downstream.
   616  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   617  			"query-status test" \
   618  			'"stage": "Running"' 3
   619  	fi
   620  
   621  }
   622  
   623  # Increase field length.
   624  function DM_150 {
   625  	run_case 150 "double-source-pessimistic" \
   626  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(20));\"; \
   627  	     run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(20));\"; \
   628  	     run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a varchar(20));\"" \
   629  		"clean_table" "pessimistic"
   630  	run_case 150 "double-source-optimistic" \
   631  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(20));\"; \
   632           run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a varchar(20));\"; \
   633           run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a varchar(20));\"" \
   634  		"clean_table" "optimistic"
   635  }
   636  
   637  function DM_151_CASE {
   638  	shardmode=$1
   639  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   640  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   641  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3);"
   642  
   643  	run_sql_source1 "alter table ${shardddl1}.${tb1} modify column a double;"
   644  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4.0);"
   645  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   646  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6);"
   647  
   648  	if [[ "$shardmode" == "pessimistic" ]]; then
   649  		# ddl: "modify column a double;" passes in worker1, but in pessimistic mode is still waiting for the other worker in the sharding group to be executed with the same ddl.
   650  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   651  			"query-status test" \
   652  			'"ALTER TABLE `shardddl`.`tb` MODIFY COLUMN `a` DOUBLE"' 2
   653  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   654  			"show-ddl-locks" \
   655  			'"ALTER TABLE `shardddl`.`tb` MODIFY COLUMN `a` DOUBLE"' 1
   656  	else
   657  		run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   658  			"query-status test" \
   659  			"Running" 3
   660  	fi
   661  
   662  	# we alter database in source2 and the ddl lock will be resolved
   663  	run_sql_source2 "alter table ${shardddl1}.${tb1} modify column a double;"
   664  	run_sql_source2 "alter table ${shardddl1}.${tb2} modify column a double;"
   665  
   666  	# insert 3 recorde to make sure optimistic mode sharding resolve can finish fast
   667  	sleep 3
   668  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7.0);"
   669  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8.0);"
   670  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9.0);"
   671  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   672  }
   673  
   674  function DM_151 {
   675  	run_case 151 "double-source-pessimistic" \
   676  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   677  	     run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   678  	     run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int);\"" \
   679  		"clean_table" "pessimistic"
   680  
   681  	run_case 151 "double-source-optimistic" \
   682  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   683  	     run_sql_source2 \"create table ${shardddl1}.${tb1} (id int primary key, a int);\"; \
   684  	     run_sql_source2 \"create table ${shardddl1}.${tb2} (id int primary key, a int);\"" \
   685  		"clean_table" "optimistic"
   686  }
   687  
   688  function DM_152_CASE {
   689  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1);"
   690  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2);"
   691  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3);"
   692  
   693  	# Add multiple fields.
   694  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column a int, add column b varchar(20), add column c double;"
   695  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column a int, add column b varchar(20), add column c double;"
   696  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column a int, add column b varchar(20), add column c double;"
   697  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,\"aaaa\",4.0);"
   698  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,\"bbbb\",5.0);"
   699  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,\"cccc\",6.0);"
   700  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   701  
   702  	# Add multiple indexes.
   703  	run_sql_source1 "alter table ${shardddl1}.${tb1} add unique index uni_a(a), add index idx_b(b);"
   704  	run_sql_source2 "alter table ${shardddl1}.${tb1} add unique index uni_a(a), add index idx_b(b);"
   705  	run_sql_source2 "alter table ${shardddl1}.${tb2} add unique index uni_a(a), add index idx_b(b);"
   706  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,\"aaaa\",7.0);"
   707  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,\"bbbb\",8.0);"
   708  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,\"cccc\",9.0);"
   709  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   710  
   711  	# Add and drop indexes.
   712  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop index idx_b, add index idx_c(c);"
   713  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop index idx_b, add index idx_c(c);"
   714  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop index idx_b, add index idx_c(c);"
   715  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,\"aaaa\",10.0);"
   716  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,\"bbbb\",11.0);"
   717  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,\"cccc\",12.0);"
   718  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   719  
   720  	# Add and drop fields.
   721  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop column b, add column d int;"
   722  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop column b, add column d int;"
   723  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop column b, add column d int;"
   724  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(13,13,13.0,13);"
   725  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(14,14,14.0,14);"
   726  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(15,15,15.0,15);"
   727  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   728  
   729  	# Drop all indexes.
   730  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop index uni_a, drop index idx_c;"
   731  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop index uni_a, drop index idx_c;"
   732  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop index uni_a, drop index idx_c;"
   733  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(16,16,16.0,16);"
   734  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(17,17,17.0,17);"
   735  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(18,18,18.0,18);"
   736  
   737  	# Drop all fields.
   738  	run_sql_source1 "alter table ${shardddl1}.${tb1} drop column a, drop column c, drop column d;"
   739  	run_sql_source2 "alter table ${shardddl1}.${tb1} drop column a, drop column c, drop column d;"
   740  	run_sql_source2 "alter table ${shardddl1}.${tb2} drop column a, drop column c, drop column d;"
   741  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(19);"
   742  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(20);"
   743  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(21);"
   744  
   745  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   746  }
   747  
   748  function DM_152 {
   749  	run_case 152 "double-source-optimistic" "init_table 111 211 212" "clean_table" "optimistic"
   750  }
   751  
   752  function DM_153_CASE {
   753  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   754  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   755  
   756  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column b int, drop column c;"
   757  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   758  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   759  
   760  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column c int, drop column b;"
   761  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   762  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   763  
   764  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   765  		"query-status test" \
   766  		"because schema conflict detected" 1 \
   767  		"add column c that wasn't fully dropped in downstream" 1
   768  
   769  	# try to fix data
   770  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   771  		"binlog-schema update test ${shardddl1} ${tb1} -s mysql-replica-01 --from-target" \
   772  		"\"result\": true" 2
   773  
   774  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   775  		"binlog replace test \"alter table ${shardddl1}.${tb1} drop column b\"" \
   776  		"\"result\": true" 2 \
   777  		"\"source 'mysql-replica-02' has no error\"" 1
   778  
   779  	run_sql_tidb "update ${shardddl}.${tb} set c=null where a=1;"
   780  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   781  }
   782  
   783  # Add fix sharding error by use target schema
   784  function DM_153 {
   785  	run_case 153 "double-source-optimistic" \
   786  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   787       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"" \
   788  		"clean_table" "optimistic"
   789  }
   790  
   791  function DM_154_CASE {
   792  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
   793  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
   794  
   795  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column b int, drop column c;"
   796  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4);"
   797  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5);"
   798  
   799  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column c int, drop column b;"
   800  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7);"
   801  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8);"
   802  
   803  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   804  		"query-status test" \
   805  		"because schema conflict detected" 1 \
   806  		"add column c that wasn't fully dropped in downstream" 1
   807  
   808  	# try to fix data
   809  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   810  		"binlog-schema update test ${shardddl1} ${tb1} -s mysql-replica-01 --from-source" \
   811  		"\"result\": true" 2
   812  
   813  	run_sql_tidb "alter table ${shardddl}.${tb} drop column b;"
   814  
   815  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   816  		"binlog skip test" \
   817  		"\"result\": true" 2 \
   818  		"\"source 'mysql-replica-02' has no error\"" 1
   819  
   820  	run_sql_tidb "update ${shardddl}.${tb} set c=null where a=1;"
   821  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   822  }
   823  
   824  # Add fix sharding error by use source schema
   825  function DM_154 {
   826  	run_case 154 "double-source-optimistic" \
   827  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   828       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c int) engine=innodb default charset=latin1 collate=latin1_bin;\"" \
   829  		"clean_table" "optimistic"
   830  }
   831  
   832  function DM_155_CASE {
   833  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1,1);"
   834  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2,2);"
   835  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(3,3,3);"
   836  
   837  	run_sql_source1 "alter table ${shardddl1}.${tb1} change c b int;"
   838  	sleep 1
   839  	random_restart 3
   840  
   841  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(4,4,4);"
   842  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(5,5,5);"
   843  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(6,6,6);"
   844  
   845  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column g int;"
   846  	sleep 1
   847  	random_restart 3
   848  
   849  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(7,7,7,7);"
   850  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(8,8,8);"
   851  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(9,9,9);"
   852  
   853  	run_sql_source1 "alter table ${shardddl1}.${tb1} change d f int;"
   854  	sleep 1
   855  	random_restart 3
   856  
   857  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(10,10,10,10);"
   858  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11,11);"
   859  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12,12);"
   860  
   861  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column e int not null after f;"
   862  	sleep 1
   863  	random_restart 3
   864  
   865  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(13,13,13,13,13);"
   866  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(14,14,14);"
   867  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(15,15,15);"
   868  
   869  	run_sql_source2 "alter table ${shardddl1}.${tb1} change c b int;"
   870  	sleep 1
   871  	random_restart 3
   872  
   873  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(16,16,16,16,16);"
   874  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(17,17,17);"
   875  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(18,18,18);"
   876  
   877  	run_sql_source2 "alter table ${shardddl1}.${tb1} change d f int;"
   878  	sleep 1
   879  	random_restart 3
   880  
   881  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(19,19,19,19,19);"
   882  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(20,20,20);"
   883  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(21,21,21);"
   884  
   885  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column g int;"
   886  	sleep 1
   887  	random_restart 3
   888  
   889  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(22,22,22,22,22);"
   890  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(23,23,23,23);"
   891  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(24,24,24);"
   892  
   893  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column e int not null after f;"
   894  	sleep 1
   895  	random_restart 3
   896  
   897  	# make sure source1 has received conflict ddl and handled
   898  	check_log_contain_with_retry "skip conflict ddls in optimistic shard mode.*ALTER TABLE \`${shardddl1}\`.\`${tb1}\` CHANGE COLUMN \`c\` \`b\` INT" \
   899  		$WORK_DIR/worker1/log/dm-worker.log
   900  	run_sql_source2 "alter table ${shardddl1}.${tb2} change c b int;"
   901  
   902  	# make sure source2 has handled tb2 ddl and trigger redirection
   903  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb2} change c b int" \
   904  		$WORK_DIR/worker2/log/dm-worker.log
   905  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(25,25,25,25,25);"
   906  
   907  	# make sure source1,2 has received and done redirection
   908  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb1} change c b int" \
   909  		$WORK_DIR/worker1/log/dm-worker.log
   910  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(26,26,26,26,26);"
   911  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(27,27,27);"
   912  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb1} change c b int" \
   913  		$WORK_DIR/worker2/log/dm-worker.log
   914  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(28,28,28,28,28);"
   915  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(29,29,29,29,29);"
   916  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(30,30,30);"
   917  
   918  	random_restart 3
   919  	# make sure source1 has received conflict ddl and handled
   920  	check_log_contain_with_retry "skip conflict ddls in optimistic shard mode.*ALTER TABLE \`${shardddl1}\`.\`${tb1}\` CHANGE COLUMN \`d\` \`f\` INT" \
   921  		$WORK_DIR/worker1/log/dm-worker.log
   922  
   923  	# make sure source2 has handled tb2 ddl and trigger redirection
   924  	run_sql_source2 "alter table ${shardddl1}.${tb2} change d f int;"
   925  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb2} change d f int" \
   926  		$WORK_DIR/worker2/log/dm-worker.log
   927  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(31,31,31,31,31);"
   928  
   929  	# make sure source1,2 has received and done redirection
   930  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb1} change d f int" \
   931  		$WORK_DIR/worker1/log/dm-worker.log
   932  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(32,32,32,32,32);"
   933  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(33,33,33);"
   934  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb1} change d f int" \
   935  		$WORK_DIR/worker2/log/dm-worker.log
   936  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(34,34,34,34,34);"
   937  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(35,35,35,35,35);"
   938  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(36,36,36);"
   939  
   940  	random_restart 3
   941  
   942  	# make sure source1 has received conflict ddl and handled
   943  	check_log_contain_with_retry "skip conflict ddls in optimistic shard mode.*ALTER TABLE \`${shardddl1}\`.\`${tb1}\` ADD COLUMN \`e\` INT" \
   944  		$WORK_DIR/worker1/log/dm-worker.log
   945  
   946  	# make sure source2 has handled tb2 ddl and trigger redirection
   947  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column e int not null after f;"
   948  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb2} add column e int not null after f" \
   949  		$WORK_DIR/worker2/log/dm-worker.log
   950  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(37,37,37,37,37);"
   951  
   952  	# make sure source1,2 has received and done redirection
   953  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb1} add column e int not null after f" \
   954  		$WORK_DIR/worker1/log/dm-worker.log
   955  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(38,38,38,38,38);"
   956  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(39,39,39,39);"
   957  	check_log_contain_with_retry "finish to handle ddls in optimistic shard mode.*alter table ${shardddl1}.${tb1} add column e int not null after f" \
   958  		$WORK_DIR/worker2/log/dm-worker.log
   959  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(40,40,40,40,40);"
   960  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(41,41,41,41,41);"
   961  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(42,42,42,42);"
   962  
   963  	random_restart 3
   964  
   965  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(43,43,43,43,43);"
   966  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(44,44,44,44,44);"
   967  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(45,45,45,45);"
   968  
   969  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column g int;"
   970  	sleep 1
   971  	random_restart 3
   972  
   973  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(46,46,46,46,46);"
   974  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(47,47,47,47,47);"
   975  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(48,48,48,48,48);"
   976  
   977  	# sleep 15 seconds to make sure both dm-workers have reached their final event
   978  	# then insert some dmls to avoid dm-worker get blocked at getting heart event which may cause 30s
   979  	# this part is used to handle case like:
   980  	# worker1 receives skip and wait redirect, and finishes all the events and start waiting to for heartbeat event
   981  	# worker2 resolves this lock, and finishes all its dmls, but worker1 is blocked at receiving heartbeat event(because there is no new data written)
   982  	for ((k = 100; k < 145; k++)); do
   983  		run_sql_source1 "insert into ${shardddl1}.${tb1} values(${k},${k},${k},${k},${k});"
   984  		k=$((k + 1))
   985  		run_sql_source2 "insert into ${shardddl1}.${tb1} values(${k},${k},${k},${k},${k});"
   986  		k=$((k + 1))
   987  		run_sql_source2 "insert into ${shardddl1}.${tb2} values(${k},${k},${k},${k},${k});"
   988  		sleep 1
   989  	done
   990  
   991  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
   992  }
   993  
   994  # Add syncing optimistic conflict sequence DDLs case
   995  function DM_155 {
   996  	run_case 155 "double-source-optimistic" \
   997  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, c int, d int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   998       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, c int, d int) engine=innodb default charset=latin1 collate=latin1_bin;\"; \
   999       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, c int, d int) engine=innodb default charset=latin1 collate=latin1_bin;\";" \
  1000  		"clean_table" "optimistic"
  1001  }
  1002  
  1003  function DM_TABLE_CHECKPOINT_BACKWARD_CASE() {
  1004  	run_sql_source1 "alter table shardddl1.tb1 change b c int;"
  1005  	for i in $(seq 1 1000); do
  1006  		run_sql_source1 "insert into shardddl1.tb1(a,c) values($i,$i)"
  1007  	done
  1008  	run_sql_source1 "alter table shardddl1.t_1 change b c int;"
  1009  	for i in $(seq 1001 2000); do
  1010  		run_sql_source1 "insert into shardddl1.tb1(a,c) values($i,$i)"
  1011  	done
  1012  	for i in $(seq 2001 3000); do
  1013  		run_sql_source1 "insert into shardddl1.t_1(a,c) values($i,$i)"
  1014  	done
  1015  	run_sql_source2 "alter table shardddl1.tb1 change b c int;"
  1016  	for i in $(seq 3001 3100); do
  1017  		run_sql_source1 "insert into shardddl1.tb1(a,c) values($i,$i)"
  1018  	done
  1019  	run_sql_source2 "alter table shardddl1.t_1 change b c int;"
  1020  	for i in $(seq 3101 3200); do
  1021  		run_sql_source1 "insert into shardddl1.tb1(a,c) values($i,$i)"
  1022  	done
  1023  	cp $cur/conf/diff_config.toml $WORK_DIR/diff_config.toml
  1024  	sed -i "s/\[routes.rule1\]/[routes.rule2]\nschema-pattern = \"shardddl[1-2]\"\ntable-pattern = \"t_1\"\ntarget-schema = \"shardddl\"\ntarget-table = \"t_1\"\n\[routes.rule1\]/g" $WORK_DIR/diff_config.toml
  1025  	sed -i "s/route-rules = \[\"rule1\"\]/route-rules = \[\"rule1\"\,\"rule2\"]/g" $WORK_DIR/diff_config.toml
  1026  	sed -i "s/target-check-tables = \[\"shardddl.tb\"\]/target-check-tables = \[\"shardddl.tb\",\"shardddl.t_1\"\]/g" $WORK_DIR/diff_config.toml
  1027  	check_sync_diff $WORK_DIR $WORK_DIR/diff_config.toml 30
  1028  }
  1029  
  1030  function DM_TABLE_CHECKPOINT_BACKWARD() {
  1031  	run_case TABLE_CHECKPOINT_BACKWARD "double-source-optimistic" \
  1032  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1033       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1034       run_sql_source1 \"create table ${shardddl1}.t_1 (a int primary key, b int);\"; \
  1035       run_sql_source2 \"create table ${shardddl1}.t_1 (a int primary key, b int);\"" \
  1036  		"clean_table" "optimistic"
  1037  }
  1038  
  1039  function DM_RESYNC_NOT_FLUSHED_CASE() {
  1040  	export GO_FAILPOINTS='github.com/pingcap/tiflow/dm/syncer/ReSyncExit=return(true)'
  1041  	restart_worker1
  1042  	restart_worker2
  1043  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
  1044  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(2,2);"
  1045  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(3,3);"
  1046  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(4,4);"
  1047  
  1048  	run_sql_source1 "alter table ${shardddl1}.${tb1} change b c int;"
  1049  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(5,5);"
  1050  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(6,6);"
  1051  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(7,7);"
  1052  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(8,8);"
  1053  
  1054  	run_sql_source1 "alter table ${shardddl1}.${tb1} add column d int not null;"
  1055  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(9,9,9);"
  1056  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(10,10);"
  1057  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(11,11);"
  1058  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(12,12);"
  1059  
  1060  	run_sql_source1 "alter table ${shardddl1}.${tb2} change b c int;"
  1061  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(13,13,13);"
  1062  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(14,14);"
  1063  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(15,15);"
  1064  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(16,16);"
  1065  
  1066  	run_sql_source1 "alter table ${shardddl1}.${tb2} add column d int not null;"
  1067  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(17,17,17);"
  1068  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(18,18,18);"
  1069  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(19,19);"
  1070  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(20,20);"
  1071  
  1072  	run_sql_source2 "alter table ${shardddl1}.${tb1} change b c int;"
  1073  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(21,21,21);"
  1074  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(22,22,22);"
  1075  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(23,23);"
  1076  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(24,24);"
  1077  
  1078  	run_sql_source2 "alter table ${shardddl1}.${tb1} add column d int not null;"
  1079  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(25,25,25);"
  1080  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(26,26,26);"
  1081  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(27,27,27);"
  1082  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(28,28);"
  1083  
  1084  	run_sql_source2 "alter table ${shardddl1}.${tb2} change b c int;"
  1085  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(29,29,29);"
  1086  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(30,30,30);"
  1087  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(31,31,31);"
  1088  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(32,32);"
  1089  
  1090  	for ((k = 100; k < 120; k++)); do
  1091  		run_sql_source1 "insert into ${shardddl1}.${tb1} values(${k},${k},${k});"
  1092  		k=$((k + 1))
  1093  		run_sql_source1 "insert into ${shardddl1}.${tb2} values(${k},${k},${k});"
  1094  		k=$((k + 1))
  1095  		run_sql_source2 "insert into ${shardddl1}.${tb1} values(${k},${k},${k});"
  1096  		k=$((k + 1))
  1097  		run_sql_source2 "insert into ${shardddl1}.${tb2} values(${k},${k});"
  1098  		sleep 1
  1099  	done
  1100  
  1101  	check_log_contain_with_retry "receive redirection operation from master" $WORK_DIR/worker1/log/dm-worker.log
  1102  	check_log_contain_with_retry "receive redirection operation from master" $WORK_DIR/worker2/log/dm-worker.log
  1103  	for ((k = 140; k < 160; k++)); do
  1104  		run_sql_source1 "insert into ${shardddl1}.${tb1} values(${k},${k},${k});"
  1105  		k=$((k + 1))
  1106  		run_sql_source1 "insert into ${shardddl1}.${tb2} values(${k},${k},${k});"
  1107  		k=$((k + 1))
  1108  		run_sql_source2 "insert into ${shardddl1}.${tb1} values(${k},${k},${k});"
  1109  		k=$((k + 1))
  1110  		run_sql_source2 "insert into ${shardddl1}.${tb2} values(${k},${k});"
  1111  		sleep 1
  1112  	done
  1113  
  1114  	# lock finished at first time, both workers should exit
  1115  	check_process_exit worker1 20
  1116  	check_process_exit worker2 20
  1117  	export GO_FAILPOINTS='github.com/pingcap/tiflow/dm/syncer/FakeRedirect=1*return("`shardddl`.`tb`")'
  1118  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
  1119  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
  1120  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
  1121  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
  1122  
  1123  	run_sql_source2 "alter table ${shardddl1}.${tb2} add column d int not null;"
  1124  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(33,33,33);"
  1125  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(34,34,34);"
  1126  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(35,35,35);"
  1127  	run_sql_source2 "insert into ${shardddl1}.${tb2} values(36,36,36);"
  1128  
  1129  	for ((k = 200; k < 280; k++)); do
  1130  		run_sql_source1 "insert into ${shardddl1}.${tb1} values(${k},${k},${k});"
  1131  		k=$((k + 1))
  1132  		run_sql_source1 "insert into ${shardddl1}.${tb2} values(${k},${k},${k});"
  1133  		k=$((k + 1))
  1134  		run_sql_source2 "insert into ${shardddl1}.${tb1} values(${k},${k},${k});"
  1135  		k=$((k + 1))
  1136  		run_sql_source2 "insert into ${shardddl1}.${tb2} values(${k},${k},${k});"
  1137  		sleep 1
  1138  	done
  1139  
  1140  	check_sync_diff $WORK_DIR $cur/conf/diff_config.toml
  1141  	export GO_FAILPOINTS=''
  1142  	restart_worker1
  1143  	restart_worker2
  1144  }
  1145  
  1146  function DM_RESYNC_NOT_FLUSHED() {
  1147  	run_case RESYNC_NOT_FLUSHED "double-source-optimistic" \
  1148  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1149  		 run_sql_source1 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"; \
  1150  		 run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1151       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
  1152  		"clean_table" "optimistic"
  1153  }
  1154  
  1155  function DM_RESYNC_TXN_INTERRUPT_CASE() {
  1156  	# continue at the middle of a dml transaction
  1157  	export GO_FAILPOINTS='github.com/pingcap/tiflow/dm/syncer/SleepInTxn=return(20)'
  1158  	restart_worker1
  1159  	restart_worker2
  1160  
  1161  	run_sql_source2 "alter table shardddl1.tb2 change b c int;"
  1162  	run_sql_with_txn "shardddl1.tb2" 2 1 10 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1163  	run_sql_with_txn "shardddl1.t_1" 2 11 50 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1164  	source2worker=$($PWD/bin/dmctl.test DEVEL --master-addr "127.0.0.1:$MASTER_PORT1" operate-source show -s "mysql-replica-02" |
  1165  		grep 'worker' | awk -F: '{print $2}' | cut -d'"' -f 2)
  1166  	# make sure source2 in skip and wait redirect
  1167  	check_log_contain_with_retry 'got a shard DDL lock operation.*CHANGE COLUMN `b` `c` INT' $WORK_DIR/$source2worker/log/dm-worker.log
  1168  	run_sql_source1 "alter table shardddl1.tb1 change b c int;"
  1169  	run_sql_with_txn "shardddl1.tb1" 2 51 60 $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
  1170  	run_sql_with_txn "shardddl1.t_1" 2 61 70 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1171  
  1172  	cp $cur/conf/diff_config.toml $WORK_DIR/diff_config.toml
  1173  	sed -i "s/\[routes.rule1\]/[routes.rule2]\nschema-pattern = \"shardddl[1-2]\"\ntable-pattern = \"t_1\"\ntarget-schema = \"shardddl\"\ntarget-table = \"t_1\"\n\[routes.rule1\]/g" $WORK_DIR/diff_config.toml
  1174  	sed -i "s/route-rules = \[\"rule1\"\]/route-rules = \[\"rule1\"\,\"rule2\"]/g" $WORK_DIR/diff_config.toml
  1175  	sed -i "s/target-check-tables = \[\"shardddl.tb\"\]/target-check-tables = \[\"shardddl.tb\",\"shardddl.t_1\"\]/g" $WORK_DIR/diff_config.toml
  1176  	check_sync_diff $WORK_DIR $WORK_DIR/diff_config.toml 30
  1177  
  1178  	# continue after gtid event but before query event
  1179  	export GO_FAILPOINTS=""
  1180  	restart_worker1
  1181  	restart_worker2
  1182  
  1183  	run_sql_source1 "alter table shardddl1.tb1 change c d int;"
  1184  	source1worker=$($PWD/bin/dmctl.test DEVEL --master-addr "127.0.0.1:$MASTER_PORT1" operate-source show -s "mysql-replica-01" |
  1185  		grep 'worker' | awk -F: '{print $2}' | cut -d'"' -f 2)
  1186  	# make sure source2 in skip and wait redirect
  1187  	check_log_contain_with_retry 'got a shard DDL lock operation.*CHANGE COLUMN `c` `d` INT' $WORK_DIR/$source1worker/log/dm-worker.log
  1188  	run_sql_source2 "alter table shardddl1.tb2 change c d int;"
  1189  	run_sql_with_txn "shardddl1.tb2" 2 101 110 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1190  	run_sql_with_txn "shardddl1.tb1" 2 111 120 $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
  1191  	run_sql_with_txn "shardddl1.tb2" 2 121 130 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1192  	run_sql_with_txn "shardddl1.t_1" 2 131 140 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1193  	check_sync_diff $WORK_DIR $WORK_DIR/diff_config.toml 30
  1194  
  1195  	run_sql_source2 "alter table shardddl1.tb2 change d e int;"
  1196  	# make sure source2 in skip and wait redirect
  1197  	source2worker=$($PWD/bin/dmctl.test DEVEL --master-addr "127.0.0.1:$MASTER_PORT1" operate-source show -s "mysql-replica-02" |
  1198  		grep 'worker' | awk -F: '{print $2}' | cut -d'"' -f 2)
  1199  	check_log_contain_with_retry 'got a shard DDL lock operation.*CHANGE COLUMN `d` `e` INT' $WORK_DIR/$source2worker/log/dm-worker.log
  1200  	run_sql_source1 "alter table shardddl1.tb1 change d e int;"
  1201  	run_sql_with_txn "shardddl1.tb2" 2 201 210 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1202  	run_sql_with_txn "shardddl1.tb1" 2 211 220 $MYSQL_HOST1 $MYSQL_PORT1 $MYSQL_PASSWORD1
  1203  	run_sql_with_txn "shardddl1.tb2" 2 221 230 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1204  	run_sql_with_txn "shardddl1.t_1" 2 231 240 $MYSQL_HOST2 $MYSQL_PORT2 $MYSQL_PASSWORD2
  1205  	check_sync_diff $WORK_DIR $WORK_DIR/diff_config.toml 30
  1206  }
  1207  
  1208  function DM_RESYNC_TXN_INTERRUPT() {
  1209  	run_case RESYNC_TXN_INTERRUPT "double-source-optimistic" \
  1210  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1211       run_sql_source2 \"create table ${shardddl1}.t_1 (a int primary key, b int);\"; \
  1212       run_sql_source2 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
  1213  		"clean_table" "optimistic"
  1214  }
  1215  
  1216  function DM_STRICT_OPTIMISTIC_SINGLE_SOURCE_CASE() {
  1217  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
  1218  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(2,2);"
  1219  
  1220  	run_sql_source1 "alter table ${shardddl1}.${tb1} add c int not null default 10;"
  1221  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(3,3,3);"
  1222  	run_sql_source1 "insert into ${shardddl1}.${tb2} values(4,4);"
  1223  
  1224  	run_sql_source1 "alter table ${shardddl1}.${tb2} add c varchar(30) not null default '10';"
  1225  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
  1226  		"query-status test" \
  1227  		'"stage": "Paused"' 1 \
  1228  		"because schema conflict detected" 1
  1229  }
  1230  
  1231  function DM_STRICT_OPTIMISTIC_SINGLE_SOURCE() {
  1232  	run_case STRICT_OPTIMISTIC_SINGLE_SOURCE "single-source-strict-optimistic" \
  1233  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1234       run_sql_source1 \"create table ${shardddl1}.${tb2} (a int primary key, b int);\"" \
  1235  		"clean_table" "optimistic"
  1236  }
  1237  
  1238  function DM_STRICT_OPTIMISTIC_DOUBLE_SOURCE_CASE() {
  1239  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(1,1);"
  1240  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(2,2);"
  1241  
  1242  	run_sql_source1 "alter table ${shardddl1}.${tb1} add c int;"
  1243  	run_sql_source1 "insert into ${shardddl1}.${tb1} values(3,3,3);"
  1244  	run_sql_source2 "insert into ${shardddl1}.${tb1} values(4,4);"
  1245  
  1246  	run_sql_source1 "alter table ${shardddl1}.${tb1} add d int not null;"
  1247  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
  1248  		"query-status test" \
  1249  		"because schema conflict detected" 1
  1250  }
  1251  
  1252  function DM_STRICT_OPTIMISTIC_DOUBLE_SOURCE() {
  1253  	run_case STRICT_OPTIMISTIC_DOUBLE_SOURCE "double-source-strict-optimistic" \
  1254  		"run_sql_source1 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"; \
  1255       run_sql_source2 \"create table ${shardddl1}.${tb1} (a int primary key, b int);\"" \
  1256  		"clean_table" "optimistic"
  1257  }
  1258  
  1259  function run() {
  1260  	init_cluster
  1261  	init_database
  1262  	DM_TABLE_CHECKPOINT_BACKWARD
  1263  	DM_RESYNC_NOT_FLUSHED
  1264  	DM_RESYNC_TXN_INTERRUPT
  1265  	DM_STRICT_OPTIMISTIC_SINGLE_SOURCE
  1266  	DM_STRICT_OPTIMISTIC_DOUBLE_SOURCE
  1267  	start=131
  1268  	end=155
  1269  	except=(140 141 144)
  1270  	for i in $(seq -f "%03g" ${start} ${end}); do
  1271  		if [[ ${except[@]} =~ $i ]]; then
  1272  			continue
  1273  		fi
  1274  		DM_${i}
  1275  		sleep 1
  1276  	done
  1277  }
  1278  
  1279  cleanup_data $shardddl
  1280  cleanup_data $shardddl1
  1281  cleanup_data $shardddl2
  1282  # also cleanup dm processes in case of last run failed
  1283  cleanup_process $*
  1284  run $*
  1285  cleanup_process $*
  1286  
  1287  echo "[$(date)] <<<<<< test case $TEST_NAME success! >>>>>>"