Skip to content

Commit

Permalink
Fix sqlx in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dynco-nym committed Sep 20, 2024
1 parent 08471e0 commit 0da76a9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nym-data-observatory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ nym-task = { path = "../common/task" }
nym-node-requests = { path = "../nym-node/nym-node-requests", features = ["openapi"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sqlx = { workspace = true, features = ["runtime-tokio-rustls", "postgres"] }
sqlx = { workspace = true, features = ["runtime-tokio-rustls", "postgres", "offline"] }
tokio = { workspace = true, features = ["process"] }
tokio-util = { workspace = true }
tracing = { workspace = true }
Expand Down
29 changes: 21 additions & 8 deletions nym-data-observatory/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ use std::io::Write;
use std::{collections::HashMap, fs::File};

const POSTGRES_USER: &str = "nym";
const POSTGRES_PASSWORD: &str = "password123$";
const POSTGRES_PASSWORD: &str = "password123";
const POSTGRES_DB: &str = "data_obs_db";

/// If you need to re-run migrations or reset the db, just run
/// cargo clean -p nym-node-status-api
#[tokio::main]
async fn main() -> Result<()> {
if let Ok(value) = std::env::var("CI") {
if value == "true" {
println!("cargo::rustc-env=SQLX_OFFLINE=true");
}
} else {
let db_url = export_db_variables()?;
run_migrations(&db_url).await?;
}

rerun_if_changed();
Ok(())
}

fn export_db_variables() -> Result<String> {
let mut map = HashMap::new();
map.insert("POSTGRES_USER", POSTGRES_USER);
map.insert("POSTGRES_PASSWORD", POSTGRES_PASSWORD);
Expand All @@ -23,18 +37,17 @@ async fn main() -> Result<()> {

let mut file = File::create(".env")?;
for (var, value) in map.iter() {
unsafe {
std::env::set_var(var, value);
}
println!("cargo::rustc-env={}={}", var, value);
writeln!(file, "{}={}", var, value).expect("Failed to write to dotenv file");
}

let mut conn = PgConnection::connect(&db_url).await?;
sqlx::migrate!("./migrations").run(&mut conn).await?;
Ok(db_url)
}

println!("cargo::rustc-env=DATABASE_URL={}", &db_url);
async fn run_migrations(db_url: &str) -> Result<()> {
let mut conn = PgConnection::connect(db_url).await?;
sqlx::migrate!("./migrations").run(&mut conn).await?;

rerun_if_changed();
Ok(())
}

Expand Down
2 changes: 0 additions & 2 deletions nym-data-observatory/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
postgres:
image: postgres:13
Expand Down
79 changes: 79 additions & 0 deletions nym-data-observatory/sqlx-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"db": "PostgreSQL",
"249faa11b88b749f50342bb5c9cc41d20896db543eed74a6f320c041bcbb723d": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Varchar",
"Text",
"Int4"
]
}
},
"query": "INSERT INTO responses\n (joke_id, joke, date_created)\n VALUES\n ($1, $2, $3)\n ON CONFLICT(joke_id) DO UPDATE SET\n joke=excluded.joke,\n date_created=excluded.date_created;"
},
"aff7fbd06728004d2f2226d20c32f1482df00de2dc1d2b4debbb2e12553d997b": {
"describe": {
"columns": [
{
"name": "joke_id",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "joke",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "date_created",
"ordinal": 2,
"type_info": "Int4"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "SELECT joke_id, joke, date_created FROM responses WHERE joke_id = $1"
},
"e53f479f8cead3dc8aa1875e5d450ad69686cf6a109e37d6c3f0623c3e9f91d0": {
"describe": {
"columns": [
{
"name": "joke_id",
"ordinal": 0,
"type_info": "Varchar"
},
{
"name": "joke",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "date_created",
"ordinal": 2,
"type_info": "Int4"
}
],
"nullable": [
false,
false,
false
],
"parameters": {
"Left": []
}
},
"query": "SELECT joke_id, joke, date_created FROM responses"
}
}

0 comments on commit 0da76a9

Please sign in to comment.