Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: improve equal view (#13313)

Danny Xu75206d8ceb6a
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 9525953e8d..921299f6d3 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -38,7 +38,7 @@
"i18n-ally.editor.preferEditor": true,
"i18n-ally.enabledFrameworks": ["vue"],
"i18n-ally.parserOptions": {},
- "go.lintOnSave": "file",
+ "go.lintOnSave": "workspace",
"go.lintTool": "staticcheck",
"go.buildTags": "mysql",
"gopls": {
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.go
index 32a463561f..26ff2d26ab 100644
--- a/backend/api/v1/schema_design_merge.go
+++ b/backend/api/v1/schema_design_merge.go
@@ -1244,6 +1244,8 @@ func equalRoutineDefinition(a, b string) bool {
}
func equalViewDefinition(a, b string) bool {
+ a = normalizeMySQLViewDefinition(a)
+ b = normalizeMySQLViewDefinition(b)
ignoreTokens := []string{
"`", " ", "(", ")", "\t", "\n", "\r",
}
@@ -1254,6 +1256,31 @@ func equalViewDefinition(a, b string) bool {
return strings.EqualFold(a, b)
}
+var qualifiedRe = regexp.MustCompile("`" + `[^` + "`" + `]` + "`" + `\.` + "`")
+
+func normalizeMySQLViewDefinition(query string) string {
+ for {
+ asIdx := strings.Index(query, " AS ")
+ if asIdx == -1 {
+ break
+ }
+ endIdx1 := strings.Index(query[asIdx:], "` ")
+ endIdx2 := strings.Index(query[asIdx:], "`,")
+ if endIdx1 == -1 && endIdx2 == -1 {
+ break
+ }
+ endIdx := -1
+ if endIdx1 >= 0 {
+ endIdx = endIdx1
+ }
+ if endIdx2 >= 0 && endIdx2 < endIdx {
+ endIdx = endIdx2
+ }
+ query = query[:asIdx] + query[(asIdx+endIdx+1):]
+ }
+ return qualifiedRe.ReplaceAllString(query, "`")
+}
+
func (n *metadataDiffViewNode) applyDiffTo(target *storepb.SchemaMetadata) error {
if target == nil {
return errors.New("target must not be nil")
diff --git a/backend/api/v1/schema_design_merge_test.go b/backend/api/v1/schema_design_merge_test.go
index ca310210fe..832b3ca89a 100644
--- a/backend/api/v1/schema_design_merge_test.go
+++ b/backend/api/v1/schema_design_merge_test.go
@@ -78,3 +78,10 @@ func TestTryMerge(t *testing.T) {
a.NoError(err)
}
}
+
+func TestNormalizeMySQLViewDefinition(t *testing.T) {
+ query := "select `p`.`id` AS `id`,extract(year_month from `p`.`yyy`) AS `extract(year_month from ``yyy``)` from `p` order by `p`.`id` limit 1000000000000"
+ want := "select `id`,extract(year_month from `yyy`) from `p` order by `id` limit 1000000000000"
+ got := normalizeMySQLViewDefinition(query)
+ require.Equal(t, want, got)
+}