embed migrations, create migration tool, make nix build work

This commit is contained in:
Cadey Ratio 2020-11-17 14:05:59 -05:00
parent 80076a2b8f
commit b0c65d5b1d
5 changed files with 104 additions and 20 deletions

32
backend/Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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<SqliteConnection> {
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(())
}

View File

@ -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 ];
}

View File

@ -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";