Skip to content

Make SQLx a player within the game #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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
Expand Down
37 changes: 29 additions & 8 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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