From e50bb7be3a079518c25fa76a513830635e8a28ee Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Mon, 4 May 2026 16:29:45 -0400 Subject: db/mod.rs: Add functions to read scripts for migration --- src/db/mod.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/db/mod.rs (limited to 'src/db') diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..1a60ce3 --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1,54 @@ +use miette::Diagnostic; +use thiserror::Error; + +pub struct Migration { + pub filename: String, + pub directory: String, + pub script_contents: String, +} + +const DB_MIGRATION_DIR: &str = "./src/db/"; + +/// To prevent accidental error coersion in other modules, std::io::Error gets cast as Migration Error here +#[derive(Debug, Diagnostic, Error)] +pub enum MigrationError { + #[error("IO error")] + IOMigrationError(#[from] std::io::Error), +} + +type Result = std::result::Result; + +/// Reads the filesystem for sql files in the DB migration directory +fn get_script_filenames() -> Result> { + let results = std::fs::read_dir(DB_MIGRATION_DIR)?; + + Ok(results + .flat_map(|entry| entry.unwrap().file_name().into_string()) + .filter(|s| s.ends_with(".sql")) + .collect()) +} + +/// Gets SQL files from the DB migration directory to provide instructions for setting up the database +pub fn get_migration_scripts() -> Result> { + let files = get_script_filenames()?; + let mut migrations = vec![]; + + for file in files { + let filename = String::from(DB_MIGRATION_DIR) + &file; + tracing::event!( + tracing::Level::TRACE, + filename = filename, + "Found migration script." + ); + + let contents = std::fs::read_to_string(filename)?; + + migrations.push(Migration { + filename: file, + directory: DB_MIGRATION_DIR.into(), + script_contents: contents, + }); + } + + Ok(migrations) +} -- cgit v1.3