Skip to content

Commit 8ccffe7

Browse files
committed
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.
1 parent cfa0f06 commit 8ccffe7

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ To deploy your application, run:
5858
git push heroku master
5959
```
6060

61-
### Running Diesel migrations during the release phase
61+
### Running migrations during the release phase
62+
63+
#### Diesel
6264

6365
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`
6466

@@ -72,6 +74,32 @@ and this one to your `Procfile`
7274
release: ./target/release/diesel migration run
7375
```
7476

77+
To customize the installation options, set the `DIESEL_FLAGS` in the `RustConfig`:
78+
79+
```sh
80+
DIESEL_FLAGS="--no-default-features --features postgres"
81+
```
82+
83+
#### SQLx
84+
85+
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`
86+
87+
```sh
88+
RUST_INSTALL_SQLX=1
89+
```
90+
91+
and this one to your `Procfile`
92+
93+
```Procfile
94+
release: ./target/release/sqlx mig run
95+
```
96+
97+
To customize the installation options, set the `SQLX_FLAGS` in the `RustConfig`:
98+
99+
```sh
100+
SQLX_FLAGS="--no-default-features --features postgres"
101+
```
102+
75103
[Heroku CLI]: https://devcenter.heroku.com/articles/heroku-command-line
76104

77105
## Specifying which version of Rust to use

bin/compile

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ CACHE_DIR=${2:-}
77
ENV_DIR=${3:-}
88
BP_DIR=$(cd $(dirname ${0:-}); cd ..; pwd)
99

10-
# Export DATABASE_URL at build time, mostly because Diesel is the best way to
11-
# do SQL in Rust right now, and Diesel will use it to generate code for the
12-
# database schema.
13-
if [ -e "$ENV_DIR/DATABASE_URL" ]; then
14-
export DATABASE_URL="$(cat $ENV_DIR/DATABASE_URL)";
15-
fi
16-
1710
# Set defaults for our configuration variables. Stable Rust is sufficiently
1811
# stable at this point that I think we can just default it.
1912
VERSION=stable
@@ -32,8 +25,18 @@ BUILD_PATH=""
3225
RUST_INSTALL_DIESEL=0
3326
# These flags are passed to `cargo install diesel`, e.g. '--no-default-features --features postgres'
3427
DIESEL_FLAGS=""
28+
# Set this to "1" in `RustConfig` to install sqlx at build time and copy it
29+
# into the target directory, next to your app binary. This makes it easy to
30+
# run migrations by adding a release step to your Procfile:
31+
# `release: ./target/release/sqlx migration run`
32+
RUST_INSTALL_SQLX=0
33+
# These flags are passed to `cargo install sqlx`, e.g. '--no-default-features --features postgres'
34+
SQLX_FLAGS=""
35+
3536
# Default build flags to pass to `cargo build`.
3637
RUST_CARGO_BUILD_FLAGS="--release"
38+
# With SQLX, I guedd we would like to use offline build as the default
39+
SQLX_OFFLINE="true"
3740

3841
# Load our toolchain configuration, if any was specified.
3942
if [ -f "$BUILD_DIR/rust-toolchain" ]; then
@@ -42,7 +45,15 @@ fi
4245

4346
# Load our configuration variables, if any were specified.
4447
if [ -f "$BUILD_DIR/RustConfig" ]; then
45-
. "$BUILD_DIR/RustConfig"
48+
echo "---> Reading RustConfig"
49+
. "$BUILD_DIR/RustConfig"
50+
fi
51+
52+
# Export DATABASE_URL at build time, mostly because Diesel is the best way to
53+
# do SQL in Rust right now, and Diesel will use it to generate code for the
54+
# database schema.
55+
if [ -e "$ENV_DIR/DATABASE_URL" ]; then
56+
export DATABASE_URL="$(cat $ENV_DIR/DATABASE_URL)";
4657
fi
4758

4859
# Standard paranoia.
@@ -125,6 +136,9 @@ fi
125136
# which describe how this needs to be named.
126137
export CARGO_TARGET_DIR="$CACHE_DIR/target"
127138

139+
## In case of SQLX, we need to update the SQLX_OFFLINE
140+
export SQLX_OFFLINE="$SQLX_OFFLINE"
141+
128142
if [ $RUST_SKIP_BUILD -ne 1 ]; then
129143
# Build our project (into CARGO_TARGET_DIR so we have caching) and copy it
130144
# back to the source tree. In theory, we could probably just copy the
@@ -150,3 +164,10 @@ if [ $RUST_INSTALL_DIESEL -eq 1 ]; then
150164
cargo install diesel_cli $DIESEL_FLAGS || echo "already installed"
151165
cp $(which diesel) target/release/
152166
fi
167+
168+
# Install sqlx sqo we can use it for migrations
169+
if [ $RUST_INSTALL_SQLX -eq 1 ]; then
170+
echo "-----> Installing sqlx"
171+
cargo install sqlx-cli $SQLX_FLAGS || echo "already installed"
172+
cp $(which sqlx) target/release/
173+
fi

0 commit comments

Comments
 (0)