Skip to content

Commit

Permalink
Automatically set SQLX_OFFLINE when crates are used as dependencies
Browse files Browse the repository at this point in the history
Users of these libraries may not have a database with the right schema.
We don't want to require that so we need to make sure to use the saved
.sqlx queries when building as a package.

The way this is done is by setting SQLX_OFFLINE=true and
SQLX_OFFLINE_DIR=.sqlx in the build.rs scripts of the relevant packages.
This means that the DATABASE_URL environment variable will be ignored
and that `cargo sqlx prepare` will not prepare queries for these
libraries.

To make it work locally, if DURABLE_DEVELOPMENT=1 then we don't do any
overrides. We then set DURABLE_DEVELOPMENT=1 in .cargo/config.toml.
  • Loading branch information
swlynch99 committed Sep 10, 2024
1 parent 78ae63a commit 8331d82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ durable = "run -p durable-cli --bin durable --"

[registries]
iop-systems = { index = "sparse+http://storage.googleapis.com/systemslab-cargo/" }

[env]
DURABLE_DEVELOPMENT = "1"
10 changes: 10 additions & 0 deletions crates/durable-client/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
let development = std::env::var_os("DURABLE_DEVELOPMENT")
.map(|var| var == "1" || var == "true")
.unwrap_or(false);

if !development {
println!("cargo::rustc-env=SQLX_OFFLINE=true");
println!("cargo::rustc-env=SQLX_OFFLINE_DIR=.sqlx");
}
}
29 changes: 26 additions & 3 deletions crates/durable-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ fn _generate(path: &Path, out: &Path, world: &str, opts: &Opts) -> anyhow::Resul
Ok(())
}

fn main() -> anyhow::Result<()> {
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());

fn generate_bindings(out_dir: &Path) -> anyhow::Result<()> {
// Method implementations that should be sync.
//
// In general sync interfaces are more efficient so it is better to make things
Expand Down Expand Up @@ -79,6 +77,10 @@ fn main() -> anyhow::Result<()> {
let output = out_dir.join("bindings.rs");
generate("wit", output, "durable:core/imports", &opts)?;

Ok(())
}

fn generate_migrations(out_dir: &Path) -> anyhow::Result<()> {
let migrator = Migrator::from_dir("migrations")?;
let embed = migrator.embed(&EmbedOptions::default());
let output = out_dir.join("migrations.rs");
Expand All @@ -88,3 +90,24 @@ fn main() -> anyhow::Result<()> {

Ok(())
}

fn set_sqlx_offline() {
let development = std::env::var_os("DURABLE_DEVELOPMENT")
.map(|var| var == "1" || var == "true")
.unwrap_or(false);

if !development {
println!("cargo::rustc-env=SQLX_OFFLINE=true");
println!("cargo::rustc-env=SQLX_OFFLINE_DIR=.sqlx");
}
}

fn main() -> anyhow::Result<()> {
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());

set_sqlx_offline();
generate_bindings(&out_dir).context("failed to generate wasmtime wit bindings")?;
generate_migrations(&out_dir).context("failed to generate database migrations")?;

Ok(())
}

0 comments on commit 8331d82

Please sign in to comment.