From 8ccffe74b5f0c4bd94863227bb18a73be00246ad Mon Sep 17 00:00:00 2001 From: Yannick Heinrich Date: Thu, 15 Jul 2021 06:28:20 +0200 Subject: [PATCH] Make SQLx a player within the game This commit adds a compatibility with the popular (I guess) SQLx crate. The implementation is largely inspired by the one for diesel. I added some info inside the README.md too. --- README.md | 30 +++++++++++++++++++++++++++++- bin/compile | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 76820fd..fea57c7 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,9 @@ To deploy your application, run: git push heroku master ``` -### Running Diesel migrations during the release phase +### Running migrations during the release phase + +#### Diesel This will install the diesel CLI at build time and make it available in your dyno. Migrations will run whenever a new version of your app is released. Add the following line to your `RustConfig` @@ -72,6 +74,32 @@ and this one to your `Procfile` release: ./target/release/diesel migration run ``` +To customize the installation options, set the `DIESEL_FLAGS` in the `RustConfig`: + +```sh +DIESEL_FLAGS="--no-default-features --features postgres" +``` + +#### SQLx + +This will install the SQLx CLI at build time and make it available in your dyno. Migrations will run whenever a new version of your app is released. Add the following line to your `RustConfig` + +```sh +RUST_INSTALL_SQLX=1 +``` + +and this one to your `Procfile` + +```Procfile +release: ./target/release/sqlx mig run +``` + +To customize the installation options, set the `SQLX_FLAGS` in the `RustConfig`: + +```sh +SQLX_FLAGS="--no-default-features --features postgres" +``` + [Heroku CLI]: https://devcenter.heroku.com/articles/heroku-command-line ## Specifying which version of Rust to use diff --git a/bin/compile b/bin/compile index 5fc3983..06548e8 100755 --- a/bin/compile +++ b/bin/compile @@ -7,13 +7,6 @@ CACHE_DIR=${2:-} ENV_DIR=${3:-} BP_DIR=$(cd $(dirname ${0:-}); cd ..; pwd) -# Export DATABASE_URL at build time, mostly because Diesel is the best way to -# do SQL in Rust right now, and Diesel will use it to generate code for the -# database schema. -if [ -e "$ENV_DIR/DATABASE_URL" ]; then - export DATABASE_URL="$(cat $ENV_DIR/DATABASE_URL)"; -fi - # Set defaults for our configuration variables. Stable Rust is sufficiently # stable at this point that I think we can just default it. VERSION=stable @@ -32,8 +25,18 @@ BUILD_PATH="" RUST_INSTALL_DIESEL=0 # These flags are passed to `cargo install diesel`, e.g. '--no-default-features --features postgres' DIESEL_FLAGS="" +# Set this to "1" in `RustConfig` to install sqlx at build time and copy it +# into the target directory, next to your app binary. This makes it easy to +# run migrations by adding a release step to your Procfile: +# `release: ./target/release/sqlx migration run` +RUST_INSTALL_SQLX=0 +# These flags are passed to `cargo install sqlx`, e.g. '--no-default-features --features postgres' +SQLX_FLAGS="" + # Default build flags to pass to `cargo build`. RUST_CARGO_BUILD_FLAGS="--release" +# With SQLX, I guedd we would like to use offline build as the default +SQLX_OFFLINE="true" # Load our toolchain configuration, if any was specified. if [ -f "$BUILD_DIR/rust-toolchain" ]; then @@ -42,7 +45,15 @@ fi # Load our configuration variables, if any were specified. if [ -f "$BUILD_DIR/RustConfig" ]; then - . "$BUILD_DIR/RustConfig" + echo "---> Reading RustConfig" + . "$BUILD_DIR/RustConfig" +fi + +# Export DATABASE_URL at build time, mostly because Diesel is the best way to +# do SQL in Rust right now, and Diesel will use it to generate code for the +# database schema. +if [ -e "$ENV_DIR/DATABASE_URL" ]; then + export DATABASE_URL="$(cat $ENV_DIR/DATABASE_URL)"; fi # Standard paranoia. @@ -125,6 +136,9 @@ fi # which describe how this needs to be named. export CARGO_TARGET_DIR="$CACHE_DIR/target" +## In case of SQLX, we need to update the SQLX_OFFLINE +export SQLX_OFFLINE="$SQLX_OFFLINE" + if [ $RUST_SKIP_BUILD -ne 1 ]; then # Build our project (into CARGO_TARGET_DIR so we have caching) and copy it # back to the source tree. In theory, we could probably just copy the @@ -150,3 +164,10 @@ if [ $RUST_INSTALL_DIESEL -eq 1 ]; then cargo install diesel_cli $DIESEL_FLAGS || echo "already installed" cp $(which diesel) target/release/ fi + +# Install sqlx sqo we can use it for migrations +if [ $RUST_INSTALL_SQLX -eq 1 ]; then + echo "-----> Installing sqlx" + cargo install sqlx-cli $SQLX_FLAGS || echo "already installed" + cp $(which sqlx) target/release/ +fi