In Rust proc macro panicked using diesel_migrations

65 views Asked by At

I wanna create a docker image using

docker build --tag docker-rust-image-test .

but it doesn't work. The error is:

0.628 error: proc macro panicked
0.628  --> src/config/db.rs:6:44
0.628   |
0.628 6 | pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
0.628   |                                            ^^^^^^^^^^^^^^^^^^^
0.628   |
0.628   = help: message: Failed to receive migrations dir from None

Here is my db.rs file:

use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;

use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();

pub type DbPool = Pool<ConnectionManager<PgConnection>>;

pub fn establish_connection_pool(database_url: &str) -> DbPool {
    let manager = ConnectionManager::<PgConnection>::new(database_url);    
    Pool::builder()
        .build(manager)
        .expect("Failed to create database pool")
}

pub fn run_migration(connection: &mut impl MigrationHarness<diesel::pg::Pg>) {
    match connection.run_pending_migrations(MIGRATIONS) {
        Ok(_) => {
            println!("Migrations successfully completed");
        }
        Err(e) => {
            panic!("error running pending migrations {}", e)
        }
    };
}

Here is my Dockerfile:

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG RUST_VERSION=1.75.0
ARG APP_NAME=hoa-backend

################################################################################
# xx is a helper for cross-compilation.
# See https://github.com/tonistiigi/xx/ for more information.
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.3.0 AS xx

FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine AS build
ARG APP_NAME
WORKDIR /app

# Copy cross-compilation utilities from the xx stage.
COPY --from=xx / /

# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev git file

# This is the architecture you’re building for, which is passed in by the builder.
# Placing it here allows the previous steps to be cached across architectures.
ARG TARGETPLATFORM

# Install cross-compilation build dependencies.
RUN xx-apk add --no-cache musl-dev gcc

RUN --mount=type=bind,source=src,target=src \
    --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
    --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
    --mount=type=cache,target=/app/target/,id=rust-cache-${APP_NAME}-${TARGETPLATFORM} \
    --mount=type=cache,target=/usr/local/cargo/git/db \
    --mount=type=cache,target=/usr/local/cargo/registry/ \
xx-cargo build --locked --release --target-dir ./target && \
cp ./target/$(xx-cargo --print-target-triple)/release/$APP_NAME /bin/server && \
xx-verify /bin/server

FROM alpine:3.18 AS final

ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser
USER appuser

COPY --from=build /bin/server /bin/

EXPOSE 8080

CMD ["/bin/server"]

this file was generated from docker init command

  1. The chatgpt says to add "embedded_migration" in Cargo.toml file like this:
diesel = { version = "2.1.4", features = ["postgres", "embedded_migration", "r2d2", "chrono"] }
diesel_migrations = "2.1.0"

but diesel doesn't have this feature

  1. It is fine to run cargo run and cargo build
  2. Also tried embed_migrations!("../../migrations");
1

There are 1 answers

0
trust_nickol On

In the provided Dockerfile the migrations-folder is not mapped or copied into the container which builds the software.

You have to add one line for the migrations folder too:

     --mount=type=bind,source=migrations,target=migrations \