Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env Support for Repositories with Multiple Crates #267

Closed
xyzd0 opened this issue Apr 18, 2020 · 4 comments · Fixed by #270
Closed

.env Support for Repositories with Multiple Crates #267

xyzd0 opened this issue Apr 18, 2020 · 4 comments · Fixed by #270
Labels
enhancement New feature or request

Comments

@xyzd0
Copy link
Contributor

xyzd0 commented Apr 18, 2020

SQLx version: 0.3.4

Current Behaviour

If I have a repository with multiple crates, and a root Cargo.toml file defining each crate as a workspace, SQLx won't pick up .env files for specific crates, it will only pick this up from the project root directory.

This is not ideal as if for example, multiple microservices were being developed in a "monorepo", each one using its own database and .env file, SQLx will only attempt to use an .env file defined at the project root directory. For example,

.
├── .env  # Only this .env file is read and is used across all crates
├── Cargo.toml
├── README.md
├── target
│   ├── debug
├── microservice1
│   ├── server
│   │   ├── Cargo.toml
│   │   └── src
│   │       ├── file.rs
│   └── tests
│       ├── Cargo.toml
│       └── src
│           └── main.rs
├── microservice2
│   ├── server
│   │   ├── Cargo.toml
│   │   └── src
│   │       ├── file.rs
│   └── tests
│       ├── Cargo.toml
│       └── src
│           └── main.rs

Preferred Behaviour

It would be better if SQLx was able to pick up .env files from each crate in the project, using the crate specific DATABASE_URL for compile-time query validation.

For example,

.
├── Cargo.toml
├── README.md
├── target
│   ├── debug
├── microservice1
│   ├── server
│   │   ├── .env  # DATABASE_URL=postgres://microservice1_db
│   │   ├── Cargo.toml
│   │   └── src
│   │       ├── file.rs
│   └── tests
│       ├── Cargo.toml
│       └── src
│           └── main.rs
├── microservice2
│   ├── server
│   │   ├── .env  # DATABASE_URL=postgres://microservice2_db
│   │   ├── Cargo.toml
│   │   └── src
│   │       ├── file.rs
│   └── tests
│       ├── Cargo.toml
│       └── src
│           └── main.rs
@abonander
Copy link
Collaborator

Yeah, currently we just rely on the implicit behavior of dotenv to load the .env file from the current working directory.

This is easy to fix if we load relative to CARGO_MANIFEST_DIR.

However, I can see this being perhaps a breaking change for some people. We should probably try CARGO_MANIFEST_DIR first and then fall back to the current working directory.

@abonander
Copy link
Collaborator

Wait... version 0.15.0? We're on 0.3.4. Did you give the version number for a different crate by mistake?

@xyzd0
Copy link
Contributor Author

xyzd0 commented Apr 18, 2020

Hah yep apologies, that was a different crate! Updated the issue body.

xyzd0 pushed a commit to xyzd0/sqlx that referenced this issue Apr 19, 2020
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists.

For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour.

Resolves launchbadge#267
xyzd0 pushed a commit to xyzd0/sqlx that referenced this issue Apr 21, 2020
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists.

For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour.

Resolves launchbadge#267
xyzd0 pushed a commit to xyzd0/sqlx that referenced this issue Apr 21, 2020
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists.

For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour.

Resolves launchbadge#267
@mehcode mehcode added the enhancement New feature or request label Apr 22, 2020
xyzd0 pushed a commit to xyzd0/sqlx that referenced this issue Apr 22, 2020
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists.

For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour.

Resolves launchbadge#267
@jsdw
Copy link

jsdw commented Apr 24, 2020

As a workaround until this is merged, you can use a small build.rs file in crates to set a different DATABASE_URL env var for each one. For example, Add a build.rs that contains something like this to each crate you want a different DATABASE_URL for:

fn main() {
    println!("cargo:rustc-env=DATABASE_URL=postgres://user:pass@localhost/dbname")
}

mehcode pushed a commit that referenced this issue Apr 24, 2020
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists.

For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour.

Resolves #267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants