github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/tests/many_pk_or_uk/main.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 main 15 16 import ( 17 "database/sql" 18 "flag" 19 "fmt" 20 "math" 21 "os" 22 "sync" 23 24 "github.com/pingcap/errors" 25 "github.com/pingcap/log" 26 "github.com/pingcap/ticdc/tests/util" 27 ) 28 29 func main() { 30 cfg := util.NewConfig() 31 err := cfg.Parse(os.Args[1:]) 32 switch errors.Cause(err) { 33 case nil: 34 case flag.ErrHelp: 35 os.Exit(0) 36 default: 37 log.S().Errorf("parse cmd flags err %s\n", err) 38 os.Exit(2) 39 } 40 41 sourceDB, err := util.CreateDB(cfg.SourceDBCfg[0]) 42 if err != nil { 43 log.S().Fatal(err) 44 } 45 defer func() { 46 if err := util.CloseDB(sourceDB); err != nil { 47 log.S().Errorf("Failed to close source database: %s\n", err) 48 } 49 }() 50 runPKorUKcases(sourceDB) 51 util.MustExec(sourceDB, "create table finish_mark(a int primary key);") 52 } 53 54 // create a table with one column id with different type 55 // test the case whether it is primary key too, this can 56 // also help test when the column is handle or not. 57 func runPKorUKcases(db *sql.DB) { 58 cases := []struct { 59 Tp string 60 Value interface{} 61 Update interface{} 62 }{ 63 { 64 Tp: "BIGINT UNSIGNED", 65 Value: uint64(math.MaxUint64), 66 Update: uint64(math.MaxUint64) - 1, 67 }, 68 { 69 Tp: "BIGINT SIGNED", 70 Value: int64(math.MaxInt64), 71 Update: int64(math.MinInt64), 72 }, 73 { 74 Tp: "INT UNSIGNED", 75 Value: uint32(math.MaxUint32), 76 Update: uint32(math.MaxUint32) - 1, 77 }, 78 { 79 Tp: "INT SIGNED", 80 Value: int32(math.MaxInt32), 81 Update: int32(math.MinInt32), 82 }, 83 { 84 Tp: "SMALLINT UNSIGNED", 85 Value: uint16(math.MaxUint16), 86 Update: uint16(math.MaxUint16) - 1, 87 }, 88 { 89 Tp: "SMALLINT SIGNED", 90 Value: int16(math.MaxInt16), 91 Update: int16(math.MinInt16), 92 }, 93 { 94 Tp: "TINYINT UNSIGNED", 95 Value: uint8(math.MaxUint8), 96 Update: uint8(math.MaxUint8) - 1, 97 }, 98 { 99 Tp: "TINYINT SIGNED", 100 Value: int8(math.MaxInt8), 101 Update: int8(math.MaxInt8) - 1, 102 }, 103 } 104 105 var g sync.WaitGroup 106 107 for i, c := range cases { 108 for j, pkOrUK := range []string{"UNIQUE NOT NULL", "PRIMARY KEY"} { 109 g.Add(1) 110 tableName := fmt.Sprintf("pk_or_uk_%d_%d", i, j) 111 pkOrUK := pkOrUK 112 c := c 113 go func() { 114 sql := fmt.Sprintf("CREATE TABLE %s(id %s %s)", tableName, c.Tp, pkOrUK) 115 util.MustExec(db, sql) 116 sql = fmt.Sprintf("INSERT INTO %s(id) values( ? )", tableName) 117 util.MustExec(db, sql, c.Value) 118 sql = fmt.Sprintf("UPDATE %s set id = ? where id = ?", tableName) 119 util.MustExec(db, sql, c.Update, c.Value) 120 sql = fmt.Sprintf("INSERT INTO %s(id) values( ? )", tableName) 121 util.MustExec(db, sql, c.Value) 122 sql = fmt.Sprintf("DELETE from %s where id = ?", tableName) 123 util.MustExec(db, sql, c.Update) 124 g.Done() 125 }() 126 } 127 } 128 g.Wait() 129 }