From d678a2c404fecb0d8f2efa67a5c5d96d7154232c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Priv=C3=A9r?= Date: Thu, 28 Dec 2023 09:05:47 +0100 Subject: [PATCH 1/5] added transsactions for sqlx databases --- src/database_drivers/maria.rs | 15 ++++++++++++++- src/database_drivers/mysql.rs | 15 ++++++++++++++- src/database_drivers/postgres.rs | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/database_drivers/maria.rs b/src/database_drivers/maria.rs index c9b54bc..4e25233 100644 --- a/src/database_drivers/maria.rs +++ b/src/database_drivers/maria.rs @@ -1,7 +1,9 @@ use crate::config; use crate::database_drivers::DatabaseDriver; use anyhow::{bail, Result}; +use log::error; use sqlx::mysql::MySqlRow; +use sqlx::Executor; use sqlx::{Connection, MySqlConnection, Row}; use std::future::Future; use std::pin::Pin; @@ -60,7 +62,18 @@ impl DatabaseDriver for MariaDBDriver { query: &'a str, ) -> Pin> + '_>> { let fut = async move { - sqlx::query(query).execute(&mut self.db).await?; + let mut tx = self.db.begin().await?; + + match tx.execute(query).await { + Ok(_) => { + tx.commit().await?; + } + Err(e) => { + error!("Error executing query: {}", e); + tx.rollback().await?; + } + } + Ok(()) }; diff --git a/src/database_drivers/mysql.rs b/src/database_drivers/mysql.rs index 44036ea..36b4777 100644 --- a/src/database_drivers/mysql.rs +++ b/src/database_drivers/mysql.rs @@ -1,7 +1,9 @@ use crate::config; use crate::database_drivers::{utils, DatabaseDriver}; use anyhow::{bail, Result}; +use log::error; use sqlx::mysql::MySqlRow; +use sqlx::Executor; use sqlx::{Connection, MySqlConnection, Row}; use std::future::Future; use std::pin::Pin; @@ -58,7 +60,18 @@ impl DatabaseDriver for MySQLDriver { query: &'a str, ) -> Pin> + '_>> { let fut = async move { - sqlx::query(query).execute(&mut self.db).await?; + let mut tx = self.db.begin().await?; + + match tx.execute(query).await { + Ok(_) => { + tx.commit().await?; + } + Err(e) => { + error!("Error executing query: {}", e); + tx.rollback().await?; + } + } + Ok(()) }; diff --git a/src/database_drivers/postgres.rs b/src/database_drivers/postgres.rs index db8c8aa..e7f7fee 100644 --- a/src/database_drivers/postgres.rs +++ b/src/database_drivers/postgres.rs @@ -1,7 +1,9 @@ use crate::config; use crate::database_drivers::DatabaseDriver; use anyhow::{bail, Result}; +use log::error; use sqlx::postgres::PgRow; +use sqlx::Executor; use sqlx::{Connection, PgConnection, Row}; use std::future::Future; use std::pin::Pin; @@ -60,7 +62,18 @@ impl DatabaseDriver for PostgresDriver { query: &'a str, ) -> Pin> + '_>> { let fut = async move { - sqlx::query(query).execute(&mut self.db).await?; + let mut tx = self.db.begin().await?; + + match tx.execute(query).await { + Ok(_) => { + tx.commit().await?; + } + Err(e) => { + error!("Error executing query: {}", e); + tx.rollback().await?; + } + } + Ok(()) }; From 0fb7b06a5293d0c28ce45534b81ae2519ffdbd3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Priv=C3=A9r?= Date: Thu, 28 Dec 2023 09:29:07 +0100 Subject: [PATCH 2/5] added transactions for libsql --- src/database_drivers/libsql.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/database_drivers/libsql.rs b/src/database_drivers/libsql.rs index cb29e77..c01f9c8 100644 --- a/src/database_drivers/libsql.rs +++ b/src/database_drivers/libsql.rs @@ -32,7 +32,16 @@ impl DatabaseDriver for LibSQLDriver { query: &'a str, ) -> Pin> + '_>> { let fut = async move { - self.db.execute(query).await?; + let tx = self.db.transaction().await?; + match tx.execute(query).await { + Ok(_) => { + tx.commit().await?; + } + Err(err) => { + tx.rollback().await?; + bail!("{:?}", err); + } + } Ok(()) }; From 19b922631813073eaa889bff0a327deaa99eb5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Priv=C3=A9r?= Date: Thu, 28 Dec 2023 09:30:42 +0100 Subject: [PATCH 3/5] changed to error! for libsql transactions error reportin --- src/database_drivers/libsql.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/database_drivers/libsql.rs b/src/database_drivers/libsql.rs index c01f9c8..29e8ba2 100644 --- a/src/database_drivers/libsql.rs +++ b/src/database_drivers/libsql.rs @@ -3,6 +3,7 @@ use anyhow::{bail, Result}; use libsql_client::{de, Client, Config}; use std::future::Future; use std::pin::Pin; +use log::error; use super::SchemaMigration; @@ -38,8 +39,8 @@ impl DatabaseDriver for LibSQLDriver { tx.commit().await?; } Err(err) => { + error!("Error executing query: {}", e); tx.rollback().await?; - bail!("{:?}", err); } } Ok(()) From 895c2d55d544ad728dc23ca2d2b7be1d1fcd5b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Priv=C3=A9r?= Date: Thu, 28 Dec 2023 09:37:05 +0100 Subject: [PATCH 4/5] fixed error --- src/database_drivers/libsql.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/database_drivers/libsql.rs b/src/database_drivers/libsql.rs index 29e8ba2..8f00e4f 100644 --- a/src/database_drivers/libsql.rs +++ b/src/database_drivers/libsql.rs @@ -1,9 +1,9 @@ use crate::database_drivers::DatabaseDriver; use anyhow::{bail, Result}; use libsql_client::{de, Client, Config}; +use log::error; use std::future::Future; use std::pin::Pin; -use log::error; use super::SchemaMigration; @@ -39,7 +39,7 @@ impl DatabaseDriver for LibSQLDriver { tx.commit().await?; } Err(err) => { - error!("Error executing query: {}", e); + error!("Error executing query: {}", err); tx.rollback().await?; } } From b16627b25cff5a44c8fd8eeed14631382be62143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Priv=C3=A9r?= Date: Thu, 28 Dec 2023 09:37:56 +0100 Subject: [PATCH 5/5] fixed lint --- src/database_drivers/sqlite.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/database_drivers/sqlite.rs b/src/database_drivers/sqlite.rs index 6418f9a..a66d89d 100644 --- a/src/database_drivers/sqlite.rs +++ b/src/database_drivers/sqlite.rs @@ -16,7 +16,7 @@ impl<'a> SqliteDriver { pub async fn new<'b>(db_url: &str) -> Result { let path = std::path::Path::new(db_url.split_once("://").unwrap().1); - if let Err(_) = File::open(path.to_str().unwrap()) { + if File::open(path.to_str().unwrap()).is_err() { if let Some(parent) = path.parent() { fs::create_dir_all(parent)?; }