Revert "Add sql transactions"
This caused the error "locked database".
This reverts commit 4489b37a3f
.
This commit is contained in:
parent
4489b37a3f
commit
9ed576b552
4 changed files with 477 additions and 625 deletions
src
29
src/main.rs
29
src/main.rs
|
@ -6,7 +6,7 @@ use emgauwa_common::models::{Controller, FromDbModel};
|
|||
use emgauwa_common::types::EmgauwaUid;
|
||||
use emgauwa_common::utils::{drop_privileges, init_logging};
|
||||
use rppal_pfd::PiFaceDigital;
|
||||
use sqlx::Transaction;
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
|
||||
use crate::relay_loop::run_relays_loop;
|
||||
|
@ -21,11 +21,11 @@ mod utils;
|
|||
mod ws;
|
||||
|
||||
async fn create_this_controller(
|
||||
tx: &mut Transaction<'_, Sqlite>,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
settings: &Settings,
|
||||
) -> Result<DbController, EmgauwaError> {
|
||||
DbController::create(
|
||||
tx,
|
||||
conn,
|
||||
&EmgauwaUid::default(),
|
||||
&settings.name,
|
||||
settings.relays.len() as i64,
|
||||
|
@ -35,12 +35,12 @@ async fn create_this_controller(
|
|||
}
|
||||
|
||||
async fn create_this_relay(
|
||||
tx: &mut Transaction<'_, Sqlite>,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
this_controller: &DbController,
|
||||
settings_relay: &settings::Relay,
|
||||
) -> Result<DbRelay, EmgauwaError> {
|
||||
let relay = DbRelay::create(
|
||||
tx,
|
||||
conn,
|
||||
&settings_relay.name,
|
||||
settings_relay.number.ok_or(EmgauwaError::Internal(
|
||||
"Relay number is missing".to_string(),
|
||||
|
@ -49,9 +49,9 @@ async fn create_this_relay(
|
|||
)
|
||||
.await?;
|
||||
|
||||
let off = DbSchedule::get_off(tx).await?;
|
||||
let off = DbSchedule::get_off(conn).await?;
|
||||
for weekday in 0..7 {
|
||||
DbJunctionRelaySchedule::set_schedule(tx, &relay, &off, weekday).await?;
|
||||
DbJunctionRelaySchedule::set_schedule(conn, &relay, &off, weekday).await?;
|
||||
}
|
||||
|
||||
Ok(relay)
|
||||
|
@ -72,20 +72,20 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
.await
|
||||
.map_err(EmgauwaError::from)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(EmgauwaError::from)?;
|
||||
let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
|
||||
|
||||
let db_controller = match DbController::get_all(&mut tx)
|
||||
let db_controller = match DbController::get_all(&mut conn)
|
||||
.await
|
||||
.map_err(EmgauwaError::from)?
|
||||
.pop()
|
||||
{
|
||||
None => futures::executor::block_on(create_this_controller(&mut tx, &settings))?,
|
||||
None => futures::executor::block_on(create_this_controller(&mut conn, &settings))?,
|
||||
Some(c) => c,
|
||||
};
|
||||
|
||||
for relay in &settings.relays {
|
||||
if DbRelay::get_by_controller_and_num(
|
||||
&mut tx,
|
||||
&mut conn,
|
||||
&db_controller,
|
||||
relay.number.ok_or(EmgauwaError::Internal(
|
||||
"Relay number is missing".to_string(),
|
||||
|
@ -95,19 +95,18 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
.map_err(EmgauwaError::from)?
|
||||
.is_none()
|
||||
{
|
||||
create_this_relay(&mut tx, &db_controller, relay)
|
||||
create_this_relay(&mut conn, &db_controller, relay)
|
||||
.await
|
||||
.map_err(EmgauwaError::from)?;
|
||||
}
|
||||
}
|
||||
|
||||
let db_controller = db_controller
|
||||
.update(&mut tx, &db_controller.name, settings.relays.len() as i64)
|
||||
.update(&mut conn, &db_controller.name, settings.relays.len() as i64)
|
||||
.await
|
||||
.map_err(EmgauwaError::from)?;
|
||||
|
||||
let this = Controller::from_db_model(&mut tx, db_controller).map_err(EmgauwaError::from)?;
|
||||
tx.commit().await.map_err(EmgauwaError::from)?;
|
||||
let this = Controller::from_db_model(&mut conn, db_controller).map_err(EmgauwaError::from)?;
|
||||
|
||||
let url = format!(
|
||||
"ws://{}:{}/api/v1/ws/controllers",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue