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", "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]] [[package]]
name = "digest" name = "digest"
version = "0.8.1" version = "0.8.1"
@ -1044,6 +1054,7 @@ dependencies = [
"chrono", "chrono",
"color-eyre", "color-eyre",
"diesel", "diesel",
"diesel_migrations",
"futures-io", "futures-io",
"hex", "hex",
"log 0.4.11", "log 0.4.11",
@ -1068,6 +1079,27 @@ dependencies = [
"url 2.1.1", "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]] [[package]]
name = "mime" name = "mime"
version = "0.2.6" version = "0.2.6"

View File

@ -10,6 +10,7 @@ edition = "2018"
askama_rocket = "0.10" askama_rocket = "0.10"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
color-eyre = "0.5" color-eyre = "0.5"
diesel_migrations = "1"
futures-io = "0.3" futures-io = "0.3"
hex = "0.4" hex = "0.4"
log = "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 { } }: { sources ? import ./nix/sources.nix, pkgs ? import sources.nixpkgs { } }:
with pkgs; with pkgs;
let let
backend = pkgs.callPackage ./backend { inherit sources pkgs; }; backend = callPackage ./backend { inherit sources pkgs; };
frontend = pkgs.callPackage ./sina { inherit sources pkgs; }; frontend = callPackage ./sina { inherit sources pkgs; };
gruvbox = pkgs.callPackage sources.gruvbox-css { }; gruvbox = callPackage sources.gruvbox-css { };
in stdenv.mkDerivation {
pname = "mi";
version = "${backend.version}";
phases = "installPhase";
installPhase = '' composite = stdenv.mkDerivation {
# service binary pname = "mi";
mkdir -p $out/bin version = "${backend.version}";
phases = "installPhase";
for file in ${backend}/bin/*; do installPhase = ''
ln -s ${backend}/bin/$(${coreutils}/bin/basename $file) $out/bin/$(${coreutils}/bin/basename $file) # service binary
done mkdir -p $out/bin
# frontend JS for file in ${backend}/bin/*; do
mkdir -p $out/public/js ln -s ${backend}/bin/$(basename $file) $out/bin/$(basename $file)
ln -s ${frontend}/Main.js $out/public/js/elm.js done
# static files # static files
cp -vrf ${./static}/* $out/public mkdir -p $out/public/
cp -vrf ${./static}/* $out/public
# migrations # frontend JS
ln -s ${./backend/migrations} $out/migrations 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"; GRUVBOX_CSS = "${gruvbox}/gruvbox.css";
DATABASE_URL = "./mi.db"; DATABASE_URL = "./mi.db";
MIGRATION_PATH = "./migrations";
ROCKET_DATABASES = ''{ main_data = { url = "${DATABASE_URL}" } }''; ROCKET_DATABASES = ''{ main_data = { url = "${DATABASE_URL}" } }'';
RUST_LOG = "info"; RUST_LOG = "info";