Add sql transactions

This commit is contained in:
Tobias Reisinger 2024-05-02 13:30:47 +02:00
parent f539c47e3b
commit 19e2ea003b
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
17 changed files with 242 additions and 240 deletions

View file

@ -1,7 +1,7 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbRelay, DbTag}; use crate::db::{DbRelay, DbTag};
@ -21,26 +21,26 @@ pub struct DbController {
impl DbController { impl DbController {
pub async fn get_all( pub async fn get_all(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<DbController>, DatabaseError> { ) -> Result<Vec<DbController>, DatabaseError> {
sqlx::query_as!(DbController, "SELECT * FROM controllers") sqlx::query_as!(DbController, "SELECT * FROM controllers")
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbController>, DatabaseError> { ) -> Result<Option<DbController>, DatabaseError> {
sqlx::query_as!(DbController, "SELECT * FROM controllers WHERE id = ?", id) sqlx::query_as!(DbController, "SELECT * FROM controllers WHERE id = ?", id)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_uid( pub async fn get_by_uid(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_uid: &EmgauwaUid, filter_uid: &EmgauwaUid,
) -> Result<Option<DbController>, DatabaseError> { ) -> Result<Option<DbController>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -48,39 +48,39 @@ impl DbController {
"SELECT * FROM controllers WHERE uid = ?", "SELECT * FROM controllers WHERE uid = ?",
filter_uid filter_uid
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_uid_or_create( pub async fn get_by_uid_or_create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
uid: &EmgauwaUid, uid: &EmgauwaUid,
new_name: &str, new_name: &str,
new_relay_count: i64, new_relay_count: i64,
) -> Result<DbController, DatabaseError> { ) -> Result<DbController, DatabaseError> {
match DbController::get_by_uid(conn, uid).await? { match DbController::get_by_uid(tx, uid).await? {
Some(tag) => Ok(tag), Some(tag) => Ok(tag),
None => DbController::create(conn, uid, new_name, new_relay_count).await, None => DbController::create(tx, uid, new_name, new_relay_count).await,
} }
} }
pub async fn get_by_tag( pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
tag: &DbTag, tag: &DbTag,
) -> Result<Vec<DbController>, DatabaseError> { ) -> Result<Vec<DbController>, DatabaseError> {
sqlx::query_as!(DbController, "SELECT schedule.* FROM controllers AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id) sqlx::query_as!(DbController, "SELECT schedule.* FROM controllers AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn delete_by_uid( pub async fn delete_by_uid(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_uid: EmgauwaUid, filter_uid: EmgauwaUid,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
if sqlx::query_scalar!("SELECT 1 FROM controllers WHERE uid = ?", filter_uid) if sqlx::query_scalar!("SELECT 1 FROM controllers WHERE uid = ?", filter_uid)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.is_none() .is_none()
{ {
@ -88,7 +88,7 @@ impl DbController {
} }
sqlx::query!("DELETE FROM controllers WHERE uid = ?", filter_uid) sqlx::query!("DELETE FROM controllers WHERE uid = ?", filter_uid)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),
@ -97,7 +97,7 @@ impl DbController {
} }
pub async fn create( pub async fn create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_uid: &EmgauwaUid, new_uid: &EmgauwaUid,
new_name: &str, new_name: &str,
new_relay_count: i64, new_relay_count: i64,
@ -109,14 +109,14 @@ impl DbController {
new_name, new_name,
new_relay_count, new_relay_count,
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn update( pub async fn update(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_name: &str, new_name: &str,
new_relay_count: i64, new_relay_count: i64,
) -> Result<DbController, DatabaseError> { ) -> Result<DbController, DatabaseError> {
@ -126,17 +126,17 @@ impl DbController {
new_relay_count, new_relay_count,
self.id, self.id,
) )
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
Self::get(conn, self.id) Self::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::UpdateGetError) .ok_or(DatabaseError::UpdateGetError)
} }
pub async fn update_active( pub async fn update_active(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_active: bool, new_active: bool,
) -> Result<DbController, DatabaseError> { ) -> Result<DbController, DatabaseError> {
sqlx::query!( sqlx::query!(
@ -144,40 +144,40 @@ impl DbController {
new_active, new_active,
self.id, self.id,
) )
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
Self::get(conn, self.id) Self::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::UpdateGetError) .ok_or(DatabaseError::UpdateGetError)
} }
pub async fn get_relays( pub async fn get_relays(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<DbRelay>, DatabaseError> { ) -> Result<Vec<DbRelay>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
DbRelay, DbRelay,
"SELECT * FROM relays WHERE controller_id = ?", "SELECT * FROM relays WHERE controller_id = ?",
self.id self.id
) )
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn all_inactive(conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> { pub async fn all_inactive(tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
sqlx::query!("UPDATE controllers SET active = 0") sqlx::query!("UPDATE controllers SET active = 0")
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
Ok(()) Ok(())
} }
pub async fn reload( pub async fn reload(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbController, DatabaseError> { ) -> Result<DbController, DatabaseError> {
Self::get(conn, self.id) Self::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }

View file

@ -1,6 +1,6 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbRelay, DbSchedule}; use crate::db::{DbRelay, DbSchedule};
@ -16,7 +16,7 @@ pub struct DbJunctionRelaySchedule {
impl DbJunctionRelaySchedule { impl DbJunctionRelaySchedule {
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> { ) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -24,13 +24,13 @@ impl DbJunctionRelaySchedule {
"SELECT * FROM junction_relay_schedule WHERE id = ?", "SELECT * FROM junction_relay_schedule WHERE id = ?",
id id
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_junction_by_relay_and_weekday( pub async fn get_junction_by_relay_and_weekday(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
relay: &DbRelay, relay: &DbRelay,
weekday: Weekday, weekday: Weekday,
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> { ) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
@ -40,13 +40,13 @@ impl DbJunctionRelaySchedule {
relay.id, relay.id,
weekday weekday
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_relays( pub async fn get_relays(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
schedule: &DbSchedule, schedule: &DbSchedule,
) -> Result<Vec<DbRelay>, DatabaseError> { ) -> Result<Vec<DbRelay>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -57,13 +57,13 @@ impl DbJunctionRelaySchedule {
ORDER BY junction_relay_schedule.weekday"#, ORDER BY junction_relay_schedule.weekday"#,
schedule.id schedule.id
) )
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_schedule( pub async fn get_schedule(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
relay: &DbRelay, relay: &DbRelay,
weekday: Weekday, weekday: Weekday,
) -> Result<Option<DbSchedule>, DatabaseError> { ) -> Result<Option<DbSchedule>, DatabaseError> {
@ -75,13 +75,13 @@ impl DbJunctionRelaySchedule {
relay.id, relay.id,
weekday weekday
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_schedules( pub async fn get_schedules(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
relay: &DbRelay, relay: &DbRelay,
) -> Result<Vec<DbSchedule>, DatabaseError> { ) -> Result<Vec<DbSchedule>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -92,18 +92,18 @@ impl DbJunctionRelaySchedule {
ORDER BY junction_relay_schedule.weekday"#, ORDER BY junction_relay_schedule.weekday"#,
relay.id relay.id
) )
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn set_schedule( pub async fn set_schedule(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
relay: &DbRelay, relay: &DbRelay,
schedule: &DbSchedule, schedule: &DbSchedule,
weekday: Weekday, weekday: Weekday,
) -> Result<DbJunctionRelaySchedule, DatabaseError> { ) -> Result<DbJunctionRelaySchedule, DatabaseError> {
match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? { match Self::get_junction_by_relay_and_weekday(tx, relay, weekday).await? {
None => sqlx::query_as!( None => sqlx::query_as!(
DbJunctionRelaySchedule, DbJunctionRelaySchedule,
"INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *", "INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *",
@ -111,7 +111,7 @@ impl DbJunctionRelaySchedule {
relay.id, relay.id,
schedule.id schedule.id
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError), .ok_or(DatabaseError::InsertGetError),
@ -123,10 +123,10 @@ impl DbJunctionRelaySchedule {
schedule.id, schedule.id,
junction.id junction.id
) )
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
Self::get(conn, junction.id) Self::get(tx, junction.id)
.await? .await?
.ok_or(DatabaseError::UpdateGetError) .ok_or(DatabaseError::UpdateGetError)
} }
@ -134,12 +134,12 @@ impl DbJunctionRelaySchedule {
} }
pub async fn set_schedules( pub async fn set_schedules(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
relay: &DbRelay, relay: &DbRelay,
schedules: Vec<&DbSchedule>, schedules: Vec<&DbSchedule>,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
for (weekday, schedule) in schedules.iter().enumerate() { for (weekday, schedule) in schedules.iter().enumerate() {
Self::set_schedule(conn, relay, schedule, weekday as Weekday).await?; Self::set_schedule(tx, relay, schedule, weekday as Weekday).await?;
} }
Ok(()) Ok(())
} }

View file

@ -1,6 +1,6 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbRelay, DbSchedule, DbTag}; use crate::db::{DbRelay, DbSchedule, DbTag};
@ -15,7 +15,7 @@ pub struct DbJunctionTag {
impl DbJunctionTag { impl DbJunctionTag {
pub async fn link_relay( pub async fn link_relay(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
tag: &DbTag, tag: &DbTag,
relay: &DbRelay, relay: &DbRelay,
) -> Result<DbJunctionTag, DatabaseError> { ) -> Result<DbJunctionTag, DatabaseError> {
@ -25,13 +25,13 @@ impl DbJunctionTag {
tag.id, tag.id,
relay.id relay.id
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn link_schedule( pub async fn link_schedule(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
tag: &DbTag, tag: &DbTag,
schedule: &DbSchedule, schedule: &DbSchedule,
) -> Result<DbJunctionTag, DatabaseError> { ) -> Result<DbJunctionTag, DatabaseError> {
@ -41,7 +41,7 @@ impl DbJunctionTag {
tag.id, tag.id,
schedule.id schedule.id
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }

View file

@ -1,7 +1,7 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbController, DbMacroAction, DbRelay, DbSchedule}; use crate::db::{DbController, DbMacroAction, DbRelay, DbSchedule};
@ -18,35 +18,35 @@ pub struct DbMacro {
} }
impl DbMacro { impl DbMacro {
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbMacro>, DatabaseError> { pub async fn get_all(tx: &mut Transaction<'_, Sqlite>) -> Result<Vec<DbMacro>, DatabaseError> {
sqlx::query_as!(DbMacro, "SELECT * FROM macros") sqlx::query_as!(DbMacro, "SELECT * FROM macros")
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbMacro>, DatabaseError> { ) -> Result<Option<DbMacro>, DatabaseError> {
sqlx::query_as!(DbMacro, "SELECT * FROM macros WHERE id = ?", id) sqlx::query_as!(DbMacro, "SELECT * FROM macros WHERE id = ?", id)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_uid( pub async fn get_by_uid(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_uid: &EmgauwaUid, filter_uid: &EmgauwaUid,
) -> Result<Option<DbMacro>, DatabaseError> { ) -> Result<Option<DbMacro>, DatabaseError> {
sqlx::query_as!(DbMacro, "SELECT * FROM macros WHERE uid = ?", filter_uid) sqlx::query_as!(DbMacro, "SELECT * FROM macros WHERE uid = ?", filter_uid)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn create( pub async fn create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_uid: EmgauwaUid, new_uid: EmgauwaUid,
new_name: &str, new_name: &str,
) -> Result<DbMacro, DatabaseError> { ) -> Result<DbMacro, DatabaseError> {
@ -56,14 +56,14 @@ impl DbMacro {
new_uid, new_uid,
new_name new_name
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> { pub async fn delete(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM macros WHERE id = ?", self.id) sqlx::query!("DELETE FROM macros WHERE id = ?", self.id)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),
@ -72,11 +72,11 @@ impl DbMacro {
} }
pub async fn delete_by_uid( pub async fn delete_by_uid(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_uid: EmgauwaUid, filter_uid: EmgauwaUid,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
if sqlx::query_scalar!("SELECT 1 FROM macros WHERE uid = ?", filter_uid) if sqlx::query_scalar!("SELECT 1 FROM macros WHERE uid = ?", filter_uid)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.is_none() .is_none()
{ {
@ -84,7 +84,7 @@ impl DbMacro {
} }
sqlx::query!("DELETE FROM macros WHERE uid = ?", filter_uid) sqlx::query!("DELETE FROM macros WHERE uid = ?", filter_uid)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),
@ -94,62 +94,62 @@ impl DbMacro {
pub async fn update( pub async fn update(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_name: &str, new_name: &str,
) -> Result<DbMacro, DatabaseError> { ) -> Result<DbMacro, DatabaseError> {
sqlx::query!("UPDATE relays SET name = ? WHERE id = ?", new_name, self.id,) sqlx::query!("UPDATE relays SET name = ? WHERE id = ?", new_name, self.id,)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
DbMacro::get(conn, self.id) DbMacro::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::UpdateGetError) .ok_or(DatabaseError::UpdateGetError)
} }
pub async fn set_actions( pub async fn set_actions(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_actions: &[RequestMacroAction], new_actions: &[RequestMacroAction],
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM macro_actions WHERE macro_id = ?", self.id) sqlx::query!("DELETE FROM macro_actions WHERE macro_id = ?", self.id)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
for new_action in new_actions { for new_action in new_actions {
let controller = DbController::get_by_uid(conn, &new_action.relay.controller_id) let controller = DbController::get_by_uid(tx, &new_action.relay.controller_id)
.await? .await?
.ok_or(DatabaseError::NotFound)?; .ok_or(DatabaseError::NotFound)?;
let relay = let relay =
DbRelay::get_by_controller_and_num(conn, &controller, new_action.relay.number) DbRelay::get_by_controller_and_num(tx, &controller, new_action.relay.number)
.await? .await?
.ok_or(DatabaseError::NotFound)?; .ok_or(DatabaseError::NotFound)?;
let schedule = DbSchedule::get_by_uid(conn, &new_action.schedule.id) let schedule = DbSchedule::get_by_uid(tx, &new_action.schedule.id)
.await? .await?
.ok_or(DatabaseError::NotFound)?; .ok_or(DatabaseError::NotFound)?;
DbMacroAction::create(conn, self, &relay, &schedule, new_action.weekday).await?; DbMacroAction::create(tx, self, &relay, &schedule, new_action.weekday).await?;
} }
Ok(()) Ok(())
} }
pub async fn get_actions( pub async fn get_actions(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<DbMacroAction>, DatabaseError> { ) -> Result<Vec<DbMacroAction>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
DbMacroAction, DbMacroAction,
"SELECT * FROM macro_actions WHERE macro_id = ?", "SELECT * FROM macro_actions WHERE macro_id = ?",
self.id self.id
) )
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_actions_weekday( pub async fn get_actions_weekday(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
weekday: i64, weekday: i64,
) -> Result<Vec<DbMacroAction>, DatabaseError> { ) -> Result<Vec<DbMacroAction>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -158,7 +158,7 @@ impl DbMacro {
self.id, self.id,
weekday weekday
) )
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }

View file

@ -1,6 +1,6 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbMacro, DbRelay, DbSchedule}; use crate::db::{DbMacro, DbRelay, DbSchedule};
@ -17,16 +17,16 @@ pub struct DbMacroAction {
impl DbMacroAction { impl DbMacroAction {
pub async fn get_all( pub async fn get_all(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<DbMacroAction>, DatabaseError> { ) -> Result<Vec<DbMacroAction>, DatabaseError> {
sqlx::query_as!(DbMacroAction, "SELECT * FROM macro_actions") sqlx::query_as!(DbMacroAction, "SELECT * FROM macro_actions")
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbMacroAction>, DatabaseError> { ) -> Result<Option<DbMacroAction>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -34,13 +34,13 @@ impl DbMacroAction {
"SELECT * FROM macro_actions WHERE id = ?", "SELECT * FROM macro_actions WHERE id = ?",
id id
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn create( pub async fn create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_macro: &DbMacro, new_macro: &DbMacro,
new_relay: &DbRelay, new_relay: &DbRelay,
new_schedule: &DbSchedule, new_schedule: &DbSchedule,
@ -54,14 +54,14 @@ impl DbMacroAction {
new_schedule.id, new_schedule.id,
new_weekday new_weekday
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> { pub async fn delete(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM macro_actions WHERE id = ?", self.id) sqlx::query!("DELETE FROM macro_actions WHERE id = ?", self.id)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),
@ -71,27 +71,27 @@ impl DbMacroAction {
pub async fn get_relay( pub async fn get_relay(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbRelay, DatabaseError> { ) -> Result<DbRelay, DatabaseError> {
DbRelay::get(conn, self.relay_id) DbRelay::get(tx, self.relay_id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }
pub async fn get_schedule( pub async fn get_schedule(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbSchedule, DatabaseError> { ) -> Result<DbSchedule, DatabaseError> {
DbSchedule::get(conn, self.schedule_id) DbSchedule::get(tx, self.schedule_id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }
pub async fn get_macro( pub async fn get_macro(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbMacro, DatabaseError> { ) -> Result<DbMacro, DatabaseError> {
DbMacro::get(conn, self.macro_id) DbMacro::get(tx, self.macro_id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }

View file

@ -46,10 +46,12 @@ pub async fn init(db: &str) -> Result<Pool<Sqlite>, EmgauwaError> {
run_migrations(&pool).await?; run_migrations(&pool).await?;
let mut pool_conn = pool.acquire().await?; let mut tx = pool.begin().await?;
DbSchedule::get_on(&mut pool_conn).await?; DbSchedule::get_on(&mut tx).await?;
DbSchedule::get_off(&mut pool_conn).await?; DbSchedule::get_off(&mut tx).await?;
tx.commit().await?;
Ok(pool) Ok(pool)
} }

View file

@ -1,7 +1,7 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag}; use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag};
@ -20,25 +20,25 @@ pub struct DbRelay {
} }
impl DbRelay { impl DbRelay {
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> { pub async fn get_all(tx: &mut Transaction<'_, Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
sqlx::query_as!(DbRelay, "SELECT * FROM relays") sqlx::query_as!(DbRelay, "SELECT * FROM relays")
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbRelay>, DatabaseError> { ) -> Result<Option<DbRelay>, DatabaseError> {
sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id) sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_controller_and_num( pub async fn get_by_controller_and_num(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
controller: &DbController, controller: &DbController,
number: i64, number: i64,
) -> Result<Option<DbRelay>, DatabaseError> { ) -> Result<Option<DbRelay>, DatabaseError> {
@ -48,38 +48,38 @@ impl DbRelay {
controller.id, controller.id,
number number
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_controller_and_num_or_create( pub async fn get_by_controller_and_num_or_create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
controller: &DbController, controller: &DbController,
number: i64, number: i64,
new_name: &str, new_name: &str,
) -> Result<(DbRelay, bool), DatabaseError> { ) -> Result<(DbRelay, bool), DatabaseError> {
match DbRelay::get_by_controller_and_num(conn, controller, number).await? { match DbRelay::get_by_controller_and_num(tx, controller, number).await? {
Some(relay) => Ok((relay, false)), Some(relay) => Ok((relay, false)),
None => { None => {
let relay = DbRelay::create(conn, new_name, number, controller).await?; let relay = DbRelay::create(tx, new_name, number, controller).await?;
Ok((relay, true)) Ok((relay, true))
} }
} }
} }
pub async fn get_by_tag( pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
tag: &DbTag, tag: &DbTag,
) -> Result<Vec<DbRelay>, DatabaseError> { ) -> Result<Vec<DbRelay>, DatabaseError> {
sqlx::query_as!(DbRelay, "SELECT relay.* FROM relays AS relay INNER JOIN junction_tag ON junction_tag.relay_id = relay.id WHERE junction_tag.tag_id = ?", tag.id) sqlx::query_as!(DbRelay, "SELECT relay.* FROM relays AS relay INNER JOIN junction_tag ON junction_tag.relay_id = relay.id WHERE junction_tag.tag_id = ?", tag.id)
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn create( pub async fn create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_name: &str, new_name: &str,
new_number: i64, new_number: i64,
new_controller: &DbController, new_controller: &DbController,
@ -91,14 +91,14 @@ impl DbRelay {
new_number, new_number,
new_controller.id, new_controller.id,
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn delete(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> { pub async fn delete(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM relays WHERE id = ?", self.id) sqlx::query!("DELETE FROM relays WHERE id = ?", self.id)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),
@ -108,68 +108,68 @@ impl DbRelay {
pub async fn update( pub async fn update(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_name: &str, new_name: &str,
) -> Result<DbRelay, DatabaseError> { ) -> Result<DbRelay, DatabaseError> {
sqlx::query!("UPDATE relays SET name = ? WHERE id = ?", new_name, self.id,) sqlx::query!("UPDATE relays SET name = ? WHERE id = ?", new_name, self.id,)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
DbRelay::get(conn, self.id) DbRelay::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::UpdateGetError) .ok_or(DatabaseError::UpdateGetError)
} }
pub async fn get_controller( pub async fn get_controller(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbController, DatabaseError> { ) -> Result<DbController, DatabaseError> {
DbController::get(conn, self.controller_id) DbController::get(tx, self.controller_id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }
pub async fn get_tags( pub async fn get_tags(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<String>, DatabaseError> { ) -> Result<Vec<String>, DatabaseError> {
sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id) sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn set_tags( pub async fn set_tags(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_tags: &[String], new_tags: &[String],
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM junction_tag WHERE relay_id = ?", self.id) sqlx::query!("DELETE FROM junction_tag WHERE relay_id = ?", self.id)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
for new_tag in new_tags { for new_tag in new_tags {
let tag: DbTag = DbTag::get_by_tag_or_create(conn, new_tag).await?; let tag: DbTag = DbTag::get_by_tag_or_create(tx, new_tag).await?;
DbJunctionTag::link_relay(conn, &tag, self).await?; DbJunctionTag::link_relay(tx, &tag, self).await?;
} }
Ok(()) Ok(())
} }
pub async fn reload( pub async fn reload(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbRelay, DatabaseError> { ) -> Result<DbRelay, DatabaseError> {
Self::get(conn, self.id) Self::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }
pub async fn get_active_schedule( pub async fn get_active_schedule(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbSchedule, DatabaseError> { ) -> Result<DbSchedule, DatabaseError> {
let weekday = utils::get_weekday(); let weekday = utils::get_weekday();
DbJunctionRelaySchedule::get_schedule(conn, self, weekday as Weekday) DbJunctionRelaySchedule::get_schedule(tx, self, weekday as Weekday)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }

View file

@ -3,7 +3,7 @@ use std::ops::DerefMut;
use chrono::NaiveTime; use chrono::NaiveTime;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::model_utils::Period; use crate::db::model_utils::Period;
@ -26,26 +26,26 @@ pub struct DbPeriods(pub Vec<Period>);
impl DbSchedule { impl DbSchedule {
pub async fn get_all( pub async fn get_all(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<DbSchedule>, DatabaseError> { ) -> Result<Vec<DbSchedule>, DatabaseError> {
sqlx::query_as!(DbSchedule, "SELECT * FROM schedules") sqlx::query_as!(DbSchedule, "SELECT * FROM schedules")
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbSchedule>, DatabaseError> { ) -> Result<Option<DbSchedule>, DatabaseError> {
sqlx::query_as!(DbSchedule, "SELECT * FROM schedules WHERE id = ?", id) sqlx::query_as!(DbSchedule, "SELECT * FROM schedules WHERE id = ?", id)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_uid( pub async fn get_by_uid(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_uid: &ScheduleUid, filter_uid: &ScheduleUid,
) -> Result<Option<DbSchedule>, DatabaseError> { ) -> Result<Option<DbSchedule>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
@ -53,23 +53,23 @@ impl DbSchedule {
"SELECT * FROM schedules WHERE uid = ?", "SELECT * FROM schedules WHERE uid = ?",
filter_uid filter_uid
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_tag( pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
tag: &DbTag, tag: &DbTag,
) -> Result<Vec<DbSchedule>, DatabaseError> { ) -> Result<Vec<DbSchedule>, DatabaseError> {
sqlx::query_as!(DbSchedule, "SELECT schedule.* FROM schedules AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id) sqlx::query_as!(DbSchedule, "SELECT schedule.* FROM schedules AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn delete_by_uid( pub async fn delete_by_uid(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_uid: ScheduleUid, filter_uid: ScheduleUid,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
let filter_uid = match filter_uid { let filter_uid = match filter_uid {
@ -79,7 +79,7 @@ impl DbSchedule {
}?; }?;
if sqlx::query_scalar!("SELECT 1 FROM schedules WHERE uid = ?", filter_uid) if sqlx::query_scalar!("SELECT 1 FROM schedules WHERE uid = ?", filter_uid)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.is_none() .is_none()
{ {
@ -87,7 +87,7 @@ impl DbSchedule {
} }
sqlx::query!("DELETE FROM schedules WHERE uid = ?", filter_uid) sqlx::query!("DELETE FROM schedules WHERE uid = ?", filter_uid)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),
@ -96,7 +96,7 @@ impl DbSchedule {
} }
pub async fn create( pub async fn create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_uid: ScheduleUid, new_uid: ScheduleUid,
new_name: &str, new_name: &str,
new_periods: &DbPeriods, new_periods: &DbPeriods,
@ -108,45 +108,45 @@ impl DbSchedule {
new_name, new_name,
new_periods, new_periods,
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn get_by_uid_or_create( pub async fn get_by_uid_or_create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
uid: ScheduleUid, uid: ScheduleUid,
name: &str, name: &str,
periods: &DbPeriods, periods: &DbPeriods,
) -> Result<(DbSchedule, bool), DatabaseError> { ) -> Result<(DbSchedule, bool), DatabaseError> {
match DbSchedule::get_by_uid(conn, &uid).await? { match DbSchedule::get_by_uid(tx, &uid).await? {
Some(schedule) => Ok((schedule, false)), Some(schedule) => Ok((schedule, false)),
None => { None => {
let schedule = DbSchedule::create(conn, uid, name, periods).await?; let schedule = DbSchedule::create(tx, uid, name, periods).await?;
Ok((schedule, true)) Ok((schedule, true))
} }
} }
} }
pub async fn get_on(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> { pub async fn get_on(tx: &mut Transaction<'_, Sqlite>) -> Result<DbSchedule, DatabaseError> {
if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::On).await? { if let Some(schedule) = DbSchedule::get_by_uid(tx, &ScheduleUid::On).await? {
return Ok(schedule); return Ok(schedule);
} }
let periods = DbPeriods(vec![Period::new_on()]); let periods = DbPeriods(vec![Period::new_on()]);
Self::create(conn, ScheduleUid::On, "On", &periods).await Self::create(tx, ScheduleUid::On, "On", &periods).await
} }
pub async fn get_off(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> { pub async fn get_off(tx: &mut Transaction<'_, Sqlite>) -> Result<DbSchedule, DatabaseError> {
if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::Off).await? { if let Some(schedule) = DbSchedule::get_by_uid(tx, &ScheduleUid::Off).await? {
return Ok(schedule); return Ok(schedule);
} }
let periods = DbPeriods(vec![]); let periods = DbPeriods(vec![]);
Self::create(conn, ScheduleUid::Off, "Off", &periods).await Self::create(tx, ScheduleUid::Off, "Off", &periods).await
} }
pub async fn update( pub async fn update(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_name: &str, new_name: &str,
new_periods: &DbPeriods, new_periods: &DbPeriods,
) -> Result<DbSchedule, DatabaseError> { ) -> Result<DbSchedule, DatabaseError> {
@ -162,35 +162,35 @@ impl DbSchedule {
new_periods, new_periods,
self.id, self.id,
) )
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
DbSchedule::get(conn, self.id) DbSchedule::get(tx, self.id)
.await? .await?
.ok_or(DatabaseError::UpdateGetError) .ok_or(DatabaseError::UpdateGetError)
} }
pub async fn get_tags( pub async fn get_tags(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<Vec<String>, DatabaseError> { ) -> Result<Vec<String>, DatabaseError> {
Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.schedule_id = ?", self.id) Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.schedule_id = ?", self.id)
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await?) .await?)
} }
pub async fn set_tags( pub async fn set_tags(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_tags: &[String], new_tags: &[String],
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
sqlx::query!("DELETE FROM junction_tag WHERE schedule_id = ?", self.id) sqlx::query!("DELETE FROM junction_tag WHERE schedule_id = ?", self.id)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await?; .await?;
for new_tag in new_tags { for new_tag in new_tags {
let tag: DbTag = DbTag::get_by_tag_or_create(conn, new_tag).await?; let tag: DbTag = DbTag::get_by_tag_or_create(tx, new_tag).await?;
DbJunctionTag::link_schedule(conn, &tag, self).await?; DbJunctionTag::link_schedule(tx, &tag, self).await?;
} }
Ok(()) Ok(())
} }

View file

@ -1,7 +1,7 @@
use std::ops::DerefMut; use std::ops::DerefMut;
use serde_derive::Serialize; use serde_derive::Serialize;
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
@ -14,7 +14,7 @@ pub struct DbTag {
impl DbTag { impl DbTag {
pub async fn create( pub async fn create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
new_tag: &str, new_tag: &str,
) -> Result<DbTag, DatabaseError> { ) -> Result<DbTag, DatabaseError> {
if new_tag.is_empty() { if new_tag.is_empty() {
@ -26,54 +26,54 @@ impl DbTag {
"INSERT INTO tags (tag) VALUES (?) RETURNING *", "INSERT INTO tags (tag) VALUES (?) RETURNING *",
new_tag new_tag
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.ok_or(DatabaseError::InsertGetError) .ok_or(DatabaseError::InsertGetError)
} }
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbTag>, DatabaseError> { pub async fn get_all(tx: &mut Transaction<'_, Sqlite>) -> Result<Vec<DbTag>, DatabaseError> {
sqlx::query_as!(DbTag, "SELECT * FROM tags") sqlx::query_as!(DbTag, "SELECT * FROM tags")
.fetch_all(conn.deref_mut()) .fetch_all(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get( pub async fn get(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
id: i64, id: i64,
) -> Result<Option<DbTag>, DatabaseError> { ) -> Result<Option<DbTag>, DatabaseError> {
sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE id = ?", id) sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE id = ?", id)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_by_tag_or_create( pub async fn get_by_tag_or_create(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
target_tag: &str, target_tag: &str,
) -> Result<DbTag, DatabaseError> { ) -> Result<DbTag, DatabaseError> {
match DbTag::get_by_tag(conn, target_tag).await? { match DbTag::get_by_tag(tx, target_tag).await? {
Some(tag) => Ok(tag), Some(tag) => Ok(tag),
None => DbTag::create(conn, target_tag).await, None => DbTag::create(tx, target_tag).await,
} }
} }
pub async fn get_by_tag( pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
target_tag: &str, target_tag: &str,
) -> Result<Option<DbTag>, DatabaseError> { ) -> Result<Option<DbTag>, DatabaseError> {
sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE tag = ?", target_tag) sqlx::query_as!(DbTag, "SELECT * FROM tags WHERE tag = ?", target_tag)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await .await
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn delete_by_tag( pub async fn delete_by_tag(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
filter_tag: &str, filter_tag: &str,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
if sqlx::query_scalar!("SELECT 1 FROM tags WHERE tag = ?", filter_tag) if sqlx::query_scalar!("SELECT 1 FROM tags WHERE tag = ?", filter_tag)
.fetch_optional(conn.deref_mut()) .fetch_optional(tx.deref_mut())
.await? .await?
.is_none() .is_none()
{ {
@ -81,7 +81,7 @@ impl DbTag {
} }
sqlx::query!("DELETE FROM tags WHERE tag = ?", filter_tag) sqlx::query!("DELETE FROM tags WHERE tag = ?", filter_tag)
.execute(conn.deref_mut()) .execute(tx.deref_mut())
.await .await
.map(|res| match res.rows_affected() { .map(|res| match res.rows_affected() {
0 => Err(DatabaseError::DeleteError), 0 => Err(DatabaseError::DeleteError),

View file

@ -4,7 +4,7 @@ use actix::MessageResponse;
use chrono::NaiveTime; use chrono::NaiveTime;
use futures::executor::block_on; use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::DbController; use crate::db::DbController;
@ -24,16 +24,16 @@ impl FromDbModel for Controller {
type DbModelCache = Vec<Relay>; type DbModelCache = Vec<Relay>;
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let relays_db = block_on(db_model.get_relays(conn))?; let relays_db = block_on(db_model.get_relays(tx))?;
let cache = convert_db_list_cache(conn, relays_db, db_model.clone())?; let cache = convert_db_list_cache(tx, relays_db, db_model.clone())?;
Self::from_db_model_cache(conn, db_model, cache) Self::from_db_model_cache(tx, db_model, cache)
} }
fn from_db_model_cache( fn from_db_model_cache(
_conn: &mut PoolConnection<Sqlite>, _tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
cache: Self::DbModelCache, cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
@ -45,10 +45,10 @@ impl FromDbModel for Controller {
} }
impl Controller { impl Controller {
pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), EmgauwaError> { pub fn reload(&mut self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), EmgauwaError> {
self.c = block_on(self.c.reload(conn))?; self.c = block_on(self.c.reload(tx))?;
for relay in &mut self.relays { for relay in &mut self.relays {
relay.reload(conn)?; relay.reload(tx)?;
} }
Ok(()) Ok(())
} }

View file

@ -1,6 +1,6 @@
use futures::executor::block_on; use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::DbMacro; use crate::db::DbMacro;
@ -19,19 +19,19 @@ impl FromDbModel for Macro {
type DbModelCache = (); type DbModelCache = ();
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
Self::from_db_model_cache(conn, db_model, ()) Self::from_db_model_cache(tx, db_model, ())
} }
fn from_db_model_cache( fn from_db_model_cache(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
_cache: Self::DbModelCache, _cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let actions_db = block_on(db_model.get_actions(conn))?; let actions_db = block_on(db_model.get_actions(tx))?;
let actions: Vec<MacroAction> = convert_db_list(conn, actions_db)?; let actions: Vec<MacroAction> = convert_db_list(tx, actions_db)?;
Ok(Macro { Ok(Macro {
m: db_model, m: db_model,

View file

@ -1,6 +1,6 @@
use futures::executor::block_on; use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbJunctionRelaySchedule, DbMacroAction}; use crate::db::{DbJunctionRelaySchedule, DbMacroAction};
@ -19,22 +19,22 @@ impl FromDbModel for MacroAction {
type DbModelCache = (); type DbModelCache = ();
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
Self::from_db_model_cache(conn, db_model, ()) Self::from_db_model_cache(tx, db_model, ())
} }
fn from_db_model_cache( fn from_db_model_cache(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
_cache: Self::DbModelCache, _cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let schedule_db = block_on(db_model.get_schedule(conn))?; let schedule_db = block_on(db_model.get_schedule(tx))?;
let schedule = Schedule::from_db_model(conn, schedule_db)?; let schedule = Schedule::from_db_model(tx, schedule_db)?;
let relay_db = block_on(db_model.get_relay(conn))?; let relay_db = block_on(db_model.get_relay(tx))?;
let relay = Relay::from_db_model(conn, relay_db)?; let relay = Relay::from_db_model(tx, relay_db)?;
let weekday = db_model.weekday; let weekday = db_model.weekday;
@ -47,8 +47,8 @@ impl FromDbModel for MacroAction {
} }
impl MacroAction { impl MacroAction {
pub async fn execute(&self, conn: &mut PoolConnection<Sqlite>) -> Result<(), EmgauwaError> { pub async fn execute(&self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), EmgauwaError> {
DbJunctionRelaySchedule::set_schedule(conn, &self.relay.r, &self.schedule.s, self.weekday) DbJunctionRelaySchedule::set_schedule(tx, &self.relay.r, &self.schedule.s, self.weekday)
.await?; .await?;
Ok(()) Ok(())
} }

View file

@ -10,7 +10,7 @@ pub use macro_action::MacroAction;
pub use r#macro::Macro; pub use r#macro::Macro;
pub use relay::Relay; pub use relay::Relay;
pub use schedule::Schedule; pub use schedule::Schedule;
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
pub use tag::Tag; pub use tag::Tag;
@ -21,14 +21,14 @@ pub trait FromDbModel {
type DbModelCache: Clone; type DbModelCache: Clone;
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> ) -> Result<Self, DatabaseError>
where where
Self: Sized; Self: Sized;
fn from_db_model_cache( fn from_db_model_cache(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
cache: Self::DbModelCache, cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> ) -> Result<Self, DatabaseError>
@ -37,15 +37,15 @@ pub trait FromDbModel {
} }
fn convert_db_list_generic<T: FromDbModel>( fn convert_db_list_generic<T: FromDbModel>(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_models: Vec<T::DbModel>, db_models: Vec<T::DbModel>,
cache: Option<T::DbModelCache>, cache: Option<T::DbModelCache>,
) -> Result<Vec<T>, DatabaseError> { ) -> Result<Vec<T>, DatabaseError> {
let mut result: Vec<T> = Vec::new(); let mut result: Vec<T> = Vec::new();
for db_model in db_models { for db_model in db_models {
let new = match &cache { let new = match &cache {
Some(c) => T::from_db_model_cache(conn, db_model, c.clone()), Some(c) => T::from_db_model_cache(tx, db_model, c.clone()),
None => T::from_db_model(conn, db_model), None => T::from_db_model(tx, db_model),
}?; }?;
result.push(new); result.push(new);
} }
@ -53,16 +53,16 @@ fn convert_db_list_generic<T: FromDbModel>(
} }
pub fn convert_db_list<T: FromDbModel>( pub fn convert_db_list<T: FromDbModel>(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_models: Vec<T::DbModel>, db_models: Vec<T::DbModel>,
) -> Result<Vec<T>, DatabaseError> { ) -> Result<Vec<T>, DatabaseError> {
convert_db_list_generic(conn, db_models, None) convert_db_list_generic(tx, db_models, None)
} }
pub fn convert_db_list_cache<T: FromDbModel>( pub fn convert_db_list_cache<T: FromDbModel>(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_models: Vec<T::DbModel>, db_models: Vec<T::DbModel>,
cache: T::DbModelCache, cache: T::DbModelCache,
) -> Result<Vec<T>, DatabaseError> { ) -> Result<Vec<T>, DatabaseError> {
convert_db_list_generic(conn, db_models, Some(cache)) convert_db_list_generic(tx, db_models, Some(cache))
} }

View file

@ -3,7 +3,7 @@ use std::time::Instant;
use chrono::NaiveTime; use chrono::NaiveTime;
use futures::executor::block_on; use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
@ -32,23 +32,23 @@ impl FromDbModel for Relay {
type DbModelCache = DbController; type DbModelCache = DbController;
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let cache = block_on(db_model.get_controller(conn))?; let cache = block_on(db_model.get_controller(tx))?;
Self::from_db_model_cache(conn, db_model, cache) Self::from_db_model_cache(tx, db_model, cache)
} }
fn from_db_model_cache( fn from_db_model_cache(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
cache: Self::DbModelCache, cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let tags = block_on(db_model.get_tags(conn))?; let tags = block_on(db_model.get_tags(tx))?;
let controller_id = cache.uid.clone(); let controller_id = cache.uid.clone();
let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?; let schedules = block_on(DbJunctionRelaySchedule::get_schedules(tx, &db_model))?;
let active_schedule = block_on(db_model.get_active_schedule(conn))?; let active_schedule = block_on(db_model.get_active_schedule(tx))?;
let is_on = None; let is_on = None;
@ -66,19 +66,19 @@ impl FromDbModel for Relay {
} }
impl Relay { impl Relay {
pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> { pub fn reload(&mut self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DatabaseError> {
self.r = block_on(self.r.reload(conn))?; self.r = block_on(self.r.reload(tx))?;
self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?; self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(tx, &self.r))?;
self.reload_active_schedule(conn)?; self.reload_active_schedule(tx)?;
Ok(()) Ok(())
} }
pub fn reload_active_schedule( pub fn reload_active_schedule(
&mut self, &mut self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<(), DatabaseError> { ) -> Result<(), DatabaseError> {
self.active_schedule = block_on(self.r.get_active_schedule(conn))?; self.active_schedule = block_on(self.r.get_active_schedule(tx))?;
Ok(()) Ok(())
} }

View file

@ -1,6 +1,6 @@
use futures::executor::block_on; use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::DbSchedule; use crate::db::DbSchedule;
@ -19,15 +19,15 @@ impl FromDbModel for Schedule {
type DbModelCache = Vec<String>; type DbModelCache = Vec<String>;
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let cache = block_on(db_model.get_tags(conn))?; let cache = block_on(db_model.get_tags(tx))?;
Self::from_db_model_cache(conn, db_model, cache) Self::from_db_model_cache(tx, db_model, cache)
} }
fn from_db_model_cache( fn from_db_model_cache(
_conn: &mut PoolConnection<Sqlite>, _tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
cache: Self::DbModelCache, cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {

View file

@ -1,7 +1,7 @@
use actix::MessageResponse; use actix::MessageResponse;
use futures::executor::block_on; use futures::executor::block_on;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbRelay, DbSchedule, DbTag}; use crate::db::{DbRelay, DbSchedule, DbTag};
@ -20,21 +20,21 @@ impl FromDbModel for Tag {
type DbModelCache = (Vec<Relay>, Vec<Schedule>); type DbModelCache = (Vec<Relay>, Vec<Schedule>);
fn from_db_model( fn from_db_model(
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {
let db_schedules = block_on(DbSchedule::get_by_tag(conn, &db_model))?; let db_schedules = block_on(DbSchedule::get_by_tag(tx, &db_model))?;
let schedules: Vec<Schedule> = convert_db_list(conn, db_schedules)?; let schedules: Vec<Schedule> = convert_db_list(tx, db_schedules)?;
let db_relays = block_on(DbRelay::get_by_tag(conn, &db_model))?; let db_relays = block_on(DbRelay::get_by_tag(tx, &db_model))?;
let relays: Vec<Relay> = convert_db_list(conn, db_relays)?; let relays: Vec<Relay> = convert_db_list(tx, db_relays)?;
let cache = (relays, schedules); let cache = (relays, schedules);
Self::from_db_model_cache(conn, db_model, cache) Self::from_db_model_cache(tx, db_model, cache)
} }
fn from_db_model_cache( fn from_db_model_cache(
_conn: &mut PoolConnection<Sqlite>, _tx: &mut Transaction<'_, Sqlite>,
db_model: Self::DbModel, db_model: Self::DbModel,
cache: Self::DbModelCache, cache: Self::DbModelCache,
) -> Result<Self, DatabaseError> { ) -> Result<Self, DatabaseError> {

View file

@ -1,5 +1,5 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::Transaction;
use sqlx::Sqlite; use sqlx::Sqlite;
use crate::db::{DbPeriods, DbSchedule}; use crate::db::{DbPeriods, DbSchedule};
@ -86,9 +86,9 @@ pub struct RequestMacroExecute {
impl RequestScheduleId { impl RequestScheduleId {
pub async fn get_schedule( pub async fn get_schedule(
&self, &self,
conn: &mut PoolConnection<Sqlite>, tx: &mut Transaction<'_, Sqlite>,
) -> Result<DbSchedule, DatabaseError> { ) -> Result<DbSchedule, DatabaseError> {
DbSchedule::get_by_uid(conn, &self.id) DbSchedule::get_by_uid(tx, &self.id)
.await? .await?
.ok_or(DatabaseError::NotFound) .ok_or(DatabaseError::NotFound)
} }