Add local CLI #144
4 changed files with 16 additions and 27 deletions
Fix insertion race condition
commit
84855d6a5c
|
|
@ -370,11 +370,12 @@ impl Database {
|
||||||
.context("Cannot fetch document version")
|
.context("Cannot fetch document version")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// inserting the document must be the last step of the transaction if there's one
|
||||||
pub async fn insert_document_version(
|
pub async fn insert_document_version(
|
||||||
&self,
|
&self,
|
||||||
vault_id: &VaultId,
|
vault_id: &VaultId,
|
||||||
version: &StoredDocumentVersion,
|
version: &StoredDocumentVersion,
|
||||||
transaction: Option<&mut Transaction<'_>>,
|
transaction: Option<Transaction<'_>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let document_id = version.document_id.as_hyphenated();
|
let document_id = version.document_id.as_hyphenated();
|
||||||
let query = sqlx::query!(
|
let query = sqlx::query!(
|
||||||
|
|
@ -401,14 +402,22 @@ impl Database {
|
||||||
version.device_id
|
version.device_id
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(transaction) = transaction {
|
if let Some(mut transaction) = transaction {
|
||||||
query.execute(&mut **transaction).await
|
query
|
||||||
|
.execute(&mut *transaction)
|
||||||
|
.await
|
||||||
|
.context("Cannot insert document version")?;
|
||||||
|
|
||||||
|
transaction
|
||||||
|
.commit()
|
||||||
|
.await
|
||||||
|
.context("Failed to commit transaction")?;
|
||||||
} else {
|
} else {
|
||||||
query
|
query
|
||||||
.execute(&self.get_connection_pool(vault_id).await?)
|
.execute(&self.get_connection_pool(vault_id).await?)
|
||||||
.await
|
.await
|
||||||
|
.context("Cannot insert document version")?;
|
||||||
}
|
}
|
||||||
.context("Cannot insert document version")?;
|
|
||||||
|
|
||||||
self.broadcasts
|
self.broadcasts
|
||||||
.send_document_update(
|
.send_document_update(
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
use anyhow::Context as _;
|
|
||||||
use axum::{
|
use axum::{
|
||||||
Extension, Json,
|
Extension, Json,
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
|
|
@ -82,15 +81,9 @@ pub async fn create_document(
|
||||||
|
|
||||||
state
|
state
|
||||||
.database
|
.database
|
||||||
.insert_document_version(&vault_id, &new_version, Some(&mut transaction))
|
.insert_document_version(&vault_id, &new_version, Some(transaction))
|
||||||
.await
|
.await
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
transaction
|
|
||||||
.commit()
|
|
||||||
.await
|
|
||||||
.context("Failed to commit successful transaction")
|
|
||||||
.map_err(server_error)?;
|
|
||||||
|
|
||||||
Ok(Json(new_version.into()))
|
Ok(Json(new_version.into()))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
use anyhow::Context as _;
|
|
||||||
use axum::{
|
use axum::{
|
||||||
Extension, Json,
|
Extension, Json,
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
|
|
@ -71,15 +70,9 @@ pub async fn delete_document(
|
||||||
|
|
||||||
state
|
state
|
||||||
.database
|
.database
|
||||||
.insert_document_version(&vault_id, &new_version, Some(&mut transaction))
|
.insert_document_version(&vault_id, &new_version, Some(transaction))
|
||||||
.await
|
.await
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
transaction
|
|
||||||
.commit()
|
|
||||||
.await
|
|
||||||
.context("Failed to commit successful transaction")
|
|
||||||
.map_err(server_error)?;
|
|
||||||
|
|
||||||
Ok(Json(new_version.into()))
|
Ok(Json(new_version.into()))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -183,16 +183,10 @@ pub async fn update_document(
|
||||||
|
|
||||||
state
|
state
|
||||||
.database
|
.database
|
||||||
.insert_document_version(&vault_id, &new_version, Some(&mut transaction))
|
.insert_document_version(&vault_id, &new_version, Some(transaction))
|
||||||
.await
|
.await
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
transaction
|
|
||||||
.commit()
|
|
||||||
.await
|
|
||||||
.context("Failed to commit successful transaction")
|
|
||||||
.map_err(server_error)?;
|
|
||||||
|
|
||||||
Ok(Json(if is_different_from_request_content {
|
Ok(Json(if is_different_from_request_content {
|
||||||
DocumentUpdateResponse::MergingUpdate(new_version.into())
|
DocumentUpdateResponse::MergingUpdate(new_version.into())
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue