Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.83k stars 108 forks source link

Proper way to cache "prisma generate" command #452

Open stefanodecillis opened 5 months ago

stefanodecillis commented 5 months ago

Trying out prisma-client-rust and I'm currently building a dockerfile.

I'm using Cargo chef to provide a cache layer to avoid compiling dependencies each time and I'm wondering if something similar could be achieved for the command cargo prisma build.

This command should generate the same file if the schema doesn't change (I guess) but it looks like docker is running this command anyway

Does someone encounter this problem and found a solution/workaround?

Brendonovich commented 5 months ago

I'm not entirely sure about docker but here's out setup for github actions

mgrog commented 4 months ago

Here's an example Dockerfile that does it, the trick was to create a empty project with the workspace entry in the cargo.toml:

# Leveraging the pre-built Docker images with 
# cargo-chef and the Rust toolchain
FROM lukemathwalker/cargo-chef:latest-rust-1.77.0 AS chef
WORKDIR /app

FROM chef AS prisma-cli
# Build prisma cli
COPY ./prisma-cli ./prisma-cli
RUN cargo init
RUN echo "[workspace]\nmembers = [\"prisma-cli\"]" > Cargo.toml
RUN cargo build --manifest-path prisma-cli/Cargo.toml

FROM prisma-cli as prisma-client-1
COPY prisma/schema.prisma prisma/schema.prisma
COPY .cargo .cargo
RUN cargo prisma generate --schema prisma/schema.prisma

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
COPY --from=prisma-client-1 /app/src/prisma.rs src/prisma.rs
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .
RUN cargo build --release

FROM gcr.io/distroless/cc as runtime
COPY --from=builder /app/target/release/app /usr/local/bin/app
STOPSIGNAL SIGINT
ENTRYPOINT ["./usr/local/bin/app"]