github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/chaos/cases/schema.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 "github.com/pingcap/tidb/pkg/parser" 18 "github.com/pingcap/tidb/pkg/parser/ast" 19 ) 20 21 const ( 22 tableType = "BASE TABLE" 23 ) 24 25 // createTableToSmithSchema converts a `CREATE TABLE` statement to a schema representation needed by go-sqlsmith. 26 // for one column in `columns`: 27 // - columns[0]: DB name 28 // - columns[1]: table name 29 // - columns[2]: table type (only use `BASE TABLE` now) 30 // - columns[3]: column name 31 // - columns[4]: column type 32 // 33 // for `indexes`: 34 // - list of index name 35 func createTableToSmithSchema(dbName, sql string) (columns [][5]string, indexes []string, err error) { 36 parser2 := parser.New() // we should have clear `SQL_MODE. 37 stmt, err := parser2.ParseOneStmt(sql, "", "") 38 if err != nil { 39 return nil, nil, err 40 } 41 42 createStmt := stmt.(*ast.CreateTableStmt) 43 for _, col := range createStmt.Cols { 44 columns = append(columns, [5]string{ 45 dbName, 46 createStmt.Table.Name.O, 47 tableType, 48 col.Name.Name.O, 49 col.Tp.String(), 50 }) 51 } 52 53 for _, cons := range createStmt.Constraints { 54 switch cons.Tp { 55 case ast.ConstraintPrimaryKey, ast.ConstraintKey, ast.ConstraintIndex, ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex: 56 indexes = append(indexes, cons.Name) 57 default: 58 continue 59 } 60 } 61 return 62 }