Skip to content

World's first open-source enterprise world model.

A document pushed as JSON has a version, so its original can be read back (#1211)

WaylandYang259931ac7026
diff --git a/crates/utopia-server/src/api/documents_routes_tests.rs b/crates/utopia-server/src/api/documents_routes_tests.rs
index c5292605..1fa19c7a 100644
--- a/crates/utopia-server/src/api/documents_routes_tests.rs
+++ b/crates/utopia-server/src/api/documents_routes_tests.rs
@@ -703,6 +703,35 @@ async fn reupload_restores_one_version_and_requeues_processing() -> anyhow::Resu
f.cleanup().await
}
+#[tokio::test]
+async fn a_pushed_document_serves_its_original() -> anyhow::Result<()> {
+ let Some(f) = Fixture::new().await? else {
+ return Ok(());
+ };
+ let pushed = b"A document pushed as JSON";
+ crate::ingest_sources::ingest_upload(
+ &f.state,
+ f.kb,
+ "pushed.md",
+ "text/markdown",
+ pushed,
+ None,
+ )
+ .await?;
+ let id: Uuid =
+ sqlx::query_scalar("SELECT id FROM documents WHERE kb_id=$1 AND filename='pushed.md'")
+ .bind(f.kb)
+ .fetch_one(&f.pool)
+ .await?;
+ assert_eq!(documents::versions(&f.pool, id).await?.len(), 1);
+ let (status, _, bytes) = f
+ .get_raw(&format!("/api/v1/documents/{id}/content"), Some(&f.token))
+ .await?;
+ assert_eq!(status, StatusCode::OK);
+ assert_eq!(bytes, pushed);
+ f.cleanup().await
+}
+
#[tokio::test]
async fn failed_upload_queue_rolls_back_creation_and_restoration() -> anyhow::Result<()> {
let Some(f) = Fixture::new().await? else {
diff --git a/crates/utopia-store/src/documents.rs b/crates/utopia-store/src/documents.rs
index acbce652..7072a99d 100644
--- a/crates/utopia-store/src/documents.rs
+++ b/crates/utopia-store/src/documents.rs
@@ -110,7 +110,10 @@ async fn create_with_time_source(
{
return restore(pool, kb_id, id).await;
}
- sqlx::query_as(
+ // 读原文要在版本账本里找到当前摘要:文档和它的第 1 版一起落,缺一不可(#1204 修了
+ // 文件上传那条路,JSON 推送走的是这里)
+ let mut tx = pool.begin().await?;
+ let document: Document = sqlx::query_as(
"INSERT INTO documents (id, kb_id, filename, mime, size_bytes, sha256, source_id,
doc_time, doc_time_source, external_key)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *",
@@ -130,14 +133,27 @@ async fn create_with_time_source(
"none"
})
.bind(external_key)
- .fetch_one(pool)
+ .fetch_one(&mut *tx)
.await
.map_err(|e| match &e {
sqlx::Error::Database(db) if db.is_unique_violation() => AppError::Conflict(format!(
"File already exists (identical content): {filename}"
)),
_ => AppError::Db(e),
- })
+ })?;
+ sqlx::query(
+ "INSERT INTO document_versions (id, document_id, version, sha256, size_bytes, doc_time)
+ VALUES ($1, $2, 1, $3, $4, $5)",
+ )
+ .bind(Uuid::now_v7())
+ .bind(document.id)
+ .bind(sha256)
+ .bind(size_bytes)
+ .bind(document.doc_time)
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ Ok(document)
}
#[allow(clippy::too_many_arguments)]