Skip to content

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

chore: fail open for dendency columns not found in differ (#13288)

h3n4lfc4562077a2f
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index 8067879335..9e57528c90 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -1185,6 +1185,11 @@ func reconcileMetadata(metadata *storepb.DatabaseSchemaMetadata, engine storepb.
}
}
}
+ for _, view := range schema.Views {
+ if engine == storepb.Engine_MYSQL || engine == storepb.Engine_TIDB {
+ view.Definition = formatViewDef(view.Definition)
+ }
+ }
}
}
@@ -1785,3 +1790,7 @@ func alignDatabaseConfig(metadata *storepb.DatabaseSchemaMetadata, config *store
}
config.SchemaConfigs = append(config.SchemaConfigs, newSchemaConfigs...)
}
+
+func formatViewDef(def string) string {
+ return strings.TrimRight(def, "; \n\r\t")
+}
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.go
index 9f66252881..07de7334e5 100644
--- a/backend/api/v1/schema_design_merge.go
+++ b/backend/api/v1/schema_design_merge.go
@@ -1172,15 +1172,15 @@ func (n *metadataDiffViewNode) tryMerge(other *metadataDiffViewNode) (bool, stri
return false, ""
}
if n.action == diffActionCreate {
- if n.head.Definition != other.head.Definition {
+ if !equalViewDefinition(n.head.Definition, other.head.Definition) {
return true, fmt.Sprintf("conflict view definition, one is %s, the other is %s", n.head.Definition, other.head.Definition)
}
}
if n.action == diffActionUpdate {
- if other.base.Definition != other.head.Definition {
- if n.base.Definition != n.head.Definition {
- if n.head.Definition != other.head.Definition {
+ if !equalViewDefinition(other.base.Definition, other.head.Definition) {
+ if !equalViewDefinition(n.base.Definition, n.head.Definition) {
+ if !equalViewDefinition(n.head.Definition, other.head.Definition) {
return true, fmt.Sprintf("conflict view definition, one is %s, the other is %s", n.head.Definition, other.head.Definition)
}
} else {
@@ -1192,6 +1192,17 @@ func (n *metadataDiffViewNode) tryMerge(other *metadataDiffViewNode) (bool, stri
return false, ""
}
+func equalViewDefinition(a, b string) bool {
+ ignoreTokens := []string{
+ "`", " ", "(", ")", "\t", "\n", "\r",
+ }
+ for _, token := range ignoreTokens {
+ a = strings.ReplaceAll(a, token, "")
+ b = strings.ReplaceAll(b, token, "")
+ }
+ return a == b
+}
+
func (n *metadataDiffViewNode) applyDiffTo(target *storepb.SchemaMetadata) error {
if target == nil {
return errors.New("target must not be nil")
diff --git a/backend/plugin/parser/mysql/differ.go b/backend/plugin/parser/mysql/differ.go
index 8904f2f941..65953dc9ef 100644
--- a/backend/plugin/parser/mysql/differ.go
+++ b/backend/plugin/parser/mysql/differ.go
@@ -375,13 +375,16 @@ func (diff *diffNode) diffView(oldDatabase, newDatabase *databaseDef) error {
// 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 {
- // 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)
+ // 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)
}
- tempViewList = append(tempViewList, tempView)
diff.createViewList = append(diff.createViewList, view)
}
}
diff --git a/backend/plugin/parser/mysql/test-data/differ/test_differ_view.yaml b/backend/plugin/parser/mysql/test-data/differ/test_differ_view.yaml
index 141aa2fb28..6d642a34d4 100644
--- a/backend/plugin/parser/mysql/test-data/differ/test_differ_view.yaml
+++ b/backend/plugin/parser/mysql/test-data/differ/test_differ_view.yaml
@@ -83,3 +83,9 @@
diff: |+
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` INNER JOIN `orders` USING (`order_id`) INNER JOIN `customers` USING (`customer_name`) GROUP BY `order_id`;
+- oldSchema: ""
+ newSchema: |
+ CREATE VIEW v1 AS SELECT * FROM t;
+ diff: |+
+ CREATE OR REPLACE ALGORITHM=UNDEFINED DEFINER=CURRENT_USER SQL SECURITY DEFINER VIEW `v1` AS SELECT * FROM t;
+