Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions for migations #22

Merged
merged 6 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/database_drivers/libsql.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;

Expand Down Expand Up @@ -38,8 +39,8 @@ impl DatabaseDriver for LibSQLDriver {
tx.commit().await?;
}
Err(err) => {
error!("Error executing query: {}", err);
tx.rollback().await?;
bail!("{:?}", err);
}
}
Ok(())
Expand Down
15 changes: 14 additions & 1 deletion src/database_drivers/maria.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,7 +62,18 @@ impl DatabaseDriver for MariaDBDriver {
query: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), anyhow::Error>> + '_>> {
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(())
};

Expand Down
15 changes: 14 additions & 1 deletion src/database_drivers/mysql.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,7 +60,18 @@ impl DatabaseDriver for MySQLDriver {
query: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), anyhow::Error>> + '_>> {
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(())
};

Expand Down
15 changes: 14 additions & 1 deletion src/database_drivers/postgres.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,7 +62,18 @@ impl DatabaseDriver for PostgresDriver {
query: &'a str,
) -> Pin<Box<dyn Future<Output = Result<(), anyhow::Error>> + '_>> {
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(())
};

Expand Down
2 changes: 1 addition & 1 deletion src/database_drivers/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'a> SqliteDriver {
pub async fn new<'b>(db_url: &str) -> Result<SqliteDriver> {
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)?;
}
Expand Down
Loading