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(structurer): handle charset in data type (#10517)
h3n4l50d9d1943189
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.goindex 96e0a66289..5da9634449 100644--- a/backend/plugin/schema/mysql/get_design_schema.go+++ b/backend/plugin/schema/mysql/get_design_schema.go@@ -685,7 +685,7 @@ func (g *mysqlDesignSchemaGenerator) EnterColumnDefinition(ctx *mysql.ColumnDefi// compare column typetypeCtx := ctx.FieldDefinition().DataType()- columnType := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(typeCtx)+ columnType := getDataTypePlainText(typeCtx)if !strings.EqualFold(columnType, column.tp) {if _, err := g.columnDefine.WriteString(ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{Start: ctx.GetStart().GetTokenIndex(),@@ -1000,3 +1000,31 @@ func writeRemainingTables(w io.StringWriter, to *storepb.DatabaseSchemaMetadata,func getTableAnnouncement(name string) string {return fmt.Sprintf("\n--\n-- Table structure for table `%s`\n--\n", name)}++// getDataTypePlainText returns the plain text of the data type,+// which excludes the charset candidate.+// For example, for "varchar(10) CHARACTER SET utf8mb4",+// it returns "varchar(10)".+func getDataTypePlainText(typeCtx mysql.IDataTypeContext) string {+ begin := typeCtx.GetStart().GetTokenIndex()+ end := typeCtx.GetStop().GetTokenIndex()+ if typeCtx.CharsetWithOptBinary() != nil {+ end = typeCtx.CharsetWithOptBinary().GetStart().GetTokenIndex() - 1+ }+ // To skip the trailing spaces, we iterate the token stream reversely and find the first default channel token index.+ for i := end; i >= begin; i-- {+ if typeCtx.GetParser().GetTokenStream().Get(i).GetChannel() == antlr.TokenDefaultChannel {+ end = i+ break+ }+ }++ if end < begin {+ return ""+ }++ return typeCtx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{+ Start: begin,+ Stop: end,+ })+}diff --git a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml b/backend/plugin/schema/mysql/testdata/get_design_schema.yamlindex 9cb2cccda7..48e09f81b8 100644--- a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml+++ b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml@@ -10,7 +10,8 @@`g` geometry NOT NULL,`j` json NOT NULL,`ti` tinyint(1) NOT NULL,- `tb` tinyblob+ `tb` tinyblob,+ `vc` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;--@@ -63,6 +64,11 @@"nullable": true,"position": 5,"defaultNull": true+ },+ {+ "name": "vc",+ "type": "varchar(10)",+ "position": 6}],"collation": "utf8mb4_0900_ai_ci"@@ -100,7 +106,8 @@`g` geometry NOT NULL,`j` json NOT NULL,`ti` tinyint(1) NOT NULL,- `tb` tinyblob+ `tb` tinyblob,+ `vc` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;--