Add a workspace document model and SQL storage adapter

This commit is contained in:
2026-11-30 17:41:20 +08:00
parent 698f126033
commit b64a88011f
9 changed files with 214 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
use db::{DeploymentConfig, StorageError, connect};
use documents::{Document, DocumentId, DocumentKey, WorkspaceId};
use serde_json::json;
fn document(workspace: &str, body: serde_json::Value) -> Document {
Document {
key: DocumentKey {
workspace: WorkspaceId::new(workspace).unwrap(),
document: DocumentId::new("same-id").unwrap(),
},
body,
}
}
#[tokio::test]
async fn workspace_documents_keep_their_identity_and_contents_after_reopen() {
let folder = tempfile::tempdir().unwrap();
let path = folder.path().join("documents.sqlite3");
let mut config = DeploymentConfig {
sqlite_path: path.to_str().unwrap().into(),
..DeploymentConfig::default()
};
let connection = connect(&config).await.unwrap();
let first = document(
&"w".repeat(WorkspaceId::MAX_BYTES),
json!({"title": "notes"}),
);
let second = document("other-workspace", json!(null));
assert!(connection.get_document(&first.key).await.unwrap().is_none());
let first_revision = connection.create_document(first.clone()).await.unwrap();
let second_revision = connection.create_document(second.clone()).await.unwrap();
assert!(second_revision > first_revision);
assert!(matches!(
connection
.create_document(document(first.key.workspace.as_str(), json!("overwrite")))
.await,
Err(StorageError::DocumentAlreadyExists)
));
drop(connection);
config.read_only = true;
let connection = connect(&config).await.unwrap();
let read = connection.get_document(&first.key).await.unwrap().unwrap();
assert_eq!(read.document, first);
assert_eq!(read.revision, first_revision);
let read = connection.get_document(&second.key).await.unwrap().unwrap();
assert_eq!(read.document, second);
assert_eq!(read.revision, second_revision);
assert!(matches!(
connection
.create_document(document("third-workspace", json!(null)))
.await,
Err(StorageError::ReadOnly)
));
}