Skip to content

Commit 01d6d26

Browse files
Restoring a document preserves a later human rejection (#1498)
Signed-off-by: Maya Kid <BinjunYann@gmail.com> Co-authored-by: WaylandYang <145302500+WaylandYang@users.noreply.github.com>
1 parent 675aaf5 commit 01d6d26

2 files changed

Lines changed: 66 additions & 7 deletions

File tree

‎crates/utopia-store/src/documents.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,14 @@ async fn restore_tx(
13371337
id: Uuid,
13381338
) -> AppResult<Document> {
13391339
lock_document_source_tx(tx, id).await?;
1340-
sqlx::query("SELECT id FROM documents WHERE id = $1 FOR UPDATE")
1341-
.bind(id)
1342-
.execute(&mut **tx)
1343-
.await?;
1340+
// 文档和这次删除作废的事实共用同一个事务时间,清墓碑前先记住它。
1341+
let deleted_at = sqlx::query_scalar::<_, Option<DateTime<Utc>>>(
1342+
"SELECT deleted_at FROM documents WHERE id = $1 FOR UPDATE",
1343+
)
1344+
.bind(id)
1345+
.fetch_optional(&mut **tx)
1346+
.await?
1347+
.flatten();
13441348
let row: Option<(Uuid, Vec<Uuid>, Vec<Uuid>)> = sqlx::query_as(
13451349
"SELECT id, invalidated_facts, superseded_chunks FROM document_deletions
13461350
WHERE document_id = $1 AND kb_id = $2 AND reverted_at IS NULL
@@ -1404,12 +1408,13 @@ async fn restore_tx(
14041408
sqlx::query(
14051409
"UPDATE facts SET invalidated_at = NULL
14061410
WHERE invalidated_at IS NOT NULL
1407-
AND (id = ANY($1) OR (id, invalidated_at) IN
1411+
AND ((id = ANY($1) AND invalidated_at = $4) OR (id, invalidated_at) IN
14081412
(SELECT * FROM unnest($2::uuid[], $3::timestamptz[])))",
14091413
)
14101414
.bind(&fact_ids)
14111415
.bind(&shared_ids)
14121416
.bind(&shared_stamps)
1417+
.bind(deleted_at)
14131418
.execute(&mut **tx)
14141419
.await?;
14151420
let touched: Vec<Uuid> = cited.iter().chain(&restored).copied().collect();

‎crates/utopia-store/tests/store/a_deletion_is_an_event.rs‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,22 @@ async fn fact(pool: &PgPool, f: &Fx, s: Uuid, o: Uuid, evidence: &[Uuid]) -> any
157157
Ok(id)
158158
}
159159

160-
async fn live(pool: &PgPool, id: Uuid) -> anyhow::Result<bool> {
160+
async fn invalidated_at(
161+
pool: &PgPool,
162+
id: Uuid,
163+
) -> anyhow::Result<Option<chrono::DateTime<chrono::Utc>>> {
161164
Ok(
162-
sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id = $1")
165+
sqlx::query_scalar("SELECT invalidated_at FROM facts WHERE id = $1")
163166
.bind(id)
164167
.fetch_one(pool)
165168
.await?,
166169
)
167170
}
168171

172+
async fn live(pool: &PgPool, id: Uuid) -> anyhow::Result<bool> {
173+
Ok(invalidated_at(pool, id).await?.is_none())
174+
}
175+
169176
async fn chunk_live(pool: &PgPool, id: Uuid) -> anyhow::Result<(bool, String)> {
170177
Ok(
171178
sqlx::query_as("SELECT superseded_at IS NULL, text FROM chunks WHERE id = $1")
@@ -373,3 +380,50 @@ async fn restoring_a_source_recovers_a_fact_after_the_last_source_was_purged() -
373380
{
374381
shared_source_restore(true).await
375382
}
383+
384+
#[tokio::test]
385+
async fn restoring_the_last_deleted_source_preserves_a_later_rejection() -> anyhow::Result<()> {
386+
let Some(url) = utopia_store::test_db::url() else {
387+
return Ok(());
388+
};
389+
let pool = PgPool::connect(&url).await?;
390+
let f = seed(&pool).await?;
391+
let result = async {
392+
let a = document(&pool, &f, "a.md", "restore-a").await?;
393+
let b = document(&pool, &f, "b.md", "restore-b").await?;
394+
let a1 = chunk(&pool, &f, a, 1).await?;
395+
let b1 = chunk(&pool, &f, b, 1).await?;
396+
let x = entity(&pool, &f, "X").await?;
397+
let y = entity(&pool, &f, "Y").await?;
398+
let z = entity(&pool, &f, "Z").await?;
399+
let shared = fact(&pool, &f, x, y, &[a1, b1]).await?;
400+
let only_b = fact(&pool, &f, x, z, &[b1]).await?;
401+
402+
documents::delete(&pool, f.kb, a, None).await?;
403+
let deletion = documents::delete(&pool, f.kb, b, None).await?;
404+
assert_eq!(deletion.invalidated_facts, 2);
405+
documents::restore(&pool, f.kb, a).await?;
406+
utopia_store::graph::reject_fact(&pool, f.kb, shared).await?;
407+
let rejected_at = invalidated_at(&pool, shared).await?.expect("fact rejected");
408+
409+
let restored = documents::restore(&pool, f.kb, b).await?;
410+
assert!(restored.deleted_at.is_none());
411+
assert!(chunk_live(&pool, b1).await?.0);
412+
assert!(live(&pool, only_b).await?, "B's fact should be restored");
413+
anyhow::ensure!(
414+
invalidated_at(&pool, shared).await? == Some(rejected_at),
415+
"restoring B must preserve the later human rejection"
416+
);
417+
Ok::<_, anyhow::Error>(())
418+
}
419+
.await;
420+
sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
421+
.bind(f.kb)
422+
.execute(&pool)
423+
.await?;
424+
sqlx::query("DELETE FROM organizations WHERE id = $1")
425+
.bind(f.org)
426+
.execute(&pool)
427+
.await?;
428+
result
429+
}

0 commit comments

Comments
 (0)