From 2e17452315a48e18b3cf6b5b310ac3f75813d1e0 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 20 Sep 2020 13:15:55 -0600 Subject: [PATCH] Move migrations to files --- src/db.rs | 33 ++++++++------------- src/migrations/0001-table_sections-down.sql | 1 + src/migrations/0001-table_sections-up.sql | 5 ++++ src/migrations/0002-section_rows-down.sql | 1 + src/migrations/0002-section_rows-up.sql | 7 +++++ 5 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 src/migrations/0001-table_sections-down.sql create mode 100644 src/migrations/0001-table_sections-up.sql create mode 100644 src/migrations/0002-section_rows-down.sql create mode 100644 src/migrations/0002-section_rows-up.sql diff --git a/src/db.rs b/src/db.rs index ea91e6c..d55099c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,27 +1,18 @@ use crate::migrations::{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")), + ) + }; +} + pub fn create_migrations() -> Migrations { let mut migs = Migrations::new(); - migs.add(SimpleMigration::new_box( - 1, - "CREATE TABLE sections ( - id INTEGER PRIMARY KEY, - name TEXT NOT NULL, - interface_id INTEGER NOT NULL - );", - "DROP TABLE sections;", - )); - migs.add(SimpleMigration::new_box( - 2, - "INSERT INTO sections (id, name, interface_id) - VALUES - (1, 'Front Yard Middle', 0), - (2, 'Front Yard Left', 1), - (3, 'Front Yard Right', 2), - (4, 'Back Yard Middle', 3), - (5, 'Back Yard Sauna', 4), - (6, 'Garden', 5);", - "DELETE FROM sections;", - )); + migs.add(include_file_migration!(1, "0001-table_sections")); + migs.add(include_file_migration!(2, "0002-section_rows")); migs } diff --git a/src/migrations/0001-table_sections-down.sql b/src/migrations/0001-table_sections-down.sql new file mode 100644 index 0000000..44a8aa1 --- /dev/null +++ b/src/migrations/0001-table_sections-down.sql @@ -0,0 +1 @@ +DROP TABLE sections; \ No newline at end of file diff --git a/src/migrations/0001-table_sections-up.sql b/src/migrations/0001-table_sections-up.sql new file mode 100644 index 0000000..720a9f7 --- /dev/null +++ b/src/migrations/0001-table_sections-up.sql @@ -0,0 +1,5 @@ +CREATE TABLE sections ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + interface_id INTEGER NOT NULL +); \ No newline at end of file diff --git a/src/migrations/0002-section_rows-down.sql b/src/migrations/0002-section_rows-down.sql new file mode 100644 index 0000000..079f815 --- /dev/null +++ b/src/migrations/0002-section_rows-down.sql @@ -0,0 +1 @@ +DELETE FROM sections; \ No newline at end of file diff --git a/src/migrations/0002-section_rows-up.sql b/src/migrations/0002-section_rows-up.sql new file mode 100644 index 0000000..2335ab7 --- /dev/null +++ b/src/migrations/0002-section_rows-up.sql @@ -0,0 +1,7 @@ +INSERT INTO sections (id, name, interface_id) +VALUES (1, 'Front Yard Middle', 0), + (2, 'Front Yard Left', 1), + (3, 'Front Yard Right', 2), + (4, 'Back Yard Middle', 3), + (5, 'Back Yard Sauna', 4), + (6, 'Garden', 5); \ No newline at end of file