Browse Source

Reorganize migrations and database stuff

master
Alex Mikhalev 4 years ago
parent
commit
8c2f76ddd2
  1. 6
      scripts/add_migration.sh
  2. 0
      src/database/migration.rs
  3. 0
      src/database/migrations/0001-table_sections-down.sql
  4. 0
      src/database/migrations/0001-table_sections-up.sql
  5. 0
      src/database/migrations/0002-section_rows-down.sql
  6. 0
      src/database/migrations/0002-section_rows-up.sql
  7. 0
      src/database/migrations/0003-table_programs-down.sql
  8. 0
      src/database/migrations/0003-table_programs-up.sql
  9. 0
      src/database/migrations/0004-program_rows-down.sql
  10. 0
      src/database/migrations/0004-program_rows-up.sql
  11. 0
      src/database/migrations/0005-view_program_sequence-down.sql
  12. 0
      src/database/migrations/0005-view_program_sequence-up.sql
  13. 8
      src/database/migrations/mod.rs
  14. 5
      src/database/mod.rs
  15. 5
      src/main.rs

6
scripts/add_migration.sh

@ -18,7 +18,7 @@ if [[ -z "$MIGRATION_NAME" ]]; then
exit 1 exit 1
fi fi
MIGRATIONS_DIR="$PROJECT_ROOT/src/migrations" MIGRATIONS_DIR="$PROJECT_ROOT/src/database/migrations"
# echo "MIGRATIONS_DIR: $MIGRATIONS_DIR" # echo "MIGRATIONS_DIR: $MIGRATIONS_DIR"
@ -57,9 +57,9 @@ for SUFFIX in "up" "down"; do
touch "$migration_file" touch "$migration_file"
done 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" echo "Inserting line in $MIGRATIONS_RS"
sed -i "/INSERT MIGRATION ABOVE/i \ sed -i "/INSERT MIGRATION ABOVE/i \
$LINE_TO_INSERT" "$MIGRATIONS_RS" $LINE_TO_INSERT" "$MIGRATIONS_RS"

0
src/migrations.rs → src/database/migration.rs

0
src/migrations/0001-table_sections-down.sql → src/database/migrations/0001-table_sections-down.sql

0
src/migrations/0001-table_sections-up.sql → src/database/migrations/0001-table_sections-up.sql

0
src/migrations/0002-section_rows-down.sql → src/database/migrations/0002-section_rows-down.sql

0
src/migrations/0002-section_rows-up.sql → src/database/migrations/0002-section_rows-up.sql

0
src/migrations/0003-table_programs-down.sql → src/database/migrations/0003-table_programs-down.sql

0
src/migrations/0003-table_programs-up.sql → src/database/migrations/0003-table_programs-up.sql

0
src/migrations/0004-program_rows-down.sql → src/database/migrations/0004-program_rows-down.sql

0
src/migrations/0004-program_rows-up.sql → src/database/migrations/0004-program_rows-up.sql

0
src/migrations/0005-view_program_sequence-down.sql → src/database/migrations/0005-view_program_sequence-down.sql

0
src/migrations/0005-view_program_sequence-up.sql → src/database/migrations/0005-view_program_sequence-up.sql

8
src/db.rs → src/database/migrations/mod.rs

@ -1,11 +1,11 @@
use crate::migrations::{Migrations, SimpleMigration}; use super::migration::{Migrations, SimpleMigration};
macro_rules! include_file_migration { macro_rules! include_file_migration {
($mig_version:expr, $mig_name:literal) => { ($mig_version:expr, $mig_name:literal) => {
SimpleMigration::new_box( SimpleMigration::new_box(
$mig_version, $mig_version,
include_str!(concat!("migrations/", $mig_name, "-up.sql")), include_str!(concat!($mig_name, "-up.sql")),
include_str!(concat!("migrations/", $mig_name, "-down.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")); migs.add(include_file_migration!(5, "0005-view_program_sequence"));
// INSERT MIGRATION ABOVE -- DO NOT EDIT THIS COMMENT // INSERT MIGRATION ABOVE -- DO NOT EDIT THIS COMMENT
migs migs
} }

5
src/database/mod.rs

@ -0,0 +1,5 @@
mod migration;
mod migrations;
pub use migration::*;
pub use migrations::create_migrations;

5
src/main.rs

@ -7,8 +7,7 @@ use rusqlite::NO_PARAMS;
use tracing::info; use tracing::info;
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
mod db; mod database;
mod migrations;
mod model; mod model;
mod option_future; mod option_future;
mod program_runner; mod program_runner;
@ -25,7 +24,7 @@ fn setup_db() -> Result<DbConnection> {
// let conn = DbConnection::open_in_memory()?; // let conn = DbConnection::open_in_memory()?;
let mut conn = DbConnection::open("test.db")?; let mut conn = DbConnection::open("test.db")?;
let migs = db::create_migrations(); let migs = database::create_migrations();
migs.apply(&mut conn)?; migs.apply(&mut conn)?;
Ok(conn) Ok(conn)

Loading…
Cancel
Save