Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: remove legacy string-based differ system from parser packages (#18003)
h3n4lee2e848b8cea
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.goindex f4cac9e86b..48b67ce0b8 100644--- a/backend/plugin/parser/base/interface.go+++ b/backend/plugin/parser/base/interface.go@@ -22,7 +22,6 @@ var (queryValidators = make(map[storepb.Engine]ValidateSQLForEditorFunc)changedResourcesGetters = make(map[storepb.Engine]ExtractChangedResourcesFunc)splitters = make(map[storepb.Engine]SplitMultiSQLFunc)- schemaDiffers = make(map[storepb.Engine]SchemaDiffFunc)completers = make(map[storepb.Engine]CompletionFunc)diagnoseCollectors = make(map[storepb.Engine]DiagnoseFunc)statementRanges = make(map[storepb.Engine]StatementRangeFunc)@@ -35,7 +34,6 @@ var (type ValidateSQLForEditorFunc func(string) (bool, bool, error)type ExtractChangedResourcesFunc func(string, string, *model.DatabaseSchema, any, string) (*ChangeSummary, error)type SplitMultiSQLFunc func(string) ([]SingleSQL, error)-type SchemaDiffFunc func(ctx DiffContext, oldStmt, newStmt string) (string, error)type CompletionFunc func(ctx context.Context, cCtx CompletionContext, statement string, caretLine int, caretOffset int) ([]Candidate, error)type DiagnoseFunc func(ctx context.Context, dCtx DiagnoseContext, statement string) ([]Diagnostic, error)type StatementRangeFunc func(ctx context.Context, sCtx StatementRangeContext, statement string) ([]Range, error)@@ -112,23 +110,6 @@ func SplitMultiSQL(engine storepb.Engine, statement string) ([]SingleSQL, error)return f(statement)}-func RegisterSchemaDiffFunc(engine storepb.Engine, f SchemaDiffFunc) {- mux.Lock()- defer mux.Unlock()- if _, dup := schemaDiffers[engine]; dup {- panic(fmt.Sprintf("Register called twice %s", engine))- }- schemaDiffers[engine] = f-}--func SchemaDiff(engine storepb.Engine, ctx DiffContext, oldStmt, newStmt string) (string, error) {- f, ok := schemaDiffers[engine]- if !ok {- return "", errors.Errorf("engine %s is not supported", engine)- }- return f(ctx, oldStmt, newStmt)-}-// RegisterCompleteFunc registers the completion function for the engine.func RegisterCompleteFunc(engine storepb.Engine, f CompletionFunc) {mux.Lock()diff --git a/backend/plugin/parser/cockroachdb/differ.go b/backend/plugin/parser/cockroachdb/differ.godeleted file mode 100644index 317249e11b..0000000000--- a/backend/plugin/parser/cockroachdb/differ.go+++ /dev/null@@ -1,48 +0,0 @@-package cockroachdb--import (- "strings"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"-)--func init() {- base.RegisterSchemaDiffFunc(storepb.Engine_COCKROACHDB, SchemaDiff)-}--// SchemaDiff computes the schema differences between two SQL statements for CockroachDB.-func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {- oldStmt = sanitizeCockroachSyntax(oldStmt)- newStmt = sanitizeCockroachSyntax(newStmt)- // Reuse the PostgreSQL schema diff implementation.- return pgparser.SchemaDiff(base.DiffContext{}, oldStmt, newStmt)-}--// sanitizeCockroachSyntax sanitizes the CockroachDB-specific syntax before reusing the PostgreSQL schema diff implementation.-// CockroachDB has some syntax that is not supported by PostgreSQL, but we want to reuse the PostgreSQL schema diff.-// Currently, we handle this with simple strings.ReplaceAll calls.-func sanitizeCockroachSyntax(sql string) string {- sql = removeVisible(sql)- sql = removePrimaryKeyASC(sql)- return sql-}--// removeVisible removes "NOT VISIBLE" and "VISIBLE" clauses.-// Postgres does not support "VISIBLE" clause for certain statements.-func removeVisible(sql string) string {- sql = remove(sql, " NOT VISIBLE")- sql = remove(sql, " VISIBLE")- return sql-}--// removePrimaryKeyASC removes "ASC" from PRIMARY KEY definitions.-// PRIMARY KEY (rowid ASC) -> PRIMARY KEY (rowid).-func removePrimaryKeyASC(sql string) string {- return remove(sql, " ASC")-}--func remove(sql, keyword string) string {- return strings.ReplaceAll(sql, keyword, "")-}diff --git a/backend/plugin/parser/mysql/differ.go b/backend/plugin/parser/mysql/differ.godeleted file mode 100644index 3177a7c973..0000000000--- a/backend/plugin/parser/mysql/differ.go+++ /dev/null@@ -1,2986 +0,0 @@-package mysql--import (- "context"- "fmt"- "io"- "reflect"- "slices"- "strconv"- "strings"-- "github.com/antlr4-go/antlr/v4"- "github.com/bytebase/parser/mysql"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- "github.com/bytebase/bytebase/backend/store/model"-)--const (- maxErrorStatementSize = 1 << 8 // 256-)--func init() {- base.RegisterSchemaDiffFunc(storepb.Engine_MYSQL, SchemaDiff)- base.RegisterSchemaDiffFunc(storepb.Engine_OCEANBASE, SchemaDiff)-}--// SchemaDiffer is the parser for MySQL dialect.-type SchemaDiffer struct {-}--// SchemaDiff returns the schema diff.-// It only supports schema information from mysqldump.-func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {- // 1. Preprocessing Stage.- diff := &diffNode{- ignoreCaseSensitive: ctx.IgnoreCaseSensitive,- alterTablePartitionedByList: make(map[string]*partitionState),- }- if err := diff.diffStatement(oldStmt, newStmt); err != nil {- return "", err- }-- return diff.deparse()-}--// diffNode defines different modification types as the safe change order.-// The safe change order means we can change them with no dependency conflicts as this order.-type diffNode struct {- // Ignore the case sensitive when comparing the table and view names.- ignoreCaseSensitive bool-- dropFunctionList []*functionDef- dropProcedureList []*procedureDef- dropEventList []*eventDef- dropTriggerList []*triggerDef-- dropForeignKeyList []*foreignKeyDef- dropCheckConstraintList []*checkDef- dropPrimaryKeyList []*primaryKeyDef- dropIndexList []*indexDef- dropIndexConstraintList []*indexConstraintDef- dropViewList []*viewDef- dropTableList []*tableDef-- createTableList []*tableDef- alterTableOptionList []*tableOptionDef- addColumnList []*columnDef- modifyColumnList []*columnDef- dropColumnList []*columnDef-- createTempViewList []*viewDef- createIndexList []*indexDef- createIndexConstraintList []*indexConstraintDef- createPrimaryKeyList []*primaryKeyDef- addCheckConstraintList []*checkDef- addForeignKeyList []*foreignKeyDef- createViewList []*viewDef-- alterTablePartitionedByList map[string]*partitionState- createEventList []*eventDef- createTriggerList []*triggerDef- createFunctionList []*functionDef- createProcedureList []*procedureDef-}--func (diff *diffNode) diffStatement(oldStatement string, newStatement string) error {- oldDatabaseDef, err := diff.buildSchemaInfo(oldStatement)- if err != nil {- return errors.Wrapf(err, "failed to parse old statement %q", oldStatement)- }-- newDatabaseDef, err := diff.buildSchemaInfo(newStatement)- if err != nil {- sqlForComment, truncated := common.TruncateString(newStatement, maxErrorStatementSize)- if truncated {- sqlForComment += "..."- }- return errors.Wrapf(err, "failed to parse new statement %q", sqlForComment)- }-- diff.diffTables(oldDatabaseDef, newDatabaseDef)-- if err := diff.diffView(oldDatabaseDef, newDatabaseDef); err != nil {- return errors.Wrapf(err, "failed to diff view")- }-- diff.diffFunction(oldDatabaseDef, newDatabaseDef)-- diff.diffProcedure(oldDatabaseDef, newDatabaseDef)-- diff.diffEvent(oldDatabaseDef.schemas[""], newDatabaseDef.schemas[""])-- diff.diffTrigger(oldDatabaseDef.schemas[""], newDatabaseDef.schemas[""])-- return nil-}--func (diff *diffNode) diffTables(oldDatabase, newDatabase *databaseDef) {- for newTableName, newTable := range newDatabase.schemas[""].tables {- oldTable, exists := oldDatabase.schemas[""].tables[newTableName]- if !exists {- diff.createTableList = append(diff.createTableList, newTable)- // Create indexes.- for _, index := range newTable.indexes {- diff.createIndexList = append(diff.createIndexList, index)- }- continue- }- diff.diffTable(oldTable, newTable)- delete(oldDatabase.schemas[""].tables, newTableName)- }-- for _, oldTable := range oldDatabase.schemas[""].tables {- diff.dropTableList = append(diff.dropTableList, oldTable)- }-}--func (diff *diffNode) diffTable(oldTable, newTable *tableDef) {- diff.diffColumn(oldTable, newTable)- diff.diffIndex(oldTable, newTable)- diff.diffPrimaryKey(oldTable, newTable)- diff.diffIndexConstraint(oldTable, newTable)- diff.diffForeignKey(oldTable, newTable)- diff.diffCheckConstraint(oldTable, newTable)- diff.diffTableOptions(oldTable, newTable)- diff.diffTablePartition(oldTable, newTable)-}--func (diff *diffNode) diffCheckConstraint(oldTable, newTable *tableDef) {- for constraintName, constraint := range newTable.checks {- if oldConstraint, ok := oldTable.checks[strings.ToLower(constraintName)]; ok {- if !isCheckConstraintEqual(oldConstraint, constraint) {- diff.dropCheckConstraintList = append(diff.dropCheckConstraintList, oldConstraint)- diff.addCheckConstraintList = append(diff.addCheckConstraintList, constraint)- }- delete(oldTable.checks, strings.ToLower(constraintName))- continue- }- diff.addCheckConstraintList = append(diff.addCheckConstraintList, constraint)- }- for _, constraint := range oldTable.checks {- diff.dropCheckConstraintList = append(diff.dropCheckConstraintList, constraint)- }-}--func isCheckConstraintEqual(o, n *checkDef) bool {- if !strings.EqualFold(o.name, n.name) {- return false- }-- if o.ctx.GetText() != n.ctx.GetText() {- return false- }-- return true-}--func (diff *diffNode) diffTableOptions(oldTable, newTable *tableDef) {- for oldTp, oldOption := range oldTable.tableOptions {- newOption, ok := newTable.tableOptions[oldTp]- if !ok {- switch oldTp {- // For table engine, table charset and table collation, if oldTable has but newTable doesn't,- // we skip drop them.- case "ENGINE", "DEFAULT COLLATE", "DEFAULT CHARACTER SET":- continue- default:- // Handle other table options- }- // We should drop the table option if it doesn't exist in the new table options.- if astOption := dropTableOption(oldOption); astOption != nil {- diff.alterTableOptionList = append(diff.alterTableOptionList, astOption)- }- continue- }- if !isTableOptionEqual(oldOption, newOption) {- diff.alterTableOptionList = append(diff.alterTableOptionList, newOption)- }- }-- for newTp, newOption := range newTable.tableOptions {- if _, ok := oldTable.tableOptions[newTp]; !ok {- diff.alterTableOptionList = append(diff.alterTableOptionList, newOption)- }- }-}--func (diff *diffNode) diffTablePartition(oldTable, newTable *tableDef) {- // The situations we supported:- // 1. Turn a table partitioned.- // 2. Add a partition.- // 3. Delete a partition.- // 4. Create a new partition table.-- // Skip for some unsupported cases.- oldHasPartition := oldTable.partition != nil && oldTable.partition.info.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED- newHasPartition := newTable.partition != nil && newTable.partition.info.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED- // Turn a partitioned table un-partitioned.- if oldHasPartition && !newHasPartition {- return- }-- // TODO(zp): We use the ALTER TABLE PARTITIONED BY statement to delegate migration calculations to the MySQL server,- // avoiding the need for manual interventions like ALTER TABLE REORGANIZE PARTITION or ALTER TABLE ADD PARTITION.- // Are there any drawbacks to this approach?- oldPartitions := oldTable.partition- newPartitions := newTable.partition- equal := reflect.DeepEqual(oldPartitions, newPartitions)- if !equal {- diff.alterTablePartitionedByList[newTable.name] = newPartitions- return- }-}--func dropTableOption(oldOption *tableOptionDef) *tableOptionDef {- switch oldOption.option {- case "ENGINE":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` ENGINE = InnoDB;", oldOption.tableName)- case "SECONDARY_ENGINE_ATTRIBUTE":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` SECONDARY_ENGINE_ATTRIBUTE = InnoDB;", oldOption.tableName)- case "DEFAULT CHARACTER SET":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` CHARACTER SET = DEFAULT;", oldOption.tableName)- case "DEFAULT COLLATE":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` DEFAULT COLLATE = utf8mb4_general_ci;", oldOption.tableName)- case "AUTO_INCREMENT":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` AUTO_INCREMENT = 0;", oldOption.tableName)- case "COMMENT":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` COMMENT = '';", oldOption.tableName)- case "AVG_ROW_LENGTH":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` AVG_ROW_LENGTH = 0;", oldOption.tableName)- case "CHECKSUM":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` CHECKSUM = 0;", oldOption.tableName)- case "COMPRESSION":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` COMPRESSION = 'None';", oldOption.tableName)- case "CONNECTION":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` CONNECTION = '';", oldOption.tableName)- case "PASSWORD":- case "KEY_BLOCK_SIZE":- case "MAX_ROWS":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` MAX_ROWS = 0;", oldOption.tableName)- case "MIN_ROWS":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` MIN_ROWS = 0;", oldOption.tableName)- case "DELAY_KEY_WRITE":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` DELAY_KEY_WRITE = 0;", oldOption.tableName)- case "ROW_FORMAT":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` ROW_FORMAT = DEFAULT;", oldOption.tableName)- case "STATS_AUTO_RECALC":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` STATS_AUTO_RECALC = DEFAULT;", oldOption.tableName)- case "PACK_KEYS":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` PACK_KEYS = DEFAULT;", oldOption.tableName)- case "TABLESPACE":- case "INSERT_METHOD":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` INSERT_METHOD = NO;", oldOption.tableName)- case "TABLE_CHECKSUM":- case "UNION":- case "ENCRYPTION":- oldOption.alterOption = fmt.Sprintf("ALTER TABLE `%s` ENCRYPTION = 'N';", oldOption.tableName)- default:- // Other table options not explicitly handled- }- return oldOption-}--// isTableOptionEqual compare the two table options value, if they are equal, returns true.-// Caller need to ensure the two table options are not nil and the type is the same.-func isTableOptionEqual(oldOption, newOption *tableOptionDef) bool {- if oldOption.option == "DEFAULT CHARACTER SET" {- return oldOption.ctx.DefaultCharset().CharsetName().GetText() == newOption.ctx.DefaultCharset().CharsetName().GetText()- }- return oldOption.ctx.GetText() == newOption.ctx.GetText()-}--func (diff *diffNode) diffPrimaryKey(oldTable, newTable *tableDef) {- oldPrimaryKey := oldTable.primaryKey- newPrimaryKey := newTable.primaryKey- if oldPrimaryKey == nil && newPrimaryKey == nil {- return- } else if oldPrimaryKey != nil && newPrimaryKey == nil {- diff.dropPrimaryKeyList = append(diff.dropPrimaryKeyList, oldPrimaryKey)- return- } else if oldPrimaryKey == nil && newPrimaryKey != nil {- diff.createPrimaryKeyList = append(diff.createPrimaryKeyList, newPrimaryKey)- return- }- if !isPrimaryKeyEqual(oldPrimaryKey, newPrimaryKey) {- diff.dropPrimaryKeyList = append(diff.dropPrimaryKeyList, oldPrimaryKey)- diff.createPrimaryKeyList = append(diff.createPrimaryKeyList, newPrimaryKey)- }-}--// isPrimaryKeyEqual returns true if definitions of two priamry indexes are the same.-func isPrimaryKeyEqual(o, n *primaryKeyDef) bool {- if o.tableName != n.tableName {- return false- }- if !isKeyPartEqual(o.columns, n.columns) {- return false- }- if o.ctx.GetText() != n.ctx.GetText() {- return false- }- return true-}--func (diff *diffNode) diffForeignKey(oldTable, newTable *tableDef) {- for _, foreignKey := range newTable.foreignKeys {- if oldForeignKey, ok := oldTable.foreignKeys[strings.ToLower(foreignKey.name)]; ok {- if !isForeignKeyEqual(oldForeignKey, foreignKey) {- diff.dropForeignKeyList = append(diff.dropForeignKeyList, oldForeignKey)- diff.addForeignKeyList = append(diff.addForeignKeyList, foreignKey)- }- delete(oldTable.foreignKeys, strings.ToLower(foreignKey.name))- continue- }- diff.addForeignKeyList = append(diff.addForeignKeyList, foreignKey)- }- for _, foreignKey := range oldTable.foreignKeys {- diff.dropForeignKeyList = append(diff.dropForeignKeyList, foreignKey)- }-}--// isForeignKeyEqual returns true if two foreign keys are the same.-func isForeignKeyEqual(o, n *foreignKeyDef) bool {- if !strings.EqualFold(o.name, n.name) {- return false- }- if !isKeyPartEqual(o.columns, n.columns) {- return false- }- if !isKeyPartEqual(o.referencedColumns, n.referencedColumns) {- return false- }- if o.referencedTable != n.referencedTable {- return false- }- return true-}--func (diff *diffNode) diffView(oldDatabase, newDatabase *databaseDef) error {- var tempViewList []*viewDef- for _, view := range newDatabase.schemas[""].views {- viewName := view.name- if diff.ignoreCaseSensitive {- viewName = strings.ToLower(viewName)- }-- oldView, ok := oldDatabase.schemas[""].views[viewName]- if ok {- if !diff.isViewEqual(view, oldView) {- diff.createViewList = append(diff.createViewList, view)- }- // We should delete the view in the oldViewMap, because we will drop the all views in the oldViewMap explicitly at last.- delete(oldDatabase.schemas[""].views, viewName)- } else {- // In branch feature, frontend send DiffMetadata request without table metadata, hence there is no dependency column info.- if len(view.columns) > 0 {- // We should create the view.- // We create the temporary view first and replace it to avoid break the dependency like mysqldump does.- tempView, err := getTempView(view)- if err != nil {- return errors.Wrapf(err, "failed to get temporary view for view %s", view.name)- }- tempViewList = append(tempViewList, tempView)- }- diff.createViewList = append(diff.createViewList, view)- }- }- diff.createTempViewList = append(diff.createTempViewList, tempViewList...)- for _, view := range oldDatabase.schemas[""].views {- diff.dropViewList = append(diff.dropViewList, view)- }- return nil-}--// getTempView returns the temporary view name and the create statement.-func getTempView(view *viewDef) (*viewDef, error) {- // We create the temp view similar to what mysqldump does.- // Create a temporary view with the same name as the view. Its columns should- // have the same name in order to satisfy views that depend on this view.- // This temporary view will be removed when the actual view is created.- // The column properties are unnecessary and not preserved in this temporary view.- // because other views only need to reference the column name.- algorithm := "UNDEFINED"- if view.ctx.ViewReplaceOrAlgorithm() != nil {- if algo := view.ctx.ViewReplaceOrAlgorithm().ViewAlgorithm(); algo != nil {- algorithm = algo.GetAlgorithm().GetText()- }- }- definer := "CURRENT_USER"- if view.ctx.DefinerClause() != nil {- definer = view.ctx.DefinerClause().User().GetText()- }- sqlSecurity := "DEFINER"- if view.ctx.ViewSuid() != nil {- if view.ctx.ViewSuid().INVOKER_SYMBOL() != nil {- sqlSecurity = "INVOKER"- }- }- var selectClause strings.Builder- if _, err := selectClause.WriteString("SELECT SQL_NO_CACHE"); err != nil {- return nil, err- }- for idx, column := range view.columns {- if idx > 0 {- if _, err := selectClause.WriteString(","); err != nil {- return nil, err- }- }- if _, err := selectClause.WriteString(fmt.Sprintf(" 1 AS `%s`", column)); err != nil {- return nil, err- }- }-- cols := ""- if view.ctx.ViewTail() != nil {- if list := view.ctx.ViewTail().ColumnInternalRefList(); list != nil {- cols = list.GetParser().GetTokenStream().GetTextFromRuleContext(list.GetRuleContext())- }- }- var viewSQL string- if cols == "" {- viewSQL = fmt.Sprintf("CREATE OR REPLACE ALGORITHM=%s DEFINER=%s SQL SECURITY %s VIEW `%s` AS %s;\n\n", algorithm, definer, sqlSecurity, view.name, selectClause.String())- } else {- viewSQL = fmt.Sprintf("CREATE OR REPLACE ALGORITHM=%s DEFINER=%s SQL SECURITY %s VIEW `%s` %s AS %s;\n\n", algorithm, definer, sqlSecurity, view.name, cols, selectClause.String())- }-- newView := &viewDef{- ctx: view.ctx,- name: view.name,- dbName: view.dbName,- }- newView.tempView = viewSQL- return newView, nil-}--func (diff *diffNode) diffFunction(oldDatabase, newDatabase *databaseDef) {- for _, function := range newDatabase.schemas[""].functions {- functionName := function.name-- oldFunction, ok := oldDatabase.schemas[""].functions[functionName]- if ok {- delete(oldDatabase.schemas[""].functions, functionName)- if isFunctionEqual(oldFunction, function) {- continue- }- diff.dropFunctionList = append(diff.dropFunctionList, oldFunction)- }- diff.createFunctionList = append(diff.createFunctionList, function)- }-- for _, function := range oldDatabase.schemas[""].functions {- diff.dropFunctionList = append(diff.dropFunctionList, function)- }-}--func isFunctionEqual(o, n *functionDef) bool {- return o.ctx.GetText() == n.ctx.GetText()-}--func (diff *diffNode) diffProcedure(oldDatabase, newDatabase *databaseDef) {- for _, procedure := range newDatabase.schemas[""].procedures {- procedureName := procedure.name-- oldProcedure, ok := oldDatabase.schemas[""].procedures[procedureName]- if ok {- delete(oldDatabase.schemas[""].procedures, procedureName)- if isProcedureEqual(oldProcedure, procedure) {- continue- }- diff.dropProcedureList = append(diff.dropProcedureList, oldProcedure)- }- diff.createProcedureList = append(diff.createProcedureList, procedure)- }-- for _, procedure := range oldDatabase.schemas[""].procedures {- diff.dropProcedureList = append(diff.dropProcedureList, procedure)- }-}--func isProcedureEqual(o, n *procedureDef) bool {- return o.ctx.GetText() == n.ctx.GetText()-}--func (diff *diffNode) diffEvent(oldSchema, newSchema *schemaDef) {- for _, event := range newSchema.events {- eventName := event.name-- oldEvent, ok := oldSchema.events[eventName]- if ok {- if !isEventEqual(oldEvent, event) {- diff.dropEventList = append(diff.dropEventList, oldEvent)- }- delete(oldSchema.events, eventName)- }- diff.createEventList = append(diff.createEventList, event)- }- for _, event := range oldSchema.events {- diff.dropEventList = append(diff.dropEventList, event)- }-}--func isEventEqual(o, n *eventDef) bool {- return o.ctx.GetText() == n.ctx.GetText()-}--func (diff *diffNode) diffTrigger(oldSchema, newSchema *schemaDef) {- for _, trigger := range newSchema.triggers {- triggerName := trigger.name-- oldTrigger, ok := oldSchema.triggers[triggerName]- if ok {- if !isTriggerEqual(oldTrigger, trigger) {- diff.dropTriggerList = append(diff.dropTriggerList, oldTrigger)- diff.createTriggerList = append(diff.createTriggerList, trigger)- }- delete(oldSchema.triggers, triggerName)- } else {- diff.createTriggerList = append(diff.createTriggerList, trigger)- }- }- for _, trigger := range oldSchema.triggers {- diff.dropTriggerList = append(diff.dropTriggerList, trigger)- }-}--func isTriggerEqual(o, n *triggerDef) bool {- return o.ctx.GetText() == n.ctx.GetText()-}--func (diff *diffNode) diffColumn(oldTable, newTable *tableDef) {- // We use a single ALTER TABLE statement to add and modify columns,- // because we need to maintain a fixed order of these two operations.- // This approach ensures that we can manipulate the column position as needed.- newColumns := convertColumnMapToSortedList(newTable.columns)- oldColumns := convertColumnMapToSortedList(oldTable.columns)- for idx, newColumnDef := range newColumns {- newColumnName := newColumnDef.name- oldColumnDef, ok := oldTable.columns[newColumnName]- if !ok {- // create- columnPosition := &columnPositionDef{- tp: "FIRST",- }- if idx > 0 {- columnPosition.tp = "AFTER"- columnPosition.relativeColumn = newColumns[idx-1].name- }- newColumnDef.columnPosition = columnPosition- diff.addColumnList = append(diff.addColumnList, newColumnDef)- continue- }- // update- columnPosition := &columnPositionDef{}- if hasColumnsIntersection(oldColumns[:oldColumnDef.id], newColumns[idx+1:]) {- if idx == 0 {- columnPosition.tp = "FIRST"- } else {- columnPosition.tp = "AFTER"- columnPosition.relativeColumn = newColumns[idx-1].name- }- }- newColumnDef.columnPosition = columnPosition- if !isColumnEqual(oldColumnDef, newColumnDef) {- diff.modifyColumnList = append(diff.modifyColumnList, newColumnDef)- }-- delete(oldTable.columns, newColumnName)- }-- // drop- for _, column := range oldTable.columns {- diff.dropColumnList = append(diff.dropColumnList, column)- }-}--func convertColumnMapToSortedList(columns map[string]*columnDef) (newColumns []*columnDef) {- for _, column := range columns {- newColumns = append(newColumns, column)- }- slices.SortFunc(newColumns, func(a, b *columnDef) int {- if a.id < b.id {- return -1- }- if a.id > b.id {- return 1- }- return 0- })- return newColumns-}--// hasColumnsIntersection returns true if two column slices have column name intersection.-func hasColumnsIntersection(a, b []*columnDef) bool {- bMap := make(map[string]bool)- for _, col := range b {- // MySQL column name is case-insensitive.- bMap[col.name] = true- }- for _, col := range a {- // MySQL column name is case-insensitive.- if _, ok := bMap[col.name]; ok {- return true- }- }- return false-}--func (diff *diffNode) diffIndexConstraint(oldTable, newTable *tableDef) {- // https://stackoverflow.com/questions/887590/does-dropping-a-table-in-mysql-also-drop-the-indexes- for indexName, newIndex := range newTable.indexConstraints {- if oldIndex, ok := oldTable.indexConstraints[indexName]; ok {- if !isIndexConstraintEqual(newIndex, oldIndex) {- diff.dropIndexConstraintList = append(diff.dropIndexConstraintList, oldIndex)- diff.createIndexConstraintList = append(diff.createIndexConstraintList, newIndex)- }- delete(oldTable.indexConstraints, indexName)- continue- }- diff.createIndexConstraintList = append(diff.createIndexConstraintList, newIndex)- }-- for _, oldIndex := range oldTable.indexConstraints {- diff.dropIndexConstraintList = append(diff.dropIndexConstraintList, oldIndex)- }-}--func isIndexConstraintEqual(n, o *indexConstraintDef) bool {- if o.name != n.name {- return false- }-- if o.ctx.GetText() != n.ctx.GetText() {- return false- }- return true-}--func (diff *diffNode) diffIndex(oldTable, newTable *tableDef) {- // https://stackoverflow.com/questions/887590/does-dropping-a-table-in-mysql-also-drop-the-indexes- for indexName, newIndex := range newTable.indexes {- if oldIndex, ok := oldTable.indexes[indexName]; ok {- if !isIndexEqual(newIndex, oldIndex) {- diff.dropIndexList = append(diff.dropIndexList, oldIndex)- diff.createIndexList = append(diff.createIndexList, newIndex)- }- delete(oldTable.indexes, indexName)- continue- }- diff.createIndexList = append(diff.createIndexList, newIndex)- }-- for _, oldIndex := range oldTable.indexes {- diff.dropIndexList = append(diff.dropIndexList, oldIndex)- }-}--func (diff *diffNode) buildSchemaInfo(statement string) (*databaseDef, error) {- return diff.parseMySQLSchemaStringToSchemaDef(statement)-}--// isViewEqual checks whether two views with same name are equal.-func (*diffNode) isViewEqual(o, n *viewDef) bool {- if o.name != n.name {- return false- }-- if o.ctx.GetText() != n.ctx.GetText() {- return false- }- return true-}--// isColumnEqual returns true if definitions of two columns with the same name are the same.-func isColumnEqual(o, n *columnDef) bool {- // column name- return o.ctx.GetText() == n.ctx.GetText()-}--// isIndexEqual returns true if definitions of two indexes are the same.-func isIndexEqual(o, n *indexDef) bool {- // CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name- // [index_type]- // ON tbl_name (key_part,...)- // [index_option]- // [algorithm_option | lock_option] ...-- // MySQL index names are case insensitive.- if !strings.EqualFold(o.name, n.name) {- return false- }- if o.ctx.GetText() != n.ctx.GetText() {- return false- }-- return true-}--func isKeyPartEqual(o, n []string) bool {- if len(o) != len(n) {- return false- }-- for idx, oldKey := range o {- if oldKey != n[idx] {- return false- }- }-- return true-}--func (diff *diffNode) deparse() (string, error) {- var buf strings.Builder- if err := sortAndWriteDropFunctionList(&buf, diff.dropFunctionList); err != nil {- return "", err- }- if err := sortAndWriteDropProcedureList(&buf, diff.dropProcedureList); err != nil {- return "", err- }- if err := sortAndWriteDropEventList(&buf, diff.dropEventList); err != nil {- return "", err- }- if err := sortAndWriteDropTriggerList(&buf, diff.dropTriggerList); err != nil {- return "", err- }-- if err := sortAndWriteDropForeignKeyList(&buf, diff.dropForeignKeyList); err != nil {- return "", err- }- if err := sortAndWriteDropCheckConstraintList(&buf, diff.dropCheckConstraintList); err != nil {- return "", err- }- if err := sortAndWriteDropPrimaryIndexList(&buf, diff.dropPrimaryKeyList); err != nil {- return "", err- }- if err := sortAndWriteDropIndexList(&buf, diff.dropIndexList); err != nil {- return "", err- }- if err := sortAndWriteDropIndexConstraintList(&buf, diff.dropIndexConstraintList); err != nil {- return "", err- }- if err := sortAndWriteDropViewList(&buf, diff.dropViewList); err != nil {- return "", err- }- if err := sortAndWriteDropTableList(&buf, diff.dropTableList); err != nil {- return "", err- }- if err := sortAndWriteCreateTableList(&buf, diff.createTableList); err != nil {- return "", err- }- if err := sortAndWriteAlertTableOptionList(&buf, diff.alterTableOptionList); err != nil {- return "", err- }-- if err := sortAndWriteAddColumnList(&buf, diff.addColumnList); err != nil {- return "", err- }- if err := sortAndWriteModifyColumnList(&buf, diff.modifyColumnList); err != nil {- return "", err- }- if err := sortAndWriteDropColumnList(&buf, diff.dropColumnList); err != nil {- return "", err- }-- if err := sortAndWriteCreateTempViewList(&buf, diff.createTempViewList); err != nil {- return "", err- }-- if err := sortAndWriteCreateIndexList(&buf, diff.createIndexList); err != nil {- return "", err- }-- if err := sortAndWriteCreateIndexConstraintList(&buf, diff.createIndexConstraintList); err != nil {- return "", err- }-- if err := sortAndWriteCreatePrimaryIndexList(&buf, diff.createPrimaryKeyList); err != nil {- return "", err- }-- if err := sortAndWriteAddCheckConstraintList(&buf, diff.addCheckConstraintList); err != nil {- return "", err- }-- if err := sortAndWriteAddForeignKeyList(&buf, diff.addForeignKeyList); err != nil {- return "", err- }- if err := sortAndWriteAlterTablePartitionedByList(&buf, diff.alterTablePartitionedByList); err != nil {- return "", err- }- if err := sortAndWriteCreateViewList(&buf, diff.createViewList); err != nil {- return "", err- }-- if err := sortAndWriteCreateFunctionList(&buf, diff.createFunctionList); err != nil {- return "", err- }- if err := sortAndWriteCreateProcedureList(&buf, diff.createProcedureList); err != nil {- return "", err- }- if err := sortAndWriteCreateEventList(&buf, diff.createEventList); err != nil {- return "", err- }- if err := sortAndWriteCreateTriggerList(&buf, diff.createTriggerList); err != nil {- return "", err- }-- return buf.String(), nil-}--func sortAndWriteAlertTableOptionList(buf *strings.Builder, tableOptions []*tableOptionDef) error {- slices.SortFunc(tableOptions, func(a, b *tableOptionDef) int {- if a.tableName < b.tableName {- return -1- }- if a.tableName > b.tableName {- return 1- }- return 0- })-- for _, tableOption := range tableOptions {- if err := writeAlertTableOptionStatement(buf, tableOption); err != nil {- return err- }- }- return nil-}--func writeAlertTableOptionStatement(buf *strings.Builder, tableOption *tableOptionDef) error {- if _, err := buf.WriteString(tableOption.alterOption + "\n\n"); err != nil {- return err- }- return nil-}--func sortAndWriteDropCheckConstraintList(buf *strings.Builder, checks []*checkDef) error {- slices.SortFunc(checks, func(a, b *checkDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, check := range checks {- if err := writeDropCheckConstraintStatement(buf, check); err != nil {- return err- }- }- return nil-}--func writeDropCheckConstraintStatement(buf *strings.Builder, check *checkDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` DROP CHECK `%s`;\n\n", check.tableName, check.name); err != nil {- return err- }- return nil-}--func sortAndWriteAddCheckConstraintList(buf *strings.Builder, checks []*checkDef) error {- slices.SortFunc(checks, func(a, b *checkDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, check := range checks {- if err := writeAddCheckConstraintStatement(buf, check); err != nil {- return err- }- }- return nil-}--func writeAddCheckConstraintStatement(buf *strings.Builder, check *checkDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` ADD ", check.tableName); err != nil {- return err- }- if _, err := buf.WriteString(check.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(check.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := buf.WriteString(";\n\n"); err != nil {- return err- }- return nil-}--func sortAndWriteAddForeignKeyList(buf *strings.Builder, fks []*foreignKeyDef) error {- slices.SortFunc(fks, func(a, b *foreignKeyDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, fk := range fks {- if err := writeAddForeignKeyStatement(buf, fk); err != nil {- return err- }- }- return nil-}--func writeAddForeignKeyStatement(buf *strings.Builder, fk *foreignKeyDef) error {- if _, err := buf.WriteString("ALTER TABLE " + fk.tableName + " ADD "); err != nil {- return err- }- if _, err := buf.WriteString(fk.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(fk.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := buf.WriteString(";\n\n"); err != nil {- return err- }- return nil-}--func sortAndWriteDropForeignKeyList(buf *strings.Builder, fks []*foreignKeyDef) error {- slices.SortFunc(fks, func(a, b *foreignKeyDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, fk := range fks {- if err := writeDropForeignKeyStatement(buf, fk); err != nil {- return err- }- }- return nil-}--func writeDropForeignKeyStatement(buf *strings.Builder, fk *foreignKeyDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` DROP FOREIGN KEY `%s`;\n\n", fk.tableName, fk.name); err != nil {- return err- }- return nil-}--func sortAndWriteDropFunctionList(buf *strings.Builder, functions []*functionDef) error {- for _, function := range functions {- if err := writeDropFunctionStatement(buf, function); err != nil {- return err- }- }- return nil-}--func writeDropFunctionStatement(buf *strings.Builder, function *functionDef) error {- if _, err := fmt.Fprintf(buf, "DROP FUNCTION IF EXISTS `%s`;\n\n", function.name); err != nil {- return err- }-- return nil-}--func sortAndWriteCreateFunctionList(buf *strings.Builder, functions []*functionDef) error {- for _, function := range functions {- if err := writeCreateFunctionStatement(buf, function); err != nil {- return err- }- }- return nil-}--func writeCreateFunctionStatement(buf *strings.Builder, function *functionDef) error {- var def strings.Builder- if _, err := def.WriteString("CREATE "); err != nil {- return err- }- if _, err := def.WriteString(function.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(function.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := def.WriteString(";;"); err != nil {- return err- }-- if _, err := fmt.Fprintf(buf, "DELIMITER ;;\n%s\nDELIMITER ;\n\n", def.String()); err != nil {- return err- }- return nil-}--func sortAndWriteDropProcedureList(buf *strings.Builder, procedures []*procedureDef) error {- for _, procedure := range procedures {- if err := writeDropProcedureStatement(buf, procedure); err != nil {- return err- }- }- return nil-}--func writeDropProcedureStatement(buf *strings.Builder, procedure *procedureDef) error {- if _, err := fmt.Fprintf(buf, "DROP PROCEDURE IF EXISTS `%s`;\n\n", procedure.name); err != nil {- return err- }-- return nil-}--func sortAndWriteCreateProcedureList(buf *strings.Builder, procedures []*procedureDef) error {- for _, procedure := range procedures {- if err := writeCreateProcedureStatement(buf, procedure); err != nil {- return err- }- }- return nil-}--func writeCreateProcedureStatement(buf *strings.Builder, procedure *procedureDef) error {- var def strings.Builder- if _, err := def.WriteString("CREATE "); err != nil {- return err- }- if _, err := def.WriteString(procedure.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(procedure.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := def.WriteString(";;"); err != nil {- return err- }-- if _, err := fmt.Fprintf(buf, "DELIMITER ;;\n%s\nDELIMITER ;\n\n", def.String()); err != nil {- return err- }- return nil-}--func sortAndWriteDropEventList(buf *strings.Builder, events []*eventDef) error {- for _, event := range events {- if err := writeDropEventStatement(buf, event); err != nil {- return err- }- }- return nil-}--func writeDropEventStatement(buf *strings.Builder, event *eventDef) error {- if _, err := fmt.Fprintf(buf, "DROP EVENT IF EXISTS `%s`;\n\n", event.name); err != nil {- return err- }-- return nil-}--func sortAndWriteCreateEventList(buf *strings.Builder, events []*eventDef) error {- for _, event := range events {- if err := writeCreateEventStatement(buf, event); err != nil {- return err- }- }- return nil-}--func writeCreateEventStatement(buf *strings.Builder, event *eventDef) error {- var def strings.Builder- if _, err := def.WriteString("CREATE "); err != nil {- return err- }- if _, err := def.WriteString(event.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(event.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := def.WriteString(";;"); err != nil {- return err- }-- if _, err := fmt.Fprintf(buf, "DELIMITER ;;\n%s\nDELIMITER ;\n\n", def.String()); err != nil {- return err- }- return nil-}--func sortAndWriteDropTriggerList(buf *strings.Builder, triggers []*triggerDef) error {- for _, trigger := range triggers {- if err := writeDropTriggerStatement(buf, trigger); err != nil {- return err- }- }- return nil-}--func writeDropTriggerStatement(buf *strings.Builder, trigger *triggerDef) error {- if _, err := fmt.Fprintf(buf, "DROP TRIGGER IF EXISTS `%s`;\n\n", trigger.name); err != nil {- return err- }-- return nil-}--func sortAndWriteCreateTriggerList(buf *strings.Builder, triggers []*triggerDef) error {- for _, trigger := range triggers {- if err := writeCreateTriggerStatement(buf, trigger); err != nil {- return err- }- }- return nil-}--func writeCreateTriggerStatement(buf *strings.Builder, trigger *triggerDef) error {- var def strings.Builder- if _, err := def.WriteString("CREATE "); err != nil {- return err- }- if _, err := def.WriteString(trigger.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(trigger.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := def.WriteString(";;"); err != nil {- return err- }-- if _, err := fmt.Fprintf(buf, "DELIMITER ;;\n%s\nDELIMITER ;\n\n", def.String()); err != nil {- return err- }- return nil-}--func sortAndWriteDropIndexList(buf *strings.Builder, indexes []*indexDef) error {- slices.SortFunc(indexes, func(a, b *indexDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, index := range indexes {- if err := writeDropIndexStatement(buf, index); err != nil {- return err- }- }- return nil-}--func writeDropIndexStatement(buf *strings.Builder, index *indexDef) error {- if _, err := fmt.Fprintf(buf, "DROP INDEX `%s` ON `%s`;\n\n", index.name, index.tableName); err != nil {- return err- }- return nil-}--func sortAndWriteDropIndexConstraintList(buf *strings.Builder, indexes []*indexConstraintDef) error {- slices.SortFunc(indexes, func(a, b *indexConstraintDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, index := range indexes {- if err := writeDropIndexConstraintStatement(buf, index); err != nil {- return err- }- }- return nil-}--func writeDropIndexConstraintStatement(buf *strings.Builder, index *indexConstraintDef) error {- if _, err := fmt.Fprintf(buf, "DROP INDEX `%s` ON `%s`;\n\n", index.name, index.tableName); err != nil {- return err- }- return nil-}--func sortAndWriteDropViewList(buf *strings.Builder, views []*viewDef) error {- slices.SortFunc(views, func(a, b *viewDef) int {- if a.name < b.name {- return -1- }- if a.name > b.name {- return 1- }- return 0- })-- for _, view := range views {- if err := writeDropViewStatement(buf, view); err != nil {- return err- }- }- return nil-}--func writeDropViewStatement(buf *strings.Builder, view *viewDef) error {- if _, err := fmt.Fprintf(buf, "DROP VIEW IF EXISTS `%s`;\n\n", view.name); err != nil {- return err- }-- return nil-}--func sortAndWriteDropTableList(buf *strings.Builder, ns []*tableDef) error {- slices.SortFunc(ns, func(a, b *tableDef) int {- if a.id < b.id {- return -1- }- if a.id > b.id {- return 1- }- return 0- })-- for _, table := range ns {- if err := writeDropTableStatement(buf, table); err != nil {- return err- }- }- return nil-}--func writeDropTableStatement(buf *strings.Builder, table *tableDef) error {- if _, err := fmt.Fprintf(buf, "DROP TABLE IF EXISTS `%s`;\n\n", table.name); err != nil {- return err- }- return nil-}--func sortAndWriteCreateTableList(buf *strings.Builder, ns []*tableDef) error {- slices.SortFunc(ns, func(a, b *tableDef) int {- if a.name < b.name {- return -1- }- if a.name > b.name {- return 1- }- return 0- })-- for _, table := range ns {- if err := writeCreateTableStatement(buf, table); err != nil {- return err- }- }- return nil-}--func writeCreateTableStatement(buf *strings.Builder, table *tableDef) error {- stmt := fmt.Sprintf("CREATE %s", table.ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{- Start: table.ctx.GetStart().GetTokenIndex(),- Stop: table.ctx.GetParser().GetTokenStream().Size() - 1,- }))- if stmt[0:12] == "CREATE TABLE" {- stmt = stmt[0:12] + " IF NOT EXISTS" + stmt[12:]- }- suffix := "\n\n"- if !strings.HasSuffix(stmt, ";") {- suffix = ";" + suffix- }- if _, err := buf.WriteString(stmt); err != nil {- return err- }- if _, err := buf.WriteString(suffix); err != nil {- return err- }- return nil-}--func sortAndWriteDropColumnList(buf *strings.Builder, columns []*columnDef) error {- slices.SortFunc(columns, func(a, b *columnDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, column := range columns {- if err := writeDropColumnStatement(buf, column); err != nil {- return err- }- }- return nil-}--func writeDropColumnStatement(buf *strings.Builder, column *columnDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` DROP COLUMN `%s`;\n\n", column.tableName, column.name); err != nil {- return err- }- return nil-}--func sortAndWriteAddColumnList(buf *strings.Builder, columns []*columnDef) error {- // we do not sort the columns here for maintaining the relative position between columns;- for _, column := range columns {- if err := writeAddColumnStatement(buf, column); err != nil {- return err- }- }- return nil-}--func writeAddColumnStatement(buf *strings.Builder, column *columnDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` ADD COLUMN ", column.tableName); err != nil {- return err- }- if _, err := buf.WriteString(column.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(column.ctx.GetRuleContext())); err != nil {- return err- }- // TODO: add column position.- if column.columnPosition != nil {- pos := ""- switch column.columnPosition.tp {- case "FIRST":- pos = " FIRST"- case "AFTER":- pos = fmt.Sprintf(" AFTER `%s`", column.columnPosition.relativeColumn)- default:- // No specific position- }- if _, err := buf.WriteString(pos); err != nil {- return err- }- }- if _, err := buf.WriteString(";\n\n"); err != nil {- return err- }- return nil-}--func sortAndWriteCreateTempViewList(buf *strings.Builder, views []*viewDef) error {- slices.SortFunc(views, func(a, b *viewDef) int {- if a.name < b.name {- return -1- }- if a.name > b.name {- return 1- }- return 0- })-- for _, view := range views {- if err := writeCreateTempViewStatement(buf, view); err != nil {- return err- }- }- return nil-}--func writeCreateTempViewStatement(buf *strings.Builder, view *viewDef) error {- if _, err := buf.WriteString(view.tempView); err != nil {- return err- }-- return nil-}--func sortAndWriteAlterTablePartitionedByList(buf *strings.Builder, partitions map[string]*partitionState) error {- // Sort by table name.- tableNames := make([]string, 0, len(partitions))- for tableName := range partitions {- tableNames = append(tableNames, tableName)- }- slices.Sort(tableNames)- for _, tableName := range tableNames {- var body strings.Builder- if _, err := body.WriteString(fmt.Sprintf("ALTER TABLE `%s` PARTITION BY ", tableName)); err != nil {- return err- }- if err := partitions[tableName].toString(&body); err != nil {- return err- }- if _, err := buf.WriteString(body.String()); err != nil {- return err- }- if _, err := buf.WriteString(";\n"); err != nil {- return err- }- }-- return nil-}--// Copy from backend/plugin/schema/mysql/state.go.-func (p *partitionState) toString(buf io.StringWriter) error {- switch p.info.tp {- case storepb.TablePartitionMetadata_RANGE, storepb.TablePartitionMetadata_RANGE_COLUMNS:- if _, err := buf.WriteString("RANGE "); err != nil {- return err- }- if p.info.tp == storepb.TablePartitionMetadata_RANGE {- fields := splitPartitionExprIntoFields(p.info.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- } else {- // I think MySQL need to write "COLUMNS " instead of " COLUMNS" here...- if _, err := buf.WriteString(" COLUMNS"); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.info.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- }- case storepb.TablePartitionMetadata_LIST, storepb.TablePartitionMetadata_LIST_COLUMNS:- if _, err := buf.WriteString("LIST "); err != nil {- return err- }- if p.info.tp == storepb.TablePartitionMetadata_LIST {- fields := splitPartitionExprIntoFields(p.info.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- } else {- // I think MySQL need to write "COLUMNS " instead of " COLUMNS" here...- if _, err := buf.WriteString(" COLUMNS"); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.info.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- }- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_LINEAR_KEY:- if p.info.tp == storepb.TablePartitionMetadata_LINEAR_HASH || p.info.tp == storepb.TablePartitionMetadata_LINEAR_KEY {- if _, err := buf.WriteString("LINEAR "); err != nil {- return err- }- }- if p.info.tp == storepb.TablePartitionMetadata_KEY || p.info.tp == storepb.TablePartitionMetadata_LINEAR_KEY {- if _, err := buf.WriteString("KEY "); err != nil {- return err- }- // NOTE: MySQL supports an ALGORITHM option with [SUB]PARTITION BY [LINEAR KEY]. ALGORITHM=1 causes the server to use the same key-hashing function as MYSQL 5.1, and ALGORITHM=1 is the only possible output in- // the following code. Sadly, I do not know how to get the key_algorithm from the INFORMATION_SCHEMA, AND 5.1 IS TOO LEGACY TO SUPPORT! So use the original one.- /*- current_comment_start is given when called from SHOW CREATE TABLE,- Then only add ALGORITHM = 1, not the default 2 or non-set 0!- For .frm current_comment_start is NULL, then add ALGORITHM if != 0.- */- // if (part_info->key_algorithm ==- // enum_key_algorithm::KEY_ALGORITHM_51 || // SHOW- // (!current_comment_start && // .frm- // (part_info->key_algorithm != enum_key_algorithm::KEY_ALGORITHM_NONE))) {- // /* If we already are within a comment, end that comment first. */- // if (current_comment_start) err += add_string(fptr, "*/ ");- // err += add_string(fptr, "/*!50611 ");- // err += add_part_key_word(fptr, partition_keywords[PKW_ALGORITHM].str);- // err += add_equal(fptr);- // err += add_space(fptr);- // err += add_int(fptr, static_cast<longlong>(part_info->key_algorithm));- // err += add_space(fptr);- // err += add_string(fptr, "*/ ");- // if (current_comment_start) {- // /* Skip new line. */- // if (current_comment_start[0] == '\n') current_comment_start++;- // err += add_string(fptr, current_comment_start);- // err += add_space(fptr);- // }- // }-- // HACK(zp): Write the part field list. In the MySQL source code, it calls append_identifier(), which considers the quote character. We should figure out the logic of it later.- // Currently, I just found that if the expr contains more than one field, it would not be quoted by '`'.- // KEY and LINEAR KEY can take the field list.- // While MySQL calls append_field_list() to write the field list, it unmasks the OPTION_QUOTE_SHOW_CREATE flag,- // for us, we do the best effort to split the expr by ',' and trim the leading and trailing '`', and write it to the buffer after joining them with ','.- fields := splitPartitionExprIntoFields(p.info.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- } else {- if _, err := buf.WriteString("HASH "); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.info.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- }- default:- return errors.Errorf("unsupported partition type: %v", p.info.tp)- }-- // NOTE: MySQL writes the default partitions in the following code, which means that the server- // takes the responsibility to generate the partitions. Sadly, we cannot get whether the user- // use this or not in the metadata. So we skip it.- /*- if ((!part_info->use_default_num_partitions) &&- part_info->use_default_partitions) {- err += add_string(fptr, "\n");- err += add_string(fptr, "PARTITIONS ");- err += add_int(fptr, part_info->num_parts);- }- */- if p.info.useDefault != 0 {- if _, err := buf.WriteString(fmt.Sprintf("\nPARTITIONS %d", p.info.useDefault)); err != nil {- return err- }- }-- isSubpartitioned := p.subInfo != nil && p.subInfo.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED- if isSubpartitioned {- if _, err := buf.WriteString("\nSUBPARTITION BY "); err != nil {- return err- }- }- // Subpartition must be hash or key.- if isSubpartitioned {- switch p.subInfo.tp {- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_LINEAR_HASH:- if p.subInfo.tp == storepb.TablePartitionMetadata_LINEAR_HASH {- if _, err := buf.WriteString("LINEAR "); err != nil {- return err- }- }- if _, err := buf.WriteString("HASH "); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.subInfo.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- case storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_KEY:- if p.subInfo.tp == storepb.TablePartitionMetadata_LINEAR_KEY {- if _, err := buf.WriteString("LINEAR "); err != nil {- return err- }- }- if _, err := buf.WriteString("KEY "); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.subInfo.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- default:- return errors.Errorf("invalid subpartition type: %v", p.subInfo.tp)- }- }-- // TODO(zp): MySQL writes the default subpartitions in the following code, which means that the server- // takes the responsibility to generate the subpartitions. Sadly, we cannot get whether the user- // use this or not in the metadata. So we skip it.- /*- if ((!part_info->use_default_num_subpartitions) &&- part_info->use_default_subpartitions) {- err += add_string(fptr, "\n");- err += add_string(fptr, "SUBPARTITIONS ");- err += add_int(fptr, part_info->num_subparts);- }- */- if isSubpartitioned && p.subInfo.useDefault != 0 {- if _, err := buf.WriteString(fmt.Sprintf("\nSUBPARTITIONS %d", p.subInfo.useDefault)); err != nil {- return err- }- }-- if p.info.useDefault == 0 {- // Write the partition list.- if len(p.partitions) == 0 {- return errors.New("empty partition list")- }- sortedPartitions := make([]*partitionDefinition, 0, len(p.partitions))- for _, partition := range p.partitions {- sortedPartitions = append(sortedPartitions, partition)- }- slices.SortFunc(sortedPartitions, func(a, b *partitionDefinition) int {- if a.id < b.id {- return -1- }- if a.id > b.id {- return 1- }- return 0- })- if _, err := buf.WriteString("\n("); err != nil {- return err- }- preposition, err := getPrepositionByType(p.info.tp)- if err != nil {- return err- }- for i, partition := range sortedPartitions {- if i != 0 {- if _, err := buf.WriteString(",\n "); err != nil {- return err- }- }- if _, err := buf.WriteString(fmt.Sprintf("PARTITION %s", partition.name)); err != nil {- return err- }- if preposition != "" {- if partition.value != "MAXVALUE" {- if _, err := buf.WriteString(fmt.Sprintf(" VALUES %s (%s)", preposition, partition.value)); err != nil {- return err- }- } else {- if _, err := buf.WriteString(fmt.Sprintf(" VALUES %s %s", preposition, partition.value)); err != nil {- return err- }- }- }-- if isSubpartitioned && p.subInfo.useDefault == 0 {- if len(partition.subpartitions) == 0 {- return errors.New("empty subpartition list")- }- if _, err := buf.WriteString("\n ("); err != nil {- return err- }- sortedSubpartitions := make([]*partitionDefinition, 0, len(partition.subpartitions))- for _, subPartition := range partition.subpartitions {- sortedSubpartitions = append(sortedSubpartitions, subPartition)- }- slices.SortFunc(sortedSubpartitions, func(a, b *partitionDefinition) int {- if a.id < b.id {- return -1- }- if a.id > b.id {- return 1- }- return 0- })- for j, subPartition := range sortedSubpartitions {- if _, err := buf.WriteString(fmt.Sprintf("SUBPARTITION %s", subPartition.name)); err != nil {- return err- }- if err := writePartitionOptions(buf); err != nil {- return err- }- if j == len(sortedSubpartitions)-1 {- if _, err := buf.WriteString(")"); err != nil {- return err- }- } else {- if _, err := buf.WriteString(",\n "); err != nil {- return err- }- }- }- } else {- if err := writePartitionOptions(buf); err != nil {- return err- }- }-- if i == len(sortedPartitions)-1 {- if _, err := buf.WriteString(")"); err != nil {- return err- }- }- }- }-- return nil-}--func writePartitionOptions(buf io.StringWriter) error {- /*- int err = 0;- err += add_space(fptr);- if (p_elem->tablespace_name) {- err += add_string(fptr, "TABLESPACE = ");- err += add_ident_string(fptr, p_elem->tablespace_name);- err += add_space(fptr);- }- if (p_elem->nodegroup_id != UNDEF_NODEGROUP)- err += add_keyword_int(fptr, "NODEGROUP", (longlong)p_elem->nodegroup_id);- if (p_elem->part_max_rows)- err += add_keyword_int(fptr, "MAX_ROWS", (longlong)p_elem->part_max_rows);- if (p_elem->part_min_rows)- err += add_keyword_int(fptr, "MIN_ROWS", (longlong)p_elem->part_min_rows);- if (!(current_thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE)) {- if (p_elem->data_file_name)- err += add_keyword_path(fptr, "DATA DIRECTORY", p_elem->data_file_name);- if (p_elem->index_file_name)- err += add_keyword_path(fptr, "INDEX DIRECTORY", p_elem->index_file_name);- }- if (p_elem->part_comment)- err += add_keyword_string(fptr, "COMMENT", true, p_elem->part_comment);- return err + add_engine(fptr, p_elem->engine_type);- */- // TODO(zp): Get all the partition options from the metadata is too complex, just write ENGINE=InnoDB for now.- if _, err := buf.WriteString(" ENGINE=InnoDB"); err != nil {- return err- }-- return nil-}--func getPrepositionByType(tp storepb.TablePartitionMetadata_Type) (string, error) {- switch tp {- case storepb.TablePartitionMetadata_RANGE:- return "LESS THAN", nil- case storepb.TablePartitionMetadata_RANGE_COLUMNS:- return "LESS THAN", nil- case storepb.TablePartitionMetadata_LIST:- return "IN", nil- case storepb.TablePartitionMetadata_LIST_COLUMNS:- return "IN", nil- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_LINEAR_KEY:- return "", nil- default:- return "", errors.Errorf("unsupported partition type: %v", tp)- }-}--// splitPartitionExprIntoFields splits the partition expression by ',', and trims the leading and trailing '`' for each element.-func splitPartitionExprIntoFields(expr string) []string {- // We do not support the expression contains parentheses, so we can split the expression by ','.- ss := strings.Split(expr, ",")- for i, s := range ss {- if strings.HasPrefix(s, "`") && strings.HasSuffix(s, "`") {- ss[i] = s[1 : len(s)-1]- }- }- return ss-}--func sortAndWriteCreateViewList(buf *strings.Builder, views []*viewDef) error {- slices.SortFunc(views, func(a, b *viewDef) int {- if a.name < b.name {- return -1- }- if a.name > b.name {- return 1- }- return 0- })-- for _, view := range views {- if err := writeCreateViewStatement(buf, view); err != nil {- return err- }- }- return nil-}--func writeCreateViewStatement(buf *strings.Builder, view *viewDef) error {- algorithm := "UNDEFINED"- if view.ctx.ViewReplaceOrAlgorithm() != nil {- if algo := view.ctx.ViewReplaceOrAlgorithm().ViewAlgorithm(); algo != nil {- algorithm = algo.GetAlgorithm().GetText()- }- }-- definer := "CURRENT_USER"- if view.ctx.DefinerClause() != nil {- definer = view.ctx.DefinerClause().User().GetText()- }- sqlSecurity := "DEFINER"- if view.ctx.ViewSuid() != nil {- if view.ctx.ViewSuid().INVOKER_SYMBOL() != nil {- sqlSecurity = "INVOKER"- }- }-- cols, selectExpr := "", ""- if view.ctx.ViewTail() != nil {- if list := view.ctx.ViewTail().ColumnInternalRefList(); list != nil {- cols = list.GetParser().GetTokenStream().GetTextFromRuleContext(list.GetRuleContext())- }- if viewSelect := view.ctx.ViewTail().ViewSelect(); viewSelect != nil {- selectExpr = viewSelect.GetParser().GetTokenStream().GetTextFromRuleContext(viewSelect.GetRuleContext())- }- } else {- return errors.Errorf("view %s has no select expr", view.name)- }- var viewSQL string- if cols == "" {- viewSQL = fmt.Sprintf("CREATE OR REPLACE ALGORITHM=%s DEFINER=%s SQL SECURITY %s VIEW `%s` AS %s;\n\n", algorithm, definer, sqlSecurity, view.name, selectExpr)- } else {- viewSQL = fmt.Sprintf("CREATE OR REPLACE ALGORITHM=%s DEFINER=%s SQL SECURITY %s VIEW `%s` %s AS %s;\n\n", algorithm, definer, sqlSecurity, view.name, cols, selectExpr)- }-- if _, err := buf.WriteString(viewSQL); err != nil {- return err- }-- return nil-}--func sortAndWriteModifyColumnList(buf *strings.Builder, columns []*columnDef) error {- slices.SortFunc(columns, func(a, b *columnDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, column := range columns {- if err := writeModifyColumnStatement(buf, column); err != nil {- return err- }- }- return nil-}--func writeModifyColumnStatement(buf *strings.Builder, column *columnDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` ", column.tableName); err != nil {- return err- }-- if _, err := buf.WriteString("MODIFY COLUMN "); err != nil {- return err- }-- if _, err := buf.WriteString(column.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(column.ctx.GetRuleContext())); err != nil {- return err- }-- if _, err := buf.WriteString(";\n\n"); err != nil {- return err- }- return nil-}--func sortAndWriteCreateIndexList(buf *strings.Builder, indexes []*indexDef) error {- slices.SortFunc(indexes, func(a, b *indexDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, index := range indexes {- if err := writeCreateIndexStatement(buf, index); err != nil {- return err- }- }- return nil-}--func writeCreateIndexStatement(buf *strings.Builder, index *indexDef) error {- if _, err := buf.WriteString("CREATE "); err != nil {- return err- }- if _, err := buf.WriteString(index.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(index.ctx.GetRuleContext())); err != nil {- return err- }- if _, err := buf.WriteString(";\n\n"); err != nil {- return err- }- return nil-}--func sortAndWriteCreateIndexConstraintList(buf *strings.Builder, indexes []*indexConstraintDef) error {- slices.SortFunc(indexes, func(a, b *indexConstraintDef) int {- c1 := fmt.Sprintf("%s.%s", a.tableName, a.name)- c2 := fmt.Sprintf("%s.%s", b.tableName, b.name)- if c1 < c2 {- return -1- }- if c1 > c2 {- return 1- }- return 0- })-- for _, index := range indexes {- if err := writeCreateIndexConstraintStatement(buf, index); err != nil {- return err- }- }- return nil-}--func writeCreateIndexConstraintStatement(buf *strings.Builder, index *indexConstraintDef) error {- indexCategory := strings.ToUpper(index.ctx.GetType_().GetText())- if indexCategory == "KEY" || indexCategory == "INDEX" {- indexCategory = ""- }- indexType := ""- if nameAndType := index.ctx.IndexNameAndType(); nameAndType != nil {- indexType = nameAndType.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{- Start: nameAndType.IndexName().GetStop().GetTokenIndex() + 1,- Stop: nameAndType.GetStop().GetTokenIndex(),- })- }- keyList := index.ctx.KeyListVariants().GetParser().GetTokenStream().GetTextFromRuleContext(index.ctx.KeyListVariants().GetRuleContext())- indexOption := index.ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{- Start: index.ctx.KeyListVariants().GetStop().GetTokenIndex() + 1,- Stop: index.ctx.GetStop().GetTokenIndex(),- })- createIndexPrefix := ""- if indexCategory != "" {- createIndexPrefix = fmt.Sprintf("%s ", indexCategory)- }- if _, err := fmt.Fprintf(buf, "CREATE %sINDEX `%s` ON `%s`%s%s%s;\n\n", createIndexPrefix, index.name, index.tableName, keyList, indexType, indexOption); err != nil {- return err- }- return nil-}--func sortAndWriteCreatePrimaryIndexList(buf *strings.Builder, primaryKeys []*primaryKeyDef) error {- slices.SortFunc(primaryKeys, func(a, b *primaryKeyDef) int {- if a.tableName < b.tableName {- return -1- }- if a.tableName > b.tableName {- return 1- }- return 0- })-- for _, primaryKey := range primaryKeys {- if err := writeCreatePrimaryIndexStatement(buf, primaryKey); err != nil {- return err- }- }- return nil-}--func writeCreatePrimaryIndexStatement(buf *strings.Builder, primary *primaryKeyDef) error {- keyList := primary.ctx.KeyListVariants().GetParser().GetTokenStream().GetTextFromRuleContext(primary.ctx.KeyListVariants().GetRuleContext())- indexOption := primary.ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{- Start: primary.ctx.KeyListVariants().GetStop().GetTokenIndex() + 1,- Stop: primary.ctx.GetStop().GetTokenIndex(),- })- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` ADD PRIMARY KEY %s%s;\n\n", primary.tableName, keyList, indexOption); err != nil {- return err- }- return nil-}--func sortAndWriteDropPrimaryIndexList(buf *strings.Builder, primaryKeys []*primaryKeyDef) error {- slices.SortFunc(primaryKeys, func(a, b *primaryKeyDef) int {- if a.tableName < b.tableName {- return -1- }- if a.tableName > b.tableName {- return 1- }- return 0- })-- for _, primaryKey := range primaryKeys {- if err := writeDropPrimaryIndexStatement(buf, primaryKey); err != nil {- return err- }- }- return nil-}--func writeDropPrimaryIndexStatement(buf *strings.Builder, primary *primaryKeyDef) error {- if _, err := fmt.Fprintf(buf, "ALTER TABLE `%s` DROP PRIMARY KEY;\n\n", primary.tableName); err != nil {- return err- }- return nil-}--type databaseDef struct {- name string- schemas map[string]*schemaDef-}--func newDatabaseDef() *databaseDef {- return &databaseDef{- schemas: make(map[string]*schemaDef),- }-}--type viewDef struct {- ctx *mysql.CreateViewContext- name string- dbName string- tempView string- columns []string-}--type functionDef struct {- ctx *mysql.CreateFunctionContext- name string-}--type procedureDef struct {- ctx *mysql.CreateProcedureContext- name string-}--type schemaDef struct {- name string- // todo: check for duplicate names.- tables map[string]*tableDef- views map[string]*viewDef- events map[string]*eventDef- triggers map[string]*triggerDef- functions map[string]*functionDef- procedures map[string]*procedureDef-}--func newSchemaDef() *schemaDef {- return &schemaDef{- tables: make(map[string]*tableDef),- views: make(map[string]*viewDef),- events: make(map[string]*eventDef),- triggers: make(map[string]*triggerDef),- functions: make(map[string]*functionDef),- procedures: make(map[string]*procedureDef),- }-}--type eventDef struct {- ctx *mysql.CreateEventContext- name string-}--type triggerDef struct {- ctx *mysql.CreateTriggerContext- name string-}--type tableDef struct {- ctx *mysql.CreateTableContext- id int- name string- columns map[string]*columnDef- indexes map[string]*indexDef- indexConstraints map[string]*indexConstraintDef- foreignKeys map[string]*foreignKeyDef- checks map[string]*checkDef- tableOptions map[string]*tableOptionDef- primaryKey *primaryKeyDef- partition *partitionState-}--type partitionState struct {- info partitionInfo- subInfo *partitionInfo- partitions map[string]*partitionDefinition-}--type partitionInfo struct {- tp storepb.TablePartitionMetadata_Type- useDefault int- expr string-}--type partitionDefinition struct {- id int- name string- value string- subpartitions map[string]*partitionDefinition-}--func newTableDef(id int, name string) *tableDef {- return &tableDef{- id: id,- name: name,- columns: make(map[string]*columnDef),- indexes: make(map[string]*indexDef),- indexConstraints: make(map[string]*indexConstraintDef),- foreignKeys: make(map[string]*foreignKeyDef),- checks: make(map[string]*checkDef),- tableOptions: make(map[string]*tableOptionDef),- }-}--type primaryKeyDef struct {- ctx *mysql.TableConstraintDefContext- columns []string- tableName string-}--type indexConstraintDef struct {- ctx *mysql.TableConstraintDefContext- name string- tableName string-}--type foreignKeyDef struct {- ctx *mysql.TableConstraintDefContext- id int- name string- columns []string- referencedTable string- referencedColumns []string- tableName string-}--type checkDef struct {- ctx *mysql.TableConstraintDefContext- id int- name string- enforced bool- tableName string-}--type tableOptionDef struct {- ctx *mysql.CreateTableOptionContext- option string- alterOption string- tableName string-}--type indexDef struct {- ctx *mysql.CreateIndexContext- id int- name string- keys []string- tp string- tableName string-}--type columnPositionDef struct {- tp string- relativeColumn string-}--type columnDef struct {- ctx *mysql.ColumnDefinitionContext- id int- name string- tp string- defaultValue string- comment string- nullable bool- visible bool- tableName string- columnPosition *columnPositionDef-}--type mysqlTransformer struct {- *mysql.BaseMySQLParserListener-- db *databaseDef- currentTable string- currView string- err error- ignoreCaseSensitive bool-}--func (diff *diffNode) parseMySQLSchemaStringToSchemaDef(schema string) (*databaseDef, error) {- list, err := ParseMySQL(schema)- if err != nil {- return nil, err- }-- listener := &mysqlTransformer{- db: newDatabaseDef(),- ignoreCaseSensitive: diff.ignoreCaseSensitive,- }- listener.db.schemas[""] = newSchemaDef()- listener.db.schemas[""].name = ""-- for _, stmt := range list {- antlr.ParseTreeWalkerDefault.Walk(listener, stmt.Tree)- }-- return listener.db, nil-}--// EnterCreateTable is called when production createTable is entered.-func (t *mysqlTransformer) EnterCreateTable(ctx *mysql.CreateTableContext) {- if t.err != nil {- return- }-- databaseName, tableName := NormalizeMySQLTableName(ctx.TableName())- if t.ignoreCaseSensitive {- databaseName = strings.ToLower(databaseName)- tableName = strings.ToLower(tableName)- }- if databaseName != "" {- if t.db.name == "" {- t.db.name = databaseName- } else if t.db.name != databaseName {- t.err = errors.New("multiple database names found: " + t.db.name + ", " + databaseName)- return- }- }- schema := t.db.schemas[""]- if _, ok := schema.tables[tableName]; ok {- t.err = errors.New("multiple table names found: " + tableName)- return- }-- schema.tables[tableName] = newTableDef(len(schema.tables), tableName)- schema.tables[tableName].ctx = ctx- t.currentTable = tableName-}--// ExitCreateTable is called when production createTable is exited.-func (t *mysqlTransformer) ExitCreateTable(_ *mysql.CreateTableContext) {- t.currentTable = ""-}--// EnterCreateTableOptions is called when production createTableOptions is entered.-func (t *mysqlTransformer) EnterCreateTableOptions(ctx *mysql.CreateTableOptionsContext) {- if t.err != nil || t.currentTable == "" {- return- }-- for _, option := range ctx.AllCreateTableOption() {- if tableOption, ok := option.(*mysql.CreateTableOptionContext); ok {- newTableOption := &tableOptionDef{- ctx: tableOption,- tableName: t.currentTable,- alterOption: fmt.Sprintf("ALTER TABLE `%s` %s;", t.currentTable, tableOption.GetParser().GetTokenStream().GetTextFromRuleContext(tableOption.GetRuleContext())),- }-- switch {- case tableOption.ENGINE_SYMBOL() != nil:- newTableOption.option = "ENGINE"- case tableOption.SECONDARY_ENGINE_SYMBOL() != nil:- newTableOption.option = "SECONDARY_ENGINE"- case tableOption.DefaultCharset() != nil:- newTableOption.option = "DEFAULT CHARACTER SET"- case tableOption.DefaultCollation() != nil:- newTableOption.option = "DEFAULT COLLATE"- case tableOption.AUTO_INCREMENT_SYMBOL() != nil:- newTableOption.option = "AUTO_INCREMENT"- case tableOption.COMMENT_SYMBOL() != nil:- newTableOption.option = "COMMENT"- case tableOption.AVG_ROW_LENGTH_SYMBOL() != nil:- newTableOption.option = "AVG_ROW_LENGTH"- case tableOption.CHECKSUM_SYMBOL() != nil:- newTableOption.option = "CHECKSUM"- case tableOption.COMPRESSION_SYMBOL() != nil:- newTableOption.option = "COMPRESSION"- case tableOption.CONNECTION_SYMBOL() != nil:- newTableOption.option = "CONNECTION"- case tableOption.PASSWORD_SYMBOL() != nil:- newTableOption.option = "PASSWORD"- case tableOption.KEY_BLOCK_SIZE_SYMBOL() != nil:- newTableOption.option = "KEY_BLOCK_SIZE"- case tableOption.MAX_ROWS_SYMBOL() != nil:- newTableOption.option = "MAX_ROWS"- case tableOption.MIN_ROWS_SYMBOL() != nil:- newTableOption.option = "MIN_ROWS"- case tableOption.DELAY_KEY_WRITE_SYMBOL() != nil:- newTableOption.option = "DELAY_KEY_WRITE"- case tableOption.ROW_FORMAT_SYMBOL() != nil:- newTableOption.option = "ROW_FORMAT"- case tableOption.STATS_PERSISTENT_SYMBOL() != nil:- newTableOption.option = "STATS_PERSISTENT"- case tableOption.STATS_AUTO_RECALC_SYMBOL() != nil:- newTableOption.option = "STATS_AUTO_RECALC"- case tableOption.PACK_KEYS_SYMBOL() != nil:- newTableOption.option = "PACK_KEYS"- case tableOption.TABLESPACE_SYMBOL() != nil:- newTableOption.option = "TABLESPACE"- case tableOption.STORAGE_SYMBOL() != nil:- newTableOption.option = "STORAGE"- case tableOption.STATS_SAMPLE_PAGES_SYMBOL() != nil:- newTableOption.option = "STATS_SAMPLE_PAGES"- case tableOption.INSERT_METHOD_SYMBOL() != nil:- newTableOption.option = "INSERT_METHOD"- case tableOption.TABLE_CHECKSUM_SYMBOL() != nil:- newTableOption.option = "TABLE_CHECKSUM"- case tableOption.UNION_SYMBOL() != nil:- newTableOption.option = "UNION"- case tableOption.ENCRYPTION_SYMBOL() != nil:- newTableOption.option = "ENCRYPTION"- case tableOption.DATA_SYMBOL() != nil && tableOption.DIRECTORY_SYMBOL() != nil:- newTableOption.option = "DATA DIRECTORY"- default:- // Other table options- }- table := t.db.schemas[""].tables[t.currentTable]- table.tableOptions[newTableOption.option] = newTableOption- }- }-}--// EnterTableConstraintDef is called when production tableConstraintDef is entered.-func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDefContext) {- if t.err != nil || t.currentTable == "" {- return- }-- table := t.db.schemas[""].tables[t.currentTable]- if ctx.GetType_() != nil {- switch strings.ToUpper(ctx.GetType_().GetText()) {- case "PRIMARY":- list := extractKeyListVariants(ctx.KeyListVariants())- table.primaryKey = &primaryKeyDef{- ctx: ctx,- columns: list,- tableName: t.currentTable,- }- case "FOREIGN":- var name string- if ctx.ConstraintName() != nil && ctx.ConstraintName().Identifier() != nil {- name = strings.ToLower(NormalizeMySQLIdentifier(ctx.ConstraintName().Identifier()))- } else if ctx.IndexName() != nil {- name = strings.ToLower(NormalizeMySQLIdentifier(ctx.IndexName().Identifier()))- }- keys := extractKeyList(ctx.KeyList())- if table.foreignKeys[name] != nil {- t.err = errors.New("multiple foreign keys found: " + name)- return- }- referencedTable, referencedColumns := extractReference(ctx.References())- fk := &foreignKeyDef{- ctx: ctx,- id: len(table.foreignKeys),- name: name,- columns: keys,- referencedTable: referencedTable,- referencedColumns: referencedColumns,- tableName: t.currentTable,- }- table.foreignKeys[name] = fk- case "KEY", "INDEX", "UNIQUE":- indexName := strings.ToLower(ctx.IndexNameAndType().IndexName().GetText())- if indexName != `''` && len(indexName) > 2 {- indexName = indexName[1 : len(indexName)-1]- }- index := &indexConstraintDef{- ctx: ctx,- name: indexName,- tableName: t.currentTable,- }- table.indexConstraints[index.name] = index- case "FULLTEXT", "SPATIAL":- indexName := strings.ToLower(ctx.IndexName().GetText())- if indexName != `''` && len(indexName) > 2 {- indexName = indexName[1 : len(indexName)-1]- }- index := &indexConstraintDef{- ctx: ctx,- name: indexName,- tableName: t.currentTable,- }- table.indexConstraints[index.name] = index- default:- // Other constraint types- }- }-- if ctx.CheckConstraint() != nil {- if _, ok := ctx.CheckConstraint().(*mysql.CheckConstraintContext); ok {- var name string- if ctx.ConstraintName() != nil && ctx.ConstraintName().Identifier() != nil {- name = NormalizeMySQLIdentifier(ctx.ConstraintName().Identifier())- }-- enforced := true- if ctx.ConstraintEnforcement() != nil {- if ctx.ConstraintEnforcement().NOT_SYMBOL() != nil {- enforced = false- }- }-- table := t.db.schemas[""].tables[t.currentTable]- ck := &checkDef{- ctx: ctx,- id: len(table.checks),- name: name,- enforced: enforced,- tableName: t.currentTable,- }- table.checks[ck.name] = ck- }- }-}--// Copy from backend/plugin/schema/mysql/state.go.-func (t *mysqlTransformer) EnterPartitionClause(ctx *mysql.PartitionClauseContext) {- if t.err != nil || t.currentTable == "" {- return- }-- table := t.db.schemas[""].tables[t.currentTable]- if table == nil {- t.err = errors.New("table not found: " + t.currentTable)- return- }-- parititonInfo := partitionInfo{}-- iTypeDefCtx := ctx.PartitionTypeDef()- if iTypeDefCtx != nil {- switch typeDefCtx := iTypeDefCtx.(type) {- case *mysql.PartitionDefKeyContext:- parititonInfo.tp = storepb.TablePartitionMetadata_KEY- if typeDefCtx.LINEAR_SYMBOL() != nil {- parititonInfo.tp = storepb.TablePartitionMetadata_LINEAR_KEY- }- // TODO(zp): handle the key algorithm- if typeDefCtx.IdentifierList() != nil {- identifiers := extractIdentifierList(typeDefCtx.IdentifierList())- for i, identifier := range identifiers {- identifier := strings.TrimSpace(identifier)- if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {- identifiers[i] = fmt.Sprintf("`%s`", identifier)- }- }- parititonInfo.expr = strings.Join(identifiers, ",")- }- case *mysql.PartitionDefHashContext:- parititonInfo.tp = storepb.TablePartitionMetadata_HASH- if typeDefCtx.LINEAR_SYMBOL() != nil {- parititonInfo.tp = storepb.TablePartitionMetadata_LINEAR_HASH- }- bitExprText := typeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(typeDefCtx.BitExpr())- bitExprFields := strings.Split(bitExprText, ",")- for i, bitExprField := range bitExprFields {- bitExprField := strings.TrimSpace(bitExprField)- if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {- bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)- }- }- parititonInfo.expr = strings.Join(bitExprFields, ",")- case *mysql.PartitionDefRangeListContext:- if typeDefCtx.RANGE_SYMBOL() != nil {- parititonInfo.tp = storepb.TablePartitionMetadata_RANGE- } else {- parititonInfo.tp = storepb.TablePartitionMetadata_LIST- }- if typeDefCtx.COLUMNS_SYMBOL() != nil {- if parititonInfo.tp == storepb.TablePartitionMetadata_RANGE {- parititonInfo.tp = storepb.TablePartitionMetadata_RANGE_COLUMNS- } else {- parititonInfo.tp = storepb.TablePartitionMetadata_LIST_COLUMNS- }-- identifierList := extractIdentifierList(typeDefCtx.IdentifierList())- for i, identifier := range identifierList {- identifier := strings.TrimSpace(identifier)- if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {- identifierList[i] = fmt.Sprintf("`%s`", identifier)- }- }- parititonInfo.expr = strings.Join(identifierList, ",")- } else {- bitExprText := typeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(typeDefCtx.BitExpr())- bitExprFields := strings.Split(bitExprText, ",")- for i, bitExprField := range bitExprFields {- bitExprField := strings.TrimSpace(bitExprField)- if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {- bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)- }- }- parititonInfo.expr = strings.Join(bitExprFields, ",")- }- default:- t.err = errors.New("unknown partition type")- return- }- }-- if n := ctx.Real_ulong_number(); n != nil {- number, err := strconv.ParseInt(n.GetText(), 10, 64)- if err != nil {- t.err = errors.Wrap(err, "failed to parse partition number")- return- }- parititonInfo.useDefault = int(number)- }-- var subInfo *partitionInfo- if subPartitionCtx := ctx.SubPartitions(); subPartitionCtx != nil {- subInfo = new(partitionInfo)- if subPartitionCtx.HASH_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_HASH- if subPartitionCtx.LINEAR_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_LINEAR_HASH- }- if bitExprCtx := subPartitionCtx.BitExpr(); bitExprCtx != nil {- bitExprText := bitExprCtx.GetParser().GetTokenStream().GetTextFromRuleContext(bitExprCtx)- bitExprFields := strings.Split(bitExprText, ",")- for i, bitExprField := range bitExprFields {- bitExprField := strings.TrimSpace(bitExprField)- if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {- bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)- }- }- subInfo.expr = strings.Join(bitExprFields, ",")- }- } else if subPartitionCtx.KEY_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_KEY- if subPartitionCtx.LINEAR_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_LINEAR_KEY- }- if identifierListParensCtx := subPartitionCtx.IdentifierListWithParentheses(); identifierListParensCtx != nil {- identifiers := extractIdentifierList(identifierListParensCtx.IdentifierList())- for i, identifier := range identifiers {- identifier := strings.TrimSpace(identifier)- if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {- identifiers[i] = fmt.Sprintf("`%s`", identifier)- }- }- subInfo.expr = strings.Join(identifiers, ",")- }- }-- if n := subPartitionCtx.Real_ulong_number(); n != nil {- number, err := strconv.ParseInt(n.GetText(), 10, 64)- if err != nil {- t.err = errors.Wrap(err, "failed to parse sub partition number")- return- }- subInfo.useDefault = int(number)- }- }-- partitionDefinitions := make(map[string]*partitionDefinition)- var allPartDefs []mysql.IPartitionDefinitionContext- if v := ctx.PartitionDefinitions(); v != nil {- allPartDefs = ctx.PartitionDefinitions().AllPartitionDefinition()- }- for i, partDef := range allPartDefs {- pd := &partitionDefinition{- id: i + 1,- name: NormalizeMySQLIdentifier(partDef.Identifier()),- }- switch parititonInfo.tp {- case storepb.TablePartitionMetadata_RANGE_COLUMNS, storepb.TablePartitionMetadata_RANGE:- if partDef.LESS_SYMBOL() == nil {- t.err = errors.New("RANGE partition but no LESS THAN clause")- return- }- if partDef.PartitionValueItemListParen() != nil {- itemsText := partDef.PartitionValueItemListParen().GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- partDef.PartitionValueItemListParen().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,- partDef.PartitionValueItemListParen().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,- ),- )- itemsTextFields := strings.Split(itemsText, ",")- for i, itemsTextField := range itemsTextFields {- itemsTextField := strings.TrimSpace(itemsTextField)- if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {- itemsTextField = itemsTextField[1 : len(itemsTextField)-1]- }- itemsTextFields[i] = itemsTextField- }- pd.value = strings.Join(itemsTextFields, ",")- } else {- pd.value = "MAXVALUE"- }- case storepb.TablePartitionMetadata_LIST_COLUMNS, storepb.TablePartitionMetadata_LIST:- if partDef.PartitionValuesIn() == nil {- t.err = errors.New("COLUMNS partition but no partition value item in IN clause")- return- }- var itemsText string- if partDef.PartitionValuesIn().OPEN_PAR_SYMBOL() != nil {- itemsText = partDef.PartitionValuesIn().GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- partDef.PartitionValuesIn().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,- partDef.PartitionValuesIn().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,- ),- )- } else {- itemsText = partDef.PartitionValuesIn().GetParser().GetTokenStream().GetTextFromRuleContext(partDef.PartitionValuesIn().PartitionValueItemListParen(0))- }-- itemsTextFields := strings.Split(itemsText, ",")- for i, itemsTextField := range itemsTextFields {- itemsTextField := strings.TrimSpace(itemsTextField)- if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {- itemsTextField = itemsTextField[1 : len(itemsTextField)-1]- }- itemsTextFields[i] = itemsTextField- }- pd.value = strings.Join(itemsTextFields, ",")- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_KEY:- default:- t.err = errors.New("unknown partition type")- return- }-- if subInfo != nil {- allSubpartitions := partDef.AllSubpartitionDefinition()- if subInfo.tp == storepb.TablePartitionMetadata_TYPE_UNSPECIFIED && len(allSubpartitions) > 0 {- t.err = errors.New("specify subpartition definition but no subpartition type specified")- return- }- subPartitionDefinitions := make(map[string]*partitionDefinition)- for i, sub := range allSubpartitions {- subpd := &partitionDefinition{- id: i + 1,- name: NormalizeMySQLTextOrIdentifier(sub.TextOrIdentifier()),- }- subPartitionDefinitions[subpd.name] = subpd- }- pd.subpartitions = subPartitionDefinitions- }-- partitionDefinitions[pd.name] = pd- }-- table.partition = &partitionState{- info: parititonInfo,- subInfo: subInfo,- partitions: partitionDefinitions,- }-}--// extract table name and column names.-func extractReference(ctx mysql.IReferencesContext) (string, []string) {- _, table := NormalizeMySQLTableRef(ctx.TableRef())- if ctx.IdentifierListWithParentheses() != nil {- columns := extractIdentifierList(ctx.IdentifierListWithParentheses().IdentifierList())- return table, columns- }- return table, nil-}--// extract column names.-func extractIdentifierList(ctx mysql.IIdentifierListContext) []string {- var result []string- for _, identifier := range ctx.AllIdentifier() {- result = append(result, NormalizeMySQLIdentifier(identifier))- }- return result-}--// extract column names.-func extractKeyListVariants(ctx mysql.IKeyListVariantsContext) []string {- if ctx.KeyList() != nil {- return extractKeyList(ctx.KeyList())- }- if ctx.KeyListWithExpression() != nil {- return extractKeyListWithExpression(ctx.KeyListWithExpression())- }- return nil-}--// extract column names.-func extractKeyListWithExpression(ctx mysql.IKeyListWithExpressionContext) []string {- var result []string- for _, key := range ctx.AllKeyPartOrExpression() {- if key.KeyPart() != nil {- keyText := NormalizeMySQLIdentifier(key.KeyPart().Identifier())- result = append(result, keyText)- } else if key.ExprWithParentheses() != nil {- keyText := key.GetParser().GetTokenStream().GetTextFromRuleContext(key.ExprWithParentheses())- result = append(result, keyText)- }- }- return result-}--// extract column names.-func extractKeyList(ctx mysql.IKeyListContext) []string {- var result []string- for _, key := range ctx.AllKeyPart() {- keyText := NormalizeMySQLIdentifier(key.Identifier())- result = append(result, keyText)- }- return result-}--// EnterColumnDefinition is called when production columnDefinition is entered.-func (t *mysqlTransformer) EnterColumnDefinition(ctx *mysql.ColumnDefinitionContext) {- if t.err != nil || t.currentTable == "" {- return- }-- _, _, columnName := NormalizeMySQLColumnName(ctx.ColumnName())- dataType := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.FieldDefinition().DataType())- table := t.db.schemas[""].tables[t.currentTable]- if _, ok := table.columns[columnName]; ok {- t.err = errors.New("multiple column names found: " + columnName + " in table " + t.currentTable)- return- }- columnState := &columnDef{- ctx: ctx,- id: len(table.columns),- name: columnName,- tp: dataType,- defaultValue: "",- comment: "",- nullable: true,- visible: true,- tableName: t.currentTable,- }-- for _, attribute := range ctx.FieldDefinition().AllColumnAttribute() {- switch {- case attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil:- columnState.nullable = false- case attribute.DEFAULT_SYMBOL() != nil && attribute.SERIAL_SYMBOL() == nil:- defaultValueStart := nextDefaultChannelTokenIndex(ctx.GetParser().GetTokenStream(), attribute.DEFAULT_SYMBOL().GetSymbol().GetTokenIndex())- defaultValue := attribute.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{- Start: defaultValueStart,- Stop: attribute.GetStop().GetTokenIndex(),- })- columnState.defaultValue = defaultValue- case attribute.COMMENT_SYMBOL() != nil:- commentStart := nextDefaultChannelTokenIndex(ctx.GetParser().GetTokenStream(), attribute.COMMENT_SYMBOL().GetSymbol().GetTokenIndex())- comment := attribute.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{- Start: commentStart,- Stop: attribute.GetStop().GetTokenIndex(),- })- if comment != `''` && len(comment) > 2 {- columnState.comment = comment[1 : len(comment)-1]- }- case attribute.INVISIBLE_SYMBOL() != nil:- columnState.visible = false- default:- // Other column attributes- }- }- // todo: need handle more types.- if strings.ToLower(dataType) == "serial" {- columnState.nullable = false- }-- table.columns[columnName] = columnState-}--func nextDefaultChannelTokenIndex(tokens antlr.TokenStream, currentIndex int) int {- for i := currentIndex + 1; i < tokens.Size(); i++ {- if tokens.Get(i).GetChannel() == antlr.TokenDefaultChannel {- return i- }- }- return 0-}--// EnterCreateIndex is called when production createIndex is entered.-func (t *mysqlTransformer) EnterCreateIndex(ctx *mysql.CreateIndexContext) {- _, tableName := NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())- if t.ignoreCaseSensitive {- tableName = strings.ToLower(tableName)- }- indexName := NormalizeMySQLIdentifier(ctx.IndexName().Identifier())- table, exists := t.db.schemas[""].tables[tableName]- if !exists {- t.err = errors.Errorf("Try to create index `%s` on table `%s`, but table not found", indexName, tableName)- return- }- // Index names are always case insensitive- if _, exists := table.indexes[strings.ToLower(indexName)]; exists {- t.err = errors.Errorf("Try to create index `%s` on table `%s`, but index already exists", indexName, tableName)- return- }- if _, exists := table.indexConstraints[strings.ToLower(indexName)]; exists {- t.err = errors.Errorf("Try to create index `%s` on table `%s`, but index already exists", indexName, tableName)- return- }- cols := extractKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())- index := &indexDef{- ctx: ctx,- id: len(table.indexes),- name: indexName,- keys: cols,- tableName: tableName,- }-- table.indexes[strings.ToLower(indexName)] = index- if ctx.GetType_() != nil {- switch strings.ToUpper(ctx.GetType_().GetText()) {- case "UNIQUE":- index.tp = "UNIQUE"- case "FULLTEXT":- index.tp = "FULLTEXT"- case "SPATIAL":- index.tp = "SPATIAL"- default:- // Other index types- }- }-}--// EnterCreateView is called when production createView is entered.-func (t *mysqlTransformer) EnterCreateView(ctx *mysql.CreateViewContext) {- _, viewName := NormalizeMySQLViewName(ctx.ViewName())- t.currView = viewName- if t.ignoreCaseSensitive {- viewName = strings.ToLower(viewName)- }-- t.db.schemas[""].views[viewName] = &viewDef{- ctx: ctx,- name: viewName,- }-}--func (t *mysqlTransformer) ExitCreateView(_ *mysql.CreateViewContext) {- if t.err != nil {- return- }-- view := t.db.schemas[""].views[t.currView]-- getter, lister := t.buildDBSchemaMetadataGetterAndLister()- querySpanExtractor := newQuerySpanExtractor(t.db.name, base.GetQuerySpanContext{- GetDatabaseMetadataFunc: getter,- ListDatabaseNamesFunc: lister,- }, t.ignoreCaseSensitive)- viewDef := view.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(view.ctx.ViewTail().ViewSelect())- fields, err := querySpanExtractor.getColumnsForView(viewDef)- if err != nil {- t.err = err- return- }- if len(fields) == 0 {- t.err = errors.Errorf("no columns found for view %s, def: %s", view.name, viewDef)- return- }- var result []string- for _, field := range fields {- result = append(result, field.Name)- }-- // the column order of createView is decided by createView statement.- // we get columns here only for create temp view.- // only for test-case.- slices.Sort(result)- view.columns = result-}--func (t *mysqlTransformer) buildDBSchemaMetadataGetterAndLister() (base.GetDatabaseMetadataFunc, base.ListDatabaseNamesFunc) {- databaseSchema := &storepb.DatabaseSchemaMetadata{- Name: t.db.name,- Schemas: make([]*storepb.SchemaMetadata, 0, len(t.db.schemas)),- }-- schemaSchema := &storepb.SchemaMetadata{- Name: "",- Tables: make([]*storepb.TableMetadata, 0, len(t.db.schemas[""].tables)),- }-- for _, table := range t.db.schemas[""].tables {- var columnList []*storepb.ColumnMetadata- for _, column := range table.columns {- columnMetadata := &storepb.ColumnMetadata{- Name: column.name,- Type: column.tp,- }- columnList = append(columnList, columnMetadata)- }- tableMetadata := &storepb.TableMetadata{- Name: table.name,- Columns: columnList,- }- schemaSchema.Tables = append(schemaSchema.Tables, tableMetadata)- }-- for _, view := range t.db.schemas[""].views {- viewMetadata := &storepb.ViewMetadata{- Name: view.name,- Definition: view.ctx.GetParser().GetTokenStream().GetTextFromRuleContext(view.ctx.ViewTail().ViewSelect()),- }- schemaSchema.Views = append(schemaSchema.Views, viewMetadata)- }-- databaseSchema.Schemas = append(databaseSchema.Schemas, schemaSchema)-- return func(_ context.Context, _, databaseName string) (string, *model.DatabaseMetadata, error) {- m := make(map[string]*model.DatabaseMetadata)- m[databaseSchema.Name] = model.NewDatabaseMetadata(databaseSchema, true /* isObjectCaseSensitive */, true /* isDetailCaseSensitive */)-- if databaseMetadata, ok := m[databaseName]; ok {- return "", databaseMetadata, nil- }-- return "", nil, errors.Errorf("database %q not found", databaseName)- }, func(_ context.Context, _ string) ([]string, error) {- return []string{databaseSchema.Name}, nil- }-}--// EnterCreateEvent is called when production createEvent is entered.-func (t *mysqlTransformer) EnterCreateEvent(ctx *mysql.CreateEventContext) {- _, eventName := NormalizeMySQLEventName(ctx.EventName())- if t.ignoreCaseSensitive {- eventName = strings.ToLower(eventName)- }-- t.db.schemas[""].events[eventName] = &eventDef{- ctx: ctx,- name: eventName,- }-}--// EnterCreateTrigger is called when production createTrigger is entered.-func (t *mysqlTransformer) EnterCreateTrigger(ctx *mysql.CreateTriggerContext) {- _, triggerName := NormalizeMySQLTriggerName(ctx.TriggerName())- if t.ignoreCaseSensitive {- triggerName = strings.ToLower(triggerName)- }-- t.db.schemas[""].triggers[triggerName] = &triggerDef{- ctx: ctx,- name: triggerName,- }-}--// EnterCreateFunction is called when production createFunction is entered.-func (t *mysqlTransformer) EnterCreateFunction(ctx *mysql.CreateFunctionContext) {- _, functionName := NormalizeMySQLFunctionName(ctx.FunctionName())- if t.ignoreCaseSensitive {- functionName = strings.ToLower(functionName)- }-- t.db.schemas[""].functions[functionName] = &functionDef{- ctx: ctx,- name: functionName,- }-}--// EnterCreateProcedure is called when production createProcedure is entered.-func (t *mysqlTransformer) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {- _, procedureName := NormalizeMySQLProcedureName(ctx.ProcedureName())- if t.ignoreCaseSensitive {- procedureName = strings.ToLower(procedureName)- }-- t.db.schemas[""].procedures[procedureName] = &procedureDef{- ctx: ctx,- name: procedureName,- }-}diff --git a/backend/plugin/parser/mysql/differ_test.go b/backend/plugin/parser/mysql/differ_test.godeleted file mode 100644index 44b05c3072..0000000000--- a/backend/plugin/parser/mysql/differ_test.go+++ /dev/null@@ -1,88 +0,0 @@-package mysql--import (- "io"- "os"- "path/filepath"- "testing"-- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type DifferTestData struct {- OldSchema string `yaml:"oldSchema"`- NewSchema string `yaml:"newSchema"`- Diff string `yal:"diff"`-}--func runDifferTest(t *testing.T, file string) {- var tests []DifferTestData- filepath := filepath.Join("test-data/differ", file)- yamlFile, err := os.Open(filepath)- require.NoError(t, err)- defer yamlFile.Close()-- byteValue, err := io.ReadAll(yamlFile)- require.NoError(t, err)- err = yaml.Unmarshal(byteValue, &tests)- require.NoError(t, err)-- for i, test := range tests {- diff, err := SchemaDiff(base.DiffContext{IgnoreCaseSensitive: false, StrictMode: true}, test.OldSchema, test.NewSchema)- require.NoErrorf(t, err, "Test Cases[%02d] Failed", i+1)- require.Equalf(t, test.Diff, diff, "Test Cases[%02d] Failed", i+1)- }-}--func TestSchemaDiffTable(t *testing.T) {- testFile := "test_differ_table.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffColumn(t *testing.T) {- testFile := "test_differ_column.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffIndex(t *testing.T) {- testFile := "test_differ_index.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffView(t *testing.T) {- testFile := "test_differ_view.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffFunction(t *testing.T) {- testFile := "test_differ_function.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffProcedure(t *testing.T) {- testFile := "test_differ_procedure.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffEvent(t *testing.T) {- testFile := "test_differ_event.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffTrigger(t *testing.T) {- testFile := "test_differ_trigger.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffConstraint(t *testing.T) {- testFile := "test_differ_constraint.yaml"- runDifferTest(t, testFile)-}--func TestSchemaDiffPartition(t *testing.T) {- testFile := "test_differ_partition.yaml"- runDifferTest(t, testFile)-}diff --git a/backend/plugin/parser/pg/differ.go b/backend/plugin/parser/pg/differ.godeleted file mode 100644index 71621fc496..0000000000--- a/backend/plugin/parser/pg/differ.go+++ /dev/null@@ -1,2973 +0,0 @@-// Package pg provides the PostgreSQL differ plugin.-package pg--import (- "bytes"- "context"- "fmt"- "io"- "log/slog"- "slices"-- "github.com/pkg/errors"-- pgquery "github.com/pganalyze/pg_query_go/v6"-- "github.com/bytebase/bytebase/backend/common/log"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy"- "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy/ast"- "github.com/bytebase/bytebase/backend/store/model"-)--func init() {- base.RegisterSchemaDiffFunc(storepb.Engine_POSTGRES, SchemaDiff)-}--// diffNode defines different modification types as the safe change order.-// The safe change order means we can change them with no dependency conflicts as this order.-type diffNode struct {- oldSchemaMap schemaMap- newSchemaMap schemaMap-- // Drop nodes- dropForeignKeyList []ast.Node- dropConstraintExceptFkList []ast.Node- dropTriggerList []ast.Node- dropIndexList []ast.Node- dropDefaultList []ast.Node- dropSequenceOwnedByList []ast.Node-- dropViewList []*ast.CreateViewStmt- dropMaterializedViewList []*ast.CreateMaterializedViewStmt-- dropColumnList []ast.Node-- dropTableList []*ast.CreateTableStmt- dropFunctionList []*ast.CreateFunctionStmt-- dropSequenceList []ast.Node- dropExtensionList []ast.Node- dropTypeList []ast.Node- dropSchemaList []ast.Node-- // Create nodes- createSchemaList []ast.Node- createTypeList []ast.Node- alterTypeList []ast.Node- createExtensionList []ast.Node- createSequenceList []ast.Node- alterSequenceExceptOwnedByList []ast.Node-- createTableList []*ast.CreateTableStmt- createFunctionList []*ast.CreateFunctionStmt-- createColumnList []ast.Node- alterColumnList []ast.Node- setSequenceOwnedByList []ast.Node- setDefaultList []ast.Node- createIndexList []ast.Node- createTriggerList []ast.Node- createConstraintExceptFkList []ast.Node- createForeignKeyList []ast.Node-- createViewList []*ast.CreateViewStmt- createMaterializedViewList []*ast.CreateMaterializedViewStmt-- setCommentList []ast.Node-}--type schemaMap map[string]*schemaInfo-type tableMap map[string]*tableInfo-type viewMap map[string]*viewInfo-type materializedViewMap map[string]*materializedViewInfo-type constraintMap map[string]*constraintInfo-type indexMap map[string]*indexInfo-type sequenceMap map[string]*sequenceInfo-type extensionMap map[string]*extensionInfo-type functionMap map[string]*functionInfo-type triggerMap map[string]*triggerInfo-type typeMap map[string]*typeInfo--type schemaInfo struct {- id int- existsInNew bool- createSchema *ast.CreateSchemaStmt- tableMap tableMap- viewMap viewMap- materializedViewMap materializedViewMap- indexMap indexMap- sequenceMap sequenceMap- extensionMap extensionMap- functionMap functionMap- typeMap typeMap-}--func newSchemaInfo(id int, createSchema *ast.CreateSchemaStmt) *schemaInfo {- return &schemaInfo{- id: id,- existsInNew: false,- createSchema: createSchema,- tableMap: make(tableMap),- viewMap: make(viewMap),- materializedViewMap: make(materializedViewMap),- indexMap: make(indexMap),- sequenceMap: make(sequenceMap),- extensionMap: make(extensionMap),- functionMap: make(functionMap),- typeMap: make(typeMap),- }-}--type tableInfo struct {- id int- existsInNew bool- createTable *ast.CreateTableStmt- constraintMap constraintMap- triggerMap triggerMap- comment string- columnCommentMap map[string]string-}--func newTableInfo(id int, createTable *ast.CreateTableStmt) *tableInfo {- return &tableInfo{- id: id,- existsInNew: false,- createTable: createTable,- constraintMap: make(constraintMap),- triggerMap: make(triggerMap),- comment: "",- columnCommentMap: make(map[string]string),- }-}--type materializedViewInfo struct {- id int- existsInNew bool- createMaterializedView *ast.CreateMaterializedViewStmt- comment string- columnCommentMap map[string]string-}--func newMaterializedViewInfo(id int, createMaterializedView *ast.CreateMaterializedViewStmt) *materializedViewInfo {- return &materializedViewInfo{- id: id,- existsInNew: false,- createMaterializedView: createMaterializedView,- comment: "",- columnCommentMap: make(map[string]string),- }-}--type viewInfo struct {- id int- existsInNew bool- createView *ast.CreateViewStmt- comment string- columnCommentMap map[string]string-}--func newViewInfo(id int, createView *ast.CreateViewStmt) *viewInfo {- return &viewInfo{- id: id,- existsInNew: false,- createView: createView,- comment: "",- columnCommentMap: make(map[string]string),- }-}--type constraintInfo struct {- id int- existsInNew bool- addConstraint *ast.AddConstraintStmt-}--func newConstraintInfo(id int, addConstraint *ast.AddConstraintStmt) *constraintInfo {- return &constraintInfo{- id: id,- existsInNew: false,- addConstraint: addConstraint,- }-}--type indexInfo struct {- id int- existsInNew bool- createIndex *ast.CreateIndexStmt-}--func newIndexInfo(id int, createIndex *ast.CreateIndexStmt) *indexInfo {- return &indexInfo{- id: id,- existsInNew: false,- createIndex: createIndex,- }-}--type sequenceInfo struct {- id int- existsInNew bool- createSequence *ast.CreateSequenceStmt- ownedByInfo *sequenceOwnedByInfo-}--func newSequenceInfo(id int, createSequence *ast.CreateSequenceStmt) *sequenceInfo {- return &sequenceInfo{- id: id,- existsInNew: false,- createSequence: createSequence,- }-}--type sequenceOwnedByInfo struct {- id int- existsInNew bool- ownedBy *ast.AlterSequenceStmt-}--func newSequenceOwnedByInfo(id int, ownedBy *ast.AlterSequenceStmt) *sequenceOwnedByInfo {- return &sequenceOwnedByInfo{- id: id,- existsInNew: false,- ownedBy: ownedBy,- }-}--type extensionInfo struct {- id int- existsInNew bool- createExtension *ast.CreateExtensionStmt-}--func newExtensionInfo(id int, createExtension *ast.CreateExtensionStmt) *extensionInfo {- return &extensionInfo{- id: id,- existsInNew: false,- createExtension: createExtension,- }-}--type functionInfo struct {- id int- existsInNew bool- createFunction *ast.CreateFunctionStmt-}--func newFunctionInfo(id int, createFunction *ast.CreateFunctionStmt) *functionInfo {- return &functionInfo{- id: id,- existsInNew: false,- createFunction: createFunction,- }-}--type triggerInfo struct {- id int- existsInNew bool- createTrigger *ast.CreateTriggerStmt-}--func newTriggerInfo(id int, createTrigger *ast.CreateTriggerStmt) *triggerInfo {- return &triggerInfo{- id: id,- existsInNew: false,- createTrigger: createTrigger,- }-}--type typeInfo struct {- id int- existsInNew bool- createType *ast.CreateTypeStmt-}--func newTypeInfo(id int, createType *ast.CreateTypeStmt) *typeInfo {- return &typeInfo{- id: id,- existsInNew: false,- createType: createType,- }-}--func (m schemaMap) addMaterializedView(id int, materializedView *ast.CreateMaterializedViewStmt) error {- if IsSystemSchema(materializedView.Name.Schema) {- return nil- }- schema, exists := m[materializedView.Name.Schema]- if !exists {- return errors.Errorf("failed to add materialized view: schema %s not found", materializedView.Name.Schema)- }- schema.materializedViewMap[materializedView.Name.Name] = newMaterializedViewInfo(id, materializedView)- return nil-}--func (m schemaMap) getMaterializedView(schemaName string, materializedViewName string) *materializedViewInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.materializedViewMap[materializedViewName]-}--func (m schemaMap) addView(id int, view *ast.CreateViewStmt) error {- if IsSystemSchema(view.Name.Schema) {- return nil- }- schema, exists := m[view.Name.Schema]- if !exists {- return errors.Errorf("failed to add view: schema %s not found", view.Name.Schema)- }- schema.viewMap[view.Name.Name] = newViewInfo(id, view)- return nil-}--func (m schemaMap) getView(schemaName string, viewName string) *viewInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.viewMap[viewName]-}--func (m schemaMap) addTable(id int, table *ast.CreateTableStmt) error {- if IsSystemSchema(table.Name.Schema) {- return nil- }- schema, exists := m[table.Name.Schema]- if !exists {- return errors.Errorf("failed to add table: schema %s not found", table.Name.Schema)- }- schema.tableMap[table.Name.Name] = newTableInfo(id, table)- return nil-}--func (m schemaMap) getTable(schemaName string, tableName string) *tableInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.tableMap[tableName]-}--func (m schemaMap) addConstraint(id int, addConstraint *ast.AddConstraintStmt) error {- if IsSystemSchema(addConstraint.Table.Schema) {- return nil- }- schema, exists := m[addConstraint.Table.Schema]- if !exists {- return errors.Errorf("failed to add constraint: schema %s not found", addConstraint.Table.Schema)- }- table, exists := schema.tableMap[addConstraint.Table.Name]- if !exists {- return errors.Errorf("failed to add constraint: table %s not found", addConstraint.Table.Name)- }- constraintName := addConstraint.Constraint.Name- if constraintName == "" {- return errors.Errorf("failed to add constraint: constraint name is empty")- }- table.constraintMap[constraintName] = newConstraintInfo(id, addConstraint)- return nil-}--func (m schemaMap) getConstraint(schemaName string, tableName string, constraintName string) *constraintInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- table, exists := schema.tableMap[tableName]- if !exists {- return nil- }- return table.constraintMap[constraintName]-}--func (m schemaMap) addExtension(id int, extension *ast.CreateExtensionStmt) error {- if IsSystemSchema(extension.Schema) {- return nil- }- schema, exists := m[extension.Schema]- if !exists {- return errors.Errorf("failed to add extension: schema %s not found", extension.Schema)- }- schema.extensionMap[extension.Name] = newExtensionInfo(id, extension)- return nil-}--func (m schemaMap) getExtension(schemaName string, extensionName string) *extensionInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.extensionMap[extensionName]-}--func (m schemaMap) addFunction(id int, function *ast.CreateFunctionStmt) error {- if IsSystemSchema(function.Function.Schema) {- return nil- }- schema, exists := m[function.Function.Schema]- if !exists {- return errors.Errorf("failed to add function: schema %s not found", function.Function.Schema)- }- signature, err := functionSignature(function.Function)- if err != nil {- return err- }- schema.functionMap[signature] = newFunctionInfo(id, function)- return nil-}--func (m schemaMap) getFunction(schemaName string, signature string) *functionInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.functionMap[signature]-}--func (m schemaMap) addIndex(id int, index *ast.CreateIndexStmt) error {- if IsSystemSchema(index.Index.Table.Schema) {- return nil- }- schema, exists := m[index.Index.Table.Schema]- if !exists {- return errors.Errorf("failed to add table: schema %s not found", index.Index.Table.Schema)- }- schema.indexMap[index.Index.Name] = newIndexInfo(id, index)- return nil-}--func (m schemaMap) getIndex(schemaName string, indexName string) *indexInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.indexMap[indexName]-}--func (m schemaMap) addSequence(id int, sequence *ast.CreateSequenceStmt) error {- if IsSystemSchema(sequence.SequenceDef.SequenceName.Schema) {- return nil- }- schema, exists := m[sequence.SequenceDef.SequenceName.Schema]- if !exists {- return errors.Errorf("failed to add sequence: schema %s not found", sequence.SequenceDef.SequenceName.Schema)- }- schema.sequenceMap[sequence.SequenceDef.SequenceName.Name] = newSequenceInfo(id, sequence)- return nil-}--func (m schemaMap) getSequence(schemaName string, sequenceName string) *sequenceInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.sequenceMap[sequenceName]-}--func (m schemaMap) addTrigger(id int, trigger *ast.CreateTriggerStmt) error {- if IsSystemSchema(trigger.Trigger.Table.Schema) {- return nil- }- schema, exists := m[trigger.Trigger.Table.Schema]- if !exists {- return errors.Errorf("failed to add trigger: schema %s not found", trigger.Trigger.Table.Schema)- }- table, exists := schema.tableMap[trigger.Trigger.Table.Name]- if !exists {- return errors.Errorf("failed to add trigger: table %s no found", trigger.Trigger.Table.Name)- }- table.triggerMap[trigger.Trigger.Name] = newTriggerInfo(id, trigger)- return nil-}--func (m schemaMap) getTrigger(schemaName string, tableName string, triggerName string) *triggerInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- table, exists := schema.tableMap[tableName]- if !exists {- return nil- }- return table.triggerMap[triggerName]-}--func (m schemaMap) addType(id int, createType *ast.CreateTypeStmt) error {- if IsSystemSchema(createType.Type.TypeName().Schema) {- return nil- }- schema, exists := m[createType.Type.TypeName().Schema]- if !exists {- return errors.Errorf("failed to add type: schema %s not found", createType.Type.TypeName().Schema)- }- schema.typeMap[createType.Type.TypeName().Name] = newTypeInfo(id, createType)- return nil-}--func (m schemaMap) getType(schemaName string, typeName string) *typeInfo {- schema, exists := m[schemaName]- if !exists {- return nil- }- return schema.typeMap[typeName]-}--func (m schemaMap) addComment(comment *ast.CommentStmt) error {- switch comment.Type {- case ast.ObjectTypeTable:- tableDef, ok := comment.Object.(*ast.TableDef)- if !ok {- return errors.Errorf("failed to add comment: expect table def, but found %v", comment.Object)- }- schema, exists := m[tableDef.Schema]- if !exists {- return errors.Errorf("failed to add comment: schema %s not found", tableDef.Schema)- }- table, exists := schema.tableMap[tableDef.Name]- if !exists {- return errors.Errorf("failed to add comment: table %s not found", tableDef.Name)- }- table.comment = comment.Comment- return nil- case ast.ObjectTypeView:- tableDef, ok := comment.Object.(*ast.TableDef)- if !ok {- return errors.Errorf("failed to add comment: expect view def, but found %v", comment.Object)- }- schema, exists := m[tableDef.Schema]- if !exists {- return errors.Errorf("failed to add comment: schema %s not found", tableDef.Schema)- }- view, exists := schema.viewMap[tableDef.Name]- if !exists {- return errors.Errorf("failed to add comment: view %s not found", tableDef.Name)- }- view.comment = comment.Comment- return nil- case ast.ObjectTypeColumn:- columnNameDef, ok := comment.Object.(*ast.ColumnNameDef)- if !ok {- return errors.Errorf("failed to add comment: expect column name def, but found %v", comment.Object)- }- schema, exists := m[columnNameDef.Table.Schema]- if !exists {- slog.Debug("failed to add comment", slog.String("schema", columnNameDef.Table.Schema))- return nil- }- table, exists := schema.tableMap[columnNameDef.Table.Name]- if exists {- table.columnCommentMap[columnNameDef.ColumnName] = comment.Comment- return nil- }- view, exists := schema.viewMap[columnNameDef.Table.Name]- if exists {- view.columnCommentMap[columnNameDef.ColumnName] = comment.Comment- return nil- }- slog.Debug("failed to add comment", slog.String("table or view", columnNameDef.Table.Name))- return nil- default:- // We only support table and column comment.- return nil- }-}--func (m schemaMap) getTableComment(schemaName string, tableName string) string {- schema, exists := m[schemaName]- if !exists {- return ""- }- table, exists := schema.tableMap[tableName]- if !exists {- return ""- }- return table.comment-}--func (m schemaMap) getViewComment(schemaName string, viewName string) string {- schema, exists := m[schemaName]- if !exists {- return ""- }- view, exists := schema.viewMap[viewName]- if !exists {- return ""- }- return view.comment-}--func (m schemaMap) removeTableComment(schemaName string, tableName string) {- schema, exists := m[schemaName]- if !exists {- return- }- table, exists := schema.tableMap[tableName]- if !exists {- return- }- table.comment = ""-}--func (m schemaMap) removeViewComment(schemaName string, viewName string) {- schema, exists := m[schemaName]- if !exists {- return- }- view, exists := schema.viewMap[viewName]- if !exists {- return- }- view.comment = ""-}--func (m schemaMap) getColumnComment(schemaName string, tableName string, columnName string) string {- schema, exists := m[schemaName]- if !exists {- return ""- }- table, exists := schema.tableMap[tableName]- if exists {- columnComment, exists := table.columnCommentMap[columnName]- if exists {- return columnComment- }- }- view, exists := schema.viewMap[tableName]- if exists {- columnComment, exists := view.columnCommentMap[columnName]- if exists {- return columnComment- }- }- return ""-}--func (m schemaMap) removeColumnComment(schemaName string, tableName string, columnName string) {- schema, exists := m[schemaName]- if !exists {- return- }- table, exists := schema.tableMap[tableName]- if exists {- delete(table.columnCommentMap, columnName)- return- }- view, exists := schema.viewMap[tableName]- if exists {- delete(view.columnCommentMap, columnName)- }-}--func onlySetOwnedBy(sequence *ast.AlterSequenceStmt) bool {- return sequence.Type == nil &&- sequence.IncrementBy == nil &&- !sequence.NoMinValue &&- sequence.MinValue == nil &&- !sequence.NoMaxValue &&- sequence.MaxValue == nil &&- sequence.StartWith == nil &&- sequence.RestartWith == nil &&- sequence.Cache == nil &&- sequence.Cycle == nil &&- !sequence.OwnedByNone &&- sequence.OwnedBy != nil-}--func (m schemaMap) addSequenceOwnedBy(id int, alterStmt *ast.AlterSequenceStmt) error {- // pg_dump will separate the SET OWNED BY clause into a ALTER SEQUENCE statement.- // There would be no other ALTER SEQUENCE statements.- if !onlySetOwnedBy(alterStmt) {- return errors.Errorf("expect OwnedBy only, but found %v", alterStmt)- }-- if IsSystemSchema(alterStmt.Name.Schema) {- return nil- }- schema, exists := m[alterStmt.Name.Schema]- if !exists {- return errors.Errorf("failed to add sequence owned by: schema %s not found", alterStmt.Name.Schema)- }- sequence, exists := schema.sequenceMap[alterStmt.Name.Name]- if !exists {- return errors.Errorf("failed to add sequence owned by: sequence %s not found", alterStmt.Name.Name)- }- sequence.ownedByInfo = newSequenceOwnedByInfo(id, alterStmt)- return nil-}--func parseAndPreprocessStatement(statement string) ([]ast.Node, []string, error) {- nodeList, err := pgrawparser.Parse(pgrawparser.ParseContext{}, statement)- if err != nil {- return nil, nil, errors.Wrapf(err, "failed to parse statement %q", statement)- }- nodes, err := mergeDefaultIntoColumn(nodeList)- if err != nil {- return nil, nil, errors.Wrapf(err, "failed to merge default into column for statement %q", statement)- }-- partitions := extractPartitions(nodes)- return nodes, partitions, nil-}--func extractPartitions(nodes []ast.Node) []string {- var partitions []string- for _, node := range nodes {- switch node := node.(type) {- case *ast.AlterTableStmt:- for _, alterItem := range node.AlterItemList {- switch item := alterItem.(type) {- case *ast.AttachPartitionStmt:- if item.Partition != nil {- partitions = append(partitions, fmt.Sprintf("%s.%s", item.Partition.Schema, item.Partition.Name))- }- default:- continue- }- }- default:- continue- }- }- return partitions-}--// mergeDefaultIntoColumn merges some statements into one statement, for example, merge-// `CREATE TABLE tbl(id INT DEFAULT '3');`,-// `ALTER TABLE ONLY tbl ALTER COLUMN id nextval('tbl_id_seq'::regclass);`-// to `CREATE TABLE tbl(id INT DEFAULT nextval('tbl_id_seq'::regclass));`.-func mergeDefaultIntoColumn(nodeList []ast.Node) ([]ast.Node, error) {- var retNodes []ast.Node-- schemaTableNameToRetNodesIdx := make(map[string]int)- for _, node := range nodeList {- switch node := node.(type) {- case *ast.CreateTableStmt:- schemaTableName := node.Name.Schema + "." + node.Name.Name- retNodes = append(retNodes, node)- schemaTableNameToRetNodesIdx[schemaTableName] = len(retNodes) - 1- case *ast.AlterTableStmt:- schemaTableName := node.Table.Schema + "." + node.Table.Name- retNodesIdx, ok := schemaTableNameToRetNodesIdx[schemaTableName]- if !ok {- // For pg_dump, this will never happen.- return nil, errors.Errorf("cannot find table %s", schemaTableName)- }- for _, alterItem := range node.AlterItemList {- switch item := alterItem.(type) {- case *ast.SetDefaultStmt:- for _, columnDef := range retNodes[retNodesIdx].(*ast.CreateTableStmt).ColumnList {- if columnDef.ColumnName == item.ColumnName {- constraintDef := &ast.ConstraintDef{- Type: ast.ConstraintTypeDefault,- Expression: item.Expression,- }- // pg_dump will ensure that there is only one default constraint, in ColumnDef or AlterTableStmt.- columnDef.ConstraintList = append(columnDef.ConstraintList, constraintDef)- break- }- }- default:- retNodes = append(retNodes, node)- }- // For pg_dump, Set Default statements will be alone in one alter tabel statement.- // So here sikp append is safe.- }- default:- retNodes = append(retNodes, node)- }- }- return retNodes, nil-}--// SchemaDiff computes the schema differences between old and new schema.-func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {- oldNodes, oldPartitions, err := parseAndPreprocessStatement(oldStmt)- if err != nil {- return "", errors.Wrapf(err, "failed to parse and preprocess old statements %q", oldStmt)- }- newNodes, newPartitions, err := parseAndPreprocessStatement(newStmt)- if err != nil {- return "", errors.Wrapf(err, "failed to and preprocess new statements %q", newStmt)- }- diff := &diffNode{- oldSchemaMap: make(schemaMap),- newSchemaMap: make(schemaMap),- }- diff.oldSchemaMap["public"] = newSchemaInfo(-1, &ast.CreateSchemaStmt{Name: "public"})- diff.newSchemaMap["public"] = newSchemaInfo(-1, &ast.CreateSchemaStmt{Name: "public"})- diff.oldSchemaMap["public"].existsInNew = true- oldPartitionMap := make(map[string]bool)- for _, partition := range oldPartitions {- oldPartitionMap[partition] = true- }- for i, node := range oldNodes {- switch stmt := node.(type) {- case *ast.CreateSchemaStmt:- diff.oldSchemaMap[stmt.Name] = newSchemaInfo(i, stmt)- case *ast.CreateTableStmt:- if _, exists := oldPartitionMap[fmt.Sprintf("%s.%s", stmt.Name.Schema, stmt.Name.Name)]; exists {- // ignore partition table- continue- }- if err := diff.oldSchemaMap.addTable(i, stmt); err != nil {- return "", err- }- case *ast.CreateViewStmt:- if err := diff.oldSchemaMap.addView(i, stmt); err != nil {- return "", err- }- case *ast.CreateMaterializedViewStmt:- if err := diff.oldSchemaMap.addMaterializedView(i, stmt); err != nil {- return "", err- }- case *ast.AlterTableStmt:- // ignore partition table- if _, exists := oldPartitionMap[fmt.Sprintf("%s.%s", stmt.Table.Schema, stmt.Table.Name)]; exists {- continue- }- for _, alterItem := range stmt.AlterItemList {- switch item := alterItem.(type) {- case *ast.AddConstraintStmt:- switch item.Constraint.Type {- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion, ast.ConstraintTypeForeign:- if err := diff.oldSchemaMap.addConstraint(i, item); err != nil {- return "", err- }- default:- return "", errors.Errorf("unsupported constraint type %d", item.Constraint.Type)- }- case *ast.AttachPartitionStmt:- // ignore attach partition- continue- default:- return "", errors.Errorf("unsupported alter table item type %T", item)- }- }- case *ast.CreateIndexStmt:- // ignore partition index- if _, exists := oldPartitionMap[fmt.Sprintf("%s.%s", stmt.Index.Table.Schema, stmt.Index.Table.Name)]; exists {- continue- }- if err := diff.oldSchemaMap.addIndex(i, stmt); err != nil {- return "", err- }- case *ast.CreateSequenceStmt:- if err := diff.oldSchemaMap.addSequence(i, stmt); err != nil {- return "", err- }- case *ast.AlterSequenceStmt:- // pg_dump will separate the SET OWNED BY clause into a ALTER SEQUENCE statement.- // There would be no other ALTER SEQUENCE statements.- if err := diff.oldSchemaMap.addSequenceOwnedBy(i, stmt); err != nil {- return "", err- }- case *ast.CreateExtensionStmt:- if err := diff.oldSchemaMap.addExtension(i, stmt); err != nil {- return "", err- }- case *ast.CreateFunctionStmt:- if err := diff.oldSchemaMap.addFunction(i, stmt); err != nil {- return "", err- }- case *ast.CreateTriggerStmt:- if err := diff.oldSchemaMap.addTrigger(i, stmt); err != nil {- return "", err- }- case *ast.CreateTypeStmt:- if err := diff.oldSchemaMap.addType(i, stmt); err != nil {- return "", err- }- case *ast.CommentStmt:- if err := diff.oldSchemaMap.addComment(stmt); err != nil {- return "", err- }- // TODO(rebelice): add default back here- }- }-- newPartitionMap := make(map[string]bool)- for _, partition := range newPartitions {- newPartitionMap[partition] = true- }- for i, node := range newNodes {- switch stmt := node.(type) {- case *ast.CreateTableStmt:- if err := diff.newSchemaMap.addTable(i, stmt); err != nil {- return "", errors.Wrapf(err, "failed to add table %s", stmt.Name.Name)- }- // ignore partition table- if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Name.Schema, stmt.Name.Name)]; exists {- continue- }- if IsSystemSchema(stmt.Name.Schema) {- continue- }- oldTable := diff.oldSchemaMap.getTable(stmt.Name.Schema, stmt.Name.Name)- // Add the new table.- if oldTable == nil {- diff.createTableList = append(diff.createTableList, stmt)- continue- }- oldTable.existsInNew = true- // Modify the table.- if err := diff.modifyTable(oldTable, stmt); err != nil {- return "", err- }- case *ast.CreateSchemaStmt:- diff.newSchemaMap[stmt.Name] = newSchemaInfo(i, stmt)- if IsSystemSchema(stmt.Name) {- continue- }- schema, hasSchema := diff.oldSchemaMap[stmt.Name]- if !hasSchema {- diff.createSchemaList = append(diff.createSchemaList, stmt)- continue- }- schema.existsInNew = true- case *ast.CreateViewStmt:- if err := diff.newSchemaMap.addView(i, stmt); err != nil {- return "", errors.Wrapf(err, "failed to add view %s", stmt.Name.Name)- }- if IsSystemSchema(stmt.Name.Schema) {- continue- }- oldView := diff.oldSchemaMap.getView(stmt.Name.Schema, stmt.Name.Name)- // Add the new view.- if oldView == nil {- diff.createViewList = append(diff.createViewList, stmt)- continue- }- oldView.existsInNew = true- if err := diff.ModifyView(oldView, i, stmt); err != nil {- return "", err- }- case *ast.CreateMaterializedViewStmt:- if err := diff.newSchemaMap.addMaterializedView(i, stmt); err != nil {- return "", errors.Wrapf(err, "failed to add materialized view %s", stmt.Name.Name)- }- if IsSystemSchema(stmt.Name.Schema) {- continue- }- oldMaterializedView := diff.oldSchemaMap.getMaterializedView(stmt.Name.Schema, stmt.Name.Name)- // Add the new materialized view.- if oldMaterializedView == nil {- diff.createMaterializedViewList = append(diff.createMaterializedViewList, stmt)- continue- }- oldMaterializedView.existsInNew = true- if err := diff.ModifyMaterializedView(oldMaterializedView, i, stmt); err != nil {- return "", err- }- case *ast.AlterTableStmt:- // ignore partition table- if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Table.Schema, stmt.Table.Name)]; exists {- continue- }- if IsSystemSchema(stmt.Table.Schema) {- continue- }- for _, alterItem := range stmt.AlterItemList {- switch item := alterItem.(type) {- case *ast.AddConstraintStmt:- switch item.Constraint.Type {- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion, ast.ConstraintTypeForeign:- oldConstraint := diff.oldSchemaMap.getConstraint(item.Table.Schema, item.Table.Name, item.Constraint.Name)- if oldConstraint == nil {- diff.appendAddConstraint(stmt.Table, []*ast.ConstraintDef{item.Constraint})- continue- }-- oldConstraint.existsInNew = true- nameEqual := oldConstraint.addConstraint.Table.Name == item.Table.Name- equal, err := isEqualConstraint(oldConstraint.addConstraint.Constraint, item.Constraint)- if err != nil {- return "", err- }- if !nameEqual || !equal {- diff.appendDropConstraint(stmt.Table, []*ast.ConstraintDef{oldConstraint.addConstraint.Constraint})- diff.appendAddConstraint(stmt.Table, []*ast.ConstraintDef{item.Constraint})- continue- }- default:- return "", errors.Errorf("unsupported constraint type %d", item.Constraint.Type)- }- case *ast.AttachPartitionStmt:- // ignore attach partition- continue- default:- return "", errors.Errorf("unsupported alter table item type %T", item)- }- }- case *ast.CreateIndexStmt:- // ignore partition index- if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Index.Table.Schema, stmt.Index.Table.Name)]; exists {- continue- }- if IsSystemSchema(stmt.Index.Table.Schema) {- continue- }- oldIndex := diff.oldSchemaMap.getIndex(stmt.Index.Table.Schema, stmt.Index.Name)- // Add the new index.- if oldIndex == nil {- diff.createIndexList = append(diff.createIndexList, stmt)- continue- }- oldIndex.existsInNew = true- // Modify the index.- diff.modifyIndex(oldIndex.createIndex, stmt)- case *ast.CreateSequenceStmt:- if IsSystemSchema(stmt.SequenceDef.SequenceName.Schema) {- continue- }- oldSequence := diff.oldSchemaMap.getSequence(stmt.SequenceDef.SequenceName.Schema, stmt.SequenceDef.SequenceName.Name)- // Add the new sequence.- if oldSequence == nil {- diff.createSequenceList = append(diff.createSequenceList, stmt)- continue- }- oldSequence.existsInNew = true- // Modify the sequence.- diff.modifySequenceExceptOwnedBy(oldSequence.createSequence, stmt)- case *ast.AlterSequenceStmt:- if !onlySetOwnedBy(stmt) {- return "", errors.Errorf("expect OwnedBy only, but found %v", stmt)- }- if IsSystemSchema(stmt.Name.Schema) {- continue- }- oldSequence := diff.oldSchemaMap.getSequence(stmt.Name.Schema, stmt.Name.Name)- // Add the new sequence owned by.- if oldSequence == nil || oldSequence.ownedByInfo == nil {- diff.setSequenceOwnedByList = append(diff.setSequenceOwnedByList, stmt)- continue- }- oldSequence.ownedByInfo.existsInNew = true- diff.modifySequenceOwnedBy(oldSequence.ownedByInfo.ownedBy, stmt)- case *ast.CreateExtensionStmt:- if IsSystemSchema(stmt.Schema) {- continue- }- oldExtension := diff.oldSchemaMap.getExtension(stmt.Schema, stmt.Name)- // Add the extension.- if oldExtension == nil {- diff.createExtensionList = append(diff.createExtensionList, stmt)- continue- }- oldExtension.existsInNew = true- // Modify the extension.- diff.modifyExtension(oldExtension.createExtension, stmt)- case *ast.CreateFunctionStmt:- if IsSystemSchema(stmt.Function.Schema) {- continue- }- signature, err := functionSignature(stmt.Function)- if err != nil {- return "", err- }- oldFunction := diff.oldSchemaMap.getFunction(stmt.Function.Schema, signature)- // Add the function.- if oldFunction == nil {- diff.createFunctionList = append(diff.createFunctionList, stmt)- continue- }- oldFunction.existsInNew = true- // Modify the function.- diff.modifyFunction(oldFunction.createFunction, stmt)- case *ast.CreateTriggerStmt:- if IsSystemSchema(stmt.Trigger.Table.Schema) {- continue- }- oldTrigger := diff.oldSchemaMap.getTrigger(stmt.Trigger.Table.Schema, stmt.Trigger.Table.Name, stmt.Trigger.Name)- // Add the trigger.- if oldTrigger == nil {- diff.createTriggerList = append(diff.createTriggerList, stmt)- continue- }- oldTrigger.existsInNew = true- // Modify the trigger.- diff.modifyTrigger(oldTrigger.createTrigger, stmt)- case *ast.CreateTypeStmt:- if IsSystemSchema(stmt.Type.TypeName().Schema) {- continue- }- oldType := diff.oldSchemaMap.getType(stmt.Type.TypeName().Schema, stmt.Type.TypeName().Name)- // Add the type.- if oldType == nil {- diff.createTypeList = append(diff.createTypeList, stmt)- continue- }- oldType.existsInNew = true- // Modify the type.- if err := diff.modifyType(oldType.createType, stmt); err != nil {- return "", err- }- case *ast.CommentStmt:- switch stmt.Type {- case ast.ObjectTypeTable:- tableDef, ok := stmt.Object.(*ast.TableDef)- if !ok {- return "", errors.Errorf("failed to add comment: expect table def, but found %v", stmt.Object)- }- oldComment := diff.oldSchemaMap.getTableComment(tableDef.Schema, tableDef.Name)- // Set the table comment.- if oldComment == "" || oldComment != stmt.Comment {- diff.setCommentList = append(diff.setCommentList, stmt)- }- diff.oldSchemaMap.removeTableComment(tableDef.Schema, tableDef.Name)- case ast.ObjectTypeView:- viewDef, ok := stmt.Object.(*ast.TableDef)- if !ok {- return "", errors.Errorf("failed to add comment: expect view def, but found %v", stmt.Object)- }- oldComment := diff.oldSchemaMap.getViewComment(viewDef.Schema, viewDef.Name)- if oldComment == "" || oldComment != stmt.Comment {- diff.setCommentList = append(diff.setCommentList, stmt)- }- diff.oldSchemaMap.removeViewComment(viewDef.Schema, viewDef.Name)- case ast.ObjectTypeColumn:- columnNameDef, ok := stmt.Object.(*ast.ColumnNameDef)- if !ok {- return "", errors.Errorf("failed to add comment: expect column name def, but found %v", stmt.Object)- }- oldComment := diff.oldSchemaMap.getColumnComment(columnNameDef.Table.Schema, columnNameDef.Table.Name, columnNameDef.ColumnName)- // Set the column comment.- if oldComment == "" || oldComment != stmt.Comment {- diff.setCommentList = append(diff.setCommentList, stmt)- }- diff.oldSchemaMap.removeColumnComment(columnNameDef.Table.Schema, columnNameDef.Table.Name, columnNameDef.ColumnName)- default:- // We only support table and column comment.- }- }- }-- // Drop remaining old objects.- diff.dropObject()-- return diff.deparse()-}--func (diff *diffNode) appendDropConstraint(table *ast.TableDef, constraintList []*ast.ConstraintDef) {- dropConstraintExceptFkStmt := &ast.AlterTableStmt{Table: table}- dropForeignStmt := &ast.AlterTableStmt{Table: table}-- for _, constraint := range constraintList {- if constraint == nil {- continue- }-- dropStmt := &ast.DropConstraintStmt{- Table: table,- ConstraintName: constraint.Name,- }-- if constraint.Type == ast.ConstraintTypeForeign {- dropForeignStmt.AlterItemList = append(dropForeignStmt.AlterItemList, dropStmt)- } else {- dropConstraintExceptFkStmt.AlterItemList = append(dropConstraintExceptFkStmt.AlterItemList, dropStmt)- }- }-- if len(dropConstraintExceptFkStmt.AlterItemList) > 0 {- diff.dropForeignKeyList = append(diff.dropForeignKeyList, dropConstraintExceptFkStmt)- }- if len(dropForeignStmt.AlterItemList) > 0 {- diff.dropConstraintExceptFkList = append(diff.dropConstraintExceptFkList, dropForeignStmt)- }-}--func (diff *diffNode) appendAddConstraint(table *ast.TableDef, constraintList []*ast.ConstraintDef) {- addConstraintExceptFkStmt := &ast.AlterTableStmt{Table: table}- addForeignStmt := &ast.AlterTableStmt{Table: table}-- for _, constraint := range constraintList {- if constraint == nil {- continue- }-- addStmt := &ast.AddConstraintStmt{- Table: table,- Constraint: constraint,- }-- if constraint.Type == ast.ConstraintTypeForeign {- addForeignStmt.AlterItemList = append(addForeignStmt.AlterItemList, addStmt)- } else {- addConstraintExceptFkStmt.AlterItemList = append(addConstraintExceptFkStmt.AlterItemList, addStmt)- }- }-- if len(addConstraintExceptFkStmt.AlterItemList) > 0 {- diff.createConstraintExceptFkList = append(diff.createConstraintExceptFkList, addConstraintExceptFkStmt)- }- if len(addForeignStmt.AlterItemList) > 0 {- diff.createForeignKeyList = append(diff.createForeignKeyList, addForeignStmt)- }-}--func (diff *diffNode) dropObject() {- // Drop the remaining old schema.- if dropSchemaStmt := dropSchema(diff.oldSchemaMap); dropSchemaStmt != nil {- diff.dropSchemaList = append(diff.dropSchemaList, dropSchemaStmt)- }-- // Drop the remaining old table.- if dropTableStmt := dropTable(diff.oldSchemaMap); dropTableStmt != nil {- diff.dropTableList = append(diff.dropTableList, dropTableStmt...)- }-- // Drop the remaining old view.- if dropViewStmt := dropView(diff.oldSchemaMap); dropViewStmt != nil {- diff.dropViewList = append(diff.dropViewList, dropViewStmt...)- }-- // Drop the remaining old materialized view.- if dropMaterializedViewStmt := dropMaterializedView(diff.oldSchemaMap); dropMaterializedViewStmt != nil {- diff.dropMaterializedViewList = append(diff.dropMaterializedViewList, dropMaterializedViewStmt...)- }-- // Drop the remaining old constraints.- diff.dropConstraint(diff.oldSchemaMap)-- // Drop the remaining old index.- if dropIndexStmt := dropIndex(diff.oldSchemaMap); dropIndexStmt != nil {- diff.dropIndexList = append(diff.dropIndexList, dropIndexStmt)- }-- // Drop the remaining old sequence owned by.- diff.dropSequenceOwnedBy(diff.oldSchemaMap)-- // Drop the remaining old sequence.- if dropSequenceStmt := dropSequence(diff.oldSchemaMap); dropSequenceStmt != nil {- diff.dropSequenceList = append(diff.dropSequenceList, dropSequenceStmt)- }-- // Drop the remaining old extension.- if dropExtensionStmt := dropExtension(diff.oldSchemaMap); dropExtensionStmt != nil {- diff.dropExtensionList = append(diff.dropExtensionList, dropExtensionStmt)- }-- // Drop the remaining old function.- if dropFunctionStmt := dropFunction(diff.oldSchemaMap); dropFunctionStmt != nil {- diff.dropFunctionList = append(diff.dropFunctionList, dropFunctionStmt...)- }-- // Drop the remaining old trigger.- diff.dropTriggerStmt(diff.oldSchemaMap)-- // Drop the remaining old type.- diff.dropTypeStmt(diff.oldSchemaMap)-- // Drop the remaining old comment.- diff.dropComment(diff.oldSchemaMap)-}--func (diff *diffNode) modifyTableByColumn(oldTableInfo *tableInfo, newTable *ast.CreateTableStmt) error {- oldTable := oldTableInfo.createTable- tableName := oldTable.Name- addColumn := &ast.AlterTableStmt{- Table: tableName,- }- dropColumn := &ast.AlterTableStmt{- Table: tableName,- }-- oldColumnMap := make(map[string]*ast.ColumnDef)- for _, column := range oldTable.ColumnList {- oldColumnMap[column.ColumnName] = column- }-- for _, newColumn := range newTable.ColumnList {- oldColumn, exists := oldColumnMap[newColumn.ColumnName]- // Add the new column.- if !exists {- addColumn.AlterItemList = append(addColumn.AlterItemList, &ast.AddColumnListStmt{- Table: tableName,- ColumnList: []*ast.ColumnDef{newColumn},- })- continue- }- // Modify the column.- if err := diff.modifyColumn(tableName, oldColumn, newColumn); err != nil {- return err- }- delete(oldColumnMap, oldColumn.ColumnName)- }-- for _, oldColumn := range oldTable.ColumnList {- if _, exists := oldColumnMap[oldColumn.ColumnName]; exists {- // No need to drop comment for the column, because we will drop the column.- delete(oldTableInfo.columnCommentMap, oldColumn.ColumnName)- dropColumn.AlterItemList = append(dropColumn.AlterItemList, &ast.DropColumnStmt{- Table: tableName,- ColumnName: oldColumn.ColumnName,- })- }- }-- if len(addColumn.AlterItemList) > 0 {- diff.createColumnList = append(diff.createColumnList, addColumn)- }- if len(dropColumn.AlterItemList) > 0 {- diff.dropColumnList = append(diff.dropColumnList, dropColumn)- }- return nil-}--func (diff *diffNode) modifyTableByConstraint(oldTable *ast.CreateTableStmt, newTable *ast.CreateTableStmt) error {- tableName := oldTable.Name- var addConstraintList []*ast.ConstraintDef- var dropConstraintList []*ast.ConstraintDef-- // Modify table for constraints.- oldConstraintMap := make(map[string]*ast.ConstraintDef)- for _, constraint := range oldTable.ConstraintList {- oldConstraintMap[constraint.Name] = constraint- }- for _, newConstraint := range newTable.ConstraintList {- oldConstraint, exists := oldConstraintMap[newConstraint.Name]- // Add the new constraint.- if !exists {- addConstraintList = append(addConstraintList, newConstraint)- continue- }-- isEqual, err := isEqualConstraint(oldConstraint, newConstraint)- if err != nil {- return err- }- if !isEqual {- dropConstraintList = append(dropConstraintList, oldConstraint)- addConstraintList = append(addConstraintList, newConstraint)- }- delete(oldConstraintMap, oldConstraint.Name)- }-- for _, oldConstraint := range oldTable.ConstraintList {- if _, exists := oldConstraintMap[oldConstraint.Name]; exists {- dropConstraintList = append(dropConstraintList, oldConstraint)- }- }-- diff.appendAddConstraint(tableName, addConstraintList)- diff.appendDropConstraint(tableName, dropConstraintList)- return nil-}--func (diff *diffNode) ModifyMaterializedView(oldMaterializedView *materializedViewInfo, _ int, newMaterializedView *ast.CreateMaterializedViewStmt) error {- if !equalMaterializedView(oldMaterializedView.createMaterializedView, newMaterializedView) {- diff.dropMaterializedViewList = append(diff.dropMaterializedViewList, oldMaterializedView.createMaterializedView)- // drop the comments of the view- oldMaterializedView.comment = ""- oldMaterializedView.columnCommentMap = make(map[string]string)- diff.createMaterializedViewList = append(diff.createMaterializedViewList, newMaterializedView)- }- return nil-}--func equalMaterializedView(oldMaterializedView *ast.CreateMaterializedViewStmt, newMaterializedView *ast.CreateMaterializedViewStmt) bool {- oldText, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{- Node: oldMaterializedView.GetOriginalNode(),- },- },- },- })- if err != nil {- slog.Debug("failed to deparse old materialized view", log.BBError(err))- return false- }- newText, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{- Node: newMaterializedView.GetOriginalNode(),- },- },- },- })- if err != nil {- slog.Debug("failed to deparse new materialized view", log.BBError(err))- return false- }- return oldText == newText-}--func (diff *diffNode) ModifyView(oldView *viewInfo, _ int, newView *ast.CreateViewStmt) error {- if !equalView(oldView.createView, newView) {- diff.dropViewList = append(diff.dropViewList, oldView.createView)- // drop the comments of the view- oldView.comment = ""- oldView.columnCommentMap = make(map[string]string)- diff.createViewList = append(diff.createViewList, newView)- }- return nil-}--func equalView(oldView *ast.CreateViewStmt, newView *ast.CreateViewStmt) bool {- // oldText, err := pgquery.Deparse(oldView.ParseResult)- // if err != nil {- // slog.Debug("failed to deparse old view", log.BBError(err))- // return false- // }- // newText, err := pgquery.Deparse(newView.ParseResult)- // if err != nil {- // slog.Debug("failed to deparse new view", log.BBError(err))- // return false- // }- oldText, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{- Node: oldView.GetOriginalNode(),- },- },- },- })- if err != nil {- slog.Debug("failed to deparse old view", log.BBError(err))- return false- }- newText, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{- Node: newView.GetOriginalNode(),- },- },- },- })- if err != nil {- slog.Debug("failed to deparse new view", log.BBError(err))- return false- }- return oldText == newText-}--func (diff *diffNode) modifyTable(oldTable *tableInfo, newTable *ast.CreateTableStmt) error {- if err := diff.modifyTableByColumn(oldTable, newTable); err != nil {- return err- }-- return diff.modifyTableByConstraint(oldTable.createTable, newTable)-}--func isEqualConstraint(oldConstraint *ast.ConstraintDef, newConstraint *ast.ConstraintDef) (bool, error) {- if oldConstraint.Type != newConstraint.Type {- return false, nil- }-- switch newConstraint.Type {- case ast.ConstraintTypeCheck:- if newConstraint.Expression.Text() != oldConstraint.Expression.Text() {- return false, nil- }- case ast.ConstraintTypeUnique, ast.ConstraintTypePrimary, ast.ConstraintTypeExclusion, ast.ConstraintTypeForeign:- // TODO(zp): To make the logic simple now, we just restore the statement, and drop and create the new one if- // there is any difference.- oldAlterTableAddConstraint, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, oldConstraint)- if err != nil {- return false, errors.Wrapf(err, "failed to deparse old alter table constraintDef: %v", oldConstraint)- }- newAlterTableAddConstraint, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, newConstraint)- if err != nil {- return false, errors.Wrapf(err, "failed to deparse new alter table constraintDef: %v", newConstraint)- }- if oldAlterTableAddConstraint != newAlterTableAddConstraint {- return false, nil- }- default:- return false, errors.Errorf("Unsupported table constraint type: %d for modifyConstraint", newConstraint.Type)- }- return true, nil-}--func (diff *diffNode) modifyColumn(tableName *ast.TableDef, oldColumn *ast.ColumnDef, newColumn *ast.ColumnDef) error {- alterTableStmt := &ast.AlterTableStmt{- Table: tableName,- }- columnName := oldColumn.ColumnName- // compare the data type- equivalent, err := equivalentType(oldColumn.Type, newColumn.Type)- if err != nil {- return err- }- if !equivalent {- alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.AlterColumnTypeStmt{- Table: tableName,- ColumnName: columnName,- Type: newColumn.Type,- })- }- // compare the NOT NULL- oldNotNull := hasNotNull(oldColumn)- newNotNull := hasNotNull(newColumn)- needSetNotNull := !oldNotNull && newNotNull- needDropNotNull := oldNotNull && !newNotNull- if needSetNotNull {- alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.SetNotNullStmt{- Table: alterTableStmt.Table,- ColumnName: columnName,- })- } else if needDropNotNull {- alterTableStmt.AlterItemList = append(alterTableStmt.AlterItemList, &ast.DropNotNullStmt{- Table: alterTableStmt.Table,- ColumnName: columnName,- })- }- // compare the DEFAULT- oldDefault, oldHasDefault := getDefault(oldColumn)- newDefault, newHasDefault := getDefault(newColumn)- needSetDefault := (!oldHasDefault && newHasDefault) || (oldHasDefault && newHasDefault && oldDefault != newDefault)- needDropDefault := oldHasDefault && !newHasDefault- if needSetDefault {- expression := &ast.UnconvertedExpressionDef{}- expression.SetText(newDefault)- diff.setDefaultList = append(diff.setDefaultList, &ast.AlterTableStmt{- Table: tableName,- AlterItemList: []ast.Node{- &ast.SetDefaultStmt{- Table: alterTableStmt.Table,- ColumnName: columnName,- Expression: expression,- },- },- })- } else if needDropDefault {- diff.dropDefaultList = append(diff.dropDefaultList, &ast.AlterTableStmt{- Table: tableName,- AlterItemList: []ast.Node{- &ast.DropDefaultStmt{- Table: alterTableStmt.Table,- ColumnName: columnName,- },- },- })- }-- // TODO(rebelice): compare other column properties- if len(alterTableStmt.AlterItemList) > 0 {- diff.alterColumnList = append(diff.alterColumnList, alterTableStmt)- }- return nil-}--func (diff *diffNode) modifyExtension(oldExtension *ast.CreateExtensionStmt, newExtension *ast.CreateExtensionStmt) {- // TODO(rebelice): not use Text(), it only works for pg_dump.- if oldExtension.Text() != newExtension.Text() {- diff.dropExtensionList = append(diff.dropExtensionList, &ast.DropExtensionStmt{- NameList: []string{oldExtension.Name},- })- diff.createExtensionList = append(diff.createExtensionList, newExtension)- }-}--func (diff *diffNode) modifyFunction(oldFunction *ast.CreateFunctionStmt, newFunction *ast.CreateFunctionStmt) {- // TODO(rebelice): not use Text(), it only works for pg_dump.- if oldFunction.Text() != newFunction.Text() {- diff.dropFunctionList = append(diff.dropFunctionList, oldFunction)- diff.createFunctionList = append(diff.createFunctionList, newFunction)- }-}--func isSubsequenceEnum(oldType ast.UserDefinedType, newType ast.UserDefinedType) bool {- oldEnum, ok := oldType.(*ast.EnumTypeDef)- if !ok {- return false- }- newEnum, ok := newType.(*ast.EnumTypeDef)- if !ok {- return false- }-- pos := 0- for _, oldLabel := range oldEnum.LabelList {- for {- if pos >= len(newEnum.LabelList) {- return false- }- if newEnum.LabelList[pos] == oldLabel {- break- }- pos++- }- }- return true-}--func (diff *diffNode) addEnumValue(oldType *ast.CreateTypeStmt, newType *ast.CreateTypeStmt) error {- oldEnum, ok := oldType.Type.(*ast.EnumTypeDef)- if !ok {- // never catch- return pgrawparser.NewConvertErrorf("expected EnumTypeDef but found %t", oldType.Type)- }- newEnum, ok := newType.Type.(*ast.EnumTypeDef)- if !ok {- // never catch- return pgrawparser.NewConvertErrorf("expected EnumTypeDef but found %t", newType.Type)- }-- // oldEnum has empty label list, so append newEnum labels.- if len(oldEnum.LabelList) == 0 {- for _, label := range newEnum.LabelList {- diff.alterTypeList = append(diff.alterTypeList, &ast.AlterTypeStmt{- Type: newType.Type.TypeName(),- AlterItemList: []ast.Node{&ast.AddEnumLabelStmt{- EnumType: newType.Type.TypeName(),- NewLabel: label,- Position: ast.PositionTypeEnd,- }},- })- }- return nil- }-- firstOldLabelPos := 0- for newEnum.LabelList[firstOldLabelPos] != oldEnum.LabelList[0] {- firstOldLabelPos++- }-- // Add Labels before first equal label by BEFORE.- for i := firstOldLabelPos - 1; i >= 0; i-- {- diff.alterTypeList = append(diff.alterTypeList, &ast.AlterTypeStmt{- Type: newType.Type.TypeName(),- AlterItemList: []ast.Node{&ast.AddEnumLabelStmt{- EnumType: newType.Type.TypeName(),- NewLabel: newEnum.LabelList[i],- Position: ast.PositionTypeBefore,- NeighborLabel: newEnum.LabelList[i+1],- }},- })- }-- // Add remaining labels by AFTER.- oldLabelPos := 1- for i := firstOldLabelPos + 1; i < len(newEnum.LabelList); i++ {- newLabel := newEnum.LabelList[i]- if len(oldEnum.LabelList) > oldLabelPos && newLabel == oldEnum.LabelList[oldLabelPos] {- oldLabelPos++- continue- }- diff.alterTypeList = append(diff.alterTypeList, &ast.AlterTypeStmt{- Type: newType.Type.TypeName(),- AlterItemList: []ast.Node{&ast.AddEnumLabelStmt{- EnumType: newType.Type.TypeName(),- NewLabel: newLabel,- Position: ast.PositionTypeAfter,- NeighborLabel: newEnum.LabelList[i-1],- }},- })- }-- return nil-}--func (diff *diffNode) modifyType(oldType *ast.CreateTypeStmt, newType *ast.CreateTypeStmt) error {- // TODO(rebelice): not use Text(), it only works for pg_dump.- if oldType.Text() != newType.Text() {- // Add enum value.- if isSubsequenceEnum(oldType.Type, newType.Type) {- return diff.addEnumValue(oldType, newType)- }-- // DROP and RE-CREATE.- diff.dropTypeList = append(diff.dropTypeList, &ast.DropTypeStmt{- TypeNameList: []*ast.TypeNameDef{oldType.Type.TypeName()},- })- diff.createTypeList = append(diff.createTypeList, newType)- }- return nil-}--func (diff *diffNode) modifyTrigger(oldTrigger *ast.CreateTriggerStmt, newTrigger *ast.CreateTriggerStmt) {- // TODO(rebelice): not use Text(), it only works for pg_dump.- if oldTrigger.Text() != newTrigger.Text() {- diff.dropTriggerList = append(diff.dropTriggerList, &ast.DropTriggerStmt{- Trigger: oldTrigger.Trigger,- })- diff.createTriggerList = append(diff.createTriggerList, newTrigger)- }-}--func (diff *diffNode) modifyIndex(oldIndex *ast.CreateIndexStmt, newIndex *ast.CreateIndexStmt) {- // TODO(rebelice): not use Text(), it only works for pg_dump.- if oldIndex.Text() != newIndex.Text() {- diff.dropIndexList = append(diff.dropIndexList, &ast.DropIndexStmt{- IndexList: []*ast.IndexDef{- {- Table: &ast.TableDef{Schema: oldIndex.Index.Table.Schema},- Name: oldIndex.Index.Name,- },- },- })- diff.createIndexList = append(diff.createIndexList, newIndex)- }-}--func (diff *diffNode) modifySequenceOwnedBy(oldSequenceOwnedBy *ast.AlterSequenceStmt, newSequenceOwnedBy *ast.AlterSequenceStmt) {- if !isEqualColumnNameDef(oldSequenceOwnedBy.OwnedBy, newSequenceOwnedBy.OwnedBy) {- diff.setDefaultList = append(diff.setDefaultList, newSequenceOwnedBy)- }-}--func (diff *diffNode) modifySequenceExceptOwnedBy(oldSequence *ast.CreateSequenceStmt, newSequence *ast.CreateSequenceStmt) {- isEqual := true- alterSequence := &ast.AlterSequenceStmt{- Name: oldSequence.SequenceDef.SequenceName,- }-- // compare data type- if !isEqualInteger(oldSequence.SequenceDef.SequenceDataType, newSequence.SequenceDef.SequenceDataType) {- alterSequence.Type = newSequence.SequenceDef.SequenceDataType- isEqual = false- }-- // compare increment- if !isEqualInt64Pointer(oldSequence.SequenceDef.IncrementBy, newSequence.SequenceDef.IncrementBy) {- alterSequence.IncrementBy = newSequence.SequenceDef.IncrementBy- isEqual = false- }-- // compare min value- if !isEqualInt64Pointer(oldSequence.SequenceDef.MinValue, newSequence.SequenceDef.MinValue) {- if newSequence.SequenceDef.MinValue == nil {- alterSequence.NoMinValue = true- } else {- alterSequence.MinValue = newSequence.SequenceDef.MinValue- }- isEqual = false- }-- // compare max value- if !isEqualInt64Pointer(oldSequence.SequenceDef.MaxValue, newSequence.SequenceDef.MaxValue) {- if newSequence.SequenceDef.MaxValue == nil {- alterSequence.NoMaxValue = true- } else {- alterSequence.MaxValue = newSequence.SequenceDef.MaxValue- }- isEqual = false- }-- // compare start with- if !isEqualInt64Pointer(oldSequence.SequenceDef.StartWith, newSequence.SequenceDef.StartWith) {- if newSequence.SequenceDef.StartWith != nil {- alterSequence.StartWith = newSequence.SequenceDef.StartWith- isEqual = false- }- }-- // compare cache- if !isEqualInt64Pointer(oldSequence.SequenceDef.Cache, newSequence.SequenceDef.Cache) {- if newSequence.SequenceDef.Cache != nil {- alterSequence.Cache = newSequence.SequenceDef.Cache- isEqual = false- }- }-- // compare cycle- if oldSequence.SequenceDef.Cycle != newSequence.SequenceDef.Cycle {- alterSequence.Cycle = &newSequence.SequenceDef.Cycle- isEqual = false- }-- if !isEqual {- diff.alterSequenceExceptOwnedByList = append(diff.alterSequenceExceptOwnedByList, alterSequence)- }-}--func isEqualTableDef(tableA *ast.TableDef, tableB *ast.TableDef) bool {- if tableA == nil && tableB == nil {- return true- }- if tableA == nil || tableB == nil {- return false- }-- if tableA.Database != tableB.Database {- return false- }-- if tableA.Schema != tableB.Schema {- return false- }-- if tableA.Name != tableB.Name {- return false- }-- return true-}--func isEqualColumnNameDef(columnA *ast.ColumnNameDef, columnB *ast.ColumnNameDef) bool {- if columnA == nil && columnB == nil {- return true- }- if columnA == nil || columnB == nil {- return false- }-- if !isEqualTableDef(columnA.Table, columnB.Table) {- return false- }-- return columnA.ColumnName == columnB.ColumnName-}--func isEqualInt64Pointer(a *int64, b *int64) bool {- if a == nil && b == nil {- return true- }- if a == nil || b == nil {- return false- }- return *a == *b-}--func isEqualInteger(typeA *ast.Integer, typeB *ast.Integer) bool {- if typeA == nil && typeB == nil {- return true- }- if typeA == nil || typeB == nil {- return false- }- return typeA.Size == typeB.Size-}--func getDefault(column *ast.ColumnDef) (string, bool) {- for _, constraint := range column.ConstraintList {- if constraint.Type == ast.ConstraintTypeDefault {- return constraint.Expression.Text(), true- }- }- return "", false-}--func hasNotNull(column *ast.ColumnDef) bool {- for _, constraint := range column.ConstraintList {- if constraint.Type == ast.ConstraintTypeNotNull {- return true- }- }- return false-}--func equivalentType(typeA ast.DataType, typeB ast.DataType) (bool, error) {- typeStringA, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, typeA)- if err != nil {- return false, err- }- typeStringB, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, typeB)- if err != nil {- return false, err- }- return typeStringA == typeStringB, nil-}--func printStmtSliceByText(buf io.Writer, nodeList []ast.Node) error {- for _, node := range nodeList {- if err := writeStringWithNewLine(buf, node.Text()); err != nil {- return err- }- if _, err := buf.Write([]byte("\n")); err != nil {- return err- }- }- return nil-}--func printStmtSlice(buf io.Writer, nodeList []ast.Node) error {- for _, node := range nodeList {- sql, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, node)- if err != nil {- return err- }- if err := writeStringWithNewLine(buf, sql); err != nil {- return err- }- if _, err := buf.Write([]byte("\n")); err != nil {- return err- }- }- return nil-}--func printDropMaterializedView(buf io.Writer, viewName *ast.TableDef) error {- if viewName == nil {- return nil- }-- if _, err := fmt.Fprint(buf, "DROP MATERIALIZED VIEW "); err != nil {- return err- }- if _, err := fmt.Fprintf(buf, `"%s"."%s"`, viewName.Schema, viewName.Name); err != nil {- return err- }- if _, err := fmt.Fprint(buf, ";\n\n"); err != nil {- return err- }- return nil-}--func printDropView(buf io.Writer, viewName *ast.TableDef) error {- if viewName == nil {- return nil- }- if _, err := fmt.Fprint(buf, "DROP VIEW "); err != nil {- return err- }- if _, err := fmt.Fprintf(buf, `"%s"."%s"`, viewName.Schema, viewName.Name); err != nil {- return err- }- if _, err := fmt.Fprint(buf, ";\n\n"); err != nil {- return err- }- return nil-}--func printDropFunction(buf io.Writer, function *ast.FunctionDef) error {- signature, err := functionSignature(function)- if err != nil {- return errors.Wrapf(err, "failed to get function signature: %v", function.Name)- }- _, err = fmt.Fprintf(buf, "%s\n\n", signature)- return err-}--func printDropTable(buf io.Writer, table *ast.TableDef) error {- if _, err := fmt.Fprint(buf, "DROP TABLE "); err != nil {- return err- }-- if _, err := fmt.Fprintf(buf, `"%s"."%s"`, table.Schema, table.Name); err != nil {- return err- }-- _, err := fmt.Fprint(buf, ";\n\n")- return err-}--func printCreateMaterializedViewStmt(buf io.Writer, view *ast.CreateMaterializedViewStmt) error {- node := view.GetOriginalNode()- text, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{Node: node},- },- },- })- if err != nil {- return err- }- err = writeStringWithNewLine(buf, text+";")- return err-}--func printCreateViewStmt(buf io.Writer, view *ast.CreateViewStmt) error {- node := view.GetOriginalNode()- node.ViewStmt.Replace = true- text, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{Node: node},- },- },- })- if err != nil {- return err- }- err = writeStringWithNewLine(buf, text+";")- return err-}--func printCreateFunctionStmt(buf io.Writer, function *ast.CreateFunctionStmt) error {- if err := writeStringWithNewLine(buf, function.Text()); err != nil {- return err- }- _, err := buf.Write([]byte("\n"))- return err-}--func printCreateTableStmt(buf io.Writer, table *ast.CreateTableStmt) error {- sql, err := pgrawparser.Deparse(pgrawparser.DeparseContext{}, table)- if err != nil {- return err- }- if err := writeStringWithNewLine(buf, sql); err != nil {- return err- }- _, err = buf.Write([]byte("\n"))- return err-}--// deparse statements as the safe change order.-func (diff *diffNode) deparse() (string, error) {- var buf bytes.Buffer-- // Print header.- if len(diff.createFunctionList) > 0 {- if _, err := buf.WriteString("SET check_function_bodies = false;\n\n"); err != nil {- return "", err- }- }-- // drop- if err := printStmtSlice(&buf, diff.dropForeignKeyList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropConstraintExceptFkList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropTriggerList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropIndexList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropDefaultList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropSequenceOwnedByList); err != nil {- return "", err- }- if err := diff.printDropFuncTableViewAndMateView(&buf); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropColumnList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropSequenceList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropExtensionList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropTypeList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.dropSchemaList); err != nil {- return "", err- }-- // create- if err := printStmtSlice(&buf, diff.createSchemaList); err != nil {- return "", err- }- if err := printStmtSliceByText(&buf, diff.createTypeList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.alterTypeList); err != nil {- return "", err- }- if err := printStmtSliceByText(&buf, diff.createExtensionList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.createSequenceList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.alterSequenceExceptOwnedByList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.createColumnList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.alterColumnList); err != nil {- return "", err- }-- if err := diff.printCreateFuncTableViewAndMateView(&buf); err != nil {- return "", err- }-- if err := printStmtSlice(&buf, diff.setSequenceOwnedByList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.setDefaultList); err != nil {- return "", err- }- if err := printStmtSliceByText(&buf, diff.createIndexList); err != nil {- return "", err- }- if err := printStmtSliceByText(&buf, diff.createTriggerList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.createConstraintExceptFkList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.createForeignKeyList); err != nil {- return "", err- }- if err := printStmtSlice(&buf, diff.setCommentList); err != nil {- return "", err- }-- return buf.String(), nil-}--func getObjectID(schema, object string) string {- return fmt.Sprintf("%s.%s", schema, object)-}--func (diff *diffNode) getNewDatabaseMetadataFunc() base.GetDatabaseMetadataFunc {- return func(context.Context, string, string) (string, *model.DatabaseMetadata, error) {- return "", model.NewDatabaseMetadata(schemaMapToMetadata(diff.newSchemaMap), true /* isObjectCaseSensitive */, true /* isDetailCaseSensitive */), nil- }-}--func (diff *diffNode) getOldDatabaseMetadataFunc() base.GetDatabaseMetadataFunc {- return func(context.Context, string, string) (string, *model.DatabaseMetadata, error) {- return "", model.NewDatabaseMetadata(schemaMapToMetadata(diff.oldSchemaMap), true /* isObjectCaseSensitive */, true /* isDetailCaseSensitive */), nil- }-}--func schemaMapToMetadata(m schemaMap) *storepb.DatabaseSchemaMetadata {- meta := storepb.DatabaseSchemaMetadata{}- for _, schema := range m {- // We only need to add the views and materialized views to the schema metadata.- // This is only use to get the resources column from query span.- meta.Schemas = append(meta.Schemas, &storepb.SchemaMetadata{- Name: schema.createSchema.Name,- Views: viewToMetadata(schema.viewMap),- MaterializedViews: materializedViewToMetadata(schema.materializedViewMap),- })- }-- return &meta-}--func materializedViewToMetadata(m materializedViewMap) []*storepb.MaterializedViewMetadata {- var materializedViews []*storepb.MaterializedViewMetadata- for _, materializedView := range m {- materializedViews = append(materializedViews, &storepb.MaterializedViewMetadata{- Name: materializedView.createMaterializedView.Name.Name,- })- }- return materializedViews-}--func viewToMetadata(m viewMap) []*storepb.ViewMetadata {- var views []*storepb.ViewMetadata- for _, view := range m {- views = append(views, &storepb.ViewMetadata{- Name: view.createView.Name.Name,- })- }- return views-}--func (diff *diffNode) printCreateFuncTableViewAndMateView(buf io.Writer) error {- graph := base.NewGraph()- functionMap := make(map[string]*ast.CreateFunctionStmt)- tableMap := make(map[string]*ast.CreateTableStmt)- viewMap := make(map[string]*ast.CreateViewStmt)- materializedViewMap := make(map[string]*ast.CreateMaterializedViewStmt)-- for _, function := range diff.createFunctionList {- signature, err := functionSignature(function.Function)- if err != nil {- return errors.Wrapf(err, "failed to get function signature for %s", function.Function.Name)- }- funcID := getObjectID(function.Function.Schema, signature)- functionMap[funcID] = function- graph.AddNode(funcID)- tSchema, tName := getFunctionReturnType(function)- graph.AddEdge(getObjectID(tSchema, tName), funcID)- }-- for _, table := range diff.createTableList {- tableID := getObjectID(table.Name.Schema, table.Name.Name)- tableMap[tableID] = table- graph.AddNode(tableID)- }-- for _, view := range diff.createViewList {- viewID := getObjectID(view.Name.Schema, view.Name.Name)- viewMap[viewID] = view- graph.AddNode(viewID)- dependency, err := diff.getViewDependency(view, diff.getNewDatabaseMetadataFunc())- if err != nil {- return errors.Wrapf(err, "failed to get view dependency for %s", viewID)- }- for _, dep := range dependency {- graph.AddEdge(dep, viewID)- }- }-- for _, materializedView := range diff.createMaterializedViewList {- viewID := getObjectID(materializedView.Name.Schema, materializedView.Name.Name)- materializedViewMap[viewID] = materializedView- graph.AddNode(viewID)- dependency, err := diff.getMaterializedViewDependency(materializedView, diff.getNewDatabaseMetadataFunc())- if err != nil {- return errors.Wrapf(err, "failed to get view dependency for %s", viewID)- }- for _, dep := range dependency {- graph.AddEdge(dep, viewID)- }- }-- orderedList, err := graph.TopologicalSort()- if err != nil {- return errors.Wrap(err, "failed to topological sort")- }-- for _, objectID := range orderedList {- if function, ok := functionMap[objectID]; ok {- if err := printCreateFunctionStmt(buf, function); err != nil {- return err- }- continue- }-- if table, ok := tableMap[objectID]; ok {- if err := printCreateTableStmt(buf, table); err != nil {- return err- }- continue- }-- if view, ok := viewMap[objectID]; ok {- if err := printCreateViewStmt(buf, view); err != nil {- return err- }- continue- }-- if materializedView, ok := materializedViewMap[objectID]; ok {- if err := printCreateMaterializedViewStmt(buf, materializedView); err != nil {- return err- }- }- }-- return nil-}--func getFunctionReturnType(function *ast.CreateFunctionStmt) (string, string) {- node := function.GetOriginalNode()- returnType := node.CreateFunctionStmt.ReturnType- if returnType == nil {- return "", ""- }-- nameList := convertNodeSliceToStringList(returnType.Names)- switch len(nameList) {- case 1:- return "public", nameList[0]- case 2:- return nameList[0], nameList[1]- default:- return "", ""- }-}--func convertNodeSliceToStringList(nodeList []*pgquery.Node) []string {- var result []string- for _, node := range nodeList {- if stringNode, ok := node.Node.(*pgquery.Node_String_); ok {- result = append(result, stringNode.String_.GetSval())- }- }- return result-}--func (diff *diffNode) printDropFuncTableViewAndMateView(buf io.Writer) error {- graph := base.NewGraph()- functionMap := make(map[string]*ast.CreateFunctionStmt)- tableMap := make(map[string]*ast.CreateTableStmt)- viewMap := make(map[string]*ast.CreateViewStmt)- materializedViewMap := make(map[string]*ast.CreateMaterializedViewStmt)-- for _, function := range diff.dropFunctionList {- signature, err := functionSignature(function.Function)- if err != nil {- return errors.Wrapf(err, "failed to get function signature for %s", function.Function.Name)- }- funcID := getObjectID(function.Function.Schema, signature)- functionMap[funcID] = function- graph.AddNode(funcID)- tSchema, tName := getFunctionReturnType(function)- graph.AddEdge(funcID, getObjectID(tSchema, tName))- }-- for _, table := range diff.dropTableList {- tableID := getObjectID(table.Name.Schema, table.Name.Name)- tableMap[tableID] = table- graph.AddNode(tableID)- }-- for _, view := range diff.dropViewList {- viewID := getObjectID(view.Name.Schema, view.Name.Name)- viewMap[viewID] = view- graph.AddNode(viewID)- dependency, err := diff.getViewDependency(view, diff.getOldDatabaseMetadataFunc())- if err != nil {- return errors.Wrapf(err, "failed to get view dependency for %s", viewID)- }- for _, dep := range dependency {- graph.AddEdge(viewID, dep)- }- }-- for _, materializedView := range diff.dropMaterializedViewList {- viewID := getObjectID(materializedView.Name.Schema, materializedView.Name.Name)- materializedViewMap[viewID] = materializedView- graph.AddNode(viewID)- dependency, err := diff.getMaterializedViewDependency(materializedView, diff.getOldDatabaseMetadataFunc())- if err != nil {- return errors.Wrapf(err, "failed to get view dependency for %s", viewID)- }- for _, dep := range dependency {- graph.AddEdge(viewID, dep)- }- }-- orderedList, err := graph.TopologicalSort()- if err != nil {- return errors.Wrap(err, "failed to topological sort")- }-- for _, objectID := range orderedList {- if function, ok := functionMap[objectID]; ok {- if err := printDropFunction(buf, function.Function); err != nil {- return err- }- continue- }-- if table, ok := tableMap[objectID]; ok {- if err := printDropTable(buf, table.Name); err != nil {- return err- }- continue- }-- if view, ok := viewMap[objectID]; ok {- if err := printDropView(buf, view.Name); err != nil {- return err- }- continue- }-- if materializedView, ok := materializedViewMap[objectID]; ok {- if err := printDropMaterializedView(buf, materializedView.Name); err != nil {- return err- }- }- }-- return nil-}--func (*diffNode) getMaterializedViewDependency(materializedView *ast.CreateMaterializedViewStmt, getDatabaseMetadata base.GetDatabaseMetadataFunc) ([]string, error) {- queryText, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{- Node: materializedView.GetOriginalNode().CreateTableAsStmt.Query.Node,- },- },- },- })- if err != nil {- return nil, err- }-- span, err := GetQuerySpan(- context.Background(),- base.GetQuerySpanContext{- GetDatabaseMetadataFunc: getDatabaseMetadata,- }, // We only need the source column, so resource is not needed.- queryText,- "", // No need database name.- "public",- false,- )- if err != nil {- return nil, err- }-- var result []string- for sourceColumn := range span.SourceColumns {- result = append(result, getObjectID(sourceColumn.Schema, sourceColumn.Table))- }- return result, nil-}--func (*diffNode) getViewDependency(view *ast.CreateViewStmt, getDatabaseMetadata base.GetDatabaseMetadataFunc) ([]string, error) {- queryText, err := pgquery.Deparse(&pgquery.ParseResult{- Stmts: []*pgquery.RawStmt{- {- Stmt: &pgquery.Node{- Node: view.GetOriginalNode().ViewStmt.Query.Node,- },- },- },- })- if err != nil {- return nil, err- }-- span, err := GetQuerySpan(- context.Background(),- base.GetQuerySpanContext{- GetDatabaseMetadataFunc: base.GetDatabaseMetadataFunc(getDatabaseMetadata),- }, // We only need the source column, so resource is not needed.- queryText,- "", // No need database name.- "public",- false,- )- if err != nil {- return nil, err- }-- var result []string- for sourceColumn := range span.SourceColumns {- result = append(result, getObjectID(sourceColumn.Schema, sourceColumn.Table))- }- return result, nil-}--func (diff *diffNode) dropConstraint(m schemaMap) {- for _, schemaInfo := range m {- if !schemaInfo.existsInNew {- continue- }- for _, tableInfo := range schemaInfo.tableMap {- if !tableInfo.existsInNew {- continue- }- var constraintInfoList []*constraintInfo- for _, constraintInfo := range tableInfo.constraintMap {- if !constraintInfo.existsInNew {- constraintInfoList = append(constraintInfoList, constraintInfo)- }- }- slices.SortFunc(constraintInfoList, func(i, j *constraintInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- var dropConstraintList []*ast.ConstraintDef- for _, constraintInfo := range constraintInfoList {- dropConstraintList = append(dropConstraintList, constraintInfo.addConstraint.Constraint)- }-- diff.appendDropConstraint(tableInfo.createTable.Name, dropConstraintList)- }- }-}--func dropMaterializedView(m schemaMap) []*ast.CreateMaterializedViewStmt {- var materializedViewList []*materializedViewInfo- for _, schema := range m {- for _, materializedView := range schema.materializedViewMap {- if materializedView.existsInNew {- // no need to drop- continue- }- materializedViewList = append(materializedViewList, materializedView)- }- }- if len(materializedViewList) == 0 {- return nil- }- slices.SortFunc(materializedViewList, func(i, j *materializedViewInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var materializedViewDefList []*ast.CreateMaterializedViewStmt- for _, materializedView := range materializedViewList {- materializedViewDefList = append(materializedViewDefList, materializedView.createMaterializedView)- }- return materializedViewDefList-}--func dropView(m schemaMap) []*ast.CreateViewStmt {- var viewList []*viewInfo- for _, schema := range m {- for _, view := range schema.viewMap {- if view.existsInNew {- // no need to drop- continue- }- viewList = append(viewList, view)- }- }- if len(viewList) == 0 {- return nil- }- slices.SortFunc(viewList, func(i, j *viewInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var viewDefList []*ast.CreateViewStmt- for _, view := range viewList {- viewDefList = append(viewDefList, view.createView)- }- return viewDefList-}--func dropTable(m schemaMap) []*ast.CreateTableStmt {- var tableList []*tableInfo- for _, schema := range m {- for _, table := range schema.tableMap {- if table.existsInNew {- // no need to drop- continue- }- tableList = append(tableList, table)- }- }- if len(tableList) == 0 {- return nil- }- slices.SortFunc(tableList, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var result []*ast.CreateTableStmt- for _, table := range tableList {- result = append(result, table.createTable)- }- return result-}--func dropSchema(m schemaMap) *ast.DropSchemaStmt {- var schemaList []*schemaInfo- for _, schema := range m {- if schema.createSchema.Name == "public" || schema.existsInNew {- continue- }- schemaList = append(schemaList, schema)- }- if len(schemaList) == 0 {- return nil- }- slices.SortFunc(schemaList, func(i, j *schemaInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var schemaNameList []string- for _, schema := range schemaList {- schemaNameList = append(schemaNameList, schema.createSchema.Name)- }- return &ast.DropSchemaStmt{- SchemaList: schemaNameList,- }-}--func dropExtension(m schemaMap) *ast.DropExtensionStmt {- var extensionList []*extensionInfo- for _, schema := range m {- for _, extension := range schema.extensionMap {- if extension.existsInNew {- // no need to drop- continue- }- extensionList = append(extensionList, extension)- }- }- if len(extensionList) == 0 {- return nil- }- slices.SortFunc(extensionList, func(i, j *extensionInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var extensionNameList []string- for _, extension := range extensionList {- extensionNameList = append(extensionNameList, extension.createExtension.Name)- }- return &ast.DropExtensionStmt{- NameList: extensionNameList,- }-}--func dropFunction(m schemaMap) []*ast.CreateFunctionStmt {- var functionList []*functionInfo- for _, schema := range m {- for _, function := range schema.functionMap {- if function.existsInNew {- // no need to drop- continue- }- functionList = append(functionList, function)- }- }- if len(functionList) == 0 {- return nil- }- slices.SortFunc(functionList, func(i, j *functionInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var result []*ast.CreateFunctionStmt- for _, function := range functionList {- result = append(result, function.createFunction)- }- return result-}--func (diff *diffNode) dropComment(m schemaMap) {- var tables []*tableInfo- var views []*viewInfo- for _, schema := range m {- for _, table := range schema.tableMap {- if !table.existsInNew {- // no need to drop, because the comments are dropped when drop table- continue- }- tables = append(tables, table)- }- for _, view := range schema.viewMap {- if !view.existsInNew {- // no need to drop, because the comments are dropped when drop view- continue- }- views = append(views, view)- }- }- slices.SortFunc(tables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- for _, table := range tables {- if table.comment != "" {- diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{- Comment: "",- Object: &ast.TableDef{Schema: table.createTable.Name.Schema, Name: table.createTable.Name.Name},- Type: ast.ObjectTypeTable,- })- }-- type commentInfo struct {- column string- comment string- }- var columnCommentList []commentInfo- for k, v := range table.columnCommentMap {- columnCommentList = append(columnCommentList, commentInfo{- column: k,- comment: v,- })- }- slices.SortFunc(columnCommentList, func(i, j commentInfo) int {- if i.column < j.column {- return -1- }- if i.column > j.column {- return 1- }- return 0- })- for _, columnComment := range columnCommentList {- diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{- Comment: "",- Object: &ast.ColumnNameDef{- Table: &ast.TableDef{Schema: table.createTable.Name.Schema, Name: table.createTable.Name.Name},- ColumnName: columnComment.column,- },- Type: ast.ObjectTypeColumn,- })- }- }-- slices.SortFunc(views, func(i, j *viewInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- for _, view := range views {- if view.comment != "" {- diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{- Comment: "",- Object: &ast.TableDef{Schema: view.createView.Name.Schema, Name: view.createView.Name.Name},- Type: ast.ObjectTypeView,- })- }-- type commentInfo struct {- column string- comment string- }- var columnCommentList []commentInfo- for k, v := range view.columnCommentMap {- columnCommentList = append(columnCommentList, commentInfo{- column: k,- comment: v,- })- }- slices.SortFunc(columnCommentList, func(i, j commentInfo) int {- if i.column < j.column {- return -1- }- if i.column > j.column {- return 1- }- return 0- })- for _, columnComment := range columnCommentList {- diff.setCommentList = append(diff.setCommentList, &ast.CommentStmt{- Comment: "",- Object: &ast.ColumnNameDef{- Table: &ast.TableDef{Schema: view.createView.Name.Schema, Name: view.createView.Name.Name},- ColumnName: columnComment.column,- },- Type: ast.ObjectTypeColumn,- })- }- }-}--func (diff *diffNode) dropTypeStmt(m schemaMap) {- var typeList []*typeInfo- for _, schema := range m {- for _, tp := range schema.typeMap {- if tp.existsInNew {- // no need to drop- continue- }- typeList = append(typeList, tp)- }- }- if len(typeList) == 0 {- return- }- slices.SortFunc(typeList, func(i, j *typeInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- for _, tp := range typeList {- diff.dropTypeList = append(diff.dropTypeList, &ast.DropTypeStmt{- TypeNameList: []*ast.TypeNameDef{tp.createType.Type.TypeName()},- })- }-}--func (diff *diffNode) dropTriggerStmt(m schemaMap) {- var triggerList []*triggerInfo- for _, schema := range m {- for _, table := range schema.tableMap {- for _, trigger := range table.triggerMap {- if trigger.existsInNew {- // no need to drop- continue- }- triggerList = append(triggerList, trigger)- }- }- }- if len(triggerList) == 0 {- return- }- slices.SortFunc(triggerList, func(i, j *triggerInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- for _, trigger := range triggerList {- diff.dropTriggerList = append(diff.dropTriggerList,- &ast.DropTriggerStmt{Trigger: trigger.createTrigger.Trigger})- }-}--func dropIndex(m schemaMap) *ast.DropIndexStmt {- var indexList []*indexInfo- for _, schema := range m {- if !schema.existsInNew {- continue- }- for _, index := range schema.indexMap {- if index.existsInNew {- // no need to drop- continue- }- tbl, ok := schema.tableMap[index.createIndex.Index.Table.Name]- if !ok {- continue- }- if !tbl.existsInNew {- continue- }- indexList = append(indexList, index)- }- }- if len(indexList) == 0 {- return nil- }- slices.SortFunc(indexList, func(i, j *indexInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var indexDefList []*ast.IndexDef- for _, index := range indexList {- indexDefList = append(indexDefList, &ast.IndexDef{- Table: &ast.TableDef{Schema: index.createIndex.Index.Table.Schema},- Name: index.createIndex.Index.Name,- })- }- return &ast.DropIndexStmt{- IndexList: indexDefList,- }-}--func (diff *diffNode) dropSequenceOwnedBy(m schemaMap) {- var sequenceOwnedByList []*sequenceOwnedByInfo- for _, schema := range m {- for _, sequence := range schema.sequenceMap {- if sequence.ownedByInfo == nil || sequence.ownedByInfo.existsInNew {- // no need to drop- continue- }- sequenceOwnedByList = append(sequenceOwnedByList, sequence.ownedByInfo)- }- }-- if len(sequenceOwnedByList) == 0 {- return- }- slices.SortFunc(sequenceOwnedByList, func(i, j *sequenceOwnedByInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- for _, sequenceOwnedBy := range sequenceOwnedByList {- diff.dropSequenceOwnedByList = append(diff.dropSequenceOwnedByList, &ast.AlterSequenceStmt{- Name: sequenceOwnedBy.ownedBy.Name,- OwnedByNone: true,- })- }-}--func dropSequence(m schemaMap) *ast.DropSequenceStmt {- var sequenceList []*sequenceInfo- for _, schema := range m {- for _, sequence := range schema.sequenceMap {- if sequence.existsInNew {- // no need to drop- continue- }- sequenceList = append(sequenceList, sequence)- }- }-- if len(sequenceList) == 0 {- return nil- }- slices.SortFunc(sequenceList, func(i, j *sequenceInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- var sequenceNameList []*ast.SequenceNameDef- for _, sequence := range sequenceList {- sequenceNameList = append(sequenceNameList, &ast.SequenceNameDef{- Schema: sequence.createSequence.SequenceDef.SequenceName.Schema,- Name: sequence.createSequence.SequenceDef.SequenceName.Name,- })- }- return &ast.DropSequenceStmt{- SequenceNameList: sequenceNameList,- }-}--func writeStringWithNewLine(out io.Writer, str string) error {- if _, err := out.Write([]byte(str)); err != nil {- return err- }- if _, err := out.Write([]byte("\n")); err != nil {- return err- }- return nil-}--// use DROP FUNCTION statement as the function signature.-func functionSignature(function *ast.FunctionDef) (string, error) {- return pgrawparser.Deparse(pgrawparser.DeparseContext{}, &ast.DropFunctionStmt{- FunctionList: []*ast.FunctionDef{function},- })-}diff --git a/backend/plugin/parser/pg/differ_test.go b/backend/plugin/parser/pg/differ_test.godeleted file mode 100644index afffada301..0000000000--- a/backend/plugin/parser/pg/differ_test.go+++ /dev/null@@ -1,73 +0,0 @@-package pg--import (- "io"- "os"- "path/filepath"- "testing"-- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- // Register PostgreSQL parser engine.- "github.com/bytebase/bytebase/backend/plugin/parser/base"- _ "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy"-)--type DifferTestData struct {- OldSchema string `yaml:"oldSchema"`- NewSchema string `yaml:"newSchema"`- Diff string `yaml:"diff"`-}--func runDifferTest(t *testing.T, file string, record bool) {- var tests []DifferTestData- filepath := filepath.Join("test-data", file)- yamlFile, err := os.Open(filepath)- require.NoError(t, err)- defer yamlFile.Close()-- byteValue, err := io.ReadAll(yamlFile)- require.NoError(t, err)- err = yaml.Unmarshal(byteValue, &tests)- require.NoError(t, err)-- for i, test := range tests {- diff, err := SchemaDiff(base.DiffContext{- IgnoreCaseSensitive: false,- StrictMode: true,- }, test.OldSchema, test.NewSchema)- require.NoError(t, err)- if record {- tests[i].Diff = diff- } else {- require.Equal(t, test.Diff, diff, test.OldSchema)- }- }-- if record {- err := yamlFile.Close()- require.NoError(t, err)- byteValue, err = yaml.Marshal(tests)- require.NoError(t, err)- err = os.WriteFile(filepath, byteValue, 0644)- require.NoError(t, err)- }-}--func TestComputeDiff(t *testing.T) {- testFileList := []string{- "test_differ_data.yaml",- // Schema- "test_differ_schema.yaml",- // Constraint- "test_differ_constraint.yaml",- // Merge- "test_differ_merge.yaml",- // Sequence- "test_differ_sequence.yaml",- }- for _, test := range testFileList {- runDifferTest(t, test, false /* record */)- }-}diff --git a/backend/plugin/parser/plsql/differ.go b/backend/plugin/parser/plsql/differ.godeleted file mode 100644index a7b153c89f..0000000000--- a/backend/plugin/parser/plsql/differ.go+++ /dev/null@@ -1,943 +0,0 @@-// Package plsql provides the plsql parser plugin.-package plsql--import (- "fmt"- "slices"- "strings"-- "github.com/antlr4-go/antlr/v4"- "github.com/pkg/errors"-- "github.com/bytebase/parser/plsql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func init() {- base.RegisterSchemaDiffFunc(storepb.Engine_ORACLE, SchemaDiff)-}--type diffNode struct {- // The different between the strict mode and non-strict mode is that the non-strict mode:- // 1. does not compare the index or constraint name, use the definition instead.- // 2. does not compare the storage option.- strictMode bool-- schemaName string- dropConstraint []string- dropIndex []string- dropColumn []string- dropTable []string- createTable []string- addColumn []string- modifyColumn []string- modifyColumnVisibility []string- addIndex []string- addConstraint []string-}--func (diff *diffNode) String() (string, error) {- var buf strings.Builder- for _, dropConstraint := range diff.dropConstraint {- if _, err := buf.WriteString(dropConstraint); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, dropIndex := range diff.dropIndex {- if _, err := buf.WriteString(dropIndex); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, dropColumn := range diff.dropColumn {- if _, err := buf.WriteString(dropColumn); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, dropTable := range diff.dropTable {- if _, err := buf.WriteString(dropTable); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, createTable := range diff.createTable {- if _, err := buf.WriteString(createTable); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, addColumn := range diff.addColumn {- if _, err := buf.WriteString(addColumn); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, modifyColumn := range diff.modifyColumn {- if _, err := buf.WriteString(modifyColumn); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, modifyColumnVisibility := range diff.modifyColumnVisibility {- if _, err := buf.WriteString(modifyColumnVisibility); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, addIndex := range diff.addIndex {- if _, err := buf.WriteString(addIndex); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- for _, addConstraint := range diff.addConstraint {- if _, err := buf.WriteString(addConstraint); err != nil {- return "", err- }- if _, err := buf.WriteString("\n"); err != nil {- return "", err- }- }- return buf.String(), nil-}--// SchemaDiff implements the differ.SchemaDiffer interface.-func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {- oldSchemaInfo, err := buildSchemaInfo(oldStmt, ctx.StrictMode)- if err != nil {- return "", errors.Wrapf(err, "failed to build schema info for old statement")- }- newSchemaInfo, err := buildSchemaInfo(newStmt, ctx.StrictMode)- if err != nil {- return "", errors.Wrapf(err, "failed to build schema info for new statement")- }-- diff := &diffNode{- strictMode: ctx.StrictMode,- schemaName: oldSchemaInfo.name,- }- var newTables []*tableInfo- for _, table := range newSchemaInfo.tableMap {- newTables = append(newTables, table)- }- slices.SortFunc(newTables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, newTable := range newTables {- tableName := newTable.name- oldTable, exists := oldSchemaInfo.tableMap[tableName]- if !exists {- diff.createTable = append(diff.createTable, getCreateTableWithoutStoreOption(newTable.createTable))- continue- }- if err := diff.diffTable(oldTable, newTable); err != nil {- return "", err- }- delete(oldSchemaInfo.tableMap, tableName)- }-- var remainingTables []*tableInfo- for _, table := range oldSchemaInfo.tableMap {- remainingTables = append(remainingTables, table)- }- slices.SortFunc(remainingTables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, table := range remainingTables {- if ctx.StrictMode {- diff.dropTable = append(diff.dropTable, fmt.Sprintf(`DROP TABLE "%s"."%s";`, oldSchemaInfo.name, table.name))- } else {- diff.dropTable = append(diff.dropTable, fmt.Sprintf(`DROP TABLE "%s";`, table.name))- }- }-- var newIndexes []*indexInfo- for _, index := range newSchemaInfo.indexMap {- newIndexes = append(newIndexes, index)- }- slices.SortFunc(newIndexes, func(i, j *indexInfo) int {- if i.pos < j.pos {- return -1- }- if i.pos > j.pos {- return 1- }- return 0- })- for _, newIndex := range newIndexes {- id := newIndex.id- oldIndex, exists := oldSchemaInfo.indexMap[id]- if !exists {- diff.addIndex = append(diff.addIndex, newIndex.createIndex.GetParser().GetTokenStream().GetTextFromRuleContext(newIndex.createIndex))- continue- }- if ctx.StrictMode {- diff.diffIndex(oldIndex, newIndex)- }- delete(oldSchemaInfo.indexMap, id)- }-- var remainingIndexes []*indexInfo- for _, index := range oldSchemaInfo.indexMap {- remainingIndexes = append(remainingIndexes, index)- }- slices.SortFunc(remainingIndexes, func(i, j *indexInfo) int {- if i.pos < j.pos {- return -1- }- if i.pos > j.pos {- return 1- }- return 0- })- for _, index := range remainingIndexes {- if ctx.StrictMode {- diff.dropIndex = append(diff.dropIndex, fmt.Sprintf(`DROP INDEX "%s"."%s";`, oldSchemaInfo.name, index.name))- } else {- diff.dropIndex = append(diff.dropIndex, fmt.Sprintf(`DROP INDEX "%s";`, index.name))- }- }-- return diff.String()-}--func getCreateTableWithoutStoreOption(createTable plsql.ICreate_tableContext) string {- if createTable.Relational_table() == nil {- return createTable.GetParser().GetTokenStream().GetTextFromRuleContext(createTable)- }- return createTable.GetParser().GetTokenStream().GetTextFromTokens(- createTable.GetStart(),- createTable.Relational_table().RIGHT_PAREN().GetSymbol(),- ) + ";"-}--func (diff *diffNode) diffIndex(oldIndex, newIndex *indexInfo) {- // TODO: compare index definition instead of text.- oldString := oldIndex.createIndex.GetParser().GetTokenStream().GetTextFromRuleContext(oldIndex.createIndex)- newString := newIndex.createIndex.GetParser().GetTokenStream().GetTextFromRuleContext(newIndex.createIndex)- if oldString != newString {- if diff.strictMode {- diff.dropIndex = append(diff.dropIndex, fmt.Sprintf(`DROP INDEX "%s"."%s";`, diff.schemaName, oldIndex.name))- } else {- diff.dropIndex = append(diff.dropIndex, fmt.Sprintf(`DROP INDEX "%s";`, oldIndex.name))- }- diff.addIndex = append(diff.addIndex, newIndex.createIndex.GetParser().GetTokenStream().GetTextFromRuleContext(newIndex.createIndex))- }-}--func (diff *diffNode) diffTable(oldTable, newTable *tableInfo) error {- if err := diff.diffColumn(oldTable, newTable); err != nil {- return err- }- return diff.diffConstraint(oldTable, newTable)-}--func (diff *diffNode) buildConstraintMap(table *tableInfo) map[string]plsql.IRelational_propertyContext {- constraintMap := make(map[string]plsql.IRelational_propertyContext)- if table.createTable.Relational_table() == nil {- return constraintMap- }- for _, item := range table.createTable.Relational_table().AllRelational_property() {- id := diff.getConstraintID(item)- if id != "" {- constraintMap[id] = item- }- }- return constraintMap-}--func (diff *diffNode) getConstraintID(ctx plsql.IRelational_propertyContext) string {- if diff.strictMode {- switch {- case ctx.Out_of_line_constraint() != nil:- constraint := ctx.Out_of_line_constraint()- if constraint.Constraint_name() == nil {- return ""- }- _, constraintName := NormalizeConstraintName(constraint.Constraint_name())- return strings.TrimSpace(constraintName)- case ctx.Out_of_line_ref_constraint() != nil:- constraint := ctx.Out_of_line_ref_constraint()- if constraint.Constraint_name() == nil {- return ""- }- _, constraintName := NormalizeConstraintName(constraint.Constraint_name())- return strings.TrimSpace(constraintName)- default:- return ""- }- } else if ctx.Out_of_line_constraint() != nil || ctx.Out_of_line_ref_constraint() != nil {- return strings.TrimSpace(EraseString(EraseContext{- eraseConstraintName: true,- eraseSchemaName: true,- eraseIndexName: true,- }, ctx, ctx.GetParser().GetTokenStream()))- }- return ""-}--func (diff *diffNode) diffConstraint(oldTable, newTable *tableInfo) error {- if newTable.createTable.Relational_table() == nil {- // TODO: support object_table and xmltype_table- return nil- }-- var addConstraints []plsql.IRelational_propertyContext- var dropConstraints []string- oldConstraintMap := diff.buildConstraintMap(oldTable)- for _, item := range newTable.createTable.Relational_table().AllRelational_property() {- switch {- case item.Out_of_line_constraint() != nil:- id := diff.getConstraintID(item)- if id == "" {- continue- }- oldConstraint, ok := oldConstraintMap[id]- if !ok {- addConstraints = append(addConstraints, item)- continue- }- // Compare the constraint definition.- if diff.strictMode && !isConstraintEqual(oldConstraint, item) {- addConstraints = append(addConstraints, item)- dropConstraints = append(dropConstraints, id)- }- delete(oldConstraintMap, id)- case item.Out_of_line_ref_constraint() != nil:- id := diff.getConstraintID(item)- oldConstraint, ok := oldConstraintMap[id]- if !ok {- addConstraints = append(addConstraints, item)- continue- }- // Compare the constraint definition.- if diff.strictMode && !isConstraintEqual(oldConstraint, item) {- addConstraints = append(addConstraints, item)- dropConstraints = append(dropConstraints, id)- }- delete(oldConstraintMap, id)- default:- // Ignore other relational properties- }- }- for _, item := range oldConstraintMap {- constraintName := getConstraintName(item)- if constraintName == "" {- continue- }- dropConstraints = append(dropConstraints, constraintName)- }-- slices.Sort(dropConstraints)-- return diff.appendConstraintDiff(newTable.name, addConstraints, dropConstraints)-}--func getConstraintName(ctx plsql.IRelational_propertyContext) string {- switch {- case ctx.Out_of_line_constraint() != nil:- constraint := ctx.Out_of_line_constraint()- if constraint.Constraint_name() == nil {- return ""- }- _, constraintName := NormalizeConstraintName(constraint.Constraint_name())- return constraintName- case ctx.Out_of_line_ref_constraint() != nil:- constraint := ctx.Out_of_line_ref_constraint()- if constraint.Constraint_name() == nil {- return ""- }- _, constraintName := NormalizeConstraintName(constraint.Constraint_name())- return constraintName- default:- return ""- }-}--func (diff *diffNode) appendConstraintDiff(tableName string, addConstraints []plsql.IRelational_propertyContext, dropConstraints []string) error {- for _, constraint := range addConstraints {- if err := diff.appendAddConstraint(tableName, constraint); err != nil {- return err- }- }- for _, constraint := range dropConstraints {- if err := diff.appendDropConstraint(tableName, constraint); err != nil {- return err- }- }- return nil-}--func (diff *diffNode) appendDropConstraint(tableName string, constraint string) error {- var buf strings.Builder-- if _, err := buf.WriteString(`ALTER TABLE "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(tableName); err != nil {- return err- }- if _, err := buf.WriteString(`" DROP CONSTRAINT "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(constraint); err != nil {- return err- }- if _, err := buf.WriteString("\";"); err != nil {- return err- }- diff.dropConstraint = append(diff.dropConstraint, buf.String())- return nil-}--func (diff *diffNode) appendAddConstraint(tableName string, constraint plsql.IRelational_propertyContext) error {- var buf strings.Builder-- if _, err := buf.WriteString(`ALTER TABLE "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(tableName); err != nil {- return err- }- if _, err := buf.WriteString(`" ADD `); err != nil {- return err- }- if _, err := buf.WriteString(constraint.GetParser().GetTokenStream().GetTextFromRuleContext(constraint)); err != nil {- return err- }- if _, err := buf.WriteString(";"); err != nil {- return err- }- diff.addConstraint = append(diff.addConstraint, buf.String())- return nil-}--func isConstraintEqual(oldConstraint, newConstraint plsql.IRelational_propertyContext) bool {- // TODO: compare constraint definition instead of text.- oldString := oldConstraint.GetParser().GetTokenStream().GetTextFromRuleContext(oldConstraint)- newString := newConstraint.GetParser().GetTokenStream().GetTextFromRuleContext(newConstraint)- return oldString == newString-}--func buildColumnMap(table *tableInfo) map[string]plsql.IColumn_definitionContext {- columnMap := make(map[string]plsql.IColumn_definitionContext)- if table.createTable.Relational_table() == nil {- return columnMap- }- for _, item := range table.createTable.Relational_table().AllRelational_property() {- if item.Column_definition() == nil {- continue- }- columnName := NormalizeIdentifierContext(item.Column_definition().Column_name().Identifier())- columnMap[columnName] = item.Column_definition()- }- return columnMap-}--func (diff *diffNode) diffColumn(oldTable, newTable *tableInfo) error {- if newTable.createTable.Relational_table() == nil {- // TODO: support object_table and xmltype_table- return nil- }-- var addColumns []plsql.IColumn_definitionContext- var modifyColumns []plsql.IColumn_definitionContext- var modifyColumnVisibility []plsql.IColumn_definitionContext- var dropColumns []string- oldColumnMap := buildColumnMap(oldTable)- for _, item := range newTable.createTable.Relational_table().AllRelational_property() {- if item.Column_definition() == nil {- continue- }-- newColumnName := NormalizeIdentifierContext(item.Column_definition().Column_name().Identifier())- oldColumn, ok := oldColumnMap[newColumnName]- if !ok {- addColumns = append(addColumns, item.Column_definition())- continue- }-- // Compare the column definition.- if !isColumnEqual(oldColumn, item.Column_definition()) {- modifyColumns = append(modifyColumns, item.Column_definition())- }- if !isColumnVisibilityEqual(oldColumn, item.Column_definition()) {- modifyColumnVisibility = append(modifyColumnVisibility, item.Column_definition())- }- delete(oldColumnMap, newColumnName)- }- for _, column := range oldColumnMap {- dropColumns = append(dropColumns, NormalizeIdentifierContext(column.Column_name().Identifier()))- }-- slices.Sort(dropColumns)-- return diff.appendColumnDiff(oldTable.name, addColumns, modifyColumns, modifyColumnVisibility, dropColumns)-}--func (diff *diffNode) appendColumnDiff(tableName string, addColumns, modifyColumns, modifyColumnVisibility []plsql.IColumn_definitionContext, dropColumns []string) error {- if len(addColumns) != 0 {- if err := diff.appendAddColumn(tableName, addColumns); err != nil {- return err- }- }- if len(modifyColumns) != 0 {- if err := diff.appendModifyColumn(tableName, modifyColumns); err != nil {- return err- }- }- if len(modifyColumnVisibility) != 0 {- if err := diff.appendModifyColumnVisibility(tableName, modifyColumnVisibility); err != nil {- return err- }- }- if len(dropColumns) != 0 {- if err := diff.appendDropColumn(tableName, dropColumns); err != nil {- return err- }- }- return nil-}--func (diff *diffNode) appendDropColumn(tableName string, dropColumns []string) error {- var buf strings.Builder-- if _, err := buf.WriteString(`ALTER TABLE "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(tableName); err != nil {- return err- }- if _, err := buf.WriteString(`" DROP (`); err != nil {- return err- }- for i, column := range dropColumns {- if i != 0 {- if _, err := buf.WriteString(`,`); err != nil {- return err- }- }- if _, err := buf.WriteString("\n "); err != nil {- return err- }- if _, err := buf.WriteString(column); err != nil {- return err- }- }- if _, err := buf.WriteString("\n);"); err != nil {- return err- }- diff.dropColumn = append(diff.dropColumn, buf.String())- return nil-}--func (diff *diffNode) appendModifyColumnVisibility(tableName string, modifyColumnVisibility []plsql.IColumn_definitionContext) error {- var buf strings.Builder-- if _, err := buf.WriteString(`ALTER TABLE "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(tableName); err != nil {- return err- }- if _, err := buf.WriteString(`" MODIFY (`); err != nil {- return err- }- for i, column := range modifyColumnVisibility {- if i != 0 {- if _, err := buf.WriteString(`,`); err != nil {- return err- }- }- if _, err := buf.WriteString("\n "); err != nil {- return err- }- if _, err := buf.WriteString(column.GetParser().GetTokenStream().GetTextFromRuleContext(column.Column_name())); err != nil {- return err- }- columnVisibility := " VISIBLE"- if column.INVISIBLE() != nil {- columnVisibility = " INVISIBLE"- }- if _, err := buf.WriteString(columnVisibility); err != nil {- return err- }- }- if _, err := buf.WriteString("\n);"); err != nil {- return err- }- diff.modifyColumnVisibility = append(diff.modifyColumnVisibility, buf.String())- return nil-}--func (diff *diffNode) appendModifyColumn(tableName string, modifyColumns []plsql.IColumn_definitionContext) error {- var buf strings.Builder-- if _, err := buf.WriteString(`ALTER TABLE "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(tableName); err != nil {- return err- }- if _, err := buf.WriteString(`" MODIFY (`); err != nil {- return err- }- for i, column := range modifyColumns {- if i != 0 {- if _, err := buf.WriteString(`,`); err != nil {- return err- }- }- if _, err := buf.WriteString("\n "); err != nil {- return err- }- if _, err := buf.WriteString(convertToModifyColumn(column)); err != nil {- return err- }- }- if _, err := buf.WriteString("\n);"); err != nil {- return err- }- diff.modifyColumn = append(diff.modifyColumn, buf.String())- return nil-}--func convertToModifyColumn(column plsql.IColumn_definitionContext) string {- var results []string- results = append(results, column.GetParser().GetTokenStream().GetTextFromRuleContext(column.Column_name()))- if column.Datatype() != nil {- results = append(results, column.GetParser().GetTokenStream().GetTextFromRuleContext(column.Datatype()))- }-- if column.COLLATE() != nil {- results = append(results, column.GetParser().GetTokenStream().GetTextFromTokens(- column.COLLATE().GetSymbol(),- column.Column_collation_name().GetStop(),- ))- }-- if column.DEFAULT() != nil && column.Expression() != nil {- results = append(results, "DEFAULT")- if column.NULL_() != nil {- results = append(results, "NO NULL")- }- results = append(results, column.GetParser().GetTokenStream().GetTextFromRuleContext(column.Expression()))- }-- for _, item := range column.AllInline_constraint() {- if item.NULL_() != nil {- if item.NOT() != nil {- results = append(results, "NOT NULL")- } else {- results = append(results, "NULL")- }- }- }-- return strings.Join(results, " ")-}--func (diff *diffNode) appendAddColumn(tableName string, addColumns []plsql.IColumn_definitionContext) error {- var buf strings.Builder-- if _, err := buf.WriteString(`ALTER TABLE "`); err != nil {- return err- }- if diff.strictMode {- if _, err := buf.WriteString(diff.schemaName); err != nil {- return err- }- if _, err := buf.WriteString(`"."`); err != nil {- return err- }- }- if _, err := buf.WriteString(tableName); err != nil {- return err- }- if _, err := buf.WriteString(`" ADD (`); err != nil {- return err- }- for i, column := range addColumns {- if i != 0 {- if _, err := buf.WriteString(`,`); err != nil {- return err- }- }- if _, err := buf.WriteString("\n "); err != nil {- return err- }- if _, err := buf.WriteString(column.GetParser().GetTokenStream().GetTextFromRuleContext(column)); err != nil {- return err- }- }- if _, err := buf.WriteString("\n);"); err != nil {- return err- }- diff.addColumn = append(diff.addColumn, buf.String())- return nil-}--func isColumnVisibilityEqual(oldColumn, newColumn plsql.IColumn_definitionContext) bool {- oldVisible := true- newVisible := true- if oldColumn.INVISIBLE() != nil {- oldVisible = false- }- if newColumn.INVISIBLE() != nil {- newVisible = false- }- return oldVisible == newVisible-}--func isColumnEqual(oldColumn, newColumn plsql.IColumn_definitionContext) bool {- // TODO: compare column definition instead of text.- // TODO: ignore visible now.- oldString := getColumnCompareString(oldColumn)- newString := getColumnCompareString(newColumn)- return oldString == newString-}--func getColumnCompareString(column plsql.IColumn_definitionContext) string {- var results []string- if column.Datatype() != nil {- results = append(results, column.GetParser().GetTokenStream().GetTextFromRuleContext(column.Datatype()))- }-- if column.COLLATE() != nil {- results = append(results, column.GetParser().GetTokenStream().GetTextFromTokens(- column.COLLATE().GetSymbol(),- column.Column_collation_name().GetStop(),- ))- }-- if column.DEFAULT() != nil {- if column.Expression() != nil {- results = append(results, column.GetParser().GetTokenStream().GetTextFromTokens(- column.DEFAULT().GetSymbol(),- column.Expression().GetStop(),- ))- } else if column.Identity_clause() != nil {- results = append(results, column.GetParser().GetTokenStream().GetTextFromTokens(- column.DEFAULT().GetSymbol(),- column.Identity_clause().GetStop(),- ))- }- }-- for _, item := range column.AllInline_constraint() {- if item.NULL_() != nil {- if item.NOT() != nil {- results = append(results, "NOT NULL")- } else {- results = append(results, "NULL")- }- }- }-- return strings.Join(results, " ")-}--func buildSchemaInfo(statement string, strictMode bool) (*schemaInfo, error) {- node, _, err := ParsePLSQL(statement)- if err != nil {- return nil, err- }-- listener := &buildSchemaInfoListener{- strictMode: strictMode,- schemaInfo: &schemaInfo{- name: "",- tableMap: make(tableMap),- indexMap: make(indexMap),- },- }- antlr.ParseTreeWalkerDefault.Walk(listener, node)- if listener.err != nil {- if strings.Contains(listener.err.Error(), "schema name mismatch") {- return nil, errors.Wrapf(listener.err, "Oracle sync schema only supports single schema, please use \"Manage based on schema\" sync mode for this operation")- }- return nil, listener.err- }- return listener.schemaInfo, nil-}--type buildSchemaInfoListener struct {- *plsql.BasePlSqlParserListener-- strictMode bool- schemaInfo *schemaInfo- err error-}--// EnterCreate_table is called when production create_table is entered.-func (l *buildSchemaInfoListener) EnterCreate_table(ctx *plsql.Create_tableContext) {- if l.err != nil {- return- }-- if ctx.Schema_name() != nil {- schemaName := NormalizeIdentifierContext(ctx.Schema_name().Identifier())- if l.schemaInfo.name == "" {- l.schemaInfo.name = schemaName- }- if schemaName != l.schemaInfo.name {- l.err = errors.Errorf("schema name mismatch: %s != %s", schemaName, l.schemaInfo.name)- return- }- }- tableName := NormalizeIdentifierContext(ctx.Table_name().Identifier())- l.schemaInfo.tableMap[tableName] = &tableInfo{- id: len(l.schemaInfo.tableMap),- name: tableName,- existsInNew: false,- createTable: ctx,- }-}--// EnterCreate_index is called when production create_index is entered.-func (l *buildSchemaInfoListener) EnterCreate_index(ctx *plsql.Create_indexContext) {- if l.err != nil {- return- }-- if _, ok := ctx.GetParent().(*plsql.Unit_statementContext); !ok {- // Skip index creation inside other statements.- return- }-- id := getIndexID(ctx, l.strictMode)- schema, index := NormalizeIndexName(ctx.Index_name())- if schema != "" && l.schemaInfo.name == "" {- l.schemaInfo.name = schema- }- if schema != "" && schema != l.schemaInfo.name {- l.err = errors.Errorf("schema name mismatch: %s != %s", schema, l.schemaInfo.name)- return- }- l.schemaInfo.indexMap[id] = &indexInfo{- pos: len(l.schemaInfo.indexMap),- id: id,- name: index,- existsInNew: false,- createIndex: ctx,- }-}--func getIndexID(ctx plsql.ICreate_indexContext, strictMode bool) string {- if strictMode {- _, indexName := NormalizeIndexName(ctx.Index_name())- return strings.TrimSpace(indexName)- }- return strings.TrimSpace(EraseString(EraseContext{- eraseIndexName: true,- eraseSchemaName: true,- eraseConstraintName: true,- eraseStoreOption: true,- }, ctx, ctx.GetParser().GetTokenStream()))-}--type tableMap map[string]*tableInfo-type indexMap map[string]*indexInfo--type schemaInfo struct {- name string- tableMap tableMap- indexMap indexMap-}--type tableInfo struct {- id int- name string- existsInNew bool- createTable plsql.ICreate_tableContext-}--type indexInfo struct {- pos int- id string- name string- existsInNew bool- createIndex plsql.ICreate_indexContext-}diff --git a/backend/plugin/parser/plsql/differ_test.go b/backend/plugin/parser/plsql/differ_test.godeleted file mode 100644index 2a222ffad4..0000000000--- a/backend/plugin/parser/plsql/differ_test.go+++ /dev/null@@ -1,73 +0,0 @@-// Package plsql provides the plsql parser plugin.-package plsql--import (- "io"- "os"- "path/filepath"- "testing"-- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type DifferTestData struct {- OldSchema string `yaml:"oldSchema"`- NewSchema string `yaml:"newSchema"`- Diff string `yaml:"diff"`-}--func runDifferTest(t *testing.T, file string, record bool, strict bool) {- var tests []DifferTestData- filepath := filepath.Join("test-data", file)- yamlFile, err := os.Open(filepath)- require.NoError(t, err)- defer yamlFile.Close()-- byteValue, err := io.ReadAll(yamlFile)- require.NoError(t, err)- err = yaml.Unmarshal(byteValue, &tests)- require.NoError(t, err)-- for i, test := range tests {- diff, err := SchemaDiff(base.DiffContext{- IgnoreCaseSensitive: false,- StrictMode: strict,- }, test.OldSchema, test.NewSchema)- require.NoError(t, err)- if record {- tests[i].Diff = diff- } else {- require.Equal(t, test.Diff, diff, test.OldSchema)- }- }-- if record {- err := yamlFile.Close()- require.NoError(t, err)- byteValue, err = yaml.Marshal(tests)- require.NoError(t, err)- err = os.WriteFile(filepath, byteValue, 0644)- require.NoError(t, err)- }-}--func TestPLSQLDiffer(t *testing.T) {- testFileList := []string{- "test_differ_data.yaml",- }- for _, file := range testFileList {- runDifferTest(t, file, false /* record */, true /* strict */)- }-}--func TestPlSQLDifferNonStrict(t *testing.T) {- testFileList := []string{- "test_differ_non_strict.yaml",- }- for _, file := range testFileList {- runDifferTest(t, file, false /* record */, false /* strict */)- }-}diff --git a/backend/plugin/parser/tidb/differ.go b/backend/plugin/parser/tidb/differ.godeleted file mode 100644index ce01766c0a..0000000000--- a/backend/plugin/parser/tidb/differ.go+++ /dev/null@@ -1,2200 +0,0 @@-package tidb--import (- "bytes"- "fmt"- "io"- "log/slog"- "reflect"- "slices"- "strconv"- "strings"-- "github.com/antlr4-go/antlr/v4"-- tidbparser "github.com/bytebase/parser/tidb"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/log"-- "github.com/pingcap/tidb/pkg/parser"- "github.com/pingcap/tidb/pkg/parser/ast"- "github.com/pingcap/tidb/pkg/parser/format"- "github.com/pingcap/tidb/pkg/parser/mysql"- "github.com/pingcap/tidb/pkg/types"- "github.com/pkg/errors"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-- // Register pingcap parser driver.- driver "github.com/pingcap/tidb/pkg/types/parser_driver"-)--const (- maxErrorStatementSize = 1 << 8 // 256-)--func init() {- base.RegisterSchemaDiffFunc(storepb.Engine_TIDB, SchemaDiff)-}--// diffNode defines different modification types as the safe change order.-// The safe change order means we can change them with no dependency conflicts as this order.-type diffNode struct {- // Ignore the case sensitive when comparing the table and view names.- ignoreCaseSensitive bool-- dropForeignKeyList []ast.Node- dropConstraintExceptFkList []ast.Node- dropIndexList []ast.Node- dropViewList []ast.Node- dropTableList []ast.Node-- createTableList []ast.Node- alterTableOptionList []ast.Node- addAndModifyColumnList []ast.Node- dropColumnList []ast.Node- createTempViewList []ast.Node- createIndexList []ast.Node- addConstraintExceptFkList []ast.Node- addForeignKeyList []ast.Node- createViewList []ast.Node- alterTablePartitionedByList map[string]*partitionState-}--func (diff *diffNode) diffSupportedStatement(oldStatement, newStatement string) error {- oldNodeList, _, err := parser.New().Parse(oldStatement, "", "")- if err != nil {- return errors.Wrapf(err, "failed to parse old statement %q", oldStatement)- }- newNodeList, _, err := parser.New().Parse(newStatement, "", "")- if err != nil {- sqlForComment, truncated := common.TruncateString(newStatement, maxErrorStatementSize)- if truncated {- sqlForComment += "..."- }- return errors.Wrapf(err, "failed to parse new statement %q", sqlForComment)- }-- oldSchemaInfo, err := diff.buildSchemaInfo(oldNodeList)- if err != nil {- return err- }- newSchemaInfo, err := diff.buildSchemaInfo(newNodeList)- if err != nil {- return err- }-- for tableName, newTable := range newSchemaInfo.tableMap {- oldTable, exists := oldSchemaInfo.tableMap[tableName]- if !exists {- newTable.createTable.IfNotExists = true- diff.createTableList = append(diff.createTableList, newTable.createTable)- // Create indexes.- for _, index := range newTable.indexMap {- diff.createIndexList = append(diff.createIndexList, index.createIndex)- }- continue- }- diff.diffTable(oldTable, newTable)- delete(oldSchemaInfo.tableMap, tableName)- }-- for _, oldTable := range oldSchemaInfo.tableMap {- diff.dropTableList = append(diff.dropTableList, &ast.DropTableStmt{- IfExists: true,- Tables: []*ast.TableName{oldTable.createTable.Table},- })- }-- var newViewList []*ast.CreateViewStmt- for _, newNode := range newNodeList {- if newView, ok := newNode.(*ast.CreateViewStmt); ok {- newViewList = append(newViewList, newView)- }- }-- if err := diff.diffView(oldSchemaInfo.viewMap, newSchemaInfo.viewMap, newViewList); err != nil {- return errors.Wrapf(err, "failed to diff view")- }-- return nil-}--func (diff *diffNode) diffView(oldViewMap viewMap, newViewMap viewMap, newViewList []*ast.CreateViewStmt) error {- var tempViewList []ast.Node- var viewList []ast.Node- for _, view := range newViewList {- viewName := view.ViewName.Name.O- if diff.ignoreCaseSensitive {- viewName = view.ViewName.Name.L- }- if newNode, ok := newViewMap[viewName]; ok {- if !diff.isViewEqual(view, newNode) {- // Skip predefined view such as the temporary view from mysqldump.- continue- }- }- oldNode, ok := oldViewMap[viewName]- if ok {- if !diff.isViewEqual(view, oldNode) {- createViewStmt := *view- createViewStmt.OrReplace = true- viewList = append(viewList, &createViewStmt)- }- // We should delete the view in the oldViewMap, because we will drop the all views in the oldViewMap explicitly at last.- delete(oldViewMap, viewName)- } else {- // We should create the view.- // We create the temporary view first and replace it to avoid break the dependency like mysqldump does.- tempViewStmt, err := getTempView(view)- if err != nil {- return errors.Wrapf(err, "failed to get temporary view for view %s", view.ViewName.Name.O)- }- tempViewList = append(tempViewList, tempViewStmt)- createViewStmt := *view- createViewStmt.OrReplace = true- viewList = append(viewList, &createViewStmt)- }- }- diff.createTempViewList = append(diff.createTempViewList, tempViewList...)- diff.createViewList = append(diff.createViewList, viewList...)-- // Remove the remaining views in the oldViewMap.- dropViewStmt := &ast.DropTableStmt{- IsView: true,- }- for _, oldView := range oldViewMap {- dropViewStmt.Tables = append(dropViewStmt.Tables, oldView.ViewName)- }- if len(dropViewStmt.Tables) > 0 {- diff.dropViewList = append(diff.dropViewList, dropViewStmt)- }- return nil-}--func (diff *diffNode) diffTable(oldTable, newTable *tableInfo) {- diff.diffTableOption(oldTable, newTable)- diff.diffColumn(oldTable, newTable)- diff.diffIndex(oldTable, newTable)- diff.diffConstraint(oldTable, newTable)- diff.diffPartition(oldTable, newTable)-}--func (diff *diffNode) diffPartition(oldTable, newTable *tableInfo) {- // The situations we supported:- // 1. Turn a table partitioned.- // 2. Add a partition.- // 3. Delete a partition.- // 4. Create a new partition table.-- // Skip for some unsupported cases.- oldHasPartition := oldTable.partition != nil && oldTable.partition.info.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED- newHasPartition := newTable.partition != nil && newTable.partition.info.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED- // Turn a partitioned table un-partitioned.- if oldHasPartition && !newHasPartition {- return- }-- // TODO(zp): We use the ALTER TABLE PARTITIONED BY statement to delegate migration calculations to the MySQL server,- // avoiding the need for manual interventions like ALTER TABLE REORGANIZE PARTITION or ALTER TABLE ADD PARTITION.- // Are there any drawbacks to this approach?- oldPartitions := oldTable.partition- newPartitions := newTable.partition- equal := reflect.DeepEqual(oldPartitions, newPartitions)- if !equal {- diff.alterTablePartitionedByList[newTable.createTable.Table.Name.O] = newPartitions- return- }-}--func (diff *diffNode) diffConstraint(oldTable, newTable *tableInfo) {- oldPrimaryKey, oldConstraintMap := buildConstraintMap(oldTable.createTable)- // Compare the create definitions.- for _, constraint := range newTable.createTable.Constraints {- switch constraint.Tp {- case ast.ConstraintPrimaryKey:- if oldPrimaryKey != nil {- if !isPrimaryKeyEqual(constraint, oldPrimaryKey) {- diff.dropConstraintExceptFkList = append(diff.dropConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropPrimaryKey,- },- },- })- diff.addConstraintExceptFkList = append(diff.addConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableAddConstraint,- Constraint: constraint,- },- },- })- }- oldPrimaryKey = nil- continue- }- diff.addConstraintExceptFkList = append(diff.addConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableAddConstraint,- Constraint: constraint,- },- },- })- case ast.ConstraintForeignKey:- if oldConstraint, ok := oldConstraintMap[strings.ToLower(constraint.Name)]; ok {- if !isForeignKeyConstraintEqual(constraint, oldConstraint) {- diff.dropForeignKeyList = append(diff.dropForeignKeyList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropForeignKey,- Name: constraint.Name,- },- },- })- diff.addForeignKeyList = append(diff.addForeignKeyList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableAddConstraint,- Constraint: constraint,- },- },- })- }- delete(oldConstraintMap, strings.ToLower(constraint.Name))- continue- }- diff.addForeignKeyList = append(diff.addForeignKeyList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableAddConstraint,- Constraint: constraint,- },- },- })- case ast.ConstraintCheck:- if oldConstraint, ok := oldConstraintMap[strings.ToLower(constraint.Name)]; ok {- if !isCheckConstraintEqual(constraint, oldConstraint) {- diff.dropConstraintExceptFkList = append(diff.dropConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropCheck,- Constraint: constraint,- },- },- })- diff.addConstraintExceptFkList = append(diff.addConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableAddConstraint,- Constraint: constraint,- },- },- })- }- delete(oldConstraintMap, strings.ToLower(constraint.Name))- continue- }- diff.addConstraintExceptFkList = append(diff.addConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableAddConstraint,- Constraint: constraint,- },- },- })- default:- // Skip other constraint types (e.g., key, index, unique)- // These are handled separately by diffIndex- }- }-- if oldPrimaryKey != nil {- diff.dropConstraintExceptFkList = append(diff.dropConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropPrimaryKey,- },- },- })- }-- for _, oldConstraint := range oldConstraintMap {- switch oldConstraint.Tp {- case ast.ConstraintCheck:- diff.dropConstraintExceptFkList = append(diff.dropConstraintExceptFkList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropCheck,- Constraint: oldConstraint,- },- },- })- case ast.ConstraintForeignKey:- diff.dropForeignKeyList = append(diff.dropForeignKeyList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropForeignKey,- Name: oldConstraint.Name,- },- },- })- default:- // Skip other constraint types- }- }-}--func (diff *diffNode) diffIndex(oldTable, newTable *tableInfo) {- for indexName, newIndex := range newTable.indexMap {- if oldIndex, ok := oldTable.indexMap[indexName]; ok {- if !isIndexEqual(newIndex.createIndex, oldIndex.createIndex) {- diff.dropIndexList = append(diff.dropIndexList, &ast.DropIndexStmt{- IndexName: oldIndex.createIndex.IndexName,- Table: oldIndex.createIndex.Table,- })- diff.createIndexList = append(diff.createIndexList, newIndex.createIndex)- }- delete(oldTable.indexMap, indexName)- continue- }- diff.createIndexList = append(diff.createIndexList, newIndex.createIndex)- }-- for _, oldIndex := range oldTable.indexMap {- diff.dropIndexList = append(diff.dropIndexList, &ast.DropIndexStmt{- IndexName: oldIndex.createIndex.IndexName,- Table: oldIndex.createIndex.Table,- })- }-}--func (diff *diffNode) diffColumn(oldTable, newTable *tableInfo) {- // We use a single ALTER TABLE statement to add and modify columns,- // because we need to maintain a fixed order of these two operations.- // This approach ensures that we can manipulate the column position as needed.- oldColumnMap := buildColumnMap(oldTable.createTable.Cols)- oldColumnPositionMap := buildColumnPositionMap(oldTable.createTable)- for idx, columnDef := range newTable.createTable.Cols {- // Column names are always case insensitive.- newColumnName := columnDef.Name.Name.L- oldColumnDef, ok := oldColumnMap[newColumnName]- if !ok {- columnPosition := &ast.ColumnPosition{Tp: ast.ColumnPositionFirst}- if idx >= 1 {- columnPosition.Tp = ast.ColumnPositionAfter- columnPosition.RelativeColumn = &ast.ColumnName{Name: ast.NewCIStr(newTable.createTable.Cols[idx-1].Name.Name.O)}- }- addAndModifyColumnStatement := &ast.AlterTableStmt{Table: newTable.createTable.Table}- addAndModifyColumnStatement.Specs = append(addAndModifyColumnStatement.Specs, &ast.AlterTableSpec{- Tp: ast.AlterTableAddColumns,- NewColumns: []*ast.ColumnDef{columnDef},- Position: columnPosition,- })- diff.addAndModifyColumnList = append(diff.addAndModifyColumnList, addAndModifyColumnStatement)- continue- }-- // Compare the column positions.- columnPosition := &ast.ColumnPosition{Tp: ast.ColumnPositionNone}- columnPosInOld := oldColumnPositionMap[newColumnName]- if hasColumnsIntersection(oldTable.createTable.Cols[:columnPosInOld], newTable.createTable.Cols[idx+1:]) {- if idx == 0 {- columnPosition.Tp = ast.ColumnPositionFirst- } else {- columnPosition.Tp = ast.ColumnPositionAfter- columnPosition.RelativeColumn = &ast.ColumnName{Name: ast.NewCIStr(newTable.createTable.Cols[idx-1].Name.Name.O)}- }- }- // Compare the column definitions.- if !isColumnEqual(oldColumnDef, columnDef) || columnPosition.Tp != ast.ColumnPositionNone {- addAndModifyColumnStatement := &ast.AlterTableStmt{Table: newTable.createTable.Table}- addAndModifyColumnStatement.Specs = append(addAndModifyColumnStatement.Specs, &ast.AlterTableSpec{- Tp: ast.AlterTableModifyColumn,- NewColumns: []*ast.ColumnDef{columnDef},- Position: columnPosition,- })- diff.addAndModifyColumnList = append(diff.addAndModifyColumnList, addAndModifyColumnStatement)- }- delete(oldColumnMap, newColumnName)- }- // TODO(zp): add an option to control whether to drop the excess columns.- for _, columnDef := range oldColumnMap {- diff.dropColumnList = append(diff.dropColumnList, &ast.AlterTableStmt{- Table: newTable.createTable.Table,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableDropColumn,- OldColumnName: &ast.ColumnName{- Name: ast.NewCIStr(columnDef.Name.Name.O),- },- },- },- })- }-}--func (diff *diffNode) diffTableOption(oldTable, newTable *tableInfo) {- if alterTableOptionStmt := diffTableOptions(newTable.createTable.Table, oldTable.createTable.Options, newTable.createTable.Options); alterTableOptionStmt != nil {- diff.alterTableOptionList = append(diff.alterTableOptionList, alterTableOptionStmt)- }-}--func (diff *diffNode) deparse() (string, error) {- var buf bytes.Buffer- if err := sortAndWriteNodeList(&buf, diff.dropForeignKeyList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.dropConstraintExceptFkList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.dropIndexList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.dropViewList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.dropTableList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.createTableList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.alterTableOptionList); err != nil {- return "", err- }- if err := writeNodeList(&buf, diff.addAndModifyColumnList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.dropColumnList); err != nil {- return "", err- }- if err := sortAndWriteAlterTablePartitionedByList(&buf, diff.alterTablePartitionedByList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.createTempViewList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.createIndexList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.addConstraintExceptFkList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.addForeignKeyList); err != nil {- return "", err- }- if err := sortAndWriteNodeList(&buf, diff.createViewList); err != nil {- return "", err- }-- return buf.String(), nil-}--func sortAndWriteAlterTablePartitionedByList(buf io.Writer, partitions map[string]*partitionState) error {- // Sort by table name.- tableNames := make([]string, 0, len(partitions))- for tableName := range partitions {- tableNames = append(tableNames, tableName)- }- slices.Sort(tableNames)- for _, tableName := range tableNames {- var body strings.Builder- if _, err := body.WriteString(fmt.Sprintf("ALTER TABLE `%s` PARTITION BY ", tableName)); err != nil {- return err- }- if err := partitions[tableName].toString(&body); err != nil {- return err- }- if _, err := buf.Write([]byte(body.String())); err != nil {- return err- }- if _, err := buf.Write([]byte(";\n")); err != nil {- return err- }- }-- return nil-}--// Copy from backend/plugin/schema/mysql/state.go.-func (p *partitionState) toString(buf io.StringWriter) error {- switch p.info.tp {- case storepb.TablePartitionMetadata_RANGE, storepb.TablePartitionMetadata_RANGE_COLUMNS:- if _, err := buf.WriteString("RANGE "); err != nil {- return err- }- if p.info.tp == storepb.TablePartitionMetadata_RANGE {- fields := splitPartitionExprIntoFields(p.info.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- } else {- // I think MySQL need to write "COLUMNS " instead of " COLUMNS" here...- if _, err := buf.WriteString(" COLUMNS"); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.info.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- }- case storepb.TablePartitionMetadata_LIST, storepb.TablePartitionMetadata_LIST_COLUMNS:- if _, err := buf.WriteString("LIST "); err != nil {- return err- }- if p.info.tp == storepb.TablePartitionMetadata_LIST {- fields := splitPartitionExprIntoFields(p.info.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- } else {- // I think MySQL need to write "COLUMNS " instead of " COLUMNS" here...- if _, err := buf.WriteString(" COLUMNS"); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.info.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- }- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_LINEAR_KEY:- if p.info.tp == storepb.TablePartitionMetadata_LINEAR_HASH || p.info.tp == storepb.TablePartitionMetadata_LINEAR_KEY {- if _, err := buf.WriteString("LINEAR "); err != nil {- return err- }- }- if p.info.tp == storepb.TablePartitionMetadata_KEY || p.info.tp == storepb.TablePartitionMetadata_LINEAR_KEY {- if _, err := buf.WriteString("KEY "); err != nil {- return err- }- // NOTE: MySQL supports an ALGORITHM option with [SUB]PARTITION BY [LINEAR KEY]. ALGORITHM=1 causes the server to use the same key-hashing function as MYSQL 5.1, and ALGORITHM=1 is the only possible output in- // the following code. Sadly, I do not know how to get the key_algorithm from the INFORMATION_SCHEMA, AND 5.1 IS TOO LEGACY TO SUPPORT! So use the original one.- /*- current_comment_start is given when called from SHOW CREATE TABLE,- Then only add ALGORITHM = 1, not the default 2 or non-set 0!- For .frm current_comment_start is NULL, then add ALGORITHM if != 0.- */- // if (part_info->key_algorithm ==- // enum_key_algorithm::KEY_ALGORITHM_51 || // SHOW- // (!current_comment_start && // .frm- // (part_info->key_algorithm != enum_key_algorithm::KEY_ALGORITHM_NONE))) {- // /* If we already are within a comment, end that comment first. */- // if (current_comment_start) err += add_string(fptr, "*/ ");- // err += add_string(fptr, "/*!50611 ");- // err += add_part_key_word(fptr, partition_keywords[PKW_ALGORITHM].str);- // err += add_equal(fptr);- // err += add_space(fptr);- // err += add_int(fptr, static_cast<longlong>(part_info->key_algorithm));- // err += add_space(fptr);- // err += add_string(fptr, "*/ ");- // if (current_comment_start) {- // /* Skip new line. */- // if (current_comment_start[0] == '\n') current_comment_start++;- // err += add_string(fptr, current_comment_start);- // err += add_space(fptr);- // }- // }-- // HACK(zp): Write the part field list. In the MySQL source code, it calls append_identifier(), which considers the quote character. We should figure out the logic of it later.- // Currently, I just found that if the expr contains more than one field, it would not be quoted by '`'.- // KEY and LINEAR KEY can take the field list.- // While MySQL calls append_field_list() to write the field list, it unmasks the OPTION_QUOTE_SHOW_CREATE flag,- // for us, we do the best effort to split the expr by ',' and trim the leading and trailing '`', and write it to the buffer after joining them with ','.- fields := splitPartitionExprIntoFields(p.info.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- } else {- if _, err := buf.WriteString("HASH "); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.info.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- }- default:- return errors.Errorf("unsupported partition type: %v", p.info.tp)- }-- // NOTE: MySQL writes the default partitions in the following code, which means that the server- // takes the responsibility to generate the partitions. Sadly, we cannot get whether the user- // use this or not in the metadata. So we skip it.- /*- if ((!part_info->use_default_num_partitions) &&- part_info->use_default_partitions) {- err += add_string(fptr, "\n");- err += add_string(fptr, "PARTITIONS ");- err += add_int(fptr, part_info->num_parts);- }- */- if p.info.useDefault != 0 {- if _, err := buf.WriteString(fmt.Sprintf("\nPARTITIONS %d", p.info.useDefault)); err != nil {- return err- }- }-- isSubpartitioned := p.subInfo != nil && p.subInfo.tp != storepb.TablePartitionMetadata_TYPE_UNSPECIFIED- if isSubpartitioned {- if _, err := buf.WriteString("\nSUBPARTITION BY "); err != nil {- return err- }- }- // Subpartition must be hash or key.- if isSubpartitioned {- switch p.subInfo.tp {- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_LINEAR_HASH:- if p.subInfo.tp == storepb.TablePartitionMetadata_LINEAR_HASH {- if _, err := buf.WriteString("LINEAR "); err != nil {- return err- }- }- if _, err := buf.WriteString("HASH "); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.subInfo.expr)- for i, field := range fields {- if !strings.Contains(field, "(") {- fields[i] = fmt.Sprintf("`%s`", field)- }- }- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- case storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_KEY:- if p.subInfo.tp == storepb.TablePartitionMetadata_LINEAR_KEY {- if _, err := buf.WriteString("LINEAR "); err != nil {- return err- }- }- if _, err := buf.WriteString("KEY "); err != nil {- return err- }- fields := splitPartitionExprIntoFields(p.subInfo.expr)- if _, err := buf.WriteString(fmt.Sprintf("(%s)", strings.Join(fields, ","))); err != nil {- return err- }- default:- return errors.Errorf("invalid subpartition type: %v", p.subInfo.tp)- }- }-- // TODO(zp): MySQL writes the default subpartitions in the following code, which means that the server- // takes the responsibility to generate the subpartitions. Sadly, we cannot get whether the user- // use this or not in the metadata. So we skip it.- /*- if ((!part_info->use_default_num_subpartitions) &&- part_info->use_default_subpartitions) {- err += add_string(fptr, "\n");- err += add_string(fptr, "SUBPARTITIONS ");- err += add_int(fptr, part_info->num_subparts);- }- */- if isSubpartitioned && p.subInfo.useDefault != 0 {- if _, err := buf.WriteString(fmt.Sprintf("\nSUBPARTITIONS %d", p.subInfo.useDefault)); err != nil {- return err- }- }-- if p.info.useDefault == 0 {- // Write the partition list.- if len(p.partitions) == 0 {- return errors.New("empty partition list")- }- sortedPartitions := make([]*partitionDefinition, 0, len(p.partitions))- for _, partition := range p.partitions {- sortedPartitions = append(sortedPartitions, partition)- }- slices.SortFunc(sortedPartitions, func(a, b *partitionDefinition) int {- if a.id < b.id {- return -1- }- if a.id > b.id {- return 1- }- return 0- })- if _, err := buf.WriteString("\n("); err != nil {- return err- }- preposition, err := getPrepositionByType(p.info.tp)- if err != nil {- return err- }- for i, partition := range sortedPartitions {- if i != 0 {- if _, err := buf.WriteString(",\n "); err != nil {- return err- }- }- if _, err := buf.WriteString(fmt.Sprintf("PARTITION %s", partition.name)); err != nil {- return err- }- if preposition != "" {- if partition.value != "MAXVALUE" {- if _, err := buf.WriteString(fmt.Sprintf(" VALUES %s (%s)", preposition, partition.value)); err != nil {- return err- }- } else {- if _, err := buf.WriteString(fmt.Sprintf(" VALUES %s %s", preposition, partition.value)); err != nil {- return err- }- }- }-- if isSubpartitioned && p.subInfo.useDefault == 0 {- if len(partition.subpartitions) == 0 {- return errors.New("empty subpartition list")- }- if _, err := buf.WriteString("\n ("); err != nil {- return err- }- sortedSubpartitions := make([]*partitionDefinition, 0, len(partition.subpartitions))- for _, subPartition := range partition.subpartitions {- sortedSubpartitions = append(sortedSubpartitions, subPartition)- }- slices.SortFunc(sortedSubpartitions, func(a, b *partitionDefinition) int {- if a.id < b.id {- return -1- }- if a.id > b.id {- return 1- }- return 0- })- for j, subPartition := range sortedSubpartitions {- if _, err := buf.WriteString(fmt.Sprintf("SUBPARTITION %s", subPartition.name)); err != nil {- return err- }- if err := writePartitionOptions(buf); err != nil {- return err- }- if j == len(sortedSubpartitions)-1 {- if _, err := buf.WriteString(")"); err != nil {- return err- }- } else {- if _, err := buf.WriteString(",\n "); err != nil {- return err- }- }- }- } else {- if err := writePartitionOptions(buf); err != nil {- return err- }- }-- if i == len(sortedPartitions)-1 {- if _, err := buf.WriteString(")"); err != nil {- return err- }- }- }- }-- return nil-}--func writePartitionOptions(buf io.StringWriter) error {- /*- int err = 0;- err += add_space(fptr);- if (p_elem->tablespace_name) {- err += add_string(fptr, "TABLESPACE = ");- err += add_ident_string(fptr, p_elem->tablespace_name);- err += add_space(fptr);- }- if (p_elem->nodegroup_id != UNDEF_NODEGROUP)- err += add_keyword_int(fptr, "NODEGROUP", (longlong)p_elem->nodegroup_id);- if (p_elem->part_max_rows)- err += add_keyword_int(fptr, "MAX_ROWS", (longlong)p_elem->part_max_rows);- if (p_elem->part_min_rows)- err += add_keyword_int(fptr, "MIN_ROWS", (longlong)p_elem->part_min_rows);- if (!(current_thd->variables.sql_mode & MODE_NO_DIR_IN_CREATE)) {- if (p_elem->data_file_name)- err += add_keyword_path(fptr, "DATA DIRECTORY", p_elem->data_file_name);- if (p_elem->index_file_name)- err += add_keyword_path(fptr, "INDEX DIRECTORY", p_elem->index_file_name);- }- if (p_elem->part_comment)- err += add_keyword_string(fptr, "COMMENT", true, p_elem->part_comment);- return err + add_engine(fptr, p_elem->engine_type);- */- // TODO(zp): Get all the partition options from the metadata is too complex, just write ENGINE=InnoDB for now.- if _, err := buf.WriteString(" ENGINE=InnoDB"); err != nil {- return err- }-- return nil-}--func getPrepositionByType(tp storepb.TablePartitionMetadata_Type) (string, error) {- switch tp {- case storepb.TablePartitionMetadata_RANGE:- return "LESS THAN", nil- case storepb.TablePartitionMetadata_RANGE_COLUMNS:- return "LESS THAN", nil- case storepb.TablePartitionMetadata_LIST:- return "IN", nil- case storepb.TablePartitionMetadata_LIST_COLUMNS:- return "IN", nil- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_LINEAR_KEY:- return "", nil- default:- return "", errors.Errorf("unsupported partition type: %v", tp)- }-}--// splitPartitionExprIntoFields splits the partition expression by ',', and trims the leading and trailing '`' for each element.-func splitPartitionExprIntoFields(expr string) []string {- // We do not support the expression contains parentheses, so we can split the expression by ','.- ss := strings.Split(expr, ",")- for i, s := range ss {- if strings.HasPrefix(s, "`") && strings.HasSuffix(s, "`") {- ss[i] = s[1 : len(s)-1]- }- }- return ss-}--// constraintMap returns a map of constraint name to constraint.-type constraintMap map[string]*ast.Constraint--// SchemaDiff returns the schema diff.-// It only supports schema information from mysqldump.-func SchemaDiff(ctx base.DiffContext, oldStmt, newStmt string) (string, error) {- diff := &diffNode{- ignoreCaseSensitive: ctx.IgnoreCaseSensitive,- alterTablePartitionedByList: make(map[string]*partitionState),- }- if err := diff.diffSupportedStatement(oldStmt, newStmt); err != nil {- return "", err- }-- return diff.deparse()-}--func writeNodeStatement(w format.RestoreWriter, n ast.Node, flags format.RestoreFlags, ending string) error {- restoreCtx := format.NewRestoreCtx(flags, w)- if err := n.Restore(restoreCtx); err != nil {- return err- }- if _, err := w.Write([]byte(";" + ending)); err != nil {- return err- }- return nil-}--func getID(node ast.Node) string {- switch in := node.(type) {- case *ast.CreateTableStmt:- return in.Table.Name.String()- case *ast.DropTableStmt:- return in.Tables[0].Name.String()- case *ast.AlterTableStmt:- for _, spec := range in.Specs {- switch spec.Tp {- case ast.AlterTableOption:- return in.Table.Name.String()- case ast.AlterTableAddColumns:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), spec.NewColumns[0].Name)- case ast.AlterTableDropColumn:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), spec.OldColumnName.Name.String())- case ast.AlterTableModifyColumn:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), spec.NewColumns[0].Name)- case ast.AlterTableAddConstraint:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), spec.Constraint.Name)- case ast.AlterTableDropForeignKey:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), spec.Name)- case ast.AlterTableDropPrimaryKey:- return in.Table.Name.String()- case ast.AlterTableDropCheck:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), spec.Constraint.Name)- default:- return in.Table.Name.String()- }- }- case *ast.CreateIndexStmt:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), in.IndexName)- case *ast.DropIndexStmt:- return fmt.Sprintf("%s.%s", in.Table.Name.String(), in.IndexName)- case *ast.CreateViewStmt:- return in.ViewName.Name.String()- }- return ""-}--func writeNodeList(w format.RestoreWriter, ns []ast.Node) error {- flags := format.DefaultRestoreFlags | format.RestoreStringWithoutCharset- for _, n := range ns {- if err := writeNodeStatement(w, n, flags, "\n"); err != nil {- return err- }- }- return nil-}--func sortAndWriteNodeList(w format.RestoreWriter, ns []ast.Node) error {- flags := format.DefaultRestoreFlags | format.RestoreStringWithoutCharset- slices.SortFunc(ns, func(a, b ast.Node) int {- idA := getID(a)- idB := getID(b)- if idA < idB {- return -1- }- if idA > idB {- return 1- }- return 0- })-- for _, n := range ns {- if err := writeNodeStatement(w, n, flags, "\n\n"); err != nil {- return err- }- }- return nil-}--type schemaInfo struct {- tableMap tableMap- viewMap viewMap-}--type partitionState struct {- info partitionInfo- subInfo *partitionInfo- partitions map[string]*partitionDefinition-}--type partitionInfo struct {- tp storepb.TablePartitionMetadata_Type- useDefault int- expr string-}--type partitionDefinition struct {- id int- name string- value string- subpartitions map[string]*partitionDefinition-}--type viewMap map[string]*ast.CreateViewStmt--type indexInfo struct {- createIndex *ast.CreateIndexStmt-}-type indexMap map[string]*indexInfo--func newIndexInfo(createIndex *ast.CreateIndexStmt) *indexInfo {- return &indexInfo{- createIndex: createIndex,- }-}--type tableInfo struct {- createTable *ast.CreateTableStmt- indexMap indexMap- partition *partitionState-}-type tableMap map[string]*tableInfo--func newTableInfo(createTable *ast.CreateTableStmt) (*tableInfo, error) {- result := &tableInfo{- createTable: createTable,- indexMap: make(indexMap),- }-- var newConstraintList []*ast.Constraint- for _, constraint := range createTable.Constraints {- if createIndex := transformConstraintToIndex(createTable.Table, constraint); createIndex != nil {- if _, exists := result.indexMap[strings.ToLower(createIndex.IndexName)]; exists {- return nil, errors.Errorf("Try to create index `%s` on table `%s`, but index already exists", createIndex.IndexName, createIndex.Table.Name.String())- }- result.indexMap[strings.ToLower(constraint.Name)] = newIndexInfo(createIndex)- } else {- newConstraintList = append(newConstraintList, constraint)- }- }- createTable.Constraints = newConstraintList-- return result, nil-}--func transformConstraintToIndex(tableName *ast.TableName, constraint *ast.Constraint) *ast.CreateIndexStmt {- indexType := ast.IndexKeyTypeNone- switch constraint.Tp {- case ast.ConstraintKey, ast.ConstraintIndex:- case ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex:- indexType = ast.IndexKeyTypeUnique- case ast.ConstraintFulltext:- indexType = ast.IndexKeyTypeFulltext- default:- return nil- }- result := &ast.CreateIndexStmt{- IndexName: constraint.Name,- Table: tableName,- IndexPartSpecifications: constraint.Keys,- IndexOption: constraint.Option,- KeyType: indexType,- }- if result.IndexOption == nil {- result.IndexOption = &ast.IndexOption{Tp: ast.IndexTypeInvalid}- }- return result-}--// buildSchemaInfo returns schema information built by statements.-func (diff *diffNode) buildSchemaInfo(nodes []ast.StmtNode) (*schemaInfo, error) {- result := &schemaInfo{- tableMap: make(tableMap),- viewMap: make(viewMap),- }- for _, node := range nodes {- switch stmt := node.(type) {- case *ast.CreateTableStmt:- var err error- tableName := stmt.Table.Name.O- if diff.ignoreCaseSensitive {- tableName = stmt.Table.Name.L- }- if result.tableMap[tableName], err = newTableInfo(stmt); err != nil {- return nil, err- }- createTableText := node.OriginalText()- list, err := ANTLRParseTiDB(createTableText)- if err != nil {- return nil, err- }-- listener := &tidbTransformer{- currentTable: stmt.Table.Name.O,- tableState: result.tableMap[tableName],- }-- for _, stmt := range list {- antlr.ParseTreeWalkerDefault.Walk(listener, stmt.Tree)- }- case *ast.CreateIndexStmt:- tableName := stmt.Table.Name.O- if diff.ignoreCaseSensitive {- tableName = stmt.Table.Name.L- }- table, exists := result.tableMap[tableName]- if !exists {- return nil, errors.Errorf("Try to create index `%s` on table `%s`, but table not found", stmt.IndexName, stmt.Table.Name.String())- }- // Index names are always case insensitive- if _, exists := table.indexMap[strings.ToLower(stmt.IndexName)]; exists {- return nil, errors.Errorf("Try to create index `%s` on table `%s`, but index already exists", stmt.IndexName, stmt.Table.Name.String())- }- table.indexMap[strings.ToLower(stmt.IndexName)] = newIndexInfo(stmt)- case *ast.CreateViewStmt:- viewName := stmt.ViewName.Name.O- if diff.ignoreCaseSensitive {- viewName = stmt.ViewName.Name.L- }- result.viewMap[viewName] = stmt- default:- }- }- return result, nil-}--type tidbTransformer struct {- *tidbparser.BaseTiDBParserListener-- tableState *tableInfo- currentTable string- err error-}--func (t *tidbTransformer) EnterPartitionClause(ctx *tidbparser.PartitionClauseContext) {- if t.err != nil || t.currentTable == "" {- return- }-- table := t.tableState- if table == nil {- t.err = errors.New("table not found: " + t.currentTable)- return- }-- parititonInfo := partitionInfo{}-- iTypeDefCtx := ctx.PartitionTypeDef()- if iTypeDefCtx != nil {- switch typeDefCtx := iTypeDefCtx.(type) {- case *tidbparser.PartitionDefKeyContext:- parititonInfo.tp = storepb.TablePartitionMetadata_KEY- if typeDefCtx.LINEAR_SYMBOL() != nil {- parititonInfo.tp = storepb.TablePartitionMetadata_LINEAR_KEY- }- // TODO(zp): handle the key algorithm- if typeDefCtx.IdentifierList() != nil {- identifiers := extractIdentifierList(typeDefCtx.IdentifierList())- for i, identifier := range identifiers {- identifier := strings.TrimSpace(identifier)- if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {- identifiers[i] = fmt.Sprintf("`%s`", identifier)- }- }- parititonInfo.expr = strings.Join(identifiers, ",")- }- case *tidbparser.PartitionDefHashContext:- parititonInfo.tp = storepb.TablePartitionMetadata_HASH- if typeDefCtx.LINEAR_SYMBOL() != nil {- parititonInfo.tp = storepb.TablePartitionMetadata_LINEAR_HASH- }- bitExprText := typeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(typeDefCtx.BitExpr())- bitExprFields := strings.Split(bitExprText, ",")- for i, bitExprField := range bitExprFields {- bitExprField := strings.TrimSpace(bitExprField)- if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {- bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)- }- }- parititonInfo.expr = strings.Join(bitExprFields, ",")- case *tidbparser.PartitionDefRangeListContext:- if typeDefCtx.RANGE_SYMBOL() != nil {- parititonInfo.tp = storepb.TablePartitionMetadata_RANGE- } else {- parititonInfo.tp = storepb.TablePartitionMetadata_LIST- }- if typeDefCtx.COLUMNS_SYMBOL() != nil {- if parititonInfo.tp == storepb.TablePartitionMetadata_RANGE {- parititonInfo.tp = storepb.TablePartitionMetadata_RANGE_COLUMNS- } else {- parititonInfo.tp = storepb.TablePartitionMetadata_LIST_COLUMNS- }-- identifierList := extractIdentifierList(typeDefCtx.IdentifierList())- for i, identifier := range identifierList {- identifier := strings.TrimSpace(identifier)- if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {- identifierList[i] = fmt.Sprintf("`%s`", identifier)- }- }- parititonInfo.expr = strings.Join(identifierList, ",")- } else {- bitExprText := typeDefCtx.GetParser().GetTokenStream().GetTextFromRuleContext(typeDefCtx.BitExpr())- bitExprFields := strings.Split(bitExprText, ",")- for i, bitExprField := range bitExprFields {- bitExprField := strings.TrimSpace(bitExprField)- if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {- bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)- }- }- parititonInfo.expr = strings.Join(bitExprFields, ",")- }- default:- t.err = errors.New("unknown partition type")- return- }- }-- if n := ctx.Real_ulong_number(); n != nil {- number, err := strconv.ParseInt(n.GetText(), 10, 64)- if err != nil {- t.err = errors.Wrap(err, "failed to parse partition number")- return- }- parititonInfo.useDefault = int(number)- }-- var subInfo *partitionInfo- if subPartitionCtx := ctx.SubPartitions(); subPartitionCtx != nil {- subInfo = new(partitionInfo)- if subPartitionCtx.HASH_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_HASH- if subPartitionCtx.LINEAR_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_LINEAR_HASH- }- if bitExprCtx := subPartitionCtx.BitExpr(); bitExprCtx != nil {- bitExprText := bitExprCtx.GetParser().GetTokenStream().GetTextFromRuleContext(bitExprCtx)- bitExprFields := strings.Split(bitExprText, ",")- for i, bitExprField := range bitExprFields {- bitExprField := strings.TrimSpace(bitExprField)- if !strings.HasPrefix(bitExprField, "`") || !strings.HasSuffix(bitExprField, "`") {- bitExprFields[i] = fmt.Sprintf("`%s`", bitExprField)- }- }- subInfo.expr = strings.Join(bitExprFields, ",")- }- } else if subPartitionCtx.KEY_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_KEY- if subPartitionCtx.LINEAR_SYMBOL() != nil {- subInfo.tp = storepb.TablePartitionMetadata_LINEAR_KEY- }- if identifierListParensCtx := subPartitionCtx.IdentifierListWithParentheses(); identifierListParensCtx != nil {- identifiers := extractIdentifierList(identifierListParensCtx.IdentifierList())- for i, identifier := range identifiers {- identifier := strings.TrimSpace(identifier)- if !strings.HasPrefix(identifier, "`") || !strings.HasSuffix(identifier, "`") {- identifiers[i] = fmt.Sprintf("`%s`", identifier)- }- }- subInfo.expr = strings.Join(identifiers, ",")- }- }-- if n := subPartitionCtx.Real_ulong_number(); n != nil {- number, err := strconv.ParseInt(n.GetText(), 10, 64)- if err != nil {- t.err = errors.Wrap(err, "failed to parse sub partition number")- return- }- subInfo.useDefault = int(number)- }- }-- partitionDefinitions := make(map[string]*partitionDefinition)- var allPartDefs []tidbparser.IPartitionDefinitionContext- if v := ctx.PartitionDefinitions(); v != nil {- allPartDefs = ctx.PartitionDefinitions().AllPartitionDefinition()- }- for i, partDef := range allPartDefs {- pd := &partitionDefinition{- id: i + 1,- name: NormalizeTiDBIdentifier(partDef.Identifier()),- }- switch parititonInfo.tp {- case storepb.TablePartitionMetadata_RANGE_COLUMNS, storepb.TablePartitionMetadata_RANGE:- if partDef.LESS_SYMBOL() == nil {- t.err = errors.New("RANGE partition but no LESS THAN clause")- return- }- if partDef.PartitionValueItemListParen() != nil {- itemsText := partDef.PartitionValueItemListParen().GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- partDef.PartitionValueItemListParen().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,- partDef.PartitionValueItemListParen().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,- ),- )- itemsTextFields := strings.Split(itemsText, ",")- for i, itemsTextField := range itemsTextFields {- itemsTextField := strings.TrimSpace(itemsTextField)- if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {- itemsTextField = itemsTextField[1 : len(itemsTextField)-1]- }- itemsTextFields[i] = itemsTextField- }- pd.value = strings.Join(itemsTextFields, ",")- } else {- pd.value = "MAXVALUE"- }- case storepb.TablePartitionMetadata_LIST_COLUMNS, storepb.TablePartitionMetadata_LIST:- if partDef.PartitionValuesIn() == nil {- t.err = errors.New("COLUMNS partition but no partition value item in IN clause")- return- }- var itemsText string- if partDef.PartitionValuesIn().OPEN_PAR_SYMBOL() != nil {- itemsText = partDef.PartitionValuesIn().GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- partDef.PartitionValuesIn().OPEN_PAR_SYMBOL().GetSymbol().GetTokenIndex()+1,- partDef.PartitionValuesIn().CLOSE_PAR_SYMBOL().GetSymbol().GetTokenIndex()-1,- ),- )- } else {- itemsText = partDef.PartitionValuesIn().GetParser().GetTokenStream().GetTextFromRuleContext(partDef.PartitionValuesIn().PartitionValueItemListParen(0))- }-- itemsTextFields := strings.Split(itemsText, ",")- for i, itemsTextField := range itemsTextFields {- itemsTextField := strings.TrimSpace(itemsTextField)- if strings.HasPrefix(itemsTextField, "`") && strings.HasSuffix(itemsTextField, "`") {- itemsTextField = itemsTextField[1 : len(itemsTextField)-1]- }- itemsTextFields[i] = itemsTextField- }- pd.value = strings.Join(itemsTextFields, ",")- case storepb.TablePartitionMetadata_HASH, storepb.TablePartitionMetadata_LINEAR_HASH, storepb.TablePartitionMetadata_KEY, storepb.TablePartitionMetadata_LINEAR_KEY:- default:- t.err = errors.New("unknown partition type")- return- }-- if subInfo != nil {- allSubpartitions := partDef.AllSubpartitionDefinition()- if subInfo.tp == storepb.TablePartitionMetadata_TYPE_UNSPECIFIED && len(allSubpartitions) > 0 {- t.err = errors.New("specify subpartition definition but no subpartition type specified")- return- }- subPartitionDefinitions := make(map[string]*partitionDefinition)- for i, sub := range allSubpartitions {- subpd := &partitionDefinition{- id: i + 1,- name: NormalizeTiDBTextOrIdentifier(sub.TextOrIdentifier()),- }- subPartitionDefinitions[subpd.name] = subpd- }- pd.subpartitions = subPartitionDefinitions- }-- partitionDefinitions[pd.name] = pd- }-- table.partition = &partitionState{- info: parititonInfo,- subInfo: subInfo,- partitions: partitionDefinitions,- }-}--// extract column names.-func extractIdentifierList(ctx tidbparser.IIdentifierListContext) []string {- var result []string- for _, identifier := range ctx.AllIdentifier() {- result = append(result, NormalizeTiDBIdentifier(identifier))- }- return result-}--// getTempView returns the temporary view name and the create statement.-func getTempView(stmt *ast.CreateViewStmt) (*ast.CreateViewStmt, error) {- // We create the temp view similar to what mysqldump does.- // Create a temporary view with the same name as the view. Its columns should- // have the same name in order to satisfy views that depend on this view.- // This temporary view will be removed when the actual view is created.- // The column properties are unnecessary and not preserved in this temporary view.- // because other views only need to reference the column name.- // Example: SELECT 1 AS colName1, 1 AS colName2.- // TODO(zp): support SDL for GitOps.- var selectFields []*ast.SelectField- // mysqldump always show field list- if len(stmt.Cols) > 0 {- for _, col := range stmt.Cols {- selectFields = append(selectFields, &ast.SelectField{- Expr: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- AsName: col,- })- }- } else {- colName, err := extractColNameFromSelectListClauseOfCreateViewStmt(stmt.Select)- if err != nil {- return nil, errors.Wrapf(err, "failed to extract column name from select list clause of create view statement")- }- for _, name := range colName {- selectFields = append(selectFields, &ast.SelectField{- Expr: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- AsName: ast.NewCIStr(name),- })- }- }-- return &ast.CreateViewStmt{- ViewName: stmt.ViewName,- Select: &ast.SelectStmt{- SelectStmtOpts: &ast.SelectStmtOpts{- // Avoid generating SQL_NO_CACHE- // https://sourcegraph.com/github.com/pingcap/tidb/-/blob/parser/ast/dml.go?L1234- SQLCache: true,- },- Fields: &ast.FieldList{- Fields: selectFields,- },- },- OrReplace: true,- // Avoid nil pointer dereference panic.- // https://sourcegraph.com/github.com/pingcap/tidb/-/blob/parser/ast/ddl.go?L1398- Definer: stmt.Definer,- Security: stmt.Security,- CheckOption: ast.CheckOptionCascaded,- }, nil-}--// extractColNameFromCreateViewStmt extracts column names from create view statement.-// For example, CREATE OR REPLACE VIEW `v` AS select `d` as `c` WILL return `c`.-func extractColNameFromSelectListClauseOfCreateViewStmt(stmt ast.Node) ([]string, error) {- var result []string- switch stmt := (stmt).(type) {- case *ast.SelectStmt:- for _, field := range stmt.Fields.Fields {- if field.WildCard != nil {- return nil, errors.New("wildcard(*) is not supported now in select statement")- }- var fieldName string- if field.AsName.O != "" {- fieldName = field.AsName.O- } else {- fieldName = field.Expr.(*ast.ColumnNameExpr).Name.Name.O- }- result = append(result, fieldName)- }- return result, nil- case *ast.SetOprStmt:- // For SetOprStmt, we focus on the first select statement, for example:- // with `tt` as (select `t1`.`id` AS `id` from `t1` union select `t2`.`id2` AS `id2` from `t2`) select `tt`.`id` AS `id` from `tt` union select `t1`.`id` AS `id` from `t1`- // we just focus on select `tt`.`id` as `id` from `tt`.- if len(stmt.SelectList.Selects) == 0 {- return nil, errors.New("select list in SetOprStmt is empty")- }- return extractColNameFromSelectListClauseOfCreateViewStmt(stmt.SelectList.Selects[0])- case *ast.SetOprSelectList:- if len(stmt.Selects) == 0 {- return nil, errors.New("select list in SetOprSelectList is empty")- }- return extractColNameFromSelectListClauseOfCreateViewStmt(stmt.Selects[0])- default:- stmtStr, err := toString(stmt)- if err != nil {- return nil, errors.Wrapf(err, "failed to convert create view statement to string")- }- return nil, errors.Errorf("unsupported create view statement %q which select is not supported node", stmtStr)- }-}--// buildColumnMap returns a map of column name to column definition on a given table.-func buildColumnMap(columnList []*ast.ColumnDef) map[string]*ast.ColumnDef {- oldColumnMap := make(map[string]*ast.ColumnDef)- for _, columnDef := range columnList {- // Column names are always case insensitive.- oldColumnMap[columnDef.Name.Name.L] = columnDef- }- return oldColumnMap-}--// buildColumnPositionMap returns a map of column name to column position.-func buildColumnPositionMap(stmt *ast.CreateTableStmt) map[string]int {- m := make(map[string]int)- for i, col := range stmt.Cols {- // Column names are always case insensitive.- m[col.Name.Name.L] = i- }- return m-}--func buildConstraintMap(stmt *ast.CreateTableStmt) (*ast.Constraint, constraintMap) {- var primaryKey *ast.Constraint- constraintMap := make(constraintMap)- for _, constraint := range stmt.Constraints {- switch constraint.Tp {- case ast.ConstraintForeignKey, ast.ConstraintCheck:- constraintMap[strings.ToLower(constraint.Name)] = constraint- case ast.ConstraintPrimaryKey:- primaryKey = constraint- default:- }- }- return primaryKey, constraintMap-}--// isColumnEqual returns true if definitions of two columns with the same name are the same.-func isColumnEqual(o, n *ast.ColumnDef) bool {- if !isColumnTypesEqual(o, n) {- return false- }- if !isColumnOptionsEqual(o.Options, n.Options) {- return false- }- return true-}--func isColumnTypesEqual(o, n *ast.ColumnDef) bool {- return o.Tp.String() == n.Tp.String()-}--func isColumnOptionsEqual(o, n []*ast.ColumnOption) bool {- oldCollate, oldNormalizeOptions := normalizeColumnOptions(o)- newCollate, newNormalizeOptions := normalizeColumnOptions(n)- if len(oldNormalizeOptions) != len(newNormalizeOptions) {- return false- }- if newCollate != nil {- if oldCollate == nil {- return false- }- if oldCollate.StrValue != newCollate.StrValue {- return false- }- }- for idx, oldOption := range oldNormalizeOptions {- oldOptionStr, err := toString(oldOption)- if err != nil {- slog.Error("failed to convert old column option to string", log.BBError(err))- return false- }- newOption := newNormalizeOptions[idx]- newOptionStr, err := toString(newOption)- if err != nil {- slog.Error("failed to convert new column option to string", log.BBError(err))- return false- }- if oldOptionStr != newOptionStr {- return false- }- }- return true-}--// normalizeColumnOptions normalizes the column options.-// It skips the NULL option, NO option and then order the options by OptionType.-func normalizeColumnOptions(options []*ast.ColumnOption) (*ast.ColumnOption, []*ast.ColumnOption) {- var retOptions []*ast.ColumnOption- var collateOption *ast.ColumnOption- for _, option := range options {- switch option.Tp {- case ast.ColumnOptionCollate:- collateOption = option- continue- case ast.ColumnOptionNull:- continue- case ast.ColumnOptionNoOption:- continue- case ast.ColumnOptionDefaultValue:- if option.Expr.GetType().GetType() == mysql.TypeNull {- continue- }- default:- // Include all other column options- }- retOptions = append(retOptions, option)- }- slices.SortFunc(retOptions, func(a, b *ast.ColumnOption) int {- if a.Tp < b.Tp {- return -1- }- if a.Tp > b.Tp {- return 1- }- return 0- })- return collateOption, retOptions-}--// isIndexEqual returns true if definitions of two indexes are the same.-func isIndexEqual(o, n *ast.CreateIndexStmt) bool {- // CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name- // [index_type]- // ON tbl_name (key_part,...)- // [index_option]- // [algorithm_option | lock_option] ...-- // MySQL index names are case insensitive.- if !strings.EqualFold(o.IndexName, n.IndexName) {- return false- }- if (o.IndexOption == nil) != (n.IndexOption == nil) {- return false- }- if o.IndexOption != nil && n.IndexOption != nil {- if o.IndexOption.Tp != n.IndexOption.Tp {- return false- }- }-- if !isKeyPartEqual(o.IndexPartSpecifications, n.IndexPartSpecifications) {- return false- }- if !isIndexOptionEqual(o.IndexOption, n.IndexOption) {- return false- }- return true-}--// isPrimaryKeyEqual returns true if definitions of two indexes are the same.-func isPrimaryKeyEqual(o, n *ast.Constraint) bool {- // {INDEX | KEY} [index_name] [index_type] (key_part,...) [index_option] ...- if o.Name != n.Name {- return false- }- if (o.Option == nil) != (n.Option == nil) {- return false- }- if o.Option != nil && n.Option != nil {- if o.Option.Tp != n.Option.Tp {- return false- }- }-- if !isKeyPartEqual(o.Keys, n.Keys) {- return false- }- if !isIndexOptionEqual(o.Option, n.Option) {- return false- }- return true-}--// isKeyPartEqual returns true if two key parts are the same.-func isKeyPartEqual(o, n []*ast.IndexPartSpecification) bool {- if len(o) != len(n) {- return false- }- // key_part: {col_name [(length)] | (expr)} [ASC | DESC]- for idx, oldKeyPart := range o {- newKeyPart := n[idx]- if (oldKeyPart.Column == nil) != (newKeyPart.Column == nil) {- return false- }- if oldKeyPart.Column != nil && newKeyPart.Column != nil {- if oldKeyPart.Column.Name.String() != newKeyPart.Column.Name.String() {- return false- }- if oldKeyPart.Length != newKeyPart.Length {- return false- }- }- if (oldKeyPart.Expr == nil) != (newKeyPart.Expr == nil) {- // if the key part uses expression instead of column name, the expression node is nil.- return false- }- if oldKeyPart.Expr != nil && newKeyPart.Expr != nil {- oldKeyPartStr, err := toString(oldKeyPart.Expr)- if err != nil {- slog.Error("failed to convert old key part to string", log.BBError(err))- return false- }- newKeyPartStr, err := toString(newKeyPart.Expr)- if err != nil {- slog.Error("failed to convert new key part to string", log.BBError(err))- return false- }- return oldKeyPartStr == newKeyPartStr- }- if oldKeyPart.Desc != newKeyPart.Desc {- return false- }- // TODO(zp): TiDB MySQL parser doesn't record the index order field in go struct, but it can parse correctly.- // https://sourcegraph.com/github.com/pingcap/tidb/-/blob/parser/parser.y?L3688- // We can support the index order until we implement the parser by ourself or https://github.com/pingcap/tidb/pull/38137 is merged.- }- return true-}--// isIndexOptionEqual returns true if two index options are the same.-func isIndexOptionEqual(o, n *ast.IndexOption) bool {- // index_option: {- // KEY_BLOCK_SIZE [=] value- // | index_type- // | WITH PARSER parser_name- // | COMMENT 'string'- // | {VISIBLE | INVISIBLE}- // |ENGINE_ATTRIBUTE [=] 'string'- // |SECONDARY_ENGINE_ATTRIBUTE [=] 'string'- // }- if (o == nil) != (n == nil) {- return false- }- if o == nil && n == nil {- return true- }- if o.KeyBlockSize != n.KeyBlockSize {- return false- }- if o.Tp != n.Tp {- return false- }- if o.ParserName.O != n.ParserName.O {- return false- }- if o.Comment != n.Comment {- return false- }- if o.Visibility != n.Visibility {- return false- }- // TODO(zp): support ENGINE_ATTRIBUTE and SECONDARY_ENGINE_ATTRIBUTE.- return true-}--// isForeignKeyEqual returns true if two foreign keys are the same.-func isForeignKeyConstraintEqual(o, n *ast.Constraint) bool {- // FOREIGN KEY [index_name] (index_col_name,...) reference_definition- if !strings.EqualFold(o.Name, n.Name) {- return false- }- if !isKeyPartEqual(o.Keys, n.Keys) {- return false- }- if !isReferenceDefinitionEqual(o.Refer, n.Refer) {- return false- }- return true-}--// isReferenceDefinitionEqual returns true if two reference definitions are the same.-func isReferenceDefinitionEqual(o, n *ast.ReferenceDef) bool {- // reference_definition:- // REFERENCES tbl_name (index_col_name,...)- // [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]- // [ON DELETE reference_option]- // [ON UPDATE reference_option]- if o.Table.Name.String() != n.Table.Name.String() {- return false- }-- if len(o.IndexPartSpecifications) != len(n.IndexPartSpecifications) {- return false- }- for idx, oldIndexColName := range o.IndexPartSpecifications {- newIndexColName := n.IndexPartSpecifications[idx]- if oldIndexColName.Column.Name.String() != newIndexColName.Column.Name.String() {- return false- }- }-- if o.Match != n.Match {- return false- }-- if (o.OnDelete == nil) != (n.OnDelete == nil) {- return false- }- if o.OnDelete != nil && n.OnDelete != nil && o.OnDelete.ReferOpt != n.OnDelete.ReferOpt {- return false- }-- if (o.OnUpdate == nil) != (n.OnUpdate == nil) {- return false- }- if o.OnUpdate != nil && n.OnUpdate != nil && o.OnUpdate.ReferOpt != n.OnUpdate.ReferOpt {- return false- }- return true-}--// trimParentheses trims outer parentheses.-func trimParentheses(expr ast.ExprNode) ast.ExprNode {- result := expr- for {- if node, yes := result.(*ast.ParenthesesExpr); yes {- result = node.Expr- } else {- break- }- }- return result-}--// isCheckConstraintEqual returns true if two check constraints are the same.-func isCheckConstraintEqual(o, n *ast.Constraint) bool {- // check_constraint_definition:- // [CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]- if !strings.EqualFold(o.Name, n.Name) {- return false- }-- if o.Enforced != n.Enforced {- return false- }-- oldExpr, err := toString(trimParentheses(o.Expr))- if err != nil {- slog.Error("failed to convert old check constraint expression to string", log.BBError(err))- return false- }- newExpr, err := toString(trimParentheses(n.Expr))- if err != nil {- slog.Error("failed to convert new check constraint expression to string", log.BBError(err))- return false- }- return oldExpr == newExpr-}--// diffTableOptions returns the diff of two table options, returns nil if they are the same.-func diffTableOptions(tableName *ast.TableName, o, n []*ast.TableOption) *ast.AlterTableStmt {- // https://dev.mysql.com/doc/refman/8.0/en/create-table.html- // table_option: {- // AUTOEXTEND_SIZE [=] value- // | AUTO_INCREMENT [=] value- // | AVG_ROW_LENGTH [=] value- // | [DEFAULT] CHARACTER SET [=] charset_name- // | CHECKSUM [=] {0 | 1}- // | [DEFAULT] COLLATE [=] collation_name- // | COMMENT [=] 'string'- // | COMPRESSION [=] {'ZLIB' | 'LZ4' | 'NONE'}- // | CONNECTION [=] 'connect_string'- // | {DATA | INDEX} DIRECTORY [=] 'absolute path to directory'- // | DELAY_KEY_WRITE [=] {0 | 1}- // | ENCRYPTION [=] {'Y' | 'N'}- // | ENGINE [=] engine_name- // | ENGINE_ATTRIBUTE [=] 'string'- // | INSERT_METHOD [=] { NO | FIRST | LAST }- // | KEY_BLOCK_SIZE [=] value- // | MAX_ROWS [=] value- // | MIN_ROWS [=] value- // | PACK_KEYS [=] {0 | 1 | DEFAULT}- // | PASSWORD [=] 'string'- // | ROW_FORMAT [=] {DEFAULT | DYNAMIC | FIXED | COMPRESSED | REDUNDANT | COMPACT}- // | START TRANSACTION // This is an internal-use table option.- // // It was introduced in MySQL 8.0.21 to permit CREATE TABLE ... SELECT to be logged as a single,- // // atomic transaction in the binary log when using row-based replication with a storage engine that supports atomic DDL.- // | SECONDARY_ENGINE_ATTRIBUTE [=] 'string'- // | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1}- // | STATS_PERSISTENT [=] {DEFAULT | 0 | 1}- // | STATS_SAMPLE_PAGES [=] value- // | TABLESPACE tablespace_name [STORAGE {DISK | MEMORY}]- // | UNION [=] (tbl_name[,tbl_name]...)- // }-- // We use map to record the table options, so we can easily find the difference.- oldOptionsMap := buildTableOptionMap(o)- newOptionsMap := buildTableOptionMap(n)-- var options []*ast.TableOption- for oldTp, oldOption := range oldOptionsMap {- newOption, ok := newOptionsMap[oldTp]- if !ok {- switch oldOption.Tp {- // For table engine, table charset and table collation, if oldTable has but newTable doesn't,- // we skip drop them.- case ast.TableOptionEngine, ast.TableOptionCharset, ast.TableOptionCollate:- continue- default:- // Drop other table options- }- // We should drop the table option if it doesn't exist in the new table options.- if astOption := dropTableOption(oldOption); astOption != nil {- options = append(options, astOption)- }- continue- }- if !isTableOptionValEqual(oldOption, newOption) {- options = append(options, newOption)- }- }- // We should add the table option if it doesn't exist in the old table options.- for newTp, newOption := range newOptionsMap {- if _, ok := oldOptionsMap[newTp]; !ok {- // We should add the table option if it doesn't exist in the old table options.- options = append(options, newOption)- }- }- if len(options) == 0 {- return nil- }- return &ast.AlterTableStmt{- Table: tableName,- Specs: []*ast.AlterTableSpec{- {- Tp: ast.AlterTableOption,- Options: options,- },- },- }-}--// dropTableOption generate the table options node need to oppended to the ALTER TABLE OPTION spec.-func dropTableOption(option *ast.TableOption) *ast.TableOption {- switch option.Tp {- case ast.TableOptionAutoIncrement:- // You cannot reset the counter to a value less than or equal to the value that is currently in use.- // For both InnoDB and MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column,- // the value is reset to the current maximum AUTO_INCREMENT column value plus one.- // https://dev.mysql.com/doc/refman/8.0/en/alter-table.html- // So we always set the auto_increment value to 0, it will be reset to the current maximum AUTO_INCREMENT column value plus one.- return &ast.TableOption{- Tp: ast.TableOptionAutoIncrement,- UintValue: 0,- }- case ast.TableOptionAvgRowLength:- // AVG_ROW_LENGTH only works in MyISAM tables.- return &ast.TableOption{- Tp: ast.TableOptionAvgRowLength,- UintValue: 0,- }- case ast.TableOptionCharset:- // TODO(zp): we use utf8mb4 as the default charset, but it's not always true. We should consider the database default charset.- defaultCharset := "utf8mb4"- if option.StrValue == defaultCharset {- return nil- }- return &ast.TableOption{- Tp: ast.TableOptionCharset,- StrValue: defaultCharset,- }- case ast.TableOptionCollate:- // TODO(zp): default collate is related with the charset.- defaultCollate := "utf8mb4_general_ci"- if option.StrValue == defaultCollate {- return nil- }- return &ast.TableOption{- Tp: ast.TableOptionCollate,- StrValue: defaultCollate,- }- case ast.TableOptionCheckSum:- return &ast.TableOption{- Tp: ast.TableOptionCheckSum,- UintValue: 0,- }- case ast.TableOptionComment:- // Set to "" to remove the comment.- return &ast.TableOption{- Tp: ast.TableOptionComment,- }- case ast.TableOptionCompression:- // TODO(zp): handle the compression- case ast.TableOptionConnection:- // Set to "" to remove the connection.- return &ast.TableOption{- Tp: ast.TableOptionConnection,- }- case ast.TableOptionDataDirectory:- case ast.TableOptionIndexDirectory:- // TODO(zp): handle the default data directory and index directory, there are a lot of situations we need to consider.- // 1. Data Directory and Index Directory will be ignored on Windows.- // 2. For a normal table, data directory and index directory cannot changed after the table is created, but for a partition table, it can be changed by USING `alter add PARTITIONS data DIRECTORY ...`- // 3. We should know the default data directory like /var/mysql/data, and the default index directory like /var/mysql/index.- case ast.TableOptionDelayKeyWrite:- return &ast.TableOption{- Tp: ast.TableOptionDelayKeyWrite,- UintValue: 0,- }- case ast.TableOptionEncryption:- return &ast.TableOption{- Tp: ast.TableOptionEncryption,- StrValue: "N",- }- case ast.TableOptionEngine:- // TODO(zp): handle the default engine- case ast.TableOptionInsertMethod:- // INSERT METHOD only support in MERGE storage engine.- // https://dev.mysql.com/doc/refman/8.0/en/create-table.html- return &ast.TableOption{- Tp: ast.TableOptionInsertMethod,- StrValue: "NO",- }- case ast.TableOptionKeyBlockSize:- // TODO(zp): InnoDB doesn't support this. And the default value will be ensured when compiling.- case ast.TableOptionMaxRows:- return &ast.TableOption{- Tp: ast.TableOptionMaxRows,- UintValue: 0,- }- case ast.TableOptionMinRows:- return &ast.TableOption{- Tp: ast.TableOptionMinRows,- UintValue: 0,- }- case ast.TableOptionPackKeys:- // TiDB doesn't support this, and the restore will always write "DEFAULT".- return &ast.TableOption{- Tp: ast.TableOptionPackKeys,- }- case ast.TableOptionPassword:- // mysqldump will not dump the password, but we handle it to pass the test.- return &ast.TableOption{- Tp: ast.TableOptionPassword,- }- case ast.TableOptionRowFormat:- return &ast.TableOption{- Tp: ast.TableOptionRowFormat,- UintValue: ast.RowFormatDefault,- }- case ast.TableOptionStatsAutoRecalc:- return &ast.TableOption{- Tp: ast.TableOptionStatsAutoRecalc,- Default: true,- }- case ast.TableOptionStatsPersistent:- // TiDB doesn't support this, and the restore will always write "DEFAULT".- return &ast.TableOption{- Tp: ast.TableOptionStatsPersistent,- }- case ast.TableOptionStatsSamplePages:- return &ast.TableOption{- Tp: ast.TableOptionStatsSamplePages,- Default: true,- }- case ast.TableOptionTablespace:- // TODO(zp): handle the table space- case ast.TableOptionUnion:- // TODO(zp): handle the union- default:- // Return nil for unhandled table options- }- return nil-}--// isTableOptionValEqual compare the two table options value, if they are equal, returns true.-// Caller need to ensure the two table options are not nil and the type is the same.-func isTableOptionValEqual(o, n *ast.TableOption) bool {- switch o.Tp {- case ast.TableOptionAutoIncrement:- // You cannot reset the counter to a value less than or equal to the value that is currently in use.- // For both InnoDB and MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column,- // the value is reset to the current maximum AUTO_INCREMENT column value plus one.- // https://dev.mysql.com/doc/refman/8.0/en/alter-table.html- return o.UintValue == n.UintValue- case ast.TableOptionAvgRowLength:- return o.UintValue == n.UintValue- case ast.TableOptionCharset:- if o.Default != n.Default {- return false- }- if o.Default && n.Default {- return true- }- return o.StrValue == n.StrValue- case ast.TableOptionCollate:- return o.StrValue == n.StrValue- case ast.TableOptionCheckSum:- return o.UintValue == n.UintValue- case ast.TableOptionComment:- return o.StrValue == n.StrValue- case ast.TableOptionCompression:- return o.StrValue == n.StrValue- case ast.TableOptionConnection:- return o.StrValue == n.StrValue- case ast.TableOptionDataDirectory:- return o.StrValue == n.StrValue- case ast.TableOptionIndexDirectory:- return o.StrValue == n.StrValue- case ast.TableOptionDelayKeyWrite:- return o.UintValue == n.UintValue- case ast.TableOptionEncryption:- return o.StrValue == n.StrValue- case ast.TableOptionEngine:- return o.StrValue == n.StrValue- case ast.TableOptionInsertMethod:- return o.StrValue == n.StrValue- case ast.TableOptionKeyBlockSize:- return o.UintValue == n.UintValue- case ast.TableOptionMaxRows:- return o.UintValue == n.UintValue- case ast.TableOptionMinRows:- return o.UintValue == n.UintValue- case ast.TableOptionPackKeys:- // TiDB doesn't support this, and the restore will always write "DEFAULT".- // The parser will ignore it. So we can only ignore it here.- // https://github.com/pingcap/tidb/blob/master/parser/parser.y#L11661- return o.Default == n.Default- case ast.TableOptionPassword:- // mysqldump will not dump the password, so we just return false here.- return false- case ast.TableOptionRowFormat:- return o.UintValue == n.UintValue- case ast.TableOptionStatsAutoRecalc:- // TiDB parser will ignore the DEFAULT value. So we just can compare the UINT value here.- // https://github.com/pingcap/tidb/blob/master/parser/parser.y#L11599- return o.UintValue == n.UintValue- case ast.TableOptionStatsPersistent:- // TiDB parser doesn't support this, it only assign the type without any value.- // https://github.com/pingcap/tidb/blob/master/parser/parser.y#L11595- return true- case ast.TableOptionStatsSamplePages:- return o.UintValue == n.UintValue- case ast.TableOptionTablespace:- return o.StrValue == n.StrValue- case ast.TableOptionUnion:- oldTableNames := o.TableNames- newTableNames := n.TableNames- if len(oldTableNames) != len(newTableNames) {- return false- }- for i, oldTableName := range oldTableNames {- newTableName := newTableNames[i]- if oldTableName.Name.O != newTableName.Name.O {- return false- }- }- return true- default:- // For unknown option types, consider them equal- return true- }-}--// isViewEqual checks whether two views with same name are equal.-func (diff *diffNode) isViewEqual(o, n *ast.CreateViewStmt) bool {- // CREATE- // [OR REPLACE]- // [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]- // [DEFINER = user]- // [SQL SECURITY { DEFINER | INVOKER }]- // VIEW view_name [(column_list)]- // AS select_statement- // [WITH [CASCADED | LOCAL] CHECK OPTION]- // We can easily replace view statement by using `CREATE OR REPLACE VIEW` statement to replace the old one.- // So we don't need to compare each part, just compare the restore string.- if diff.ignoreCaseSensitive {- oldViewStr, err := toLowerNameString(o)- if err != nil {- slog.Error("fail to convert old view to string", log.BBError(err))- return false- }- newViewStr, err := toLowerNameString(n)- if err != nil {- slog.Error("fail to convert new view to string", log.BBError(err))- return false- }- return oldViewStr == newViewStr- }- oldViewStr, err := toString(o)- if err != nil {- slog.Error("fail to convert old view to string", log.BBError(err))- return false- }- newViewStr, err := toString(n)- if err != nil {- slog.Error("fail to convert new view to string", log.BBError(err))- return false- }- return oldViewStr == newViewStr-}--// buildTableOptionMap builds a map of table options.-func buildTableOptionMap(options []*ast.TableOption) map[ast.TableOptionType]*ast.TableOption {- m := make(map[ast.TableOptionType]*ast.TableOption)- for _, option := range options {- m[option.Tp] = option- }- return m-}--// hasColumnsIntersection returns true if two column slices have column name intersection.-func hasColumnsIntersection(a, b []*ast.ColumnDef) bool {- bMap := make(map[string]bool)- for _, col := range b {- // MySQL column name is case insensitive.- bMap[col.Name.Name.L] = true- }- for _, col := range a {- // MySQL column name is case insensitive.- if _, ok := bMap[col.Name.Name.L]; ok {- return true- }- }- return false-}diff --git a/backend/plugin/parser/tidb/differ_column_test.go b/backend/plugin/parser/tidb/differ_column_test.godeleted file mode 100644index 59938bfd31..0000000000--- a/backend/plugin/parser/tidb/differ_column_test.go+++ /dev/null@@ -1,292 +0,0 @@-package tidb--import (- "testing"-)--func TestColumnExist(t *testing.T) {- tests := []testCase{- // Missing columns- {- old: `CREATE TABLE book(id INT, PRIMARY KEY(id));`,- new: `CREATE TABLE book(id INT, price INT, PRIMARY KEY(id));`,- want: "ALTER TABLE `book` ADD COLUMN `price` INT AFTER `id`;\n",- },- {- old: `CREATE TABLE book(id INT, PRIMARY KEY(id))`,- new: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,- want: "ALTER TABLE `book` ADD COLUMN `price` INT AFTER `id`;\nALTER TABLE `book` ADD COLUMN `code` VARCHAR(50) AFTER `price`;\n",- },- {- old: ``,- new: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,- want: "CREATE TABLE IF NOT EXISTS `book` (`id` INT,`price` INT,`code` VARCHAR(50),PRIMARY KEY(`id`));\n\n",- },- {- old: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,- new: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,- want: "",- },- // excess columns- {- old: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,- new: `CREATE TABLE book(price INT, code VARCHAR(50));`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` DROP COLUMN `id`;\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestColumnType(t *testing.T) {- tests := []testCase{- // Different types.- {- old: `CREATE TABLE book(id INT);`,- new: `CREATE TABLE book(id VARCHAR(50));`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` VARCHAR(50);\n",- },-- {- old: `CREATE TABLE book(id INT, isbn VARCHAR(50));`,- new: `CREATE TABLE book(id VARCHAR(50), isbn VARCHAR(100));`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` VARCHAR(50);\nALTER TABLE `book` MODIFY COLUMN `isbn` VARCHAR(100);\n",- },- {- old: `CREATE TABLE book(id INT, isbn VARCHAR(50));`,- new: `CREATE TABLE book(id INT, isbn VARCHAR(50));`,- want: "",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestColumnOption(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(id INT COLUMN_FORMAT FIXED, PRIMARY KEY(id));`,- new: `CREATE TABLE book(id INT COLUMN_FORMAT DYNAMIC, PRIMARY KEY(id));`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` INT COLUMN_FORMAT DYNAMIC;\n",- },- // NULL option not match.- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50));`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50);\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL DEFAULT 'Harry Potter');`,- new: `CREATE TABLE book(name VARCHAR(50) NULL DEFAULT 'Harry Potter');`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NULL DEFAULT 'Harry Potter';\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL DEFAULT 'Harry Potter');`,- new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter');`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) DEFAULT 'Harry Potter';\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NULL);`,- new: `CREATE TABLE book(name VARCHAR(50));`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50));`,- new: `CREATE TABLE book(name VARCHAR(50) NULL);`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) NULL);`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- want: "",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestColumnComment(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Author Name' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COMMENT 'Book Name' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Author Name' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'AUTHOR NAME' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COMMENT 'AUTHOR NAME' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COMMENT 'Book Name' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,- want: "",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestColumnDefaultValue(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,- want: "",- },- {- old: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,- new: `CREATE TABLE book(id INT NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` INT NOT NULL;\n",- },- {- old: `CREATE TABLE book(id INT NOT NULL);`,- new: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` INT DEFAULT 0 NOT NULL;\n",- },- {- old: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,- new: `CREATE TABLE book(id INT DEFAULT 1 NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` INT DEFAULT 1 NOT NULL;\n",- },- {- old: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,- new: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,- want: "",- },- // Function Call- {- old: "CREATE TABLE action(action_id smallint(5) unsigned NOT NULL AUTO_INCREMENT," +- "`last_update` timestamp not null default current_timestamp);",- new: "CREATE TABLE action(action_id smallint(5) unsigned NOT NULL AUTO_INCREMENT," +- "`last_update` timestamp not null default current_timestamp(1));",- want: "ALTER TABLE `action` MODIFY COLUMN `last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(1);\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestColumnCollate(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Harry Potter' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL;\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Holmes' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Holmes' NOT NULL);`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Holmes' NOT NULL);`,- new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,- want: "",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestColumnOrder(t *testing.T) {- tests := []testCase{- // Append the column to the end of the table.- {- old: `CREATE TABLE book(id INT PRIMARY KEY);`,- new: `CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, author VARCHAR(50) NOT NULL);`,- want: "ALTER TABLE `book` ADD COLUMN `name` VARCHAR(50) NOT NULL AFTER `id`;\nALTER TABLE `book` ADD COLUMN `author` VARCHAR(50) NOT NULL AFTER `name`;\n",- },- // Add the column at the beginning of the table.- {- old: `CREATE TABLE book(author VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, author VARCHAR(50) NOT NULL);`,- want: "ALTER TABLE `book` ADD COLUMN `id` INT PRIMARY KEY FIRST;\nALTER TABLE `book` ADD COLUMN `name` VARCHAR(50) NOT NULL AFTER `id`;\n",- },- // Add the column in the middle of the table.- {- old: `CREATE TABLE book(id INT PRIMARY KEY, author VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, author VARCHAR(50) NOT NULL);`,- want: "ALTER TABLE `book` ADD COLUMN `name` VARCHAR(50) NOT NULL AFTER `id`;\n",- },- // Modify the existing column order.- {- old: `CREATE TABLE book(author VARCHAR(50) NOT NULL, id INT PRIMARY KEY, name VARCHAR(50) NOT NULL);`,- new: `CREATE TABLE book(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, author VARCHAR(50) NOT NULL);`,- want: "ALTER TABLE `book` MODIFY COLUMN `id` INT PRIMARY KEY FIRST;\nALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NOT NULL AFTER `id`;\n",- },- // Complicated case.- {- old: `CREATE TABLE book(c1 INT, c2 INT, c3 INT, c4 INT, c5 INT);`,- new: `CREATE TABLE book(c6 INT, c2 INT, c3 INT, c7 INT, c8 INT, c4 INT, c5 INT);`,- want: "ALTER TABLE `book` ADD COLUMN `c6` INT FIRST;\n" + // c6, c1, c2, c3, c4, c5- "ALTER TABLE `book` ADD COLUMN `c7` INT AFTER `c3`;\n" + // c6, c1, c2, c3, c7, c4, c5- "ALTER TABLE `book` ADD COLUMN `c8` INT AFTER `c7`;\n" + // c6, c1, c2, c3, c7, c8, c4, c5- "ALTER TABLE `book` DROP COLUMN `c1`;\n\n", // c6, c2, c3, c7, c8, c4, c5-- },- {- old: `CREATE TABLE book(c1 INT, c2 INT, c3 INT, c4 INT, c8 INT);`,- new: `CREATE TABLE book(c9 INT, c8 VARCHAR(10), c4 INT, c2 VARCHAR(10), c9 INT);`,- want: "ALTER TABLE `book` ADD COLUMN `c9` INT FIRST;\n" +- "ALTER TABLE `book` MODIFY COLUMN `c8` VARCHAR(10) AFTER `c9`;\n" +- "ALTER TABLE `book` MODIFY COLUMN `c4` INT AFTER `c8`;\n" +- "ALTER TABLE `book` MODIFY COLUMN `c2` VARCHAR(10);\n" +- "ALTER TABLE `book` ADD COLUMN `c9` INT AFTER `c2`;\n" +- "ALTER TABLE `book` DROP COLUMN `c1`;\n\n" +- "ALTER TABLE `book` DROP COLUMN `c3`;\n\n",- },- {- old: `CREATE TABLE t(a int);`,- new: `CREATE TABLE t(b int);`,- want: "ALTER TABLE `t` ADD COLUMN `b` INT FIRST;\n" +- "ALTER TABLE `t` DROP COLUMN `a`;\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}diff --git a/backend/plugin/parser/tidb/differ_constraint_test.go b/backend/plugin/parser/tidb/differ_constraint_test.godeleted file mode 100644index 9770ea2b54..0000000000--- a/backend/plugin/parser/tidb/differ_constraint_test.go+++ /dev/null@@ -1,529 +0,0 @@-package tidb--import (- "testing"-- "github.com/pingcap/tidb/pkg/parser/ast"- "github.com/pingcap/tidb/pkg/parser/opcode"- "github.com/pingcap/tidb/pkg/types"- driver "github.com/pingcap/tidb/pkg/types/parser_driver"- "github.com/stretchr/testify/require"-)--func TestIsKeyPartEqual(t *testing.T) {- tests := []struct {- old []*ast.IndexPartSpecification- new []*ast.IndexPartSpecification- eq bool- }{- {- old: []*ast.IndexPartSpecification{- // `id` + 1- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Plus,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- },- new: []*ast.IndexPartSpecification{- // `id` * 2- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Mul,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(2),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- },- eq: false,- },- {- old: []*ast.IndexPartSpecification{- // `id` + 1- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Plus,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- },- new: []*ast.IndexPartSpecification{- // `id` + 1- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Plus,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- // `id` * 2- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Mul,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(2),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- },- eq: false,- },- {- old: []*ast.IndexPartSpecification{- // `id` + 1- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Plus,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- },- new: []*ast.IndexPartSpecification{- // `id` + 1- {- Expr: &ast.BinaryOperationExpr{- Op: opcode.Plus,- L: &ast.ColumnNameExpr{- Name: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- R: &driver.ValueExpr{- Datum: types.NewDatum(1),- },- },- Column: &ast.ColumnName{- Name: ast.NewCIStr("id"),- },- },- },- eq: true,- },- }- a := require.New(t)- for _, test := range tests {- got := isKeyPartEqual(test.old, test.new)- a.Equalf(test.eq, got, "old: %v, new: %v", test.old, test.new)- }-}--func TestIsIndexOptionEqual(t *testing.T) {- tests := []struct {- old *ast.IndexOption- new *ast.IndexOption- eq bool- }{- {- old: nil,- new: nil,- eq: true,- },- {- old: &ast.IndexOption{- KeyBlockSize: 1024,- },- new: nil,- eq: false,- },- {- old: &ast.IndexOption{- KeyBlockSize: 1024,- },- new: nil,- eq: false,- },- {- old: &ast.IndexOption{- KeyBlockSize: 1024,- Tp: ast.IndexTypeBtree,- ParserName: ast.NewCIStr("parser"),- Comment: "comment",- Visibility: ast.IndexVisibilityVisible,- },- new: &ast.IndexOption{- KeyBlockSize: 1024,- Tp: ast.IndexTypeHash,- ParserName: ast.NewCIStr("parser"),- Comment: "commen_idx",- Visibility: ast.IndexVisibilityInvisible,- },- eq: false,- },- {- old: &ast.IndexOption{- KeyBlockSize: 1024,- Tp: ast.IndexTypeBtree,- ParserName: ast.NewCIStr("parser"),- Comment: "comment",- Visibility: ast.IndexVisibilityVisible,- },- new: &ast.IndexOption{- KeyBlockSize: 1024,- Tp: ast.IndexTypeBtree,- ParserName: ast.NewCIStr("parser"),- Comment: "comment",- Visibility: ast.IndexVisibilityVisible,- },- eq: true,- },- }-- a := require.New(t)- for _, test := range tests {- got := isIndexOptionEqual(test.old, test.new)- a.Equalf(test.eq, got, "old: %v, new: %v", test.old, test.new)- }-}--func TestIndexType(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE(name));`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx USING HASH(name));`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` (`name`) USING HASH;\n\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE(name));`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE(name));`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name));`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name));`,- want: "",- },- }-- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestIndexOption(t *testing.T) {- tests := []testCase{- // KEY_BLOCK_SIZE not match.- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name) KEY_BLOCK_SIZE=30);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name) KEY_BLOCK_SIZE=50);`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` (`name`) KEY_BLOCK_SIZE=50;\n\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY (name) KEY_BLOCK_SIZE=30);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY (name) KEY_BLOCK_SIZE=50);`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD PRIMARY KEY(`name`) KEY_BLOCK_SIZE=50;\n\n",- },- // WITH PARSER not match.- // {- // old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, FULLTEXT INDEX book_idx(name) WITH PARSER parser_a);`,- // new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, FULLTEXT INDEX book_idx(name) WITH PARSER parser_b);`,- // want: "DROP INDEX `book_idx` ON `book`;\n\n" +- // "CREATE FULLTEXT INDEX `book_idx` ON `book` (`name`) WITH PARSER `parser_b`;\n\n",- // },- // {- // old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY (name) WITH PARSER parser_a);`,- // new: `CREATE TABLE book(name VARCHAR(50) NOT NULL,CONSTRAINT PRIMARY KEY (name) WITH PARSER parser_b);`,- // want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- // "ALTER TABLE `book` ADD PRIMARY KEY (`name`) WITH PARSER `parser_b`;\n\n",- // },- // COMMENT not match.- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name) COMMENT 'comment_b');`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` (`name`) COMMENT 'comment_b';\n\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(name) COMMENT 'comment_b');`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD PRIMARY KEY(`name`) COMMENT 'comment_b';\n\n",- },- // VISIBILITY not match.- {-- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name) VISIBLE);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, INDEX book_idx(name) INVISIBLE);`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` (`name`) INVISIBLE;\n\n",- },- {-- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(name) VISIBLE);`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(name) INVISIBLE);`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD PRIMARY KEY(`name`) INVISIBLE;\n\n",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, FULLTEXT INDEX book_idx(name) KEY_BLOCK_SIZE=30 COMMENT 'no difference!');`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, FULLTEXT INDEX book_idx(name) KEY_BLOCK_SIZE=30 COMMENT 'no difference!');`,- want: "",- },- {- old: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(name) KEY_BLOCK_SIZE=30 COMMENT 'no difference!');`,- new: `CREATE TABLE book(name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(name) KEY_BLOCK_SIZE=30 COMMENT 'no difference!');`,- want: "",- },- }-- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestKeyPart(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE (id, name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE (id) COMMENT 'comment_a');`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` (`id`) USING BTREE COMMENT 'comment_a';\n\n",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(id, name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(id) COMMENT 'comment_a');`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD PRIMARY KEY(`id`) COMMENT 'comment_a';\n\n",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE (id, name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE ((id + 1)) COMMENT 'comment_a');`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` ((`id`+1)) USING BTREE COMMENT 'comment_a';\n\n",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY (id, name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY ((id + 1)) COMMENT 'comment_a');`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD PRIMARY KEY((`id`+1)) COMMENT 'comment_a';\n\n",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE ((id + 1)) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE ((id + 2)) COMMENT 'comment_a');`,- want: "DROP INDEX `book_idx` ON `book`;\n\n" +- "CREATE INDEX `book_idx` ON `book` ((`id`+2)) USING BTREE COMMENT 'comment_a';\n\n",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY ((id + 1)) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY ((id + 2)) COMMENT 'comment_a');`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD PRIMARY KEY((`id`+2)) COMMENT 'comment_a';\n\n",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE (id, name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE (id, name) COMMENT 'comment_a');`,- want: "",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY (id, name) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY (id, name) COMMENT 'comment_a');`,- want: "",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE ((id + 1)) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, INDEX book_idx USING BTREE ((id + 1)) COMMENT 'comment_a');`,- want: "",- },- {- old: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY ((id + 1)) COMMENT 'comment_a');`,- new: `CREATE TABLE book(id INT, name VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY ((id + 1)) COMMENT 'comment_a');`,- want: "",- },- }-- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestForeignKeyDefination(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE department(id INT, name VARCHAR(50) NOT NULL, PRIMARY KEY(department));- CREATE TABLE employee(id INT, name VARCHAR(50) NOT NULL, department_id INT, PRIMARY KEY(id), FOREIGN KEY employee_ibfk_1(department_id) REFERENCES department(id));`,- new: `CREATE TABLE department(id INT, name VARCHAR(50) NOT NULL, PRIMARY KEY(department));- CREATE TABLE employee(id INT, name VARCHAR(50) NOT NULL, department_id INT, PRIMARY KEY(id), FOREIGN KEY fk_2(department_id) REFERENCES department(id));`,- want: "ALTER TABLE `employee` DROP FOREIGN KEY `employee_ibfk_1`;\n\nALTER TABLE `employee` ADD CONSTRAINT `fk_2` FOREIGN KEY (`department_id`) REFERENCES `department`(`id`);\n\n",- },- {- old: "CREATE TABLE `department` (" +- " `id` int NOT NULL," +- " `name` varchar(50) NOT NULL," +- " PRIMARY KEY (`id`)," +- " KEY `id_name_idx` (`id`,`name`)" +- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;" +-- "CREATE TABLE `employee` (" +- " `id` int NOT NULL," +- " `name` varchar(50) NOT NULL," +- " `department_id` int DEFAULT NULL," +- " `department_name` varchar(50) DEFAULT NULL," +- " PRIMARY KEY (`id`)," +- " KEY `department_id_name_idx` (`department_id`,`department_name`)," +- " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`, `department_name`) REFERENCES `department` (`id`, `name`)" +- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",-- new: "CREATE TABLE `department` (" +- " `id` int NOT NULL," +- " `name` varchar(50) NOT NULL," +- " PRIMARY KEY (`id`)," +- " KEY `id_idx` (`id`)" +- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;" +-- "CREATE TABLE `employee` (" +- " `id` int NOT NULL," +- " `name` varchar(50) NOT NULL," +- " `department_id` int DEFAULT NULL," +- " `department_name` varchar(50) DEFAULT NULL," +- " PRIMARY KEY (`id`)," +- " KEY `department_id_idx` (`department_id`)," +- " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`)" +- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",-- want: "ALTER TABLE `employee` DROP FOREIGN KEY `employee_ibfk_1`;\n\n" +- "DROP INDEX `id_name_idx` ON `department`;\n\n" +- "DROP INDEX `department_id_name_idx` ON `employee`;\n\n" +- "CREATE INDEX `id_idx` ON `department` (`id`);\n\n" +- "CREATE INDEX `department_id_idx` ON `employee` (`department_id`);\n\n" +- "ALTER TABLE `employee` ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `department`(`id`);\n\n",- },- // Reference itself.- {- old: "CREATE TABLE `employeee` (" +- " `id` int NOT NULL," +- " `name` varchar(50) NOT NULL," +- " `leader_id` int DEFAULT NULL," +- " PRIMARY KEY (`id`)," +- " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`leader_id`) REFERENCES `employeee` (`id`)" +- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",-- new: "CREATE TABLE `employeee` (" +- " `id` int NOT NULL," +- " `name` varchar(50) NOT NULL," +- " `leader_id` int DEFAULT NULL," +- " `manager_id` int DEFAULT NULL," +- " PRIMARY KEY (`id`)," +- " CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`manager_id`) REFERENCES `employeee` (`id`)" +- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;",-- want: "ALTER TABLE `employeee` DROP FOREIGN KEY `employee_ibfk_1`;\n\n" +- "ALTER TABLE `employeee` ADD COLUMN `manager_id` INT DEFAULT NULL AFTER `leader_id`;\n" +- "ALTER TABLE `employeee` ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`manager_id`) REFERENCES `employeee`(`id`);\n\n",- },- }-- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestCheckConstraint(t *testing.T) {- tests := []testCase{- {- old: `CREATE TABLE book(a INT, CONSTRAINT t1_chk_1 CHECK (a>0))`,- new: `CREATE TABLE book(a INT, CONSTRAINT t1_chk_1 CHECK ((a > 0)))`,- want: "",- },- {- old: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0));",- new: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 1));",- want: "ALTER TABLE `book` DROP CHECK `check_price`;\n\n" +- "ALTER TABLE `book` ADD CONSTRAINT `check_price` CHECK(`price`>1) ENFORCED;\n\n",- },- {- old: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0));",- new: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0) NOT ENFORCED);",- want: "ALTER TABLE `book` DROP CHECK `check_price`;\n\n" +- "ALTER TABLE `book` ADD CONSTRAINT `check_price` CHECK(`price`>0) NOT ENFORCED;\n\n",- },- {- old: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0), CONSTRAINT `check_price2` CHECK(price > 1));",- new: "CREATE TABLE book(id INT, price INT, PRIMARY KEY(id), CONSTRAINT `check_price` CHECK (price > 0) NOT ENFORCED);",- want: "ALTER TABLE `book` DROP CHECK `check_price`;\n\n" +- "ALTER TABLE `book` DROP CHECK `check_price2`;\n\n" +- "ALTER TABLE `book` ADD CONSTRAINT `check_price` CHECK(`price`>0) NOT ENFORCED;\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestConstraint(t *testing.T) {- tests := []testCase{- // ADD COLUMN -> DROP PRIMARY KEY -> ADD PRIMARY KEY- {- old: `CREATE TABLE book(id INT, name VARCHAR(50), CONSTRAINT PRIMARY KEY(id, name));`,- new: `CREATE TABLE book(id INT, name VARCHAR(50), address VARCHAR(50) NOT NULL, CONSTRAINT PRIMARY KEY(id, address));`,- want: "ALTER TABLE `book` DROP PRIMARY KEY;\n\n" +- "ALTER TABLE `book` ADD COLUMN `address` VARCHAR(50) NOT NULL AFTER `name`;\n" +- "ALTER TABLE `book` ADD PRIMARY KEY(`id`, `address`);\n\n",- },- // ADD COLUMN -> ADD INDEX WITH ANOTHER NAME-> DROP INDEX- {- old: `CREATE TABLE book(id INT, name VARCHAR(50), INDEX id_name_idx (id, name));`,- new: `CREATE TABLE book(id INT, name VARCHAR(50), address VARCHAR(50) NOT NULL, INDEX id_address_idx (id, address));`,- want: "DROP INDEX `id_name_idx` ON `book`;\n\n" +- "ALTER TABLE `book` ADD COLUMN `address` VARCHAR(50) NOT NULL AFTER `name`;\n" +- "CREATE INDEX `id_address_idx` ON `book` (`id`, `address`);\n\n",- },- // ADD COLUMN -> ADD INDEX WITH SAME NAME -> DROP INDEX- {- old: `CREATE TABLE book(id INT, name VARCHAR(50), INDEX idx (id, name));`,- new: `CREATE TABLE book(id INT, name VARCHAR(50), address VARCHAR(50) NOT NULL, INDEX idx (id, address));`,- want: "DROP INDEX `idx` ON `book`;\n\n" +- "ALTER TABLE `book` ADD COLUMN `address` VARCHAR(50) NOT NULL AFTER `name`;\n" +- "CREATE INDEX `idx` ON `book` (`id`, `address`);\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}diff --git a/backend/plugin/parser/tidb/differ_table_test.go b/backend/plugin/parser/tidb/differ_table_test.godeleted file mode 100644index 1932bb67d2..0000000000--- a/backend/plugin/parser/tidb/differ_table_test.go+++ /dev/null@@ -1,384 +0,0 @@-package tidb--import (- "io"- "os"- "path/filepath"- "testing"-- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--func TestTable(t *testing.T) {- tests := []testCase{- {- old: ``,- new: `CREATE TABLE book(id INT, price INT, PRIMARY KEY(id));- CREATE TABLE author(id INT, name VARCHAR(255), PRIMARY KEY(id));- `,- want: "CREATE TABLE IF NOT EXISTS `author` (`id` INT,`name` VARCHAR(255),PRIMARY KEY(`id`));\n\n" +- "CREATE TABLE IF NOT EXISTS `book` (`id` INT,`price` INT,PRIMARY KEY(`id`));\n\n",- },- {- old: `CREATE TABLE author(id INT, name VARCHAR(255), PRIMARY KEY(id))`,- new: `CREATE TABLE book(id INT, price INT, PRIMARY KEY(id));- CREATE TABLE author(id INT, name VARCHAR(255), PRIMARY KEY(id));- `,- want: "CREATE TABLE IF NOT EXISTS `book` (`id` INT,`price` INT,PRIMARY KEY(`id`));\n\n",- },- {- old: `CREATE TABLE book(id INT, price INT, PRIMARY KEY(id));- CREATE TABLE author(id INT, name VARCHAR(255), PRIMARY KEY(id))`,- new: `CREATE TABLE book(id INT, price INT, PRIMARY KEY(id));- CREATE TABLE author(id INT, name VARCHAR(255), PRIMARY KEY(id));- `,- want: "",- },- // Drop excess table- {- old: `CREATE TABLE book(id INT, price_id INT, PRIMARY KEY(id), CONSTRAINT fk_price_id FOREIGN KEY (price_id) REFERENCES price(id));- CREATE TABLE price(id INT, PRIMARY KEY(id));`,- new: `CREATE TABLE book(id INT, PRIMARY KEY(id));`,- want: "ALTER TABLE `book` DROP FOREIGN KEY `fk_price_id`;\n\n" +- "DROP TABLE IF EXISTS `price`;\n\n" +- "ALTER TABLE `book` DROP COLUMN `price_id`;\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestSchemaDiffPartition(t *testing.T) {- testFile := "test_differ_partition.yaml"- runDifferTest(t, testFile, false /* record */)-}--type DifferTestData struct {- OldSchema string `yaml:"oldSchema"`- NewSchema string `yaml:"newSchema"`- Diff string `yal:"diff"`-}--func runDifferTest(t *testing.T, file string, record bool) {- var tests []DifferTestData- filepath := filepath.Join("test-data/differ", file)- yamlFile, err := os.Open(filepath)- require.NoError(t, err)- defer yamlFile.Close()-- byteValue, err := io.ReadAll(yamlFile)- require.NoError(t, err)- err = yaml.Unmarshal(byteValue, &tests)- require.NoError(t, err)-- for i, test := range tests {- diff, err := SchemaDiff(base.DiffContext{IgnoreCaseSensitive: false, StrictMode: true}, test.OldSchema, test.NewSchema)- require.NoErrorf(t, err, "Test Cases[%02d] Failed", i+1)- if record {- tests[i].Diff = diff- } else {- require.Equalf(t, test.Diff, diff, "Test Cases[%02d] Failed", i+1)- }- }-- if record {- err := yamlFile.Close()- require.NoError(t, err)- byteValue, err = yaml.Marshal(tests)- require.NoError(t, err)- err = os.WriteFile(filepath, byteValue, 0644)- require.NoError(t, err)- }-}--func TestTableOption(t *testing.T) {- tests := []testCase{- // AUTO_INCREMENT- {- old: `CREATE TABLE book(id INT AUTO_INCREMENT, CONSTRAINT PRIMARY KEY(id)) AUTO_INCREMENT = 4;`,- new: `CREATE TABLE book(id INT AUTO_INCREMENT, CONSTRAINT PRIMARY KEY(id)) AUTO_INCREMENT = 10;`,- want: "ALTER TABLE `book` AUTO_INCREMENT = 10;\n\n",- },- {- old: `CREATE TABLE book(id INT AUTO_INCREMENT, CONSTRAINT PRIMARY KEY(id)) AUTO_INCREMENT = 4;`,- new: `CREATE TABLE book(id INT AUTO_INCREMENT, CONSTRAINT PRIMARY KEY(id));`,- want: "ALTER TABLE `book` AUTO_INCREMENT = 0;\n\n",- },- // AVG_ROW_LENGTH- {- old: `CREATE TABLE book(id INT) AVG_ROW_LENGTH = 1;`,- new: `CREATE TABLE book(id INT) AVG_ROW_LENGTH = 2;`,- want: "ALTER TABLE `book` AVG_ROW_LENGTH = 2;\n\n",- },- {- old: `CREATE TABLE book(id INT) AVG_ROW_LENGTH = 1;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` AVG_ROW_LENGTH = 0;\n\n",- },- // DEFAULT CHARSET- {- old: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8;`,- new: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8mb4;`,- want: "ALTER TABLE `book` DEFAULT CHARACTER SET = UTF8MB4;\n\n",- },- {- old: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8;`,- new: `CREATE TABLE book(id INT);`,- want: "",- },- // DEFAULT COLLATE- {- old: `CREATE TABLE book(id INT) DEFAULT COLLATE = latin1_swedish_ci;`,- new: `CREATE TABLE book(id INT) DEFAULT COLLATE = utf8mb4_general_ci;`,- want: "ALTER TABLE `book` DEFAULT COLLATE = UTF8MB4_GENERAL_CI;\n\n",- },- {- old: `CREATE TABLE book(id INT) DEFAULT COLLATE = latin1_swedish_ci;`,- new: `CREATE TABLE book(id INT);`,- want: "",- },- // CHECKSUM- {- old: `CREATE TABLE book(id INT) CHECKSUM = 1;`,- new: `CREATE TABLE book(id INT) CHECKSUM = 0;`,- want: "ALTER TABLE `book` CHECKSUM = 0;\n\n",- },- {- old: `CREATE TABLE book(id INT) CHECKSUM = 1;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` CHECKSUM = 0;\n\n",- },- // COMMENT- {- old: `CREATE TABLE book(id INT) COMMENT = 'old';`,- new: `CREATE TABLE book(id INT) COMMENT = 'new';`,- want: "ALTER TABLE `book` COMMENT = 'new';\n\n",- },- {- old: `CREATE TABLE book(id INT) COMMENT = 'old';`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` COMMENT = '';\n\n",- },- // TODO(zp): handle drop COMPRESSION- {- old: `CREATE TABLE book(id INT) COMPRESSION = 'ZLIB';`,- new: `CREATE TABLE book(id INT) COMPRESSION = 'LZ4';`,- want: "ALTER TABLE `book` COMPRESSION = 'LZ4';\n\n",- },- // CONNECTION- {- old: `CREATE TABLE book(id INT) CONNECTION = 'old';`,- new: `CREATE TABLE book(id INT) CONNECTION = 'new';`,- want: "ALTER TABLE `book` CONNECTION = 'new';\n\n",- },- {- old: `CREATE TABLE book(id INT) CONNECTION = 'old';`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` CONNECTION = '';\n\n",- },- // TODO(zp): handle drop DATA DIRECTORY- {- old: `CREATE TABLE book(id INT) DATA DIRECTORY = 'old';`,- new: `CREATE TABLE book(id INT) DATA DIRECTORY = 'new';`,- want: "ALTER TABLE `book` DATA DIRECTORY = 'new';\n\n",- },- // TODO(zp): handle drop INDEX DIRECTORY- {- old: `CREATE TABLE book(id INT) INDEX DIRECTORY = 'old';`,- new: `CREATE TABLE book(id INT) INDEX DIRECTORY = 'new';`,- want: "ALTER TABLE `book` INDEX DIRECTORY = 'new';\n\n",- },- // DELAY_KEY_WRITE- {- old: `CREATE TABLE book(id INT) DELAY_KEY_WRITE = 1;`,- new: `CREATE TABLE book(id INT) DELAY_KEY_WRITE = 0;`,- want: "ALTER TABLE `book` DELAY_KEY_WRITE = 0;\n\n",- },- {- old: `CREATE TABLE book(id INT) DELAY_KEY_WRITE = 1;`,- new: `CREATE TABLE book(id INT)`,- want: "ALTER TABLE `book` DELAY_KEY_WRITE = 0;\n\n",- },- // ENCRYPTION- {- old: `CREATE TABLE book(id INT) ENCRYPTION = 'Y';`,- new: `CREATE TABLE book(id INT) ENCRYPTION = 'N';`,- want: "ALTER TABLE `book` ENCRYPTION = 'N';\n\n",- },- {- old: `CREATE TABLE book(id INT) ENCRYPTION = 'Y';`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` ENCRYPTION = 'N';\n\n",- },- // INSERT_METHOD- // TODO(zp): enable this test if the upstream repo fix it.- // https://github.com/pingcap/tidb/pull/38355- // {- // old: `CREATE TABLE book(id INT) INSERT_METHOD = LAST;`,- // new: `CREATE TABLE book(id INT) INSERT_METHOD = FIRST;`,- // want: "ALTER TABLE `book` INSERT_METHOD = FIRST;\n",- // },- // {- // old: `CREATE TABLE book(id INT) INSERT_METHOD = LAST;`,- // new: `CREATE TABLE book(id INT);`,- // want: "ALTER TABLE `book` INSERT_METHOD = NO;\n",- // },- // TODO(zp): KEY_BLOCK_SIZE-- // MAX_ROWS- {- old: `CREATE TABLE book(id INT) MAX_ROWS = 100;`,- new: `CREATE TABLE book(id INT) MAX_ROWS = 200;`,- want: "ALTER TABLE `book` MAX_ROWS = 200;\n\n",- },- {- old: `CREATE TABLE book(id INT) MAX_ROWS = 100;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` MAX_ROWS = 0;\n\n",- },- // MIN_ROWS- {- old: `CREATE TABLE book(id INT) MIN_ROWS = 100;`,- new: `CREATE TABLE book(id INT) MIN_ROWS = 200;`,- want: "ALTER TABLE `book` MIN_ROWS = 200;\n\n",- },- {- old: `CREATE TABLE book(id INT) MIN_ROWS = 100;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` MIN_ROWS = 0;\n\n",- },- // PACK_KEYS- // TODO(zp): alter table pack_keys- {- old: `CREATE TABLE book(id INT) PACK_KEYS = 1;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` PACK_KEYS = DEFAULT /* TableOptionPackKeys is not supported */ ;\n\n",- },- // PACK_KEYS- // TODO(zp): alter table pack_keys- {- old: `CREATE TABLE book(id INT) PACK_KEYS = 1;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` PACK_KEYS = DEFAULT /* TableOptionPackKeys is not supported */ ;\n\n",- },- // PASSWORD- {- old: `CREATE TABLE book(id INT) PASSWORD = 'old';`,- new: `CREATE TABLE book(id INT) PASSWORD = 'new';`,- want: "ALTER TABLE `book` PASSWORD = 'new';\n\n",- },- {- old: `CREATE TABLE book(id INT) PASSWORD = 'old';`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` PASSWORD = '';\n\n",- },- // ROW_FORMAT- {- old: `CREATE TABLE book(id INT) ROW_FORMAT = DYNAMIC;`,- new: `CREATE TABLE book(id INT) ROW_FORMAT = COMPRESSED;`,- want: "ALTER TABLE `book` ROW_FORMAT = COMPRESSED;\n\n",- },- {- old: `CREATE TABLE book(id INT) ROW_FORMAT = DYNAMIC;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` ROW_FORMAT = DEFAULT;\n\n",- },- // STATS_AUTO_RECALC- {- old: `CREATE TABLE book(id INT) STATS_AUTO_RECALC = 1;`,- new: `CREATE TABLE book(id INT) STATS_AUTO_RECALC = 0;`,- want: "ALTER TABLE `book` STATS_AUTO_RECALC = 0;\n\n",- },- {- old: `CREATE TABLE book(id INT) STATS_AUTO_RECALC = 1;`,- new: `CREATE TABLE book(id INT);`,- want: "ALTER TABLE `book` STATS_AUTO_RECALC = DEFAULT;\n\n",- },- // TODO(zp): STATS_PERSISTENT-- // UNION- {- old: `CREATE TABLE book(id INT) UNION = (book2);`,- new: `CREATE TABLE book(id INT) UNION = (book2, book3);`,- want: "ALTER TABLE `book` UNION = (`book2`,`book3`);\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}--func TestView(t *testing.T) {- tests := []testCase{- {- old: `CREATE VIEW book AS SELECT * FROM book;`,- new: `CREATE VIEW book AS SELECT * FROM book2;`,- want: "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `book` AS SELECT * FROM `book2`;\n\n",- },- {- old: `CREATE VIEW order_incomes AS- SELECT- order_id,- customer_name,- SUM(ordered_quantity * product_price) total- FROM- order_details- INNER JOIN orders USING (order_id)- INNER JOIN customers USING (customer_name)- GROUP BY order_id;`,-- new: `CREATE VIEW order_incomes AS- SELECT- order_id,- customer_name,- SUM(ordered_quantity * product_price) + 1 total- FROM- order_details- INNER JOIN orders USING (order_id)- INNER JOIN customers USING (customer_name)- GROUP BY order_id;`,-- want: "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `order_incomes` AS " +- "SELECT `order_id`,`customer_name`,SUM(`ordered_quantity`*`product_price`)+1 AS `total` " +- "FROM (`order_details` JOIN `orders` USING (`order_id`)) JOIN `customers` USING (`customer_name`) GROUP BY `order_id`;\n\n",- },- // mysqldump temporary view- {- old: `CREATE VIEW a AS SELECT 1 AS id, 1 AS name;- CREATE VIEW a AS SELECT id, name FROM book;- `,-- new: `CREATE VIEW a AS SELECT 1 AS id;- CREATE VIEW a AS SELECT id FROM book;- `,-- want: "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `a` AS SELECT `id` FROM `book`;\n\n",- },- // SDL for gitops dependency view- {- old: `CREATE VIEW a AS SELECT id, name FROM book`,- new: `CREATE VIEW a AS SELECT id FROM book;- CREATE VIEW b AS SELECT id FROM a;- `,- want: "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `b` AS SELECT 1 AS `id`;\n\n" +- "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `a` AS SELECT `id` FROM `book`;\n\n" +- "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `b` AS SELECT `id` FROM `a`;\n\n",- },- {- old: `CREATE VIEW a AS SELECT id, name FROM book`,- new: `CREATE VIEW a AS SELECT id AS a_id FROM book;- CREATE VIEW b AS SELECT a_id AS b_id FROM a;- `,- want: "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `b` AS SELECT 1 AS `b_id`;\n\n" +- "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `a` AS SELECT `id` AS `a_id` FROM `book`;\n\n" +- "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `b` AS SELECT `a_id` AS `b_id` FROM `a`;\n\n",- },- {- old: ``,- new: `CREATE VIEW a AS WITH cte AS (SELECT id, name FROM book) SELECT id, name FROM cte UNION SELECT c, d FROM e;`,- want: "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `a` AS SELECT 1 AS `id`,1 AS `name`;\n\n" +- "CREATE OR REPLACE ALGORITHM = UNDEFINED DEFINER = CURRENT_USER SQL SECURITY DEFINER VIEW `a` AS WITH `cte` AS (SELECT `id`,`name` FROM `book`) SELECT `id`,`name` FROM `cte` UNION SELECT `c`,`d` FROM `e`;\n\n",- },- }- testDiffWithoutDisableForeignKeyCheck(t, tests)-}diff --git a/backend/plugin/parser/tidb/differ_test.go b/backend/plugin/parser/tidb/differ_test.godeleted file mode 100644index c58c32e000..0000000000--- a/backend/plugin/parser/tidb/differ_test.go+++ /dev/null@@ -1,27 +0,0 @@-package tidb--import (- "testing"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type testCase struct {- old string- new string- want string-}--func testDiffWithoutDisableForeignKeyCheck(t *testing.T, testCases []testCase) {- a := require.New(t)- for _, test := range testCases {- out, err := SchemaDiff(base.DiffContext{- IgnoreCaseSensitive: true,- StrictMode: true,- }, test.old, test.new)- a.NoError(err)- a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)- }-}diff --git a/backend/plugin/parser/tidb/differ_utils.go b/backend/plugin/parser/tidb/differ_utils.godeleted file mode 100644index 1f071d33b1..0000000000--- a/backend/plugin/parser/tidb/differ_utils.go+++ /dev/null@@ -1,28 +0,0 @@-package tidb--import (- "bytes"-- "github.com/pingcap/tidb/pkg/parser/ast"- "github.com/pingcap/tidb/pkg/parser/format"-- "github.com/pkg/errors"-)--func toString(node ast.Node) (string, error) {- var buf bytes.Buffer- restoreFlag := format.DefaultRestoreFlags | format.RestoreStringWithoutCharset- if err := node.Restore(format.NewRestoreCtx(restoreFlag, &buf)); err != nil {- return "", errors.Wrapf(err, "cannot restore node %v", node)- }- return buf.String(), nil-}--func toLowerNameString(node ast.Node) (string, error) {- var buf bytes.Buffer- restoreFlag := format.DefaultRestoreFlags | format.RestoreStringWithoutCharset | format.RestoreNameLowercase- if err := node.Restore(format.NewRestoreCtx(restoreFlag, &buf)); err != nil {- return "", errors.Wrapf(err, "cannot restore node %v", node)- }- return buf.String(), nil-}diff --git a/backend/plugin/parser/tsql/diff_test.go b/backend/plugin/parser/tsql/diff_test.godeleted file mode 100644index 6f20db171f..0000000000--- a/backend/plugin/parser/tsql/diff_test.go+++ /dev/null@@ -1,60 +0,0 @@-package tsql--import (- "io"- "os"- "path/filepath"- "testing"-- "github.com/stretchr/testify/require"- "gopkg.in/yaml.v3"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--type DifferTestData struct {- OldSchema string `yaml:"oldSchema"`- NewSchema string `yaml:"newSchema"`- Diff string `yaml:"diff"`-}--func runDifferTest(t *testing.T, file string, record bool) {- var tests []DifferTestData- filepath := filepath.Join("test-data", file)- yamlFile, err := os.Open(filepath)- require.NoError(t, err)- defer yamlFile.Close()-- byteValue, err := io.ReadAll(yamlFile)- require.NoError(t, err)- err = yaml.Unmarshal(byteValue, &tests)- require.NoError(t, err)-- for i, test := range tests {- diff, err := SchemaDiff(base.DiffContext{}, test.OldSchema, test.NewSchema)- require.NoError(t, err)- if record {- tests[i].Diff = diff- } else {- require.Equal(t, test.Diff, diff, test.OldSchema)- }- }-- if record {- err := yamlFile.Close()- require.NoError(t, err)- byteValue, err = yaml.Marshal(tests)- require.NoError(t, err)- err = os.WriteFile(filepath, byteValue, 0644)- require.NoError(t, err)- }-}--func TestTSQLDiffer(t *testing.T) {- testFileList := []string{- "test_differ_data.yaml",- }- for _, file := range testFileList {- runDifferTest(t, file, false /* record */)- }-}diff --git a/backend/plugin/parser/tsql/differ.go b/backend/plugin/parser/tsql/differ.godeleted file mode 100644index b0041267a1..0000000000--- a/backend/plugin/parser/tsql/differ.go+++ /dev/null@@ -1,723 +0,0 @@-package tsql--import (- "fmt"- "slices"- "strings"-- "github.com/antlr4-go/antlr/v4"- "github.com/pkg/errors"-- "github.com/bytebase/parser/tsql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/parser/base"-)--// In fact, SQL Server is possible to create a case-sensitive database and case-insensitive database on one instance.-// https://www.webucator.com/article/how-to-check-case-sensitivity-in-sql-server/-// But by default, SQL Server is case-insensitive.--func init() {- base.RegisterSchemaDiffFunc(storepb.Engine_MSSQL, SchemaDiff)-}--type diffNode struct {- dropConstraint []string- dropIndex []string- dropColumn []string- dropTable []string- dropSchema []string- createSchema []string- createTable []string- addColumn []string- modifyColumn []string- addIndex []string- addConstraint []string-}--func (d *diffNode) String() (string, error) {- var buf strings.Builder- for _, dropConstraint := range d.dropConstraint {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", dropConstraint); err != nil {- return "", err- }- }- for _, dropIndex := range d.dropIndex {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", dropIndex); err != nil {- return "", err- }- }- for _, dropColumn := range d.dropColumn {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", dropColumn); err != nil {- return "", err- }- }- for _, dropTable := range d.dropTable {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", dropTable); err != nil {- return "", err- }- }- for _, dropSchema := range d.dropSchema {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", dropSchema); err != nil {- return "", err- }- }- for _, createSchema := range d.createSchema {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", createSchema); err != nil {- return "", err- }- }- for _, createTable := range d.createTable {- if _, err := fmt.Fprintf(&buf, "%s\n\n", createTable); err != nil {- return "", err- }- }- for _, addColumn := range d.addColumn {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", addColumn); err != nil {- return "", err- }- }- for _, modifyColumn := range d.modifyColumn {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", modifyColumn); err != nil {- return "", err- }- }- for _, addIndex := range d.addIndex {- if _, err := fmt.Fprintf(&buf, "%s\n\n", addIndex); err != nil {- return "", err- }- }- for _, addConstraint := range d.addConstraint {- if _, err := fmt.Fprintf(&buf, "%s;\n\n", addConstraint); err != nil {- return "", err- }- }- return buf.String(), nil-}--func SchemaDiff(_ base.DiffContext, oldStmt, newStmt string) (string, error) {- oldSchemaMap, err := buildSchemaMap(oldStmt)- if err != nil {- return "", errors.Wrapf(err, "failed to build schema info for old statement")- }- newSchemaMap, err := buildSchemaMap(newStmt)- if err != nil {- return "", errors.Wrapf(err, "failed to build schema info for new statement")- }-- diff := &diffNode{}- var newSchemas []*schemaInfo- for _, newSchema := range newSchemaMap {- newSchemas = append(newSchemas, newSchema)- }- slices.SortFunc(newSchemas, func(i, j *schemaInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, newSchema := range newSchemas {- lowerSchema := newSchema.lowerName- oldSchema, exists := oldSchemaMap[lowerSchema]- if !exists {- diff.addSchema(newSchema)- continue- }- oldSchema.existsInNew = true- if err := diff.diffSchema(oldSchema, newSchema); err != nil {- return "", errors.Wrapf(err, "failed to diff schema %q", newSchema.name)- }- }- var remainingSchemas []*schemaInfo- for _, oldSchema := range oldSchemaMap {- if oldSchema.existsInNew {- continue- }- remainingSchemas = append(remainingSchemas, oldSchema)- }- slices.SortFunc(remainingSchemas, func(i, j *schemaInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, oldSchema := range remainingSchemas {- diff.dropFullSchema(oldSchema)- oldSchema.existsInNew = true- }- return diff.String()-}--func (d *diffNode) dropFullSchema(schema *schemaInfo) {- var tables []*tableInfo- for _, table := range schema.tableMap {- tables = append(tables, table)- }- slices.SortFunc(tables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, table := range tables {- d.dropFullTable(schema.name, table)- }- d.dropSchema = append(d.dropSchema, fmt.Sprintf("DROP SCHEMA [%s]", schema.name))-}--func (d *diffNode) diffSchema(oldSchema, newSchema *schemaInfo) error {- var newTables []*tableInfo- for _, newTable := range newSchema.tableMap {- newTables = append(newTables, newTable)- }- slices.SortFunc(newTables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, newTable := range newTables {- lowerTable := newTable.lowerName- oldTable, exists := oldSchema.tableMap[lowerTable]- if !exists {- d.createTable = append(d.createTable, newTable.node.GetParser().GetTokenStream().GetTextFromRuleContext(newTable.node))- continue- }- oldTable.existsInNew = true- if err := d.diffTable(newSchema.name, oldTable, newTable); err != nil {- return errors.Wrapf(err, "failed to diff table %q", newTable.name)- }- }- var remainingTables []*tableInfo- for _, oldTable := range oldSchema.tableMap {- if oldTable.existsInNew {- continue- }- remainingTables = append(remainingTables, oldTable)- }- slices.SortFunc(remainingTables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, oldTable := range remainingTables {- d.dropFullTable(oldSchema.name, oldTable)- oldTable.existsInNew = true- }- return nil-}--func (d *diffNode) dropFullTable(schemaName string, table *tableInfo) {- var indexes []*indexInfo- for _, index := range table.indexMap {- indexes = append(indexes, index)- }- slices.SortFunc(indexes, func(i, j *indexInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, index := range indexes {- d.dropIndex = append(d.dropIndex, fmt.Sprintf("DROP INDEX [%s] ON [%s].[%s]", index.name, schemaName, table.name))- }- d.dropTable = append(d.dropTable, fmt.Sprintf("DROP TABLE [%s].[%s]", schemaName, table.name))-}--func (d *diffNode) diffTable(schemaName string, oldTable, newTable *tableInfo) error {- if err := d.diffColumn(schemaName, oldTable, newTable); err != nil {- return errors.Wrapf(err, "failed to diff column for table %q", newTable.name)- }- if err := d.diffConstraint(schemaName, oldTable, newTable); err != nil {- return errors.Wrapf(err, "failed to diff constraint for table %q", newTable.name)- }-- var newIndexes []*indexInfo- for _, newIndex := range newTable.indexMap {- newIndexes = append(newIndexes, newIndex)- }- slices.SortFunc(newIndexes, func(i, j *indexInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, newIndex := range newIndexes {- lowerIndex := newIndex.lowerName- oldIndex, exists := oldTable.indexMap[lowerIndex]- if !exists {- d.addIndex = append(d.addIndex, newIndex.node.GetParser().GetTokenStream().GetTextFromRuleContext(newIndex.node))- continue- }- oldIndex.existsInNew = true- d.diffIndex(schemaName, oldTable.name, oldIndex, newIndex)- }- var remainingIndexes []*indexInfo- for _, oldIndex := range oldTable.indexMap {- if oldIndex.existsInNew {- continue- }- remainingIndexes = append(remainingIndexes, oldIndex)- }- slices.SortFunc(remainingIndexes, func(i, j *indexInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, oldIndex := range remainingIndexes {- d.dropIndex = append(d.dropIndex, fmt.Sprintf("DROP INDEX [%s] ON [%s].[%s]", oldIndex.name, schemaName, oldTable.name))- oldIndex.existsInNew = true- }- return nil-}--func (d *diffNode) diffIndex(schemaName, tableName string, oldIndex, newIndex *indexInfo) {- oldString := oldIndex.node.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- oldIndex.node.GetStart().GetTokenIndex(),- oldIndex.node.Id_(0).GetStart().GetTokenIndex()-1,- )) + oldIndex.node.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- oldIndex.node.Table_name().GetStop().GetTokenIndex()+1,- oldIndex.node.GetStop().GetTokenIndex(),- ))- newString := newIndex.node.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- newIndex.node.GetStart().GetTokenIndex(),- newIndex.node.Id_(0).GetStart().GetTokenIndex()-1,- )) + newIndex.node.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- newIndex.node.Table_name().GetStop().GetTokenIndex()+1,- newIndex.node.GetStop().GetTokenIndex(),- ))-- if oldString != newString {- d.dropIndex = append(d.dropIndex, fmt.Sprintf("DROP INDEX [%s] ON [%s].[%s]", oldIndex.name, schemaName, tableName))- d.addIndex = append(d.addIndex, newIndex.node.GetParser().GetTokenStream().GetTextFromRuleContext(newIndex.node))- }-}--func (d *diffNode) diffConstraint(schemaName string, oldTable, newTable *tableInfo) error {- var addConstraints []tsql.ITable_constraintContext- var dropConstraints []string-- oldConstraintMap := buildConstraintMap(oldTable)- for _, item := range newTable.node.Column_def_table_constraints().AllColumn_def_table_constraint() {- constraint := item.Table_constraint()- if constraint == nil {- continue- }- if constraint.Id_(0) == nil {- continue- }- _, lowerConstraint := NormalizeTSQLIdentifier(constraint.Id_(0))- oldConstraint, ok := oldConstraintMap[lowerConstraint]- if !ok {- addConstraints = append(addConstraints, constraint)- continue- }-- if !isConstraintEqual(oldConstraint, constraint) {- dropConstraints = append(dropConstraints, lowerConstraint)- addConstraints = append(addConstraints, constraint)- }- delete(oldConstraintMap, lowerConstraint)- }- for _, constraint := range oldConstraintMap {- constraintName, _ := NormalizeTSQLIdentifier(constraint.Id_(0))- dropConstraints = append(dropConstraints, constraintName)- }-- return d.appendConstraintDiff(schemaName, newTable.name, addConstraints, dropConstraints)-}--func (d *diffNode) appendConstraintDiff(schemaName, tableName string, addConstraints []tsql.ITable_constraintContext, dropConstraints []string) error {- if len(addConstraints) > 0 {- if err := d.appendAddConstraint(schemaName, tableName, addConstraints); err != nil {- return err- }- }- if len(dropConstraints) > 0 {- if err := d.appendDropConstraint(schemaName, tableName, dropConstraints); err != nil {- return err- }- }- return nil-}--func (d *diffNode) appendDropConstraint(schemaName, tableName string, dropConstraints []string) error {- for _, constraintName := range dropConstraints {- var buf strings.Builder- if _, err := fmt.Fprintf(&buf, "ALTER TABLE [%s].[%s] DROP CONSTRAINT [%s]", schemaName, tableName, constraintName); err != nil {- return err- }- d.dropConstraint = append(d.dropConstraint, buf.String())- }- return nil-}--func (d *diffNode) appendAddConstraint(schemaName, tableName string, addConstraints []tsql.ITable_constraintContext) error {- var buf strings.Builder-- if _, err := fmt.Fprintf(&buf, "ALTER TABLE [%s].[%s] ADD", schemaName, tableName); err != nil {- return err- }- for i, addConstraint := range addConstraints {- if i > 0 {- if _, err := fmt.Fprint(&buf, ","); err != nil {- return err- }- }- if _, err := fmt.Fprintf(&buf, "\n %s", addConstraint.GetParser().GetTokenStream().GetTextFromRuleContext(addConstraint)); err != nil {- return err- }- }- d.addConstraint = append(d.addConstraint, buf.String())- return nil-}--func (d *diffNode) diffColumn(schemaName string, oldTable, newTable *tableInfo) error {- var addColumns []tsql.IColumn_definitionContext- var modifyColumns []tsql.IColumn_definitionContext- var dropColumns []string-- oldColumnMap := buildColumnMap(oldTable)- for _, item := range newTable.node.Column_def_table_constraints().AllColumn_def_table_constraint() {- column := item.Column_definition()- if column == nil {- continue- }- _, lowerColumn := NormalizeTSQLIdentifier(column.Id_())- oldColumn, ok := oldColumnMap[lowerColumn]- if !ok {- addColumns = append(addColumns, column)- continue- }-- // Compare the column definition.- if !isColumnEqual(oldColumn, column) {- modifyColumns = append(modifyColumns, column)- }- delete(oldColumnMap, lowerColumn)- }- for _, column := range oldColumnMap {- columnName, _ := NormalizeTSQLIdentifier(column.Id_())- dropColumns = append(dropColumns, columnName)- }-- return d.appendColumnDiff(schemaName, newTable.name, addColumns, modifyColumns, dropColumns)-}--func (d *diffNode) appendColumnDiff(schemaName, tableName string, addColumns, modifyColumns []tsql.IColumn_definitionContext, dropColumns []string) error {- if len(addColumns) > 0 {- if err := d.appendAddColumn(schemaName, tableName, addColumns); err != nil {- return err- }- }- if len(modifyColumns) > 0 {- if err := d.appendModifyColumn(schemaName, tableName, modifyColumns); err != nil {- return err- }- }- if len(dropColumns) > 0 {- if err := d.appendDropColumn(schemaName, tableName, dropColumns); err != nil {- return err- }- }- return nil-}--func (d *diffNode) appendDropColumn(schemaName, tableName string, dropColumns []string) error {- for _, columnName := range dropColumns {- var buf strings.Builder- if _, err := fmt.Fprintf(&buf, "ALTER TABLE [%s].[%s] DROP COLUMN [%s]", schemaName, tableName, columnName); err != nil {- return err- }- d.dropColumn = append(d.dropColumn, buf.String())- }- return nil-}--func (d *diffNode) appendModifyColumn(schemaName, tableName string, modifyColumns []tsql.IColumn_definitionContext) error {- for _, modifyColumn := range modifyColumns {- var buf strings.Builder- if _, err := fmt.Fprintf(&buf, "ALTER TABLE [%s].[%s] ALTER COLUMN %s", schemaName, tableName, modifyColumn.GetParser().GetTokenStream().GetTextFromRuleContext(modifyColumn)); err != nil {- return err- }- d.modifyColumn = append(d.modifyColumn, buf.String())- }- return nil-}--func (d *diffNode) appendAddColumn(schemaName, tableName string, addColumns []tsql.IColumn_definitionContext) error {- var buf strings.Builder-- if _, err := fmt.Fprintf(&buf, "ALTER TABLE [%s].[%s] ADD", schemaName, tableName); err != nil {- return err- }- for i, addColumn := range addColumns {- if i > 0 {- if _, err := fmt.Fprint(&buf, ","); err != nil {- return err- }- }- if _, err := fmt.Fprintf(&buf, "\n %s", addColumn.GetParser().GetTokenStream().GetTextFromRuleContext(addColumn)); err != nil {- return err- }- }- d.addColumn = append(d.addColumn, buf.String())- return nil-}--func isColumnEqual(oldColumn, newColumn tsql.IColumn_definitionContext) bool {- oldString := oldColumn.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- oldColumn.Id_().GetStop().GetTokenIndex()+1,- oldColumn.GetStop().GetTokenIndex(),- ))- newString := newColumn.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- newColumn.Id_().GetStop().GetTokenIndex()+1,- newColumn.GetStop().GetTokenIndex(),- ))- return oldString == newString-}--func buildColumnMap(table *tableInfo) map[string]tsql.IColumn_definitionContext {- m := make(map[string]tsql.IColumn_definitionContext)- for _, item := range table.node.Column_def_table_constraints().AllColumn_def_table_constraint() {- if column := item.Column_definition(); column != nil {- _, lowerColumn := NormalizeTSQLIdentifier(column.Id_())- m[lowerColumn] = column- }- }- return m-}--func isConstraintEqual(oldConstraint, newConstraint tsql.ITable_constraintContext) bool {- oldString := oldConstraint.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- oldConstraint.Id_(0).GetStop().GetTokenIndex()+1,- oldConstraint.GetStop().GetTokenIndex(),- ))- newString := newConstraint.GetParser().GetTokenStream().GetTextFromInterval(- antlr.NewInterval(- newConstraint.Id_(0).GetStop().GetTokenIndex()+1,- newConstraint.GetStop().GetTokenIndex(),- ))- return oldString == newString-}--func buildConstraintMap(table *tableInfo) map[string]tsql.ITable_constraintContext {- m := make(map[string]tsql.ITable_constraintContext)- for _, item := range table.node.Column_def_table_constraints().AllColumn_def_table_constraint() {- if constraint := item.Table_constraint(); constraint != nil {- if constraint.Id_(0) == nil {- continue- }- _, lowerConstraint := NormalizeTSQLIdentifier(constraint.Id_(0))- m[lowerConstraint] = constraint- }- }- return m-}--func (d *diffNode) addSchema(schema *schemaInfo) {- d.createSchema = append(d.createSchema, schema.node.GetParser().GetTokenStream().GetTextFromRuleContext(schema.node))-- var newTables []*tableInfo- for _, table := range schema.tableMap {- newTables = append(newTables, table)- }- slices.SortFunc(newTables, func(i, j *tableInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })-- for _, table := range newTables {- d.createTable = append(d.createTable, table.node.GetParser().GetTokenStream().GetTextFromRuleContext(table.node))- var newIndexes []*indexInfo- for _, index := range table.indexMap {- newIndexes = append(newIndexes, index)- }- slices.SortFunc(newIndexes, func(i, j *indexInfo) int {- if i.id < j.id {- return -1- }- if i.id > j.id {- return 1- }- return 0- })- for _, index := range newIndexes {- d.addIndex = append(d.addIndex, index.node.GetParser().GetTokenStream().GetTextFromRuleContext(index.node))- }- }-}--func buildSchemaMap(stmt string) (schemaMap, error) {- node, err := ParseTSQL(stmt)- if err != nil {- return nil, errors.Wrapf(err, "failed to parse TSQL statement")- }-- m := make(schemaMap)- m[defaultSchema] = newSchemaInfo(0, defaultSchema, nil)- listener := &buildSchemaInfoListener{- m: m,- }-- antlr.ParseTreeWalkerDefault.Walk(listener, node.Tree)- return listener.m, listener.err-}--type buildSchemaInfoListener struct {- *tsql.BaseTSqlParserListener-- m schemaMap- err error-}--func (l *buildSchemaInfoListener) EnterCreate_schema(ctx *tsql.Create_schemaContext) {- if l.err != nil {- return- }- if ctx.GetSchema_name() == nil {- return- }- originalName, lowerName := NormalizeTSQLIdentifier(ctx.GetSchema_name())- l.m[lowerName] = newSchemaInfo(len(l.m), originalName, ctx)-}--func (l *buildSchemaInfoListener) EnterCreate_table(ctx *tsql.Create_tableContext) {- if l.err != nil {- return- }- _, schemaName, tableName := normalizeTableNameSeparated(ctx.Table_name(), "" /* fallbackDatabase */, defaultSchema, false /* caseSensitive */)- schemaInfo := l.m[strings.ToLower(schemaName)]- if schemaInfo == nil {- l.err = errors.Errorf("schema %q not found", schemaName)- return- }- lowerTable := strings.ToLower(tableName)- if _, ok := schemaInfo.tableMap[lowerTable]; ok {- l.err = errors.Errorf("table %q already exists in schema %q", tableName, schemaName)- return- }- schemaInfo.tableMap[lowerTable] = newTableInfo(len(schemaInfo.tableMap), tableName, ctx)-}--func (l *buildSchemaInfoListener) EnterCreate_index(ctx *tsql.Create_indexContext) {- if l.err != nil {- return- }- _, schemaName, tableName := normalizeTableNameSeparated(ctx.Table_name(), "" /* fallbackDatabase */, defaultSchema, false /* caseSensitive */)- schemaInfo := l.m[strings.ToLower(schemaName)]- if schemaInfo == nil {- l.err = errors.Errorf("schema %q not found", schemaName)- return- }- lowerTable := strings.ToLower(tableName)- tableInfo := schemaInfo.tableMap[lowerTable]- if tableInfo == nil {- l.err = errors.Errorf("table %q not found in schema %q", tableName, schemaName)- return- }- indexName, lowerIndex := NormalizeTSQLIdentifier(ctx.Id_(0))- if _, ok := tableInfo.indexMap[lowerIndex]; ok {- l.err = errors.Errorf("index %q already exists in schema %q", indexName, schemaName)- return- }- tableInfo.indexMap[lowerIndex] = newIndexInfo(len(tableInfo.indexMap), indexName, ctx)-}--type schemaMap map[string]*schemaInfo-type tableMap map[string]*tableInfo-type indexMap map[string]*indexInfo--type schemaInfo struct {- id int- name string- lowerName string- tableMap tableMap- node *tsql.Create_schemaContext- existsInNew bool-}--func newSchemaInfo(id int, name string, node *tsql.Create_schemaContext) *schemaInfo {- return &schemaInfo{- id: id,- name: name,- lowerName: strings.ToLower(name),- tableMap: make(tableMap),- node: node,- }-}--type tableInfo struct {- id int- name string- lowerName string- indexMap indexMap- node *tsql.Create_tableContext- existsInNew bool-}--func newTableInfo(id int, name string, node *tsql.Create_tableContext) *tableInfo {- return &tableInfo{- id: id,- name: name,- lowerName: strings.ToLower(name),- indexMap: make(indexMap),- node: node,- }-}--type indexInfo struct {- id int- name string- lowerName string- node *tsql.Create_indexContext- existsInNew bool-}--func newIndexInfo(id int, name string, node *tsql.Create_indexContext) *indexInfo {- return &indexInfo{- id: id,- name: name,- lowerName: strings.ToLower(name),- node: node,- }-}