Skip to content

Commit

Permalink
Build script for sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
dynco-nym committed Sep 12, 2024
1 parent de4b024 commit 1340ab2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ jobs:
command: fmt
args: --all -- --check

# due to sqlx dependency, prepare environment before building this package
- name: Pre-build steps for `node-status-api`
run: |
cargo install sqlx-cli
cd nym-node-status-api
./launch_node_status_api.sh -i
- name: Build `node-status-api`
uses: actions-rs/cargo@v1
with:
command: build
args: --package nym-node-status-api

- name: Build all binaries
uses: actions-rs/cargo@v1
with:
Expand Down
5 changes: 5 additions & 0 deletions nym-node-status-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[build-dependencies]
anyhow = { workspace = true }
tokio = { workspace = true, features = ["macros" ] }
sqlx = { workspace = true, features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate"] }
33 changes: 33 additions & 0 deletions nym-node-status-api/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::{anyhow, Result};
use sqlx::{Connection, SqliteConnection};

const SQLITE_DB_FILENAME: &str = "nym-node-status-api.sqlite";

#[tokio::main]
async fn main() -> Result<()> {
let out_dir = read_env_var("OUT_DIR")?;
let database_path = format!("sqlite://{}/{}?mode=rwc", out_dir, SQLITE_DB_FILENAME);

let mut conn = SqliteConnection::connect(&database_path).await?;
sqlx::migrate!("./migrations").run(&mut conn).await?;

#[cfg(target_family = "unix")]
println!("cargo::rustc-env=DATABASE_URL=sqlite://{}", &database_path);

#[cfg(target_family = "windows")]
// for some strange reason we need to add a leading `/` to the windows path even though it's
// not a valid windows path... but hey, it works...
println!("cargo::rustc-env=DATABASE_URL=sqlite:///{}", &database_path);

rerun_if_changed();
Ok(())
}

fn read_env_var(var: &str) -> Result<String> {
std::env::var(var).map_err(|_| anyhow!("You need to set {} env var", var))
}

fn rerun_if_changed() {
println!("cargo::rerun-if-changed=migrations");
println!("cargo::rerun-if-changed=src/db/queries");
}

0 comments on commit 1340ab2

Please sign in to comment.