Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.76k stars 104 forks source link

Allow to build with env("DATABASE_URL") in prisma.schema #299

Closed amallek closed 1 year ago

amallek commented 1 year ago

Bug description

With the current version 0.6.6 it is indeed possible to create the prisma.rs with the help of prisma generate, if you use env("DATABASE_URL") as database URL in schema.prisma:

datasource db {
    provider = "postgresql
    url = env("DATABASE_URL")
}

However, it is unfortunately not possible to run cargo run/build. The following error occurs:

cargo run --bin wpman                    
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/wpman`
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', .cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/76693ba/src/client.rs:161:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

With RUST_BACKTRACE=1:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', .cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/76693ba/src/client.rs:161:18
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/panicking.rs:64:14
   2: core::panicking::panic
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/panicking.rs:111:5
   3: core::option::Option<T>::unwrap
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/option.rs:778:21
   4: prisma_client_rust::client::PrismaClientInternals::new::{{closure}}
             at .cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/76693ba/src/client.rs:157:27
   5: wpman::prisma::_prisma::PrismaClientBuilder::build::{{closure}}
             at ./src/prisma.rs:32266:13
   6: wpman::main::{{closure}}
             at ./src/main.rs:14:85
   7: tokio::runtime::park::CachedParkThread::block_on::{{closure}}
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/park.rs:283:63
   8: tokio::runtime::coop::with_budget
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::park::CachedParkThread::block_on
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/park.rs:283:31
  11: tokio::runtime::context::BlockingRegionGuard::block_on
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/context.rs:315:13
  12: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/multi_thread/mod.rs:66:9
  13: tokio::runtime::runtime::Runtime::block_on
             at .cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/runtime.rs:304:45
  14: wpman::main
             at ./src/main.rs:67:5
  15: core::ops::function::FnOnce::call_once
             at /rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/ops/function.rs:507:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

How to reproduce

Use

datasource db {
    provider = "postgresql"
    url      = env("DATABASE_URL")
}

in prisma.schema and try to run cargo build to reproduce. This will produce the error above.

Change back to

datasource db {
    provider = "postgresql"
    url      = "postgresql://..."
}

and cargo build works well

Expected behavior

It would be handy if env() was supported consistently.

Prisma information

generator client {
    provider = "cargo prisma"
    output   = "../src/prisma.rs"
}
datasource db {
    provider = "postgresql"
    url      = env("DATABASE_URL")
}

Environment & setup

OS: MacOS Database: Postgres

Brendonovich commented 1 year ago

I expect you're using a .env file? This is fixed on main and will be released soon, since I now use dotenv rather than std::env.

amallek commented 1 year ago

That's correct. Thanks; looking forward to the release.

jareducherek commented 1 year ago

Also just encountered this issue, looking forward to the release!

FlucTuAteDev commented 8 months ago

I've just encountered this issue. Seems like it still panics when the .env file is in the prisma/ folder. It works fine when it's in the root folder though.

Brendonovich commented 8 months ago

@FlucTuAteDev .env files in the prisma folder won't be detected, they have to be in the same folder as you run the cli/your application from.

FlucTuAteDev commented 8 months ago

Oh, okay. I might have missed that. The official docs says that it can be located there. Also when running cargo prisma generate it says that it loaded the envs from the prisma folder:

Environment variables loaded from prisma\.env
Prisma schema loaded from prisma\schema.prisma

✔ Generated Prisma Client Rust to .\src\prisma.rs in 361ms