Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions crates/utopia-store/src/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,10 +1337,14 @@ async fn restore_tx(
id: Uuid,
) -> AppResult<Document> {
lock_document_source_tx(tx, id).await?;
sqlx::query("SELECT id FROM documents WHERE id = $1 FOR UPDATE")
.bind(id)
.execute(&mut **tx)
.await?;
// 文档和这次删除作废的事实共用同一个事务时间,清墓碑前先记住它。
let deleted_at = sqlx::query_scalar::<_, Option<DateTime<Utc>>>(
"SELECT deleted_at FROM documents WHERE id = $1 FOR UPDATE",
)
.bind(id)
.fetch_optional(&mut **tx)
.await?
.flatten();
let row: Option<(Uuid, Vec<Uuid>, Vec<Uuid>)> = sqlx::query_as(
"SELECT id, invalidated_facts, superseded_chunks FROM document_deletions
WHERE document_id = $1 AND kb_id = $2 AND reverted_at IS NULL
Expand Down Expand Up @@ -1404,12 +1408,13 @@ async fn restore_tx(
sqlx::query(
"UPDATE facts SET invalidated_at = NULL
WHERE invalidated_at IS NOT NULL
AND (id = ANY($1) OR (id, invalidated_at) IN
AND ((id = ANY($1) AND invalidated_at = $4) OR (id, invalidated_at) IN
(SELECT * FROM unnest($2::uuid[], $3::timestamptz[])))",
)
.bind(&fact_ids)
.bind(&shared_ids)
.bind(&shared_stamps)
.bind(deleted_at)
.execute(&mut **tx)
.await?;
let touched: Vec<Uuid> = cited.iter().chain(&restored).copied().collect();
Expand Down
58 changes: 56 additions & 2 deletions crates/utopia-store/tests/store/a_deletion_is_an_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,22 @@ async fn fact(pool: &PgPool, f: &Fx, s: Uuid, o: Uuid, evidence: &[Uuid]) -> any
Ok(id)
}

async fn live(pool: &PgPool, id: Uuid) -> anyhow::Result<bool> {
async fn invalidated_at(
pool: &PgPool,
id: Uuid,
) -> anyhow::Result<Option<chrono::DateTime<chrono::Utc>>> {
Ok(
sqlx::query_scalar("SELECT invalidated_at IS NULL FROM facts WHERE id = $1")
sqlx::query_scalar("SELECT invalidated_at FROM facts WHERE id = $1")
.bind(id)
.fetch_one(pool)
.await?,
)
}

async fn live(pool: &PgPool, id: Uuid) -> anyhow::Result<bool> {
Ok(invalidated_at(pool, id).await?.is_none())
}

async fn chunk_live(pool: &PgPool, id: Uuid) -> anyhow::Result<(bool, String)> {
Ok(
sqlx::query_as("SELECT superseded_at IS NULL, text FROM chunks WHERE id = $1")
Expand Down Expand Up @@ -373,3 +380,50 @@ async fn restoring_a_source_recovers_a_fact_after_the_last_source_was_purged() -
{
shared_source_restore(true).await
}

#[tokio::test]
async fn restoring_the_last_deleted_source_preserves_a_later_rejection() -> anyhow::Result<()> {
let Some(url) = utopia_store::test_db::url() else {
return Ok(());
};
let pool = PgPool::connect(&url).await?;
let f = seed(&pool).await?;
let result = async {
let a = document(&pool, &f, "a.md", "restore-a").await?;
let b = document(&pool, &f, "b.md", "restore-b").await?;
let a1 = chunk(&pool, &f, a, 1).await?;
let b1 = chunk(&pool, &f, b, 1).await?;
let x = entity(&pool, &f, "X").await?;
let y = entity(&pool, &f, "Y").await?;
let z = entity(&pool, &f, "Z").await?;
let shared = fact(&pool, &f, x, y, &[a1, b1]).await?;
let only_b = fact(&pool, &f, x, z, &[b1]).await?;

documents::delete(&pool, f.kb, a, None).await?;
let deletion = documents::delete(&pool, f.kb, b, None).await?;
assert_eq!(deletion.invalidated_facts, 2);
documents::restore(&pool, f.kb, a).await?;
utopia_store::graph::reject_fact(&pool, f.kb, shared).await?;
let rejected_at = invalidated_at(&pool, shared).await?.expect("fact rejected");

let restored = documents::restore(&pool, f.kb, b).await?;
assert!(restored.deleted_at.is_none());
assert!(chunk_live(&pool, b1).await?.0);
assert!(live(&pool, only_b).await?, "B's fact should be restored");
anyhow::ensure!(
invalidated_at(&pool, shared).await? == Some(rejected_at),
"restoring B must preserve the later human rejection"
);
Ok::<_, anyhow::Error>(())
}
.await;
sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
.bind(f.kb)
.execute(&pool)
.await?;
sqlx::query("DELETE FROM organizations WHERE id = $1")
.bind(f.org)
.execute(&pool)
.await?;
result
}
Loading