Repository: Brendonovich/prisma-client-rust (language: rust)

Issue:
Allow binary location to be defined via env variables
# Unsupported linux distro
I'm using `PCR` on NixOS and I get this error when using the CLI (`cargo run --bin prisma -- db push`):
```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', /home/zioth/.cargo/git/checkouts/prisma-client-rust-fa967aa5ad0ec391/c965b89/cli/src/prisma_cli.rs:40:18
```
Which is caused by this line of code in [prisma-client-rust/crates/cli/src/prisma_cli.rs#L40
](https://github.com/Brendonovich/prisma-client-rust/blob/main/crates/cli/src/prisma_cli.rs#L40
)
Prisma does not have precompiled binaries for NixOS so I had to add them with a flake:
```nix
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};

        packages = with pkgs; [
          # Snip ... tauri deps

          openssl_3
          glib
          nodejs_20
        ];
      in
      {
        devShell = pkgs.mkShell {
          buildInputs = packages;

          shellHook = let prisma-engines = pkgs.prisma-engines; in ''
              PATH="$PWD/node_modules/.bin:$PATH"
              export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath libraries}:$LD_LIBRARY_PATH
          '';
          PRISMA_SCHEMA_ENGINE_BINARY = "${pkgs.prisma-engines}/bin/schema-engine";
          PRISMA_QUERY_ENGINE_BINARY = "${pkgs.prisma-engines}/bin/query-engine";
          PRISMA_QUERY_ENGINE_LIBRARY = "${pkgs.prisma-engines}/lib/libquery_engine.node";
          PRISMA_FMT_BINARY = "${pkgs.prisma-engines}/bin/prisma-fmt";
        };
      });
}
```

Would you be interested in adding support for setting the binary locations via env variables?


# Maybe this is related
Also, idk if this is related, when calling `pnpm prisma db push` I get this error:
(`schema.prisma` contains both the PCR and native prisma-ts generators)
```
Warning Precompiled engine files are not available for nixos.
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "db.sqlite" at "file:./db.sqlite"

The database is already in sync with the Prisma schema.

Running generate... (Use --skip-generate to skip the generators)
Warning Precompiled engine files are not available for nixos.
Generator "cargo prisma" failed:

    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
warning: the following packages contain code that will be rejected by a future version of Rust: bigint v4.4.3
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
     Running `/home/zioth/projects/apps/chess/target/debug/prisma`
thread 'main' panicked at 'Failed to deserialize DMMF from Prisma engines: Error { path: Path { segments: [Map { key: "generator" }, Map { key: "binary
Targets" }, Seq { index: 0 }] }, original: Error("invalid type: map, expected a string", line: 1, column: 1936) }', /home/zioth/.cargo/git/checkouts/pr
isma-client-rust-fa967aa5ad0ec391/c965b89/sdk/src/runtime.rs:57:26
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

✔ Generated Prisma Client (5.0.0 | library) to ./node_modules/@prisma/client in 45ms
```



Discussion / hints:
Also trying to run this on alpine seems to not work. 

```Details: /root/.cache/prisma/binaries/cli/4.8.0/d6e67a83f971b175a593ccc12e15c4a757f93ffe/prisma-query-engine-debian-openssl-1.1.x: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory```

presumably because it gets its binaries from 

> Downloading https://binaries.prisma.sh/all_commits/d6e67a83f971b175a593ccc12e15c4a757f93ffe/debian-openssl-1.1.x/migration-engine.gz to /root/.cache/prisma/binaries/cli/4.8.0/d6e67a83f971b175a593ccc12e15c4a757f93ffe/prisma-migration-engine-debian-openssl-1.1.x
> Downloading https://binaries.prisma.sh/all_commits/d6e67a83f971b175a593ccc12e15c4a757f93ffe/debian-openssl-1.1.x/introspection-engine.gz to /root/.cache/prisma/binaries/cli/4.8.0/d6e67a83f971b175a593ccc12e15c4a757f93ffe/prisma-introspection-engine-debian-openssl-1.1.x
> Downloading https://binaries.prisma.sh/all_commits/d6e67a83f971b175a593ccc12e15c4a757f93ffe/debian-openssl-1.1.x/prisma-fmt.gz to /root/.cache/prisma/binaries/cli/4.8.0/d6e67a83f971b175a593ccc12e15c4a757f93ffe/prisma-prisma-fmt-debian-openssl-1.1.x

Fix patch:
diff --git a/crates/cli/src/binaries/mod.rs b/crates/cli/src/binaries/mod.rs
index f5552bd1..e634e46c 100644
--- a/crates/cli/src/binaries/mod.rs
+++ b/crates/cli/src/binaries/mod.rs
@@ -48,20 +48,6 @@ pub fn global_cache_dir() -> PathBuf {
         .join(PRISMA_CLI_VERSION)
 }
 
-pub fn fetch_native(to_dir: &PathBuf) -> Result<(), String> {
-    if !to_dir.is_absolute() {
-        Err("to_dir must be absolute".to_string())?;
-    }
-
-    download_cli(to_dir)?;
-
-    for e in &ENGINES {
-        download_engine(&e.name, &to_dir)?;
-    }
-
-    Ok(())
-}
-
 pub fn download_cli(to_dir: &PathBuf) -> Result<(), String> {
     let cli = prisma_cli_name();
 
@@ -92,7 +78,7 @@ pub fn download_cli(to_dir: &PathBuf) -> Result<(), String> {
     Ok(())
 }
 
-fn download_engine(engine_name: &str, to_dir: &PathBuf) -> Result<(), String> {
+pub fn download_engine(engine_name: &str, to_dir: &PathBuf) -> Result<(), String> {
     let os_name = platform::binary_platform_name();
 
     let to = platform::check_for_extension(
diff --git a/crates/cli/src/prisma_cli.rs b/crates/cli/src/prisma_cli.rs
index b6715bae..bd412009 100644
--- a/crates/cli/src/prisma_cli.rs
+++ b/crates/cli/src/prisma_cli.rs
@@ -5,14 +5,13 @@ use std::process::Command;
 pub fn main(args: &Vec<String>) {
     let dir = binaries::global_cache_dir();
 
-    binaries::fetch_native(&dir).unwrap();
-
+    binaries::download_cli(&dir).unwrap();
     let prisma = binaries::prisma_cli_name();
-
-    let mut cmd = Command::new(dir.join(prisma));
     let binary_name =
         platform::check_for_extension(&platform::name(), &platform::binary_platform_name());
 
+    let mut cmd = Command::new(dir.join(prisma));
+
     cmd.args(args);
 
     cmd.envs(env::vars());
@@ -25,6 +24,7 @@ pub fn main(args: &Vec<String>) {
                 cmd.env(e.env, path);
             }
             Err(_) => {
+                binaries::download_engine(&e.name, &dir).unwrap();
                 let path = dir
                     .join(binaries::ENGINE_VERSION)
                     .join(format!("prisma-{}-{}", e.name, binary_name));


Test patch:
diff --git a/integration-tests/tests/lib.rs b/integration-tests/tests/lib.rs
index 0040ea9e..31b49cbf 100644
--- a/integration-tests/tests/lib.rs
+++ b/integration-tests/tests/lib.rs
@@ -8,7 +8,7 @@ mod utils;
 
 #[tokio::test]
 async fn aaaa_run_migrations() -> TestResult {
-    let client = db::new_client().await.unwrap();
+    let client = db::PrismaClient::_builder().build().await.unwrap();
 
     client._db_push().accept_data_loss().await.unwrap();