Skip to content

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

chore: more view equal (#13319)

Danny Xu4b34680064d9
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.go
index 26ff2d26ab..f6c5bb4574 100644
--- a/backend/api/v1/schema_design_merge.go
+++ b/backend/api/v1/schema_design_merge.go
@@ -1259,22 +1259,34 @@ func equalViewDefinition(a, b string) bool {
var qualifiedRe = regexp.MustCompile("`" + `[^` + "`" + `]` + "`" + `\.` + "`")
func normalizeMySQLViewDefinition(query string) string {
+ query = strings.TrimSpace(query)
+ if query == "" {
+ return ""
+ }
+ if query[len(query)-1] != ';' {
+ query += ";"
+ }
+ trailSymbols := []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
+ var endCandidates []int
+ for _, symbol := range trailSymbols {
+ i := strings.Index(query[asIdx:], symbol)
+ if i >= 0 {
+ endCandidates = append(endCandidates, i)
+ }
}
- endIdx := -1
- if endIdx1 >= 0 {
- endIdx = endIdx1
+ if len(endCandidates) == 0 {
+ break
}
- if endIdx2 >= 0 && endIdx2 < endIdx {
- endIdx = endIdx2
+ endIdx := endCandidates[0]
+ for _, v := range endCandidates {
+ if v < endIdx {
+ endIdx = v
+ }
}
query = query[:asIdx] + query[(asIdx+endIdx+1):]
}
diff --git a/backend/api/v1/schema_design_merge_test.go b/backend/api/v1/schema_design_merge_test.go
index 832b3ca89a..4512dc89ad 100644
--- a/backend/api/v1/schema_design_merge_test.go
+++ b/backend/api/v1/schema_design_merge_test.go
@@ -80,8 +80,24 @@ func TestTryMerge(t *testing.T) {
}
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)
+ for i, test := range []struct {
+ query string
+ want string
+ }{
+ {
+ 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;",
+ },
+ {
+ query: "select 12 AS `12`",
+ want: "select 12;",
+ },
+ {
+ query: "select 12 AS `12`; ",
+ want: "select 12;",
+ },
+ } {
+ got := normalizeMySQLViewDefinition(test.query)
+ require.Equal(t, test.want, got, i)
+ }
}