From b0c65d5b1d24bb5fbbc3d7125f70c489bc0560b6 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Tue, 17 Nov 2020 14:05:59 -0500 Subject: [PATCH] embed migrations, create migration tool, make nix build work --- backend/Cargo.lock | 32 +++++++++++++++++ backend/Cargo.toml | 1 + backend/src/bin/migrate_database.rs | 34 ++++++++++++++++++ default.nix | 56 ++++++++++++++++++----------- shell.nix | 1 + 5 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 backend/src/bin/migrate_database.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 076259a..efb5510 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -485,6 +485,16 @@ dependencies = [ "syn 1.0.40", ] +[[package]] +name = "diesel_migrations" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf3cde8413353dc7f5d72fa8ce0b99a560a359d2c5ef1e5817ca731cd9008f4c" +dependencies = [ + "migrations_internals", + "migrations_macros", +] + [[package]] name = "digest" version = "0.8.1" @@ -1044,6 +1054,7 @@ dependencies = [ "chrono", "color-eyre", "diesel", + "diesel_migrations", "futures-io", "hex", "log 0.4.11", @@ -1068,6 +1079,27 @@ dependencies = [ "url 2.1.1", ] +[[package]] +name = "migrations_internals" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b4fc84e4af020b837029e017966f86a1c2d5e83e64b589963d5047525995860" +dependencies = [ + "diesel", +] + +[[package]] +name = "migrations_macros" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9753f12909fd8d923f75ae5c3258cae1ed3c8ec052e1b38c93c21a6d157f789c" +dependencies = [ + "migrations_internals", + "proc-macro2 1.0.21", + "quote 1.0.7", + "syn 1.0.40", +] + [[package]] name = "mime" version = "0.2.6" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 3a5810e..ede9454 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" askama_rocket = "0.10" chrono = { version = "0.4", features = ["serde"] } color-eyre = "0.5" +diesel_migrations = "1" futures-io = "0.3" hex = "0.4" log = "0.4" diff --git a/backend/src/bin/migrate_database.rs b/backend/src/bin/migrate_database.rs new file mode 100644 index 0000000..9fc8f80 --- /dev/null +++ b/backend/src/bin/migrate_database.rs @@ -0,0 +1,34 @@ +#[macro_use] +extern crate diesel_migrations; +#[macro_use] +extern crate tracing; + +use color_eyre::eyre::{eyre, Result}; +use diesel::prelude::*; +use mi::APPLICATION_NAME; +use std::env; + +diesel_migrations::embed_migrations!("./migrations"); + +pub fn establish_connection() -> Result { + let database_url = env::var("DATABASE_URL").unwrap_or("./mi.db".to_string()); + SqliteConnection::establish(&database_url) + .map_err(|why| eyre!("can't connect to {}: {}", database_url, why)) +} + +fn main() -> Result<()> { + color_eyre::install()?; + tracing_subscriber::fmt::init(); + + info!("{} migrator starting up", APPLICATION_NAME); + + info!("running migrations"); + let connection = establish_connection()?; + embedded_migrations::run_with_output(&connection, &mut std::io::stdout()).map_err(|why| { + error!("migration error: {}", why); + why + })?; + info!("migrations succeeded"); + + Ok(()) +} diff --git a/default.nix b/default.nix index 906c404..6628f4b 100644 --- a/default.nix +++ b/default.nix @@ -1,30 +1,46 @@ { sources ? import ./nix/sources.nix, pkgs ? import sources.nixpkgs { } }: with pkgs; let - backend = pkgs.callPackage ./backend { inherit sources pkgs; }; - frontend = pkgs.callPackage ./sina { inherit sources pkgs; }; - gruvbox = pkgs.callPackage sources.gruvbox-css { }; -in stdenv.mkDerivation { - pname = "mi"; - version = "${backend.version}"; - phases = "installPhase"; + backend = callPackage ./backend { inherit sources pkgs; }; + frontend = callPackage ./sina { inherit sources pkgs; }; + gruvbox = callPackage sources.gruvbox-css { }; - installPhase = '' - # service binary - mkdir -p $out/bin + composite = stdenv.mkDerivation { + pname = "mi"; + version = "${backend.version}"; + phases = "installPhase"; - for file in ${backend}/bin/*; do - ln -s ${backend}/bin/$(${coreutils}/bin/basename $file) $out/bin/$(${coreutils}/bin/basename $file) - done + installPhase = '' + # service binary + mkdir -p $out/bin - # frontend JS - mkdir -p $out/public/js - ln -s ${frontend}/Main.js $out/public/js/elm.js + for file in ${backend}/bin/*; do + ln -s ${backend}/bin/$(basename $file) $out/bin/$(basename $file) + done - # static files - cp -vrf ${./static}/* $out/public + # static files + mkdir -p $out/public/ + cp -vrf ${./static}/* $out/public - # migrations - ln -s ${./backend/migrations} $out/migrations + # frontend JS + rm $out/public/elm.js + ln -s ${frontend}/Main.min.js $out/public/elm.js + ''; + }; + + wrapper = writeScriptBin "mi-backend" '' + #!${pkgs.stdenv.shell} + set -e + set -x + + export RUST_LOG=info + export DATABASE_URL=./mi.db + export ROCKET_DATABASES='{ main_data = { url = "./mi.db" } }'; + ${composite}/bin/migrate_database + export ROCKET_ASSET_PATH=${composite}/public + exec ${composite}/bin/mi ''; +in symlinkJoin { + name = "mi"; + paths = [ wrapper composite ]; } diff --git a/shell.nix b/shell.nix index ff11663..881b63b 100644 --- a/shell.nix +++ b/shell.nix @@ -34,6 +34,7 @@ in pkgs.mkShell rec { GRUVBOX_CSS = "${gruvbox}/gruvbox.css"; DATABASE_URL = "./mi.db"; + MIGRATION_PATH = "./migrations"; ROCKET_DATABASES = ''{ main_data = { url = "${DATABASE_URL}" } }''; RUST_LOG = "info";