Refactor initialization of on/off schedule
This commit is contained in:
		
							parent
							
								
									5b54f40ec0
								
							
						
					
					
						commit
						a90ea25b87
					
				
					 4 changed files with 47 additions and 58 deletions
				
			
		| 
						 | 
				
			
			@ -1,14 +1,9 @@
 | 
			
		|||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
use log::{info, trace};
 | 
			
		||||
use sqlx::migrate::Migrator;
 | 
			
		||||
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::types::ScheduleUid;
 | 
			
		||||
 | 
			
		||||
mod controllers;
 | 
			
		||||
pub mod errors;
 | 
			
		||||
mod model_utils;
 | 
			
		||||
| 
						 | 
				
			
			@ -24,36 +19,10 @@ pub use tag::DbTag;
 | 
			
		|||
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
 | 
			
		||||
 | 
			
		||||
pub async fn run_migrations(pool: &Pool<Sqlite>) {
 | 
			
		||||
	info!("Running migrations");
 | 
			
		||||
	log::info!("Running migrations");
 | 
			
		||||
	MIGRATOR.run(pool).await.expect("Failed to run migrations.");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn init_schedule(
 | 
			
		||||
	pool: &Pool<Sqlite>,
 | 
			
		||||
	uid: &ScheduleUid,
 | 
			
		||||
	name: &str,
 | 
			
		||||
	periods: DbPeriods,
 | 
			
		||||
) -> Result<(), DatabaseError> {
 | 
			
		||||
	trace!("Initializing schedule {:?}", name);
 | 
			
		||||
	match DbSchedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await? {
 | 
			
		||||
		Some(_) => Ok(()),
 | 
			
		||||
		None => {
 | 
			
		||||
			trace!("Schedule {:?} not found, inserting", name);
 | 
			
		||||
			sqlx::query_as!(
 | 
			
		||||
				DbSchedule,
 | 
			
		||||
				"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
				uid,
 | 
			
		||||
				name,
 | 
			
		||||
				periods,
 | 
			
		||||
			)
 | 
			
		||||
			.fetch_optional(pool)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
			.map(|_| ())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn init(db: &str) -> Pool<Sqlite> {
 | 
			
		||||
	let options = SqliteConnectOptions::from_str(db)
 | 
			
		||||
		.expect("Error parsing database path")
 | 
			
		||||
| 
						 | 
				
			
			@ -68,18 +37,14 @@ pub async fn init(db: &str) -> Pool<Sqlite> {
 | 
			
		|||
 | 
			
		||||
	run_migrations(&pool).await;
 | 
			
		||||
 | 
			
		||||
	init_schedule(&pool, &ScheduleUid::Off, "Off", DbPeriods(vec![]))
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Error initializing schedule Off");
 | 
			
		||||
	let mut pool_conn = pool.acquire().await.unwrap();
 | 
			
		||||
 | 
			
		||||
	init_schedule(
 | 
			
		||||
		&pool,
 | 
			
		||||
		&ScheduleUid::On,
 | 
			
		||||
		"On",
 | 
			
		||||
		DbPeriods(vec![Period::new_on()]),
 | 
			
		||||
	)
 | 
			
		||||
	.await
 | 
			
		||||
	.expect("Error initializing schedule On");
 | 
			
		||||
	DbSchedule::get_on(&mut pool_conn)
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Failed to init 'on' schedule");
 | 
			
		||||
	DbSchedule::get_off(&mut pool_conn)
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Failed to init 'off' schedule");
 | 
			
		||||
 | 
			
		||||
	pool
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -88,14 +88,14 @@ impl DbSchedule {
 | 
			
		|||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_uid: ScheduleUid,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &DbPeriods,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		let uid = ScheduleUid::default();
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbSchedule,
 | 
			
		||||
			"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
			uid,
 | 
			
		||||
			new_uid,
 | 
			
		||||
			new_name,
 | 
			
		||||
			new_periods,
 | 
			
		||||
		)
 | 
			
		||||
| 
						 | 
				
			
			@ -104,6 +104,22 @@ impl DbSchedule {
 | 
			
		|||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_on(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::On).await? {
 | 
			
		||||
			return Ok(schedule);
 | 
			
		||||
		}
 | 
			
		||||
		let periods = DbPeriods(vec![Period::new_on()]);
 | 
			
		||||
		Self::create(conn, ScheduleUid::On, "On", &periods).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_off(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::Off).await? {
 | 
			
		||||
			return Ok(schedule);
 | 
			
		||||
		}
 | 
			
		||||
		let periods = DbPeriods(vec![]);
 | 
			
		||||
		Self::create(conn, ScheduleUid::Off, "Off", &periods).await
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue