github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/shardddl/optimism/ops_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package optimism
    15  
    16  import (
    17  	. "github.com/pingcap/check"
    18  )
    19  
    20  func (t *testForEtcd) TestDeleteInfosOperationsSchema(c *C) {
    21  	defer clearTestInfoOperation(c)
    22  
    23  	var (
    24  		task       = "test"
    25  		source     = "mysql-replica-1"
    26  		upSchema   = "foo-1"
    27  		upTable    = "bar-1"
    28  		downSchema = "foo"
    29  		downTable  = "bar"
    30  		DDLs       = []string{"ALTER TABLE bar ADD COLUMN c1 INT"}
    31  		info       = NewInfo(task, source, upSchema, upTable, downSchema, downTable, DDLs, nil, nil)
    32  		op         = NewOperation("test-ID", task, source, upSchema, upTable, DDLs, ConflictResolved, "", false, []string{})
    33  	)
    34  
    35  	// put info.
    36  	rev, err := PutInfo(etcdTestCli, info)
    37  	c.Assert(err, IsNil)
    38  	ifm, _, err := GetAllInfo(etcdTestCli)
    39  	c.Assert(err, IsNil)
    40  	c.Assert(ifm, HasLen, 1)
    41  	infoWithVer := info
    42  	infoWithVer.Version = 1
    43  	infoWithVer.Revision = rev
    44  	c.Assert(ifm[task][source][upSchema][upTable], DeepEquals, infoWithVer)
    45  
    46  	// put operation.
    47  	rev, _, err = PutOperation(etcdTestCli, false, op, 0)
    48  	c.Assert(err, IsNil)
    49  	opm, _, err := GetAllOperations(etcdTestCli)
    50  	c.Assert(err, IsNil)
    51  	c.Assert(opm, HasLen, 1)
    52  	op.Revision = rev
    53  	c.Assert(opm[task][source][upSchema][upTable], DeepEquals, op)
    54  
    55  	// DELETE info and operation with version 0
    56  	_, deleted, err := DeleteInfosOperationsColumns(etcdTestCli, []Info{info}, []Operation{op}, genDDLLockID(info))
    57  	c.Assert(err, IsNil)
    58  	c.Assert(deleted, IsFalse)
    59  
    60  	// data still exist
    61  	ifm, _, err = GetAllInfo(etcdTestCli)
    62  	c.Assert(err, IsNil)
    63  	c.Assert(ifm, HasLen, 1)
    64  	opm, _, err = GetAllOperations(etcdTestCli)
    65  	c.Assert(err, IsNil)
    66  	c.Assert(opm, HasLen, 1)
    67  
    68  	// DELETE info and operation with version 1
    69  	_, deleted, err = DeleteInfosOperationsColumns(etcdTestCli, []Info{infoWithVer}, []Operation{op}, genDDLLockID(infoWithVer))
    70  	c.Assert(err, IsNil)
    71  	c.Assert(deleted, IsTrue)
    72  
    73  	// verify no info & operation exist.
    74  	ifm, _, err = GetAllInfo(etcdTestCli)
    75  	c.Assert(err, IsNil)
    76  	c.Assert(ifm, HasLen, 0)
    77  	opm, _, err = GetAllOperations(etcdTestCli)
    78  	c.Assert(err, IsNil)
    79  	c.Assert(opm, HasLen, 0)
    80  }
    81  
    82  func (t *testForEtcd) TestSourceTablesInfo(c *C) {
    83  	defer clearTestInfoOperation(c)
    84  
    85  	var (
    86  		task       = "task"
    87  		source     = "mysql-replica-1"
    88  		downSchema = "foo"
    89  		downTable  = "bar"
    90  		st1        = NewSourceTables(task, source)
    91  		st2        = NewSourceTables(task, source)
    92  	)
    93  
    94  	st1.AddTable("db", "tbl-1", downSchema, downTable)
    95  	st1.AddTable("db", "tbl-2", downSchema, downTable)
    96  	st2.AddTable("db", "tbl-2", downSchema, downTable)
    97  	st2.AddTable("db", "tbl-3", downSchema, downTable)
    98  
    99  	// put source tables
   100  	rev1, err := PutSourceTables(etcdTestCli, st1)
   101  	c.Assert(err, IsNil)
   102  	c.Assert(rev1, Greater, int64(0))
   103  
   104  	stm, rev2, err := GetAllSourceTables(etcdTestCli)
   105  	c.Assert(err, IsNil)
   106  	c.Assert(rev2, Equals, rev1)
   107  	c.Assert(stm, HasLen, 1)
   108  	c.Assert(stm[task], HasLen, 1)
   109  	c.Assert(stm[task][source], DeepEquals, st1)
   110  
   111  	// put/update source tables
   112  	rev4, err := PutSourceTables(etcdTestCli, st2)
   113  	c.Assert(err, IsNil)
   114  	c.Assert(rev4, Greater, rev1)
   115  
   116  	stm, rev5, err := GetAllSourceTables(etcdTestCli)
   117  	c.Assert(err, IsNil)
   118  	c.Assert(rev5, Equals, rev4)
   119  	c.Assert(stm, HasLen, 1)
   120  	c.Assert(stm[task], HasLen, 1)
   121  	c.Assert(stm[task][source], DeepEquals, st2)
   122  }