From 8c2f76ddd2e83d6e665749f3bbef0883e8da1e38 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 20 Sep 2020 17:32:28 -0600 Subject: [PATCH] Reorganize migrations and database stuff --- scripts/add_migration.sh | 6 +++--- src/{migrations.rs => database/migration.rs} | 0 .../migrations/0001-table_sections-down.sql | 0 src/{ => database}/migrations/0001-table_sections-up.sql | 0 src/{ => database}/migrations/0002-section_rows-down.sql | 0 src/{ => database}/migrations/0002-section_rows-up.sql | 0 .../migrations/0003-table_programs-down.sql | 0 src/{ => database}/migrations/0003-table_programs-up.sql | 0 src/{ => database}/migrations/0004-program_rows-down.sql | 0 src/{ => database}/migrations/0004-program_rows-up.sql | 0 .../migrations/0005-view_program_sequence-down.sql | 0 .../migrations/0005-view_program_sequence-up.sql | 0 src/{db.rs => database/migrations/mod.rs} | 8 ++++---- src/database/mod.rs | 5 +++++ src/main.rs | 5 ++--- 15 files changed, 14 insertions(+), 10 deletions(-) rename src/{migrations.rs => database/migration.rs} (100%) rename src/{ => database}/migrations/0001-table_sections-down.sql (100%) rename src/{ => database}/migrations/0001-table_sections-up.sql (100%) rename src/{ => database}/migrations/0002-section_rows-down.sql (100%) rename src/{ => database}/migrations/0002-section_rows-up.sql (100%) rename src/{ => database}/migrations/0003-table_programs-down.sql (100%) rename src/{ => database}/migrations/0003-table_programs-up.sql (100%) rename src/{ => database}/migrations/0004-program_rows-down.sql (100%) rename src/{ => database}/migrations/0004-program_rows-up.sql (100%) rename src/{ => database}/migrations/0005-view_program_sequence-down.sql (100%) rename src/{ => database}/migrations/0005-view_program_sequence-up.sql (100%) rename src/{db.rs => database/migrations/mod.rs} (76%) create mode 100644 src/database/mod.rs diff --git a/scripts/add_migration.sh b/scripts/add_migration.sh index 5b86592..2c84cb9 100755 --- a/scripts/add_migration.sh +++ b/scripts/add_migration.sh @@ -18,7 +18,7 @@ if [[ -z "$MIGRATION_NAME" ]]; then exit 1 fi -MIGRATIONS_DIR="$PROJECT_ROOT/src/migrations" +MIGRATIONS_DIR="$PROJECT_ROOT/src/database/migrations" # echo "MIGRATIONS_DIR: $MIGRATIONS_DIR" @@ -57,9 +57,9 @@ for SUFFIX in "up" "down"; do touch "$migration_file" done -LINE_TO_INSERT=" migs.add(include_file_migration!($NEXT_MIGRATION, \"$NEXT_MIGRATION_PREFIX-$MIGRATION_NAME\"));" +LINE_TO_INSERT="\ \ \ \ migs.add(include_file_migration!($NEXT_MIGRATION, \"$NEXT_MIGRATION_PREFIX-$MIGRATION_NAME\"));" -MIGRATIONS_RS="$PROJECT_ROOT/src/db.rs" +MIGRATIONS_RS="$PROJECT_ROOT/src/database/migrations/mod.rs" echo "Inserting line in $MIGRATIONS_RS" sed -i "/INSERT MIGRATION ABOVE/i \ $LINE_TO_INSERT" "$MIGRATIONS_RS" diff --git a/src/migrations.rs b/src/database/migration.rs similarity index 100% rename from src/migrations.rs rename to src/database/migration.rs diff --git a/src/migrations/0001-table_sections-down.sql b/src/database/migrations/0001-table_sections-down.sql similarity index 100% rename from src/migrations/0001-table_sections-down.sql rename to src/database/migrations/0001-table_sections-down.sql diff --git a/src/migrations/0001-table_sections-up.sql b/src/database/migrations/0001-table_sections-up.sql similarity index 100% rename from src/migrations/0001-table_sections-up.sql rename to src/database/migrations/0001-table_sections-up.sql diff --git a/src/migrations/0002-section_rows-down.sql b/src/database/migrations/0002-section_rows-down.sql similarity index 100% rename from src/migrations/0002-section_rows-down.sql rename to src/database/migrations/0002-section_rows-down.sql diff --git a/src/migrations/0002-section_rows-up.sql b/src/database/migrations/0002-section_rows-up.sql similarity index 100% rename from src/migrations/0002-section_rows-up.sql rename to src/database/migrations/0002-section_rows-up.sql diff --git a/src/migrations/0003-table_programs-down.sql b/src/database/migrations/0003-table_programs-down.sql similarity index 100% rename from src/migrations/0003-table_programs-down.sql rename to src/database/migrations/0003-table_programs-down.sql diff --git a/src/migrations/0003-table_programs-up.sql b/src/database/migrations/0003-table_programs-up.sql similarity index 100% rename from src/migrations/0003-table_programs-up.sql rename to src/database/migrations/0003-table_programs-up.sql diff --git a/src/migrations/0004-program_rows-down.sql b/src/database/migrations/0004-program_rows-down.sql similarity index 100% rename from src/migrations/0004-program_rows-down.sql rename to src/database/migrations/0004-program_rows-down.sql diff --git a/src/migrations/0004-program_rows-up.sql b/src/database/migrations/0004-program_rows-up.sql similarity index 100% rename from src/migrations/0004-program_rows-up.sql rename to src/database/migrations/0004-program_rows-up.sql diff --git a/src/migrations/0005-view_program_sequence-down.sql b/src/database/migrations/0005-view_program_sequence-down.sql similarity index 100% rename from src/migrations/0005-view_program_sequence-down.sql rename to src/database/migrations/0005-view_program_sequence-down.sql diff --git a/src/migrations/0005-view_program_sequence-up.sql b/src/database/migrations/0005-view_program_sequence-up.sql similarity index 100% rename from src/migrations/0005-view_program_sequence-up.sql rename to src/database/migrations/0005-view_program_sequence-up.sql diff --git a/src/db.rs b/src/database/migrations/mod.rs similarity index 76% rename from src/db.rs rename to src/database/migrations/mod.rs index 4234fda..6aa06a1 100644 --- a/src/db.rs +++ b/src/database/migrations/mod.rs @@ -1,11 +1,11 @@ -use crate::migrations::{Migrations, SimpleMigration}; +use super::migration::{Migrations, SimpleMigration}; macro_rules! include_file_migration { ($mig_version:expr, $mig_name:literal) => { SimpleMigration::new_box( $mig_version, - include_str!(concat!("migrations/", $mig_name, "-up.sql")), - include_str!(concat!("migrations/", $mig_name, "-down.sql")), + include_str!(concat!($mig_name, "-up.sql")), + include_str!(concat!($mig_name, "-down.sql")), ) }; } @@ -19,4 +19,4 @@ pub fn create_migrations() -> Migrations { migs.add(include_file_migration!(5, "0005-view_program_sequence")); // INSERT MIGRATION ABOVE -- DO NOT EDIT THIS COMMENT migs -} +} \ No newline at end of file diff --git a/src/database/mod.rs b/src/database/mod.rs new file mode 100644 index 0000000..bc51812 --- /dev/null +++ b/src/database/mod.rs @@ -0,0 +1,5 @@ +mod migration; +mod migrations; + +pub use migration::*; +pub use migrations::create_migrations; diff --git a/src/main.rs b/src/main.rs index 6ed0769..b773725 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,7 @@ use rusqlite::NO_PARAMS; use tracing::info; use tracing_subscriber::EnvFilter; -mod db; -mod migrations; +mod database; mod model; mod option_future; mod program_runner; @@ -25,7 +24,7 @@ fn setup_db() -> Result { // let conn = DbConnection::open_in_memory()?; let mut conn = DbConnection::open("test.db")?; - let migs = db::create_migrations(); + let migs = database::create_migrations(); migs.apply(&mut conn)?; Ok(conn)