Skip to content

Commit

Permalink
added dump flow
Browse files Browse the repository at this point in the history
  • Loading branch information
akashthawaitcc committed Dec 20, 2024
1 parent 8253898 commit 58a6db8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions sources/mysql/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"fmt"
"regexp"
"sort"
"strings"

Expand All @@ -34,6 +35,8 @@ import (
"github.com/GoogleCloudPlatform/spanner-migration-tool/streaming"
)

var collationRegex = regexp.MustCompile(constants.DB_COLLATION_REGEX)

// InfoSchemaImpl is MySQL specific implementation for InfoSchema.
type InfoSchemaImpl struct {
DbName string
Expand Down
39 changes: 37 additions & 2 deletions sources/mysql/mysqldump.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
Expand All @@ -37,7 +38,7 @@ import (
var valuesRegexp = regexp.MustCompile("\\((.*?)\\)")
var insertRegexp = regexp.MustCompile("INSERT\\sINTO\\s(.*?)\\sVALUES\\s")
var unsupportedRegexp = regexp.MustCompile("function|procedure|trigger")
var collationRegex = regexp.MustCompile(constants.DB_COLLATION_REGEX)
var dbcollationRegex = regexp.MustCompile("_[_A-Za-z0-9]+('([^']*)')")

// MysqlSpatialDataTypes is an array of all MySQL spatial data types.
var MysqlSpatialDataTypes = []string{"geometrycollection", "multipoint", "multilinestring", "multipolygon", "point", "linestring", "polygon", "geometry"}
Expand Down Expand Up @@ -254,6 +255,9 @@ func processCreateTable(conv *internal.Conv, stmt *ast.CreateTableStmt) {
var keys []schema.Key
var fkeys []schema.ForeignKey
var index []schema.Index

checkConstraints := getCheckConstraints(stmt.Constraints)

for _, element := range stmt.Cols {
_, col, constraint, err := processColumn(conv, tableName, element)
if err != nil {
Expand Down Expand Up @@ -299,7 +303,9 @@ func processCreateTable(conv *internal.Conv, stmt *ast.CreateTableStmt) {
ColDefs: colDef,
PrimaryKeys: keys,
ForeignKeys: fkeys,
Indexes: index}
Indexes: index,
CheckConstraints: checkConstraints,
}
for _, constraint := range stmt.Constraints {
processConstraint(conv, tableId, constraint, "CREATE TABLE", conv.SrcSchema[tableId].ColNameIdMap)
}
Expand Down Expand Up @@ -333,6 +339,35 @@ func processConstraint(conv *internal.Conv, tableId string, constraint *ast.Cons
conv.SrcSchema[tableId] = st
}

// method to get check constraints using tiDB parser
func getCheckConstraints(constraints []*ast.Constraint) (checkConstraints []schema.CheckConstraint) {
for _, constraint := range constraints {
if constraint.Tp == ast.ConstraintCheck {
exp := expressionToString(constraint.Expr)
exp = dbcollationRegex.ReplaceAllString(exp, "$1")
checkConstraint := schema.CheckConstraint{
Name: constraint.Name,
Expr: exp,
ExprId: internal.GenerateCheckConstrainstExprId (),
Id: internal.GenerateCheckConstrainstId(),
}
checkConstraints = append(checkConstraints, checkConstraint)
}
}
return checkConstraints
}

// converts an AST expression node to its string representation.
func expressionToString(expr ast.Node) string {
var sb strings.Builder
restoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
if err := expr.Restore(restoreCtx); err != nil {
fmt.Errorf("Error restoring expression: %v\n", err)
return ""
}
return sb.String()
}

// toSchemaKeys converts a string list of MySQL keys to schema keys.
// Note that we map all MySQL keys to ascending ordered schema keys.
// For primary keys: this is fine because MySQL primary keys are always ascending.
Expand Down

0 comments on commit 58a6db8

Please sign in to comment.