Skip to content

World's first open-source enterprise world model.

Reject rule updates that retain Y conditions without a join (#1126)

Daniel408dacbf507e
diff --git a/crates/utopia-store/src/business_rules.rs b/crates/utopia-store/src/business_rules.rs
index 0cb06db8..160af072 100644
--- a/crates/utopia-store/src/business_rules.rs
+++ b/crates/utopia-store/src/business_rules.rs
@@ -303,6 +303,27 @@ pub async fn update(
.await?;
insert_conditions(&mut tx, rule_id, cs).await?;
}
+ if conditions.is_some() || conclusion.is_some() {
+ // 在持有规则行锁的事务内校验最终条件,保留条件和并发更新都不能绕过。
+ let reads_y_without_join: bool = sqlx::query_scalar(
+ "SELECT EXISTS (
+ SELECT 1 FROM attribute_rules r
+ JOIN attribute_rule_conditions c ON c.rule_id = r.id
+ WHERE r.kb_id = $1 AND r.id = $2
+ AND r.join_predicate_id IS NULL AND c.subject_side = 'y'
+ )",
+ )
+ .bind(kb_id)
+ .bind(rule_id)
+ .fetch_one(&mut *tx)
+ .await?;
+ if reads_y_without_join {
+ return Err(AppError::invalid(
+ "condition_side_without_join",
+ "Only a joined rule can read the other side of an edge.",
+ ));
+ }
+ }
// 定义变了才开新版本;改名、改描述、开关不算(0060)
record_version(&mut tx, kb_id, rule_id).await?;
tx.commit().await?;
diff --git a/crates/utopia-store/tests/a_rule_concludes_a_relation.rs b/crates/utopia-store/tests/a_rule_concludes_a_relation.rs
index 7aa3f060..3a926ffa 100644
--- a/crates/utopia-store/tests/a_rule_concludes_a_relation.rs
+++ b/crates/utopia-store/tests/a_rule_concludes_a_relation.rs
@@ -6,7 +6,7 @@
//! persisted row carries its object and the full three-part proof.
use sqlx::PgPool;
-use utopia_store::business_rules::ConditionInput;
+use utopia_store::business_rules::{self, ConclusionInput, ConditionInput};
use uuid::Uuid;
struct Fixture {
@@ -211,6 +211,122 @@ fn conditions(f: &Fixture) -> [ConditionInput; 2] {
]
}
+fn pressure_condition(f: &Fixture, side: &str) -> [ConditionInput; 1] {
+ [ConditionInput {
+ group: 0,
+ predicate_id: f.pressure,
+ op: "gt".into(),
+ operand: Some(serde_json::json!(80.0)),
+ side: side.into(),
+ }]
+}
+
+/// 只改结论、不带条件的更新会清掉连接而把 Y 侧条件留下(#1109):整条更新被拒,什么都不写
+#[tokio::test]
+async fn dropping_the_join_is_refused_while_the_rule_keeps_y_conditions() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = PgPool::connect(&url).await?;
+ utopia_store::db::migrate(&pool).await?;
+ let f = seed(&pool).await?;
+
+ let run = async {
+ let rule = business_rules::create(
+ &pool,
+ f.kb,
+ "high pressure on the joined entity",
+ "",
+ f.well,
+ "relation",
+ None,
+ Some(f.upstream_of),
+ None,
+ None,
+ Some(f.supplies),
+ &pressure_condition(&f, "y"),
+ )
+ .await?;
+ let attribute = ConclusionInput {
+ kind: "attribute".into(),
+ type_id: None,
+ predicate_id: Some(f.depth),
+ value: Some(serde_json::json!(1.0)),
+ expr: None,
+ join_predicate_id: None,
+ };
+ let saved = || async {
+ sqlx::query_as::<_, (String, Option<Uuid>, String)>(
+ "SELECT r.conclusion, r.join_predicate_id, c.subject_side
+ FROM attribute_rules r JOIN attribute_rule_conditions c ON c.rule_id = r.id
+ WHERE r.id = $1",
+ )
+ .bind(rule)
+ .fetch_one(&pool)
+ .await
+ };
+
+ let refused =
+ business_rules::update(&pool, f.kb, rule, None, None, None, None, Some(&attribute))
+ .await
+ .expect_err("the Y condition the rule keeps still needs its join");
+ assert!(
+ matches!(
+ &refused,
+ utopia_core::AppError::Invalid {
+ code: "condition_side_without_join",
+ ..
+ }
+ ),
+ "{refused:?}"
+ );
+ assert_eq!(
+ saved().await?,
+ ("relation".into(), Some(f.supplies), "y".into())
+ );
+
+ // 同一次更新里把条件换到 X 侧,去掉连接就是合法的
+ let on_x = pressure_condition(&f, "x");
+ business_rules::update(
+ &pool,
+ f.kb,
+ rule,
+ None,
+ None,
+ None,
+ Some(&on_x),
+ Some(&attribute),
+ )
+ .await?;
+ assert_eq!(saved().await?, ("attribute".into(), None, "x".into()));
+
+ // 修复之前存下的非法规则仍然能改名、停用:只有动了条件或结论才校验
+ sqlx::query("UPDATE attribute_rule_conditions SET subject_side = 'y' WHERE rule_id = $1")
+ .bind(rule)
+ .execute(&pool)
+ .await?;
+ business_rules::update(
+ &pool,
+ f.kb,
+ rule,
+ Some("disabled"),
+ None,
+ Some(false),
+ None,
+ None,
+ )
+ .await?;
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+
+ sqlx::query("DELETE FROM organizations WHERE id = $1")
+ .bind(f.org)
+ .execute(&pool)
+ .await?;
+ run
+}
+
#[tokio::test]
async fn a_joined_rule_reads_the_other_side_of_a_declared_edge() -> anyhow::Result<()> {
let Some(url) = utopia_store::test_db::url() else {