From 1bc9ae01d8f282e0eaaf71cf8034d36533fa9b5a Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 5 Jan 2025 12:48:07 +0100 Subject: [PATCH] refactor: some lil things --- src/db/mod.rs | 32 ++++++++++++++++++++++++++++++++ src/db/plant.rs | 16 ++++++++-------- src/main.rs | 36 ++---------------------------------- src/server/plants.rs | 2 +- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index 69a1a13..6ac39fc 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -45,3 +45,35 @@ impl From for DbError { Self::Db(value) } } + +pub fn run_migrations(pool: &DbPool, migrations: &[&str]) -> rusqlite::Result<()> { + let mut conn = pool.get().unwrap(); + + // If the migration version query fails, we assume it's because the table isn't there yet so we + // try to run the first migration + let mut next_version = conn + .query_row("select max(version) from migration_version", (), |row| { + row.get::<_, usize>(0).map(|n| n + 1) + }) + .unwrap_or(0); + + while next_version < migrations.len() { + let tx = conn.transaction()?; + + tx.execute(migrations[next_version], ())?; + + let cur_time = chrono::Local::now().timestamp(); + tx.execute( + "insert into migration_version values ($1, $2)", + (next_version, cur_time), + )?; + + tx.commit()?; + + tracing::info!("Applied migration {next_version}"); + + next_version += 1; + } + + Ok(()) +} diff --git a/src/db/plant.rs b/src/db/plant.rs index 1b4faed..21e3609 100644 --- a/src/db/plant.rs +++ b/src/db/plant.rs @@ -11,6 +11,13 @@ pub struct Plant { description: String, } +#[derive(Deserialize)] +pub struct NewPlant { + name: String, + species: String, + description: String, +} + impl Plant { pub fn from_row(row: &Row<'_>) -> Result { Ok(Self { @@ -30,7 +37,7 @@ impl Plant { Ok(plants?) } - pub fn with_id(pool: &DbPool, id: i32) -> Result, DbError> { + pub fn by_id(pool: &DbPool, id: i32) -> Result, DbError> { let conn = pool.get()?; let mut stmt = conn.prepare("select * from plants where id = $1")?; @@ -50,13 +57,6 @@ impl Plant { } } -#[derive(Deserialize)] -pub struct NewPlant { - name: String, - species: String, - description: String, -} - impl NewPlant { pub fn insert(self, pool: &DbPool) -> Result { let conn = pool.get()?; diff --git a/src/main.rs b/src/main.rs index 09d541c..10d098b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod server; use std::sync::Arc; -use r2d2_sqlite::{rusqlite, SqliteConnectionManager}; +use r2d2_sqlite::SqliteConnectionManager; use tera::Tera; use tower_http::compression::CompressionLayer; @@ -29,7 +29,7 @@ async fn main() { let manager = SqliteConnectionManager::file("db.sqlite"); let pool = r2d2::Pool::new(manager).unwrap(); - run_migrations(&pool).unwrap(); + db::run_migrations(&pool, &MIGRATIONS).unwrap(); let tera = load_templates(); let ctx = Context { @@ -48,38 +48,6 @@ async fn main() { .unwrap(); } -fn run_migrations(pool: &db::DbPool) -> rusqlite::Result<()> { - let mut conn = pool.get().unwrap(); - - // If the migration version query fails, we assume it's because the table isn't there yet so we - // try to run the first migration - let mut next_version = conn - .query_row("select max(version) from migration_version", (), |row| { - row.get::<_, usize>(0).map(|n| n + 1) - }) - .unwrap_or(0); - - while next_version < MIGRATIONS.len() { - let tx = conn.transaction()?; - - tx.execute(MIGRATIONS[next_version], ())?; - - let cur_time = chrono::Local::now().timestamp(); - tx.execute( - "insert into migration_version values ($1, $2)", - (next_version, cur_time), - )?; - - tx.commit()?; - - tracing::info!("Applied migration {next_version}"); - - next_version += 1; - } - - Ok(()) -} - fn load_templates() -> Tera { let mut tera = Tera::default(); diff --git a/src/server/plants.rs b/src/server/plants.rs index a518a78..f2839a8 100644 --- a/src/server/plants.rs +++ b/src/server/plants.rs @@ -24,7 +24,7 @@ async fn get_plant_page( Path(plant_id): Path, ) -> Html { let (plant, comments) = tokio::task::spawn_blocking(move || { - let plant = Plant::with_id(&ctx.pool, plant_id)?.unwrap(); + let plant = Plant::by_id(&ctx.pool, plant_id)?.unwrap(); let comments = plant.comments(&ctx.pool)?; Ok::<_, DbError>((plant, comments))