|
| 1 | +// Copyright 2018 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 | + "bufio" |
| 18 | + "context" |
| 19 | + "flag" |
| 20 | + "fmt" |
| 21 | + _ "github.com/go-sql-driver/mysql" |
| 22 | + "github.com/pingcap/errors" |
| 23 | + "github.com/pingcap/tidb-tools/pkg/dbutil" |
| 24 | + "github.com/pingcap/tidb-tools/pkg/ddl-checker" |
| 25 | + "os" |
| 26 | + "strings" |
| 27 | + "unicode" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + mode = auto |
| 32 | + executableChecker *checker.ExecutableChecker |
| 33 | + ddlSyncer *checker.DDLSyncer |
| 34 | + reader *bufio.Reader |
| 35 | + tidbContext = context.Background() |
| 36 | + |
| 37 | + host = flag.String("host", "127.0.0.1", "MySQL host") |
| 38 | + port = flag.Int("port", 3306, "MySQL port") |
| 39 | + username = flag.String("user", "root", "User name") |
| 40 | + password = flag.String("password", "", "Password") |
| 41 | + schema = flag.String("schema", "", "Schema") |
| 42 | +) |
| 43 | + |
| 44 | +const ( |
| 45 | + welcomeInfo = "ExecutableChecker: Check if SQL can be successfully executed by TiDB\n" + |
| 46 | + "Copyright 2018 PingCAP, Inc.\n\n" + |
| 47 | + "You can switch modes using the `SETMOD` command.\n" + |
| 48 | + "Auto mode: The program will automatically synchronize the dependent table structure from MySQL " + |
| 49 | + "and delete the conflict table\n" + |
| 50 | + "Prompt mode: The program will ask you before synchronizing the dependent table structure from MYSQL\n" + |
| 51 | + "Offline mode: This program doesn't need to connect to MySQL, and doesn't perform anything other than executing the input SQL.\n\n" + |
| 52 | + setmodUsage + "\n" |
| 53 | + |
| 54 | + setmodUsage = "SETMOD usage: SETMOD <MODCODE>; MODCODE = [\"Auto\", \"Prompt\", \"Offline\"] (case insensitive).\n" |
| 55 | + |
| 56 | + auto = "auto" |
| 57 | + prompt = "prompt" |
| 58 | + offline = "offline" |
| 59 | +) |
| 60 | + |
| 61 | +func main() { |
| 62 | + fmt.Print(welcomeInfo) |
| 63 | + initialise() |
| 64 | + mainLoop() |
| 65 | + destroy() |
| 66 | +} |
| 67 | + |
| 68 | +func initialise() { |
| 69 | + flag.Parse() |
| 70 | + var err error |
| 71 | + reader = bufio.NewReader(os.Stdin) |
| 72 | + executableChecker, err = checker.NewExecutableChecker() |
| 73 | + if err != nil { |
| 74 | + fmt.Printf("[DDLChecker] Init failed, can't create ExecutableChecker: %s\n", err.Error()) |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + executableChecker.Execute(tidbContext, "use test;") |
| 78 | + dbInfo := &dbutil.DBConfig{ |
| 79 | + User: *username, |
| 80 | + Password: *password, |
| 81 | + Host: *host, |
| 82 | + Port: *port, |
| 83 | + Schema: *schema, |
| 84 | + } |
| 85 | + ddlSyncer, err = checker.NewDDLSyncer(dbInfo, executableChecker) |
| 86 | + if err != nil { |
| 87 | + fmt.Printf("[DDLChecker] Init failed, can't open mysql database: %s\n", err.Error()) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func destroy() { |
| 93 | + executableChecker.Close() |
| 94 | + ddlSyncer.Close() |
| 95 | +} |
| 96 | + |
| 97 | +func mainLoop() { |
| 98 | + var input string |
| 99 | + var err error |
| 100 | + for isContinue := true; isContinue; isContinue = handler(input) { |
| 101 | + fmt.Printf("[%s] > ", strings.ToTitle(mode)) |
| 102 | + input, err = reader.ReadString(';') |
| 103 | + if err != nil { |
| 104 | + fmt.Printf("[DDLChecker] Read stdin error: %s\n", err.Error()) |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +func handler(input string) bool { |
| 110 | + lowerTrimInput := strings.ToLower(strings.TrimFunc(input, func(r rune) bool { |
| 111 | + return unicode.IsSpace(r) || r == ';' |
| 112 | + })) |
| 113 | + // cmd exit |
| 114 | + if lowerTrimInput == "exit" { |
| 115 | + return false |
| 116 | + } |
| 117 | + // cmd setmod |
| 118 | + if strings.HasPrefix(lowerTrimInput, "setmod") { |
| 119 | + x := strings.TrimSpace(lowerTrimInput[6:]) |
| 120 | + switch x { |
| 121 | + case auto, prompt, offline: |
| 122 | + mode = x |
| 123 | + default: |
| 124 | + fmt.Print(setmodUsage) |
| 125 | + } |
| 126 | + return true |
| 127 | + } |
| 128 | + stmt, err := executableChecker.Parse(input) |
| 129 | + if err != nil { |
| 130 | + fmt.Println("[DDLChecker] SQL parse error: ", err.Error()) |
| 131 | + return true |
| 132 | + } |
| 133 | + if !checker.IsDDL(stmt) { |
| 134 | + fmt.Println("[DDLChecker] Warning: The input SQL isn't a DDL") |
| 135 | + } |
| 136 | + if mode != offline { |
| 137 | + // auto and query mod |
| 138 | + neededTables, _ := checker.GetTablesNeededExist(stmt) |
| 139 | + nonNeededTables, err := checker.GetTablesNeededNonExist(stmt) |
| 140 | + // skip when stmt isn't a DDLNode |
| 141 | + if err == nil && (mode == auto || (mode == prompt && promptAutoSync(neededTables, nonNeededTables))) { |
| 142 | + err := syncTablesFromMysql(neededTables) |
| 143 | + if err != nil { |
| 144 | + return true |
| 145 | + } |
| 146 | + err = dropTables(nonNeededTables) |
| 147 | + if err != nil { |
| 148 | + return true |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + err = executableChecker.Execute(tidbContext, input) |
| 153 | + if err == nil { |
| 154 | + fmt.Println("[DDLChecker] SQL execution succeeded") |
| 155 | + } else { |
| 156 | + fmt.Println("[DDLChecker] SQL execution failed:", err.Error()) |
| 157 | + } |
| 158 | + return true |
| 159 | +} |
| 160 | + |
| 161 | +func syncTablesFromMysql(tableNames []string) error { |
| 162 | + for _, tableName := range tableNames { |
| 163 | + fmt.Println("[DDLChecker] Syncing Table", tableName) |
| 164 | + err := ddlSyncer.SyncTable(tidbContext, *schema, tableName) |
| 165 | + if err != nil { |
| 166 | + fmt.Println("[DDLChecker] Sync table failure:", err.Error()) |
| 167 | + return errors.Trace(err) |
| 168 | + } |
| 169 | + } |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func dropTables(tableNames []string) error { |
| 174 | + for _, tableName := range tableNames { |
| 175 | + fmt.Println("[DDLChecker] Dropping table", tableName) |
| 176 | + err := executableChecker.DropTable(tidbContext, tableName) |
| 177 | + if err != nil { |
| 178 | + fmt.Println("[DDLChecker] Drop table", tableName, "Error:", err.Error()) |
| 179 | + return errors.Trace(err) |
| 180 | + } |
| 181 | + } |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +func promptAutoSync(neededTable []string, nonNeededTable []string) bool { |
| 186 | + return promptYorN("[DDLChecker] Do you want to synchronize table %v from MySQL "+ |
| 187 | + "and drop table %v in DDLChecker?(Y/N)", neededTable, nonNeededTable) |
| 188 | +} |
| 189 | + |
| 190 | +func promptYorN(format string, a ...interface{}) bool { |
| 191 | + for { |
| 192 | + fmt.Printf(format, a...) |
| 193 | + innerLoop: |
| 194 | + for { |
| 195 | + result, err := reader.ReadString('\n') |
| 196 | + if err != nil { |
| 197 | + fmt.Printf("[DDLChecker] Read stdin error: %s\n", err.Error()) |
| 198 | + return false |
| 199 | + } |
| 200 | + switch strings.ToLower(strings.TrimSpace(result)) { |
| 201 | + case "y", "yes": |
| 202 | + return true |
| 203 | + case "n", "no": |
| 204 | + return false |
| 205 | + case "": |
| 206 | + continue innerLoop |
| 207 | + default: |
| 208 | + break innerLoop |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments