Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

chore(nix): add database url to nix dev shell (#4664)

Will Hutchinson04494c25054e
diff --git a/.github/workflows/code_check_cloud_storage.yml b/.github/workflows/code_check_cloud_storage.yml
index 7714355b1f..59eb7af162 100644
--- a/.github/workflows/code_check_cloud_storage.yml
+++ b/.github/workflows/code_check_cloud_storage.yml
@@ -115,6 +115,7 @@ jobs:
env:
RUSTFLAGS: '-Dwarnings -Dclippy::disallowed_methods -C link-arg=-fuse-ld=mold'
RUSTDOCFLAGS: '-Dwarnings'
+ SQLX_OFFLINE: 'true'
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
diff --git a/nix/cloud-storage.nix b/nix/cloud-storage.nix
index 52f6a4e957..d4cc32340c 100644
--- a/nix/cloud-storage.nix
+++ b/nix/cloud-storage.nix
@@ -834,6 +834,10 @@
CXXFLAGS_aarch64_unknown_linux_gnu = "-I${pkgs.curl.dev}/include";
CFLAGS_x86_64_unknown_linux_gnu = "-I${pkgs.curl.dev}/include";
CXXFLAGS_x86_64_unknown_linux_gnu = "-I${pkgs.curl.dev}/include";
+ # Default for local SQLx/cargo workflows so individual crates do
+ # not need their own .env files. The run-dev env resolver ignores
+ # this localhost default so Doppler's dev DATABASE_URL wins.
+ DATABASE_URL = "postgres://user:password@localhost:5432/macrodb";
}
// pkgs.lib.optionalAttrs isLinux {
LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath libraries}";
diff --git a/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer.rs b/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer.rs
index 6f14daf36d..db17676e14 100644
--- a/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer.rs
+++ b/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer.rs
@@ -22,6 +22,9 @@ use anyhow::{Context, Result};
use super::instance::Instance;
use super::{Mode, local_env};
+#[cfg(test)]
+mod test;
+
/// The fully resolved environment for a run.
pub struct ResolvedEnv {
pub merged: BTreeMap<String, String>,
@@ -67,8 +70,12 @@ pub fn resolve(
// Process env — override only keys already present, so a developer can
// `FOO=bar just run_local` without dumping their whole environment in.
+ // For run-dev, do not let the Nix shell's localhost SQLx default replace
+ // Doppler's shared-dev database URL.
for key in env.keys().cloned().collect::<Vec<_>>() {
- if let Ok(v) = std::env::var(&key) {
+ if let Ok(v) = std::env::var(&key)
+ && should_overlay_process_env(mode, &key, &v)
+ {
env.insert(key, v);
}
}
@@ -94,6 +101,14 @@ pub fn resolve(
})
}
+fn should_overlay_process_env(mode: Mode, key: &str, value: &str) -> bool {
+ !(mode == Mode::Dev && key == "DATABASE_URL" && is_local_database_url(value))
+}
+
+pub(super) fn is_local_database_url(value: &str) -> bool {
+ value.contains("@postgres:") || value.contains("localhost")
+}
+
/// Dev-only overrides: pin the environment + project name, and strip any local
/// infra endpoints that leaked in (from `--env-file`/process env) so services
/// use real dev resources. Local's identity/ports/secrets are owned entirely by
diff --git a/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer/test.rs b/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer/test.rs
new file mode 100644
index 0000000000..4c82f2f77f
--- /dev/null
+++ b/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/env_layer/test.rs
@@ -0,0 +1,33 @@
+use super::*;
+
+#[test]
+fn run_dev_keeps_doppler_database_url_when_shell_has_local_default() {
+ assert!(!should_overlay_process_env(
+ Mode::Dev,
+ "DATABASE_URL",
+ "postgres://user:password@localhost:5432/macrodb"
+ ));
+ assert!(!should_overlay_process_env(
+ Mode::Dev,
+ "DATABASE_URL",
+ "postgres://user:password@postgres:5432/macrodb"
+ ));
+}
+
+#[test]
+fn run_dev_still_allows_non_local_database_overrides() {
+ assert!(should_overlay_process_env(
+ Mode::Dev,
+ "DATABASE_URL",
+ "postgres://user:[email protected]:5432/macrodb"
+ ));
+}
+
+#[test]
+fn local_mode_keeps_local_database_override() {
+ assert!(should_overlay_process_env(
+ Mode::Local,
+ "DATABASE_URL",
+ "postgres://user:password@localhost:5432/macrodb"
+ ));
+}
diff --git a/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/validate.rs b/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/validate.rs
index e954ff7348..81e72b9afe 100644
--- a/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/validate.rs
+++ b/rust/cloud-storage/tools/xtask/crates/xtask_local/src/local/validate.rs
@@ -180,7 +180,7 @@ pub fn local_env(
}
match env.get("DATABASE_URL") {
None => failures.push("run-dev requires a dev DATABASE_URL (none found)".into()),
- Some(v) if v.contains("@postgres:") || v.contains("localhost") => {
+ Some(v) if env_layer::is_local_database_url(v) => {
failures.push(format!(
"run-dev DATABASE_URL={v} points at local infra; expected a shared-dev host"
));
diff --git a/rust/cloud-storage/tools/xtask/crates/xtask_workflows/src/workflows/code_check_cloud_storage.rs b/rust/cloud-storage/tools/xtask/crates/xtask_workflows/src/workflows/code_check_cloud_storage.rs
index 39aef26acb..ad0f69f162 100644
--- a/rust/cloud-storage/tools/xtask/crates/xtask_workflows/src/workflows/code_check_cloud_storage.rs
+++ b/rust/cloud-storage/tools/xtask/crates/xtask_workflows/src/workflows/code_check_cloud_storage.rs
@@ -64,6 +64,7 @@ fn check() -> Job {
"-Dwarnings -Dclippy::disallowed_methods -C link-arg=-fuse-ld=mold",
))
.add_env(("RUSTDOCFLAGS", "-Dwarnings"))
+ .add_env(("SQLX_OFFLINE", "true"))
.add_step(steps::checkout(false, false))
.add_step(steps::mount_cache_volume())
.add_step(steps::setup_nix())