From 19b77083bbe8b78f86de0982a718a972612c3479 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 29 Sep 2021 21:16:26 +0100 Subject: [PATCH] Fix Dockerfile on x86_64 and run migrations at boot --- Cargo.lock | 34 ++++++++++++++++++++++++++++++++++ Dockerfile | 12 ++++-------- chartered-db/Cargo.toml | 2 ++ chartered-db/src/lib.rs | 14 +++++++++++++- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46a4b8e..f9c73bf 100644 --- a/Cargo.lock +++ a/Cargo.lock @@ -200,11 +200,13 @@ "chrono", "diesel", "diesel_logger", + "diesel_migrations", "displaydoc", "dotenv", "hex", "http", "itertools", + "libsqlite3-sys", "option_set", "rand", "serde", @@ -418,6 +420,16 @@ dependencies = [ "diesel", "log", +] + +[[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]] @@ -835,6 +847,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290b64917f8b0cb885d9de0f9959fe1f775d7fa12f1da2db9001c1c8ab60f89d" dependencies = [ + "cc", "pkg-config", "vcpkg", ] @@ -874,6 +887,27 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[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", + "quote", + "syn", +] [[package]] name = "mime" diff --git a/Dockerfile b/Dockerfile index 7e10eaa..b86b97d 100644 --- a/Dockerfile +++ a/Dockerfile @@ -1,21 +1,17 @@ FROM rust:alpine AS builder RUN apk add --update gcc g++ build-base alpine-sdk sqlite-dev WORKDIR /app COPY . /app RUN cargo build --release -FROM alpine AS chartered-git -RUN apk add --update sqlite sqlite-dev && \ - ln -s /lib/ld-musl-$(uname -m).so.1 /lib/ld-linux-$(uname -m).so.1 +FROM scratch AS chartered-git WORKDIR /app COPY --from=builder /app/target/release/chartered-git /app/chartered-git -ENV RUST_LOG=info +ENV RUST_LOG=debug ENTRYPOINT ["/app/chartered-git"] -FROM alpine AS chartered-web -RUN apk add --update sqlite sqlite-dev && \ - ln -s /lib/ld-musl-$(uname -m).so.1 /lib/ld-linux-$(uname -m).so.1 +FROM scratch AS chartered-web WORKDIR /app COPY --from=builder /app/target/release/chartered-web /app/chartered-web -ENV RUST_LOG=info +ENV RUST_LOG=debug ENTRYPOINT ["/app/chartered-web"] diff --git a/chartered-db/Cargo.toml b/chartered-db/Cargo.toml index 0c3880b..e8ce0c8 100644 --- a/chartered-db/Cargo.toml +++ a/chartered-db/Cargo.toml @@ -14,10 +14,12 @@ chrono = "0.4" diesel = { version = "1", features = ["sqlite", "r2d2", "chrono"] } diesel_logger = "0.1" +diesel_migrations = "1.4" displaydoc = "0.2" hex = "0.4" http = "0.2" itertools = "0.10" +libsqlite3-sys = { version = "*", features = ["bundled"] } # https://github.com/rusqlite/rusqlite/issues/914 option_set = "0.1" rand = "0.8" serde = { version = "1", features = ["derive"] } diff --git a/chartered-db/src/lib.rs b/chartered-db/src/lib.rs index b18f8af..c35c0fc 100644 --- a/chartered-db/src/lib.rs +++ a/chartered-db/src/lib.rs @@ -38,8 +38,8 @@ pub mod users; pub mod uuid; -#[macro_use] -extern crate diesel; +#[macro_use] extern crate diesel; +#[macro_use] extern crate diesel_migrations; use diesel::{ expression::{grouped::Grouped, AsExpression, Expression}, @@ -54,14 +54,22 @@ pub type ConnectionPool = Arc>>>; pub type Result = std::result::Result; +embed_migrations!(); + pub fn init() -> Result { - Ok(Arc::new(Pool::new(ConnectionManager::new("chartered.db"))?)) + let pool = Pool::new(ConnectionManager::new("chartered.db"))?; + + embedded_migrations::run_with_output(&pool.get()?, &mut std::io::stdout())?; + + Ok(Arc::new(pool)) } #[derive(Error, Display, Debug)] pub enum Error { /// Failed to initialise to database connection pool Connection(#[from] diesel::r2d2::PoolError), + /// Failed to run migrations to bring database schema up-to-date: {0} + MigrationError(#[from] diesel_migrations::RunMigrationsError), /// {0} Query(#[from] diesel::result::Error), /// Failed to complete query task -- rgit 0.1.3