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 anyhow::{Context, Result};
|
||||||
use models::{
|
use models::{
|
||||||
|
|
@ -21,6 +21,7 @@ impl Database {
|
||||||
pub async fn try_new(config: &DatabaseConfig) -> Result<Self> {
|
pub async fn try_new(config: &DatabaseConfig) -> Result<Self> {
|
||||||
let connection_options = SqliteConnectOptions::from_str(&config.sqlite_url)?
|
let connection_options = SqliteConnectOptions::from_str(&config.sqlite_url)?
|
||||||
.create_if_missing(true)
|
.create_if_missing(true)
|
||||||
|
.busy_timeout(Duration::from_secs(3600))
|
||||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
||||||
|
|
||||||
let pool = SqlitePoolOptions::new()
|
let pool = SqlitePoolOptions::new()
|
||||||
|
|
@ -49,13 +50,26 @@ impl Database {
|
||||||
.context("Cannot check for pending migrations")
|
.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
|
self.connection_pool
|
||||||
.begin()
|
.begin()
|
||||||
.await
|
.await
|
||||||
.context("Cannot create transaction")
|
.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
|
/// Return the latest state of all non-deleted documents in the vault
|
||||||
pub async fn get_latest_documents(
|
pub async fn get_latest_documents(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ pub async fn create_document(
|
||||||
|
|
||||||
let mut transaction = state
|
let mut transaction = state
|
||||||
.database
|
.database
|
||||||
.create_transaction()
|
.create_write_transaction()
|
||||||
.await
|
.await
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ pub async fn delete_document(
|
||||||
|
|
||||||
let mut transaction = state
|
let mut transaction = state
|
||||||
.database
|
.database
|
||||||
.create_transaction()
|
.create_write_transaction()
|
||||||
.await
|
.await
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ pub async fn update_document(
|
||||||
|
|
||||||
let mut transaction = state
|
let mut transaction = state
|
||||||
.database
|
.database
|
||||||
.create_transaction()
|
.create_write_transaction()
|
||||||
.await
|
.await
|
||||||
.map_err(server_error)?;
|
.map_err(server_error)?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue