Add write transactions
This commit is contained in:
parent
c0fd041532
commit
5bea3c94c1
4 changed files with 19 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::str::FromStr;
|
||||
use std::{str::FromStr, time::Duration};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use models::{
|
||||
|
|
@ -21,6 +21,7 @@ impl Database {
|
|||
pub async fn try_new(config: &DatabaseConfig) -> Result<Self> {
|
||||
let connection_options = SqliteConnectOptions::from_str(&config.sqlite_url)?
|
||||
.create_if_missing(true)
|
||||
.busy_timeout(Duration::from_secs(3600))
|
||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
||||
|
||||
let pool = SqlitePoolOptions::new()
|
||||
|
|
@ -49,13 +50,26 @@ impl Database {
|
|||
.context("Cannot check for pending migrations")
|
||||
}
|
||||
|
||||
pub async fn create_transaction(&self) -> Result<Transaction<'_>> {
|
||||
/// Attempting to write from this transaction might result in a
|
||||
/// database locked error. Use this transaction for read-only operations.
|
||||
pub async fn create_readonly_transaction(&self) -> Result<Transaction<'_>> {
|
||||
self.connection_pool
|
||||
.begin()
|
||||
.await
|
||||
.context("Cannot create transaction")
|
||||
}
|
||||
|
||||
pub async fn create_write_transaction(&self) -> Result<Transaction<'_>> {
|
||||
let mut transaction = self.create_readonly_transaction().await?;
|
||||
|
||||
// sqlx doesn't support immediate transactions for sqlite: https://github.com/launchbadge/sqlx/issues/481
|
||||
sqlx::query!("END; BEGIN IMMEDIATE;")
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
/// Return the latest state of all non-deleted documents in the vault
|
||||
pub async fn get_latest_documents(
|
||||
&self,
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ pub async fn create_document(
|
|||
|
||||
let mut transaction = state
|
||||
.database
|
||||
.create_transaction()
|
||||
.create_write_transaction()
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ pub async fn delete_document(
|
|||
|
||||
let mut transaction = state
|
||||
.database
|
||||
.create_transaction()
|
||||
.create_write_transaction()
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ pub async fn update_document(
|
|||
|
||||
let mut transaction = state
|
||||
.database
|
||||
.create_transaction()
|
||||
.create_write_transaction()
|
||||
.await
|
||||
.map_err(server_error)?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue