From b6daa0b16d0aedf495932cae9d00728357899ae0 Mon Sep 17 00:00:00 2001 From: "Oleksii (Alexey) Orlenko" Date: Mon, 27 Oct 2025 11:49:44 +0100 Subject: [PATCH 01/23] feat(psl)!: remove `directUrl` and `shadowDatabaseUrl` from schema (#5654) Ref: https://linear.app/prisma-company/issue/TML-1339/remove-url-directurl-shadowdatabaseurl-datasource-attributes-from-psl --- AGENTS.md | 186 ++++++++++ prisma-fmt/src/get_config.rs | 98 +---- prisma-fmt/src/get_dmmf.rs | 130 +------ prisma-fmt/src/text_document_completion.rs | 24 +- .../text_document_completion/datasource.rs | 40 +- prisma-fmt/src/validate.rs | 10 +- .../result.json | 20 - .../result.json | 25 -- .../schema.prisma | 10 - .../datasource_env_db_direct_url/result.json | 11 - .../schema.prisma | 10 - .../result.json | 11 - .../schema.prisma | 10 - .../datasource_multischema/result.json | 20 - .../result.json | 25 -- .../schema.prisma | 10 - .../tests/text_document_completion/tests.rs | 4 - psl/diagnostics/src/error.rs | 16 +- .../src/validate/datasource_loader.rs | 46 +-- psl/psl/tests/config/datasources.rs | 21 +- psl/psl/tests/config/sources.rs | 349 ------------------ .../direct_url_same_as_shadow.prisma | 16 +- .../datasource/url_same_as_shadow.prisma | 8 +- .../generator/multiple_prisma_clients.prisma | 1 - .../multiple_prisma_clients_same_name.prisma | 1 - query-engine/dmmf/test_files/views.json | 10 +- .../tests/re_introspection/mssql.rs | 66 ---- .../sql-migration-tests/src/test_api.rs | 14 +- .../tests/migrations/mssql/multi_schema.rs | 9 +- .../tests/migrations/postgres/multi_schema.rs | 9 +- .../postgres/empty_directurl.prisma | 21 -- 31 files changed, 271 insertions(+), 960 deletions(-) create mode 100644 AGENTS.md delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/result.json delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/schema.prisma delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/result.json delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/schema.prisma delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/result.json delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/schema.prisma delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/result.json delete mode 100644 prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/schema.prisma delete mode 100644 schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_directurl.prisma diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000000..60a09d621287 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,186 @@ +# Agent Playbook — Prisma Engines + +## 1. Big Picture +- This repo hosts the **Prisma Engines**: PSL (schema parser/validator), schema-engine (migrate, introspect), query components (legacy query engine, new query compiler), driver adapters, and utilities shared with Prisma Client. +- Prisma 7 roadmap status: + - `directUrl` and `shadowDatabaseUrl` are **invalid** in PSL. + - `url` remains temporarily (legacy query engine still consumes it); removal is a follow-up. + - CLI/tests override connection info via schema-engine CLI (`--datasource`) or shared `TestApi::new_engine_with_connection_strings`. + - Reference commit: `34b5a692b7bd79939a9a2c3ef97d816e749cda2f` (driver adapter override plumbing). +- Prisma is deprecating the **native Rust query engine** in favor of the **Query Compiler (QC)** architecture: + - Query planning happens in Rust (`query-compiler` crate). Output: an expression tree (“query plan”). + - Query interpretation/execution runs in Prisma Client TypeScript using driver adapters. The interpreter has no knowledge of connection strings or even whether it talks to a real DB. + - A compatibility harness (`qc-test-runner.ts` in the main repo) emulates legacy query engine behavior for the test suite until QE removal is complete. + - MongoDB support is not yet implemented for QC; Prisma 7 will ship without MongoDB, to be added later. + +--- + +## 2. Repository Orientation +Key directories: +- `psl/` – Prisma Schema Language parser, validator, config tooling. +- `schema-engine/` – Migration/introspection engine plus test suites. +- `prisma-fmt/` – Language server & formatter entry point (tests rely on `expect!` snapshots). +- `libs/` – Shared libraries (metrics, value types, test setup). +- `schema-engine/sql-migration-tests` / `sql-introspection-tests` – Heavy integration suites (require DBs). +- `query-engine/` – Legacy query execution stack (Rust). +- `query-compiler/` – New query planner + associated WASM + playground. +- `libs/` – Shared libraries (metrics, value types, driver adapters, test setup). +- `driver-adapters/` – Rust-side adapter utilities for the new query interpreter. + +Supporting infra: +- Tests use Rust `cargo test`. Some suites expect database URLs in env (see §5). +- `test-setup` crate provisions databases when env vars are defined (Docker-based in CI). +- `UPDATE_EXPECT=1 cargo test …` regenerates `expect!` snapshots (common when diagnostics shift). + +--- + +## 3. Current Domain Knowledge +### Datasource URLs +- PSL rejects `directUrl`/`shadowDatabaseUrl` with targeted diagnostics (`DatamodelError::new_datasource_*_removed_error`). +- Parser still records `url` (and uses span for override fallbacks). +- `Datasource::override_urls()` now fakes spans because overrides bypass PSL parsing. +- Schema-engine tests must supply overrides via `TestApi::new_engine_with_connection_strings(connection_string, Some(shadow_connection))`. The wrapper returns an `EngineTestApi`. +- Old fixtures relying on `directUrl` inside PSL must be rewritten or deleted. +- Query compiler already assumes datasource URLs are supplied externally (from Prisma Client). + +### Text Completions +- `prisma-fmt` completions now only offer `url` (no more direct/shadow suggestions). +- Completion scenarios removed for the deprecated properties. Expect JSON fixtures to change if docs/completions change again. + +### Diagnostics / Tests +- Many tests assert on colored output via `expect!`. Always regenerate expectations when diagnostics wording changes. +- Integration tests around multi-schema migrations still need real DB URLs; without them they skip/fail early. +- Query compiler tests use insta snapshots (`query-compiler/tests`). Regenerate with `UPDATE_EXPECT=1 cargo test -p query-compiler`. +- Query engine connector tests rely on `cargo insta` snapshots too (see `connector-test-kit-rs` README). + +--- + +## 4. Typical Workflows +### Linting / Formatting +- Rustfmt + cargo fmt (standard). JSON fixtures kept raw (no formatter). +- Full lint pass (formatting + clippy warnings as errors): + ```bash + make pedantic + ``` + This runs `cargo fmt -- --check` and `cargo clippy --all-features --all-targets -Dwarnings`. Fix the compiler/clippy diagnostics first, then formatting. + +### Running Tests +1. **Fast PSL/LSP suites** + ```bash + cargo test -p prisma-fmt -F psl/all + ``` + Use `UPDATE_EXPECT=1` to refresh snapshots. + +2. **Unit tests in PSL** + ```bash + cargo test -p psl -F all + ``` + +3. **Unit tests for the whole workspace** + ```bash + make test-unit + ``` + Use this one if you can't figure out the correct cargo features for a specific crate. + Some library crates may be tricky to compile in isolation without feature unification. + Unit tests are very fast so there's no problem running them for the whole workspace. + Note that `cargo test` for the whole workspace won't work because of the Node-API + symbol dependencies in the `query-engine-node-api` crate, use the makefile target. + +3. **Schema engine SQL tests** + Require DB env vars (see `.test_database_urls/` in repo root). Example: + ```bash + source .test_database_urls/postgres + cargo test -p sql-migration-tests migration_with_shadow_database -- --nocapture + ``` + +4. **Schema engine integration** + Similar pattern; rely on generated DB URLs. Without env vars tests will refuse to run (by design). + +5. **Query compiler snapshots** + ```bash + UPDATE_EXPECT=1 cargo test -p query-compiler + ``` + Graphviz (`dot`) optional; set `RENDER_DOT_TO_PNG` for visuals (requires Graphviz installed). + +6. **Query engine connector tests** + ```bash + make dev-postgres15 # or appropriate make target to spin up DB & config + cargo test -p query-engine-tests -- --nocapture + ``` + Requires `.test_config` or env vars; see `query-engine/connector-test-kit-rs/README.md`. + +7. **Query engine Node API / C-ABI builds** + - Node addon: `cargo build -p query-engine-node-api`. + - C-ABI (used by React Native): `cargo build -p query-engine-c-abi`. + +### Updating expect! snapshots +```bash +UPDATE_EXPECT=1 cargo test -p prisma-fmt [optional::test::path] +``` +Ensure diffs make sense and rerun without `UPDATE_EXPECT` to confirm. + +--- + +## 5. Environment Essentials +- **Databases**: env vars follow `TEST_DATABASE_URL`, `TEST_SHADOW_DATABASE_URL`, etc. Use the `.test_database_urls/` helper scripts or docker-compose setup from team docs. +- **Linear tickets**: two key Prisma 7 projects – *Breaking Changes* and *New Features*. Search via Linear MCP server if context needed. +- **Feature flags**: driver adapters live behind configuration (`prisma.config.ts` with `engine: 'classic' | 'js'`). Schema engine CLI accepts `--datasource` JSON payload – reuse the structure from commit `34b5a69…`. +- **Graphviz (`dot`)**: optional but useful for rendering query graphs (required if `RENDER_DOT_TO_PNG` set in QC tests/playground). +- **Node.js**: required when working with query-engine Node bindings or QC interpreter harness. +- **Docker**: used for local DBs via `docker-compose.yml`; make targets (`make dev-postgres15`, `make start-mongo6`, etc.) orchestrate containers + config files. + +--- + +## 6. Common Gotchas +- Running prisma-fmt tests after updating diagnostics **without** refreshing expect files will cause failures. Always run with `UPDATE_EXPECT=1`. +- Some fixtures expect **CRLF** endings (`create_missing_block_composite_type_crlf`). Avoid rewriting line endings when not necessary. If Git warns, restore file from `HEAD`. +- Integration tests bail with “Missing TEST_DATABASE_URL”. Set env vars or skip running them locally. +- `TestApi` inside `sql-migration-tests` exposes `new_engine_with_connection_strings`; use it to pass overrides. +- When touching overrides, update both PSL and schema-engine sides; they share assumptions about spans and optional URLs. +- Query compiler shares substantial code with query engine (e.g., `query_core`, `query_structure`). Changes in shared crates affect both paths—be mindful of feature flags. +- Many query engine tests still assume native QE; the `qc-test-runner` harness (in main repo) ensures QC behaves like QE for now. Expect follow-up cleanup once QE removal completes. +- MongoDB currently runs only on legacy QE; QC MongoDB support is pending. Avoid regressing existing QE tests until QC parity is achieved. + +--- + +## 7. Useful Commands & Snippets +- Show diff for specific file: + `git diff path/to/file.rs` +- Re-run single Rust test: + `cargo test -p prisma-fmt validate::tests::validate_direct_url_direct_empty -- --nocapture` +- Search for legacy attributes: + `rg "directUrl"`, `rg "shadowDatabaseUrl"` +- Build schema-engine CLI: + `cargo build -p schema-engine-cli` +- Build query compiler WASM: + `make build-qc-wasm` +- Build legacy query engine binary: + `cargo build -p query-engine` +- Query compiler playground (generate plan + graph): + `cargo run -p query-compiler-playground` + +Prefer using Makefile targets that take care of setting up the environment correctly or running prerequisite commands. + +--- + +## 8. Open Themes / Future Tasks +- Removing `url` from PSL will be a follow-up; expect similar pattern (PSL error + override path). +- Query engine removal: remaining QE dependencies/tests need to migrate to QC or be deleted once QE is gone. +- Additional schema-engine tests may need migration to the new override helper. +- Documentation updates (internal + public) should mirror code changes; check when editing diagnostics to keep docs consistent. +- Query compiler MongoDB support: implement translation path + driver adapters, update tests once ready. +- Post-QE cleanup: strip QE-specific branches in shared crates (`query_core`, `query_structure`), simplify driver adapter plumbing. + +--- + +## 9. External References +- Prisma Config (`prisma.config.ts`) implementation lives in the main Prisma repo (`@prisma/config` package). +- Linear roadmap items for Prisma 7 (Breaking Changes, New Features) hold context. +- Commit `34b5a69…` – canonical example for datasource override wiring. +- Query engine connector test guide: `query-engine/connector-test-kit-rs/README.md`. +- QC playground usage: `query-compiler/query-compiler-playground/`. +- QC harness in Prisma repo: `packages/cli/src/__tests__/queryCompiler/qc-test-runner.ts` (mirrors QE behavior). + +--- + +**When modifying anything involving diagnostics or fixtures:** run relevant tests, refresh expectations, and ensure Git diffs are readable (no accidental CRLF/encoding swaps). Keep this file updated whenever we discover new traps.*** diff --git a/prisma-fmt/src/get_config.rs b/prisma-fmt/src/get_config.rs index 6bf3d4d96f04..72b0b26ddf11 100644 --- a/prisma-fmt/src/get_config.rs +++ b/prisma-fmt/src/get_config.rs @@ -291,7 +291,7 @@ mod tests { } #[test] - fn get_config_direct_url_value() { + fn get_config_direct_url_should_error() { let schema = r#" datasource thedb { provider = "postgresql" @@ -307,101 +307,7 @@ mod tests { } }); let expected = expect![[ - r#"{"config":{"generators":[],"datasources":[{"name":"thedb","provider":"postgresql","activeProvider":"postgresql","url":{"fromEnvVar":"DBURL","value":"postgresql://example.com/mydb"},"directUrl":{"fromEnvVar":null,"value":"postgresql://example.com/direct"},"schemas":[],"sourceFilePath":"schema.prisma"}],"warnings":[]},"errors":[]}"# - ]]; - let response = get_config(&request.to_string()); - expected.assert_eq(&response); - } - - #[test] - fn get_config_direct_url_env() { - let schema = r#" - datasource thedb { - provider = "postgresql" - url = env("DBURL") - directUrl = env("DBDIRURL") - } - "#; - - let request = json!({ - "prismaSchema": schema, - "env": { - "DBURL": "postgresql://example.com/mydb", - "DBDIRURL": "postgresql://example.com/direct" - } - }); - let expected = expect![[ - r#"{"config":{"generators":[],"datasources":[{"name":"thedb","provider":"postgresql","activeProvider":"postgresql","url":{"fromEnvVar":"DBURL","value":"postgresql://example.com/mydb"},"directUrl":{"fromEnvVar":"DBDIRURL","value":"postgresql://example.com/direct"},"schemas":[],"sourceFilePath":"schema.prisma"}],"warnings":[]},"errors":[]}"# - ]]; - let response = get_config(&request.to_string()); - expected.assert_eq(&response); - } - - #[test] - fn get_config_direct_url_direct_empty() { - let schema = r#" - datasource thedb { - provider = "postgresql" - url = env("DBURL") - directUrl = "" - } - "#; - - let request = json!({ - "prismaSchema": schema, - "env": { - "DBURL": "postgresql://example.com/mydb", - } - }); - let expected = expect![[ - r#"{"config":{"generators":[],"datasources":[{"name":"thedb","provider":"postgresql","activeProvider":"postgresql","url":{"fromEnvVar":"DBURL","value":null},"directUrl":{"fromEnvVar":null,"value":""},"schemas":[],"sourceFilePath":"schema.prisma"}],"warnings":[]},"errors":[{"file_name":"schema.prisma","message":"\u001b[1;91merror\u001b[0m: \u001b[1mError validating datasource `thedb`: You must provide a nonempty direct URL\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:5\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m 4 | \u001b[0m url = env(\"DBURL\")\n\u001b[1;94m 5 | \u001b[0m directUrl = \u001b[1;91m\"\"\u001b[0m\n\u001b[1;94m | \u001b[0m\n"}]}"# - ]]; - let response = get_config(&request.to_string()); - expected.assert_eq(&response); - } - - #[test] - fn get_config_direct_url_env_not_found() { - let schema = r#" - datasource thedb { - provider = "postgresql" - url = env("DBURL") - directUrl = env("DOES_NOT_EXIST") - } - "#; - - let request = json!({ - "prismaSchema": schema, - "env": { - "DBURL": "postgresql://example.com/mydb", - } - }); - let expected = expect![[ - r#"{"config":{"generators":[],"datasources":[{"name":"thedb","provider":"postgresql","activeProvider":"postgresql","url":{"fromEnvVar":"DBURL","value":null},"directUrl":{"fromEnvVar":"DOES_NOT_EXIST","value":null},"schemas":[],"sourceFilePath":"schema.prisma"}],"warnings":[]},"errors":[{"file_name":"schema.prisma","message":"\u001b[1;91merror\u001b[0m: \u001b[1mEnvironment variable not found: DOES_NOT_EXIST.\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:5\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m 4 | \u001b[0m url = env(\"DBURL\")\n\u001b[1;94m 5 | \u001b[0m directUrl = \u001b[1;91menv(\"DOES_NOT_EXIST\")\u001b[0m\n\u001b[1;94m | \u001b[0m\n"}]}"# - ]]; - let response = get_config(&request.to_string()); - expected.assert_eq(&response); - } - - #[test] - fn get_config_direct_url_env_is_empty() { - let schema = r#" - datasource thedb { - provider = "postgresql" - url = env("DBURL") - directUrl = env("DOES_NOT_EXIST") - } - "#; - - let request = json!({ - "prismaSchema": schema, - "env": { - "DBURL": "postgresql://example.com/mydb", - "DOES_NOT_EXIST": "", - } - }); - let expected = expect![[ - r#"{"config":{"generators":[],"datasources":[{"name":"thedb","provider":"postgresql","activeProvider":"postgresql","url":{"fromEnvVar":"DBURL","value":null},"directUrl":{"fromEnvVar":"DOES_NOT_EXIST","value":null},"schemas":[],"sourceFilePath":"schema.prisma"}],"warnings":[]},"errors":[{"file_name":"schema.prisma","message":"\u001b[1;91merror\u001b[0m: \u001b[1mError validating datasource `thedb`: You must provide a nonempty direct URL. The environment variable `DOES_NOT_EXIST` resolved to an empty string.\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:5\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m 4 | \u001b[0m url = env(\"DBURL\")\n\u001b[1;94m 5 | \u001b[0m directUrl = \u001b[1;91menv(\"DOES_NOT_EXIST\")\u001b[0m\n\u001b[1;94m | \u001b[0m\n"}]}"# + r#"{"config":{"generators":[],"datasources":[{"name":"thedb","provider":"postgresql","activeProvider":"postgresql","url":{"fromEnvVar":"DBURL","value":"postgresql://example.com/mydb"},"schemas":[],"sourceFilePath":"schema.prisma"}],"warnings":[]},"errors":[{"file_name":"schema.prisma","message":"\u001b[1;91merror\u001b[0m: \u001b[1mThe datasource property `directUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:5\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m 4 | \u001b[0m url = env(\"DBURL\")\n\u001b[1;94m 5 | \u001b[0m \u001b[1;91mdirectUrl = \"postgresql://example.com/direct\"\u001b[0m\n\u001b[1;94m | \u001b[0m\n"}]}"# ]]; let response = get_config(&request.to_string()); expected.assert_eq(&response); diff --git a/prisma-fmt/src/get_dmmf.rs b/prisma-fmt/src/get_dmmf.rs index 52defba52885..8dc026ca2ded 100644 --- a/prisma-fmt/src/get_dmmf.rs +++ b/prisma-fmt/src/get_dmmf.rs @@ -204,132 +204,12 @@ mod tests { "prismaSchema": schema, }); - let expected = expect![[r#" - { - "datamodel": { - "enums": [], - "models": [], - "types": [], - "indexes": [] - }, - "schema": { - "inputObjectTypes": {}, - "outputObjectTypes": { - "prisma": [ - { - "name": "Query", - "fields": [] - }, - { - "name": "Mutation", - "fields": [ - { - "name": "executeRaw", - "args": [ - { - "name": "query", - "isRequired": true, - "isNullable": false, - "inputTypes": [ - { - "type": "String", - "location": "scalar", - "isList": false - } - ] - }, - { - "name": "parameters", - "isRequired": false, - "isNullable": false, - "inputTypes": [ - { - "type": "Json", - "location": "scalar", - "isList": false - } - ] - } - ], - "isNullable": false, - "outputType": { - "type": "Json", - "location": "scalar", - "isList": false - } - }, - { - "name": "queryRaw", - "args": [ - { - "name": "query", - "isRequired": true, - "isNullable": false, - "inputTypes": [ - { - "type": "String", - "location": "scalar", - "isList": false - } - ] - }, - { - "name": "parameters", - "isRequired": false, - "isNullable": false, - "inputTypes": [ - { - "type": "Json", - "location": "scalar", - "isList": false - } - ] - } - ], - "isNullable": false, - "outputType": { - "type": "Json", - "location": "scalar", - "isList": false - } - } - ] - } - ] - }, - "enumTypes": { - "prisma": [ - { - "name": "TransactionIsolationLevel", - "values": [ - "ReadUncommitted", - "ReadCommitted", - "RepeatableRead", - "Serializable" - ] - } - ] - }, - "fieldRefTypes": {} - }, - "mappings": { - "modelOperations": [], - "otherOperations": { - "read": [], - "write": [ - "executeRaw", - "queryRaw" - ] - } - } - }"#]]; - - let response = get_dmmf(&request.to_string()).unwrap(); - - let prettified_response = - serde_json::to_string_pretty(&serde_json::from_str::(&response).unwrap()).unwrap(); + let expected = expect![[ + r#"{"error_code":"P1012","message":"\u001b[1;91merror\u001b[0m: \u001b[1mThe datasource property `directUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:5\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m 4 | \u001b[0m url = env(\"DBURL\")\n\u001b[1;94m 5 | \u001b[0m \u001b[1;91mdirectUrl = \"\"\u001b[0m\n\u001b[1;94m | \u001b[0m\n\nValidation Error Count: 1"}"# + ]]; - expected.assert_eq(&prettified_response); + let response = get_dmmf(&request.to_string()).unwrap_err(); + expected.assert_eq(&response); } #[test] diff --git a/prisma-fmt/src/text_document_completion.rs b/prisma-fmt/src/text_document_completion.rs index 276b7f5fe6e2..e2b631066f63 100644 --- a/prisma-fmt/src/text_document_completion.rs +++ b/prisma-fmt/src/text_document_completion.rs @@ -148,14 +148,6 @@ fn push_ast_completions(ctx: CompletionContext<'_>, completion_list: &mut Comple datasource::url_completion(completion_list); } - if !ds_has_prop(&ctx, "shadowDatabaseUrl") { - datasource::shadow_db_completion(completion_list); - } - - if !ds_has_prop(&ctx, "directUrl") { - datasource::direct_url_completion(completion_list); - } - if !ds_has_prop(&ctx, "relationMode") { datasource::relation_mode_completion(completion_list); } @@ -168,19 +160,7 @@ fn push_ast_completions(ctx: CompletionContext<'_>, completion_list: &mut Comple ast::SourcePosition::Property("url", ast::PropertyPosition::FunctionValue("env")), ) => datasource::url_env_db_completion(completion_list, "url", ctx), - ast::SchemaPosition::DataSource( - _source_id, - ast::SourcePosition::Property("directUrl", ast::PropertyPosition::FunctionValue("env")), - ) => datasource::url_env_db_completion(completion_list, "directUrl", ctx), - - ast::SchemaPosition::DataSource( - _source_id, - ast::SourcePosition::Property("shadowDatabaseUrl", ast::PropertyPosition::FunctionValue("env")), - ) => datasource::url_env_db_completion(completion_list, "shadowDatabaseUrl", ctx), - - ast::SchemaPosition::DataSource(_source_id, ast::SourcePosition::Property("url", _)) - | ast::SchemaPosition::DataSource(_source_id, ast::SourcePosition::Property("directUrl", _)) - | ast::SchemaPosition::DataSource(_source_id, ast::SourcePosition::Property("shadowDatabaseUrl", _)) => { + ast::SchemaPosition::DataSource(_source_id, ast::SourcePosition::Property("url", _)) => { datasource::url_env_completion(completion_list); datasource::url_quotes_completion(completion_list); } @@ -193,8 +173,6 @@ fn ds_has_prop(ctx: &CompletionContext<'_>, prop: &str) -> bool { if let Some(ds) = ctx.datasource() { match prop { "relationMode" => ds.relation_mode_defined(), - "directurl" => ds.direct_url_defined(), - "shadowDatabaseUrl" => ds.shadow_url_defined(), "url" => ds.url_defined(), "provider" => ds.provider_defined(), _ => false, diff --git a/prisma-fmt/src/text_document_completion/datasource.rs b/prisma-fmt/src/text_document_completion/datasource.rs index 3b2e12a148dc..e8912a310402 100644 --- a/prisma-fmt/src/text_document_completion/datasource.rs +++ b/prisma-fmt/src/text_document_completion/datasource.rs @@ -25,42 +25,6 @@ pub(super) fn relation_mode_completion(completion_list: &mut CompletionList) { }) } -pub(super) fn direct_url_completion(completion_list: &mut CompletionList) { - completion_list.items.push(CompletionItem { - label: "directUrl".to_owned(), - insert_text: Some(r#"directUrl = $0"#.to_owned()), - insert_text_format: Some(InsertTextFormat::SNIPPET), - kind: Some(CompletionItemKind::FIELD), - documentation: Some(Documentation::MarkupContent(MarkupContent { - kind: MarkupKind::Markdown, - value: format_completion_docs( - r#"directUrl = "String" | env("ENVIRONMENT_VARIABLE")"#, - r#"Connection URL for direct connection to the database. [Learn more](https://pris.ly/d/data-proxy-cli)."#, - None, - ) - })), - ..Default::default() - }) -} - -pub(super) fn shadow_db_completion(completion_list: &mut CompletionList) { - completion_list.items.push(CompletionItem { - label: "shadowDatabaseUrl".to_owned(), - insert_text: Some(r#"shadowDatabaseUrl = $0"#.to_owned()), - insert_text_format: Some(InsertTextFormat::SNIPPET), - kind: Some(CompletionItemKind::FIELD), - documentation: Some(Documentation::MarkupContent(MarkupContent { - kind: MarkupKind::Markdown, - value: format_completion_docs( - r#"shadowDatabaseUrl = "String" | env("ENVIRONMENT_VARIABLE")"#, - r#"Connection URL including authentication info to use for Migrate's [shadow database](https://pris.ly/d/migrate-shadow)."#, - None, - ), - })), - ..Default::default() - }) -} - pub(super) fn url_completion(completion_list: &mut CompletionList) { completion_list.items.push(CompletionItem { label: "url".to_owned(), @@ -139,9 +103,7 @@ pub(super) fn url_quotes_completion(completion_list: &mut CompletionList) { pub(super) fn url_env_db_completion(completion_list: &mut CompletionList, kind: &str, ctx: CompletionContext<'_>) { let text = match kind { "url" => "DATABASE_URL", - "directUrl" => "DIRECT_URL", - "shadowDatabaseUrl" => "SHADOW_DATABASE_URL", - _ => unreachable!(), + _ => panic!(r#"invalid `kind` passed to `url_db_completion`: "{kind}""#), }; let insert_text = if add_quotes(ctx.params, ctx.db.source(ctx.initiating_file_id)) { diff --git a/prisma-fmt/src/validate.rs b/prisma-fmt/src/validate.rs index abef8b287283..06b93d2d8724 100644 --- a/prisma-fmt/src/validate.rs +++ b/prisma-fmt/src/validate.rs @@ -116,7 +116,8 @@ mod tests { "prismaSchema": schema, }); - validate(&request.to_string()).unwrap(); + let response = validate(&request.to_string()); + assert!(response.is_ok()) } #[test] @@ -133,7 +134,12 @@ mod tests { "prismaSchema": schema, }); - validate(&request.to_string()).unwrap(); + let expected = expect![[ + r#"{"error_code":"P1012","message":"\u001b[1;91merror\u001b[0m: \u001b[1mThe datasource property `directUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:5\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m 4 | \u001b[0m url = env(\"DBURL\")\n\u001b[1;94m 5 | \u001b[0m \u001b[1;91mdirectUrl = \"\"\u001b[0m\n\u001b[1;94m | \u001b[0m\n\nValidation Error Count: 1"}"# + ]]; + + let response = validate(&request.to_string()).unwrap_err(); + expected.assert_eq(&response); } #[test] diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_default_completions/result.json b/prisma-fmt/tests/text_document_completion/scenarios/datasource_default_completions/result.json index bb0ad048abb1..3ad8ea1b30c8 100644 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_default_completions/result.json +++ b/prisma-fmt/tests/text_document_completion/scenarios/datasource_default_completions/result.json @@ -21,26 +21,6 @@ "insertText": "url = $0", "insertTextFormat": 2 }, - { - "label": "shadowDatabaseUrl", - "kind": 5, - "documentation": { - "kind": "markdown", - "value": "```prisma\nshadowDatabaseUrl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL including authentication info to use for Migrate's [shadow database](https://pris.ly/d/migrate-shadow).\n\n" - }, - "insertText": "shadowDatabaseUrl = $0", - "insertTextFormat": 2 - }, - { - "label": "directUrl", - "kind": 5, - "documentation": { - "kind": "markdown", - "value": "```prisma\ndirectUrl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL for direct connection to the database. [Learn more](https://pris.ly/d/data-proxy-cli).\n\n" - }, - "insertText": "directUrl = $0", - "insertTextFormat": 2 - }, { "label": "relationMode", "kind": 5, diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/result.json b/prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/result.json deleted file mode 100644 index d08a1920e27b..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/result.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "isIncomplete": false, - "items": [ - { - "label": "env()", - "kind": 10, - "documentation": { - "kind": "markdown", - "value": "```prisma\nenv(_ environmentVariable: string)\n```\n___\nSpecifies a datasource via an environment variable. When running a Prisma CLI command that needs the database connection URL (e.g. `prisma db pull`), you need to make sure that the `DATABASE_URL` environment variable is set. One way to do so is by creating a `.env` file. Note that the file must be in the same directory as your schema.prisma file to automatically be picked up by the Prisma CLI.\"\n\n_@param_ environmentVariable The environment variable in which the database connection URL is stored." - }, - "insertText": "env($0)", - "insertTextFormat": 2 - }, - { - "label": "\"\"", - "kind": 10, - "documentation": { - "kind": "markdown", - "value": "```prisma\n\"connectionString\"\n```\n___\nConnection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/prisma-schema).\n\n" - }, - "insertText": "\"$0\"", - "insertTextFormat": 2 - } - ] -} \ No newline at end of file diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/schema.prisma b/prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/schema.prisma deleted file mode 100644 index 497ea26c2f09..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/schema.prisma +++ /dev/null @@ -1,10 +0,0 @@ -generator client { - provider = "prisma-client" - previewFeatures = [] -} - -datasource db { - provider = "postgresql" - url = "" - directUrl = <|> -} diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/result.json b/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/result.json deleted file mode 100644 index 084922869773..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/result.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "isIncomplete": false, - "items": [ - { - "label": "DIRECT_URL", - "kind": 21, - "insertText": "DIRECT_URL", - "insertTextFormat": 1 - } - ] -} \ No newline at end of file diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/schema.prisma b/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/schema.prisma deleted file mode 100644 index 6756ae03a508..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/schema.prisma +++ /dev/null @@ -1,10 +0,0 @@ -generator client { - provider = "prisma-client" - previewFeatures = [] -} - -datasource db { - provider = "postgresql" - url = env("") - directUrl = env("<|>") -} diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/result.json b/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/result.json deleted file mode 100644 index 03d4a6d3093a..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/result.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "isIncomplete": false, - "items": [ - { - "label": "SHADOW_DATABASE_URL", - "kind": 21, - "insertText": "SHADOW_DATABASE_URL", - "insertTextFormat": 1 - } - ] -} \ No newline at end of file diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/schema.prisma b/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/schema.prisma deleted file mode 100644 index 4ac5e3751ab1..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_shadowdb_url/schema.prisma +++ /dev/null @@ -1,10 +0,0 @@ -generator client { - provider = "prisma-client" - previewFeatures = [] -} - -datasource db { - provider = "postgresql" - url = env("") - shadowDatabaseUrl = env("<|>") -} diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_multischema/result.json b/prisma-fmt/tests/text_document_completion/scenarios/datasource_multischema/result.json index d7203e608d39..5afc190e2a9b 100644 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_multischema/result.json +++ b/prisma-fmt/tests/text_document_completion/scenarios/datasource_multischema/result.json @@ -1,26 +1,6 @@ { "isIncomplete": false, "items": [ - { - "label": "shadowDatabaseUrl", - "kind": 5, - "documentation": { - "kind": "markdown", - "value": "```prisma\nshadowDatabaseUrl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL including authentication info to use for Migrate's [shadow database](https://pris.ly/d/migrate-shadow).\n\n" - }, - "insertText": "shadowDatabaseUrl = $0", - "insertTextFormat": 2 - }, - { - "label": "directUrl", - "kind": 5, - "documentation": { - "kind": "markdown", - "value": "```prisma\ndirectUrl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL for direct connection to the database. [Learn more](https://pris.ly/d/data-proxy-cli).\n\n" - }, - "insertText": "directUrl = $0", - "insertTextFormat": 2 - }, { "label": "relationMode", "kind": 5, diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/result.json b/prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/result.json deleted file mode 100644 index d08a1920e27b..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/result.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "isIncomplete": false, - "items": [ - { - "label": "env()", - "kind": 10, - "documentation": { - "kind": "markdown", - "value": "```prisma\nenv(_ environmentVariable: string)\n```\n___\nSpecifies a datasource via an environment variable. When running a Prisma CLI command that needs the database connection URL (e.g. `prisma db pull`), you need to make sure that the `DATABASE_URL` environment variable is set. One way to do so is by creating a `.env` file. Note that the file must be in the same directory as your schema.prisma file to automatically be picked up by the Prisma CLI.\"\n\n_@param_ environmentVariable The environment variable in which the database connection URL is stored." - }, - "insertText": "env($0)", - "insertTextFormat": 2 - }, - { - "label": "\"\"", - "kind": 10, - "documentation": { - "kind": "markdown", - "value": "```prisma\n\"connectionString\"\n```\n___\nConnection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/prisma-schema).\n\n" - }, - "insertText": "\"$0\"", - "insertTextFormat": 2 - } - ] -} \ No newline at end of file diff --git a/prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/schema.prisma b/prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/schema.prisma deleted file mode 100644 index c7e0b587d8df..000000000000 --- a/prisma-fmt/tests/text_document_completion/scenarios/datasource_shadowdb_url_arguments/schema.prisma +++ /dev/null @@ -1,10 +0,0 @@ -generator client { - provider = "prisma-client" - previewFeatures = [] -} - -datasource db { - provider = "postgresql" - url = "" - shadowDatabaseUrl = <|> -} diff --git a/prisma-fmt/tests/text_document_completion/tests.rs b/prisma-fmt/tests/text_document_completion/tests.rs index 1066c32fdfbb..ec9fdf7ed322 100644 --- a/prisma-fmt/tests/text_document_completion/tests.rs +++ b/prisma-fmt/tests/text_document_completion/tests.rs @@ -47,9 +47,5 @@ scenarios! { datasource_default_completions datasource_multischema datasource_url_arguments - datasource_direct_url_arguments - datasource_shadowdb_url_arguments datasource_env_db_url - datasource_env_db_direct_url - datasource_env_db_shadowdb_url } diff --git a/psl/diagnostics/src/error.rs b/psl/diagnostics/src/error.rs index 66161ba3f9f6..8651f1567842 100644 --- a/psl/diagnostics/src/error.rs +++ b/psl/diagnostics/src/error.rs @@ -348,18 +348,14 @@ impl DatamodelError { Self::new(format!("Datasource provider not known: \"{provider}\"."), span) } - pub fn new_shadow_database_is_same_as_main_url_error(source_name: &str, span: Span) -> DatamodelError { - let msg = format!( - "shadowDatabaseUrl is the same as url for datasource \"{source_name}\". Please specify a different database as shadow database to avoid data loss." - ); - Self::new(msg, span) + pub fn new_datasource_direct_url_removed_error(span: Span) -> DatamodelError { + let msg = "The datasource property `directUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource"; + Self::new(msg.to_string(), span) } - pub fn new_shadow_database_is_same_as_direct_url_error(source_name: &str, span: Span) -> DatamodelError { - let msg = format!( - "shadowDatabaseUrl is the same as directUrl for datasource \"{source_name}\". Please specify a different database as shadow database to avoid data loss." - ); - Self::new(msg, span) + pub fn new_datasource_shadow_database_url_removed_error(span: Span) -> DatamodelError { + let msg = "The datasource property `shadowDatabaseUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource"; + Self::new(msg.to_string(), span) } pub fn new_preview_feature_not_known_error( diff --git a/psl/psl-core/src/validate/datasource_loader.rs b/psl/psl-core/src/validate/datasource_loader.rs index e08d973708ce..f46a1a0d914c 100644 --- a/psl/psl-core/src/validate/datasource_loader.rs +++ b/psl/psl-core/src/validate/datasource_loader.rs @@ -159,42 +159,12 @@ fn lift_datasource( } }; - let shadow_database_url = match args.remove(SHADOW_DATABASE_URL_KEY) { - Some((_span, shadow_db_url_arg)) => match StringFromEnvVar::coerce(shadow_db_url_arg, diagnostics) { - Some(shadow_db_url) => Some(shadow_db_url) - .filter(|s| !s.as_literal().map(|literal| literal.is_empty()).unwrap_or(false)) - .map(|url| (url, shadow_db_url_arg.span())), - None => None, - }, - - _ => None, - }; - - let (direct_url, direct_url_span) = match args.remove(DIRECT_URL_KEY) { - Some((_, direct_url)) => ( - StringFromEnvVar::coerce(direct_url, diagnostics), - Some(direct_url.span()), - ), - - None => (None, None), - }; - - if let Some((shadow_url, _)) = &shadow_database_url { - if let (Some(direct_url), Some(direct_url_span)) = (&direct_url, direct_url_span) - && shadow_url == direct_url - { - diagnostics.push_error(DatamodelError::new_shadow_database_is_same_as_direct_url_error( - source_name, - direct_url_span, - )); - } + if let Some((span, _)) = args.remove(SHADOW_DATABASE_URL_KEY) { + diagnostics.push_error(DatamodelError::new_datasource_shadow_database_url_removed_error(span)); + } - if shadow_url == &url { - diagnostics.push_error(DatamodelError::new_shadow_database_is_same_as_main_url_error( - source_name, - url_span, - )); - } + if let Some((span, _)) = args.remove(DIRECT_URL_KEY) { + diagnostics.push_error(DatamodelError::new_datasource_direct_url_removed_error(span)); } preview_features_guardrail(&mut args, diagnostics); @@ -253,11 +223,11 @@ fn lift_datasource( active_provider: active_connector.provider_name(), url, url_span, - direct_url, - direct_url_span, + direct_url: None, + direct_url_span: None, documentation, active_connector, - shadow_database_url, + shadow_database_url: None, relation_mode, connector_data, }) diff --git a/psl/psl/tests/config/datasources.rs b/psl/psl/tests/config/datasources.rs index 94eaf1d43ff4..586baa41db6e 100644 --- a/psl/psl/tests/config/datasources.rs +++ b/psl/psl/tests/config/datasources.rs @@ -251,7 +251,7 @@ fn empty_schema_property_should_error() { } #[test] -fn parse_direct_url_should_work() { +fn parse_direct_url_should_error() { let schema = indoc! {r#" generator js { provider = "prisma-client" @@ -264,17 +264,16 @@ fn parse_direct_url_should_work() { } "#}; - // assert_valid(schema); - let config = parse_configuration(schema); - - let result = config - .datasources - .first() - .and_then(|ds| ds.direct_url.clone()) - .and_then(|url| url.from_env_var) - .unwrap(); + let expect = expect![[r#" + error: The datasource property `directUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource + --> schema.prisma:8 +  |  +  7 |  url = env("DATABASE_URL") +  8 |  directUrl = env("DIRECT_DATABASE_URL") +  |  + "#]]; - assert_eq!("DIRECT_DATABASE_URL", result); + expect_error(schema, &expect); } #[test] diff --git a/psl/psl/tests/config/sources.rs b/psl/psl/tests/config/sources.rs index b66e556f264f..a217ff3e4bed 100644 --- a/psl/psl/tests/config/sources.rs +++ b/psl/psl/tests/config/sources.rs @@ -237,70 +237,6 @@ fn must_error_if_wrong_protocol_is_used_for_mysql() { expectation.assert_eq(&error) } -#[test] -fn must_error_if_wrong_protocol_is_used_for_mysql_shadow_database_url() { - let dml = indoc! {r#" - datasource myds { - provider = "mysql" - url = "mysql://" - shadowDatabaseUrl = "postgresql://" - } - "#}; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_shadow_database_url() - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect![[r#" - error: Error validating datasource `myds`: the shadow database URL must start with the protocol `mysql://`. - --> schema.prisma:4 -  |  -  3 |  url = "mysql://" -  4 |  shadowDatabaseUrl = "postgresql://" -  |  - "#]]; - - expectation.assert_eq(&error) -} - -#[test] -fn must_not_error_for_empty_shadow_database_urls_derived_load_env_vars() { - unsafe { std::env::set_var("EMPTY_SHADOW_DB URL_0129", " ") }; - - let schema = indoc! {r#" - datasource myds { - provider = "postgres" - url = "postgres://" - shadowDatabaseUrl = env("EMPTY_SHADOW_DB URL_0129") - } - "#}; - - let config = parse_configuration(schema); - let shadow_database_url = config.datasources[0].load_shadow_database_url().unwrap(); - - unsafe { std::env::remove_var("EMPTY_SHADOW_DB URL_0129") }; - assert!(shadow_database_url.is_none()); -} - -#[test] -fn must_not_error_for_shadow_database_urls_derived_from_missing_env_vars() { - let schema = indoc! {r#" - datasource myds { - provider = "postgres" - url = "postgres://" - shadowDatabaseUrl = env("SHADOW_DATABASE_URL_NOT_SET_21357") - } - "#}; - - let config = parse_configuration(schema); - let shadow_database_url = config.datasources[0].load_shadow_database_url().unwrap(); - - assert!(shadow_database_url.is_none()); -} - #[test] fn must_error_if_wrong_protocol_is_used_for_postgresql() { let dml = indoc! {r#" @@ -329,35 +265,6 @@ fn must_error_if_wrong_protocol_is_used_for_postgresql() { expectation.assert_eq(&error) } -#[test] -fn must_error_if_wrong_protocol_is_used_for_postgresql_shadow_database_url() { - let dml = indoc! {r#" - datasource myds { - provider = "postgresql" - url = "postgresql://" - shadowDatabaseUrl = "mysql://" - } - "#}; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_shadow_database_url() - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect![[r#" - error: Error validating datasource `myds`: the shadow database URL must start with the protocol `postgresql://` or `postgres://`. - --> schema.prisma:4 -  |  -  3 |  url = "postgresql://" -  4 |  shadowDatabaseUrl = "mysql://" -  |  - "#]]; - - expectation.assert_eq(&error) -} - #[test] fn must_error_if_wrong_protocol_is_used_for_sqlite() { let dml = indoc! {r#" @@ -736,259 +643,3 @@ fn relation_mode_default() { fn load_env_var(key: &str) -> Option { std::env::var(key).ok() } - -#[test] -fn must_error_for_empty_direct_urls() { - let dml = indoc! {r#" - datasource myds { - provider = "sqlite" - directUrl = "" - url = "file://hostfoo" - } - "#}; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_direct_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect![[r#" - error: Error validating datasource `myds`: You must provide a nonempty direct URL - --> schema.prisma:3 -  |  -  2 |  provider = "sqlite" -  3 |  directUrl = "" -  |  - "#]]; - - expectation.assert_eq(&error) -} - -#[test] -fn must_error_for_empty_env_direct_urls() { - unsafe { std::env::set_var("DB_DIRECT_URL_EMPTY_0001", " ") }; - let dml = indoc! {r#" - datasource myds { - provider = "sqlite" - directUrl = env("DB_DIRECT_URL_EMPTY_0001") - url = "file://hostfoo" - } - "#}; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_direct_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect![[r#" - error: Error validating datasource `myds`: You must provide a nonempty direct URL. The environment variable `DB_DIRECT_URL_EMPTY_0001` resolved to an empty string. - --> schema.prisma:3 -  |  -  2 |  provider = "sqlite" -  3 |  directUrl = env("DB_DIRECT_URL_EMPTY_0001") -  |  - "#]]; - - expectation.assert_eq(&error) -} - -#[test] -fn must_error_for_missing_env_direct_urls() { - let dml = indoc! {r#" - datasource myds { - provider = "sqlite" - directUrl = env("MISSING_DIRECT_ENV_VAR_0001") - url = "file://hostfoo" - } - "#}; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_direct_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect![[r#" - error: Environment variable not found: MISSING_DIRECT_ENV_VAR_0001. - --> schema.prisma:3 -  |  -  2 |  provider = "sqlite" -  3 |  directUrl = env("MISSING_DIRECT_ENV_VAR_0001") -  |  - "#]]; - - expectation.assert_eq(&error) -} - -#[test] -fn directurl_should_work_with_proxy_url() { - let dml = indoc! {r#" - datasource myds { - provider = "postgres" - directUrl = env("DATABASE_URL_0001") - url = "prisma://localhost:1234" - } - "#}; - - unsafe { std::env::set_var("DATABASE_URL_0001", "postgres://hostfoo") }; - - let config = parse_config(dml).unwrap(); - - let result = config.datasources[0] - .load_direct_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap(); - - let expectation = expect!("postgres://hostfoo"); - - // make sure other tests that run afterwards are not run in a modified environment - unsafe { std::env::remove_var("DATABASE_URL_0001") }; - - expectation.assert_eq(&result) -} - -#[test] -fn load_url_should_not_work_with_proxy_url() { - let dml = indoc! {r#" - datasource myds { - provider = "postgres" - directUrl = env("DIRECT_URL_0002") - url = env("DATABASE_URL_0002") - } - "#}; - - unsafe { std::env::set_var("DATABASE_URL_0002", "prisma://hostbar") }; - unsafe { std::env::set_var("DIRECT_URL_0002", "postgres://hostfoo") }; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect!([r#" - error: Error validating datasource `myds`: the URL must start with the protocol `postgresql://` or `postgres://`. - - To use a URL with protocol `prisma://`, you need to either enable Accelerate or the Data Proxy. - Enable Accelerate via `prisma generate --accelerate` or the Data Proxy via `prisma generate --data-proxy.` - - More information about Data Proxy: https://pris.ly/d/data-proxy -  - --> schema.prisma:4 -  |  -  3 |  directUrl = env("DIRECT_URL_0002") -  4 |  url = env("DATABASE_URL_0002") -  |  - "#]); - - // make sure other tests that run afterwards are not run in a modified environment - unsafe { std::env::remove_var("DATABASE_URL_0002") }; - unsafe { std::env::remove_var("DIRECT_URL_0002") }; - - expectation.assert_eq(&error) -} - -#[test] -fn load_url_no_validation_should_work_with_proxy_url() { - let dml = indoc! {r#" - datasource myds { - provider = "postgres" - directUrl = env("DIRECT_URL_0003") - url = env("DATABASE_URL_0003") - } - "#}; - - unsafe { std::env::set_var("DATABASE_URL_0003", "prisma://hostbar") }; - unsafe { std::env::set_var("DIRECT_URL_0003", "postgres://hostfoo") }; - - let config = parse_config(dml).unwrap(); - - let result = config.datasources[0] - .load_url_no_validation(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap(); - - let expectation = expect!("prisma://hostbar"); - - // make sure other tests that run afterwards are not run in a modified environment - unsafe { std::env::remove_var("DATABASE_URL_0003") }; - unsafe { std::env::remove_var("DIRECT_URL_0003") }; - - expectation.assert_eq(&result) -} - -#[test] -fn directurl_should_not_use_prisma_scheme_when_using_env_vars() { - unsafe { std::env::set_var("DATABASE_URL_0004", "prisma://hostbar") }; - unsafe { std::env::set_var("DIRECT_URL_0004", "prisma://hostfoo") }; - - let dml = indoc! {r#" - datasource myds { - provider = "postgres" - directUrl = env("DIRECT_URL_0004") - url = env("DATABASE_URL_0004") - } - "#}; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_direct_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect!([r#" - error: Error validating datasource `myds`: You must provide a direct URL that points directly to the database. Using `prisma` in URL scheme is not allowed. - --> schema.prisma:3 -  |  -  2 |  provider = "postgres" -  3 |  directUrl = env("DIRECT_URL_0004") -  |  - "#]); - - expectation.assert_eq(&error); - - // make sure other tests that run afterwards are not run in a modified environment - unsafe { std::env::remove_var("DIRECT_URL_0004") }; - unsafe { std::env::remove_var("DATABASE_URL_0004") }; -} - -#[test] -fn directurl_should_not_use_prisma_scheme() { - let dml = indoc! {r#" - datasource myds { - provider = "postgres" - directUrl = "prisma://kekw.lol" - url = env("DATABASE_URL_0005") - } - "#}; - - unsafe { std::env::set_var("DATABASE_URL_0005", "prisma://hostbar") }; - - let config = parse_config(dml).unwrap(); - - let error = config.datasources[0] - .load_direct_url(load_env_var) - .map_err(|e| e.to_pretty_string("schema.prisma", dml)) - .unwrap_err(); - - let expectation = expect!([r#" - error: Error validating datasource `myds`: You must provide a direct URL that points directly to the database. Using `prisma` in URL scheme is not allowed. - --> schema.prisma:3 -  |  -  2 |  provider = "postgres" -  3 |  directUrl = "prisma://kekw.lol" -  |  - "#]); - - expectation.assert_eq(&error); - - // make sure other tests that run afterwards are not run in a modified environment - unsafe { std::env::remove_var("DIRECT_URL_0005") }; -} diff --git a/psl/psl/tests/validation/datasource/direct_url_same_as_shadow.prisma b/psl/psl/tests/validation/datasource/direct_url_same_as_shadow.prisma index 413b87060c9c..53202b0b2ccd 100644 --- a/psl/psl/tests/validation/datasource/direct_url_same_as_shadow.prisma +++ b/psl/psl/tests/validation/datasource/direct_url_same_as_shadow.prisma @@ -5,15 +5,15 @@ datasource testds { shadowDatabaseUrl = "mysql://testurl" } -// error: shadowDatabaseUrl is the same as directUrl for datasource "testds". Please specify a different database as shadow database to avoid data loss. -// --> schema.prisma:4 +// error: The datasource property `shadowDatabaseUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource +// --> schema.prisma:5 //  |  -//  3 |  url = "mysql://testurl" -//  4 |  directUrl = "mysql://testurl" +//  4 |  directUrl = "mysql://testurl" +//  5 |  shadowDatabaseUrl = "mysql://testurl" //  |  -// error: shadowDatabaseUrl is the same as url for datasource "testds". Please specify a different database as shadow database to avoid data loss. -// --> schema.prisma:3 +// error: The datasource property `directUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource +// --> schema.prisma:4 //  |  -//  2 |  provider = "mysql" -//  3 |  url = "mysql://testurl" +//  3 |  url = "mysql://testurl" +//  4 |  directUrl = "mysql://testurl" //  |  diff --git a/psl/psl/tests/validation/datasource/url_same_as_shadow.prisma b/psl/psl/tests/validation/datasource/url_same_as_shadow.prisma index 471a614a42da..083565597d3d 100644 --- a/psl/psl/tests/validation/datasource/url_same_as_shadow.prisma +++ b/psl/psl/tests/validation/datasource/url_same_as_shadow.prisma @@ -4,9 +4,9 @@ datasource testds { shadowDatabaseUrl = "mysql://testurl" } -// error: shadowDatabaseUrl is the same as url for datasource "testds". Please specify a different database as shadow database to avoid data loss. -// --> schema.prisma:3 +// error: The datasource property `shadowDatabaseUrl` is no longer supported in schema files. Move connection URLs to `prisma.config.ts`. See https://pris.ly/d/config-datasource +// --> schema.prisma:4 //  |  -//  2 |  provider = "mysql" -//  3 |  url = "mysql://testurl" +//  3 |  url = "mysql://testurl" +//  4 |  shadowDatabaseUrl = "mysql://testurl" //  |  diff --git a/psl/psl/tests/validation/generator/multiple_prisma_clients.prisma b/psl/psl/tests/validation/generator/multiple_prisma_clients.prisma index f3c9b84b3dfc..eb717c69cd15 100644 --- a/psl/psl/tests/validation/generator/multiple_prisma_clients.prisma +++ b/psl/psl/tests/validation/generator/multiple_prisma_clients.prisma @@ -12,5 +12,4 @@ generator edge { datasource db { provider = "postgresql" url = env("DATABASE_URL") - directUrl = env("DIRECT_URL") } diff --git a/psl/psl/tests/validation/generator/multiple_prisma_clients_same_name.prisma b/psl/psl/tests/validation/generator/multiple_prisma_clients_same_name.prisma index 68af7b5f44aa..cb3ee4f80f49 100644 --- a/psl/psl/tests/validation/generator/multiple_prisma_clients_same_name.prisma +++ b/psl/psl/tests/validation/generator/multiple_prisma_clients_same_name.prisma @@ -12,7 +12,6 @@ generator client { datasource db { provider = "postgresql" url = env("DATABASE_URL") - directUrl = env("DIRECT_URL") } // error: The generator "client" cannot be defined because a generator with that name already exists. // --> schema.prisma:6 diff --git a/query-engine/dmmf/test_files/views.json b/query-engine/dmmf/test_files/views.json index aa6b49b64f4c..bcbbd24269c1 100644 --- a/query-engine/dmmf/test_files/views.json +++ b/query-engine/dmmf/test_files/views.json @@ -116,8 +116,12 @@ "type": "User", "nativeType": null, "relationName": "ProfileToUser", - "relationFromFields": ["userId"], - "relationToFields": ["id"], + "relationFromFields": [ + "userId" + ], + "relationToFields": [ + "id" + ], "isGenerated": false, "isUpdatedAt": false }, @@ -252,4 +256,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/schema-engine/sql-introspection-tests/tests/re_introspection/mssql.rs b/schema-engine/sql-introspection-tests/tests/re_introspection/mssql.rs index d7fd169e9962..f4292f636c58 100644 --- a/schema-engine/sql-introspection-tests/tests/re_introspection/mssql.rs +++ b/schema-engine/sql-introspection-tests/tests/re_introspection/mssql.rs @@ -401,69 +401,3 @@ async fn re_introspecting_custom_compound_unique_names(api: &mut TestApi) -> Tes Ok(()) } - -#[test_connector(tags(Mssql))] -async fn direct_url(api: &mut TestApi) { - let setup = format!( - r#" - CREATE TABLE [{schema_name}].[User] ( - id INTEGER, - [lastupdated] DATETIME, - [lastupdated2] DATETIME2, - - CONSTRAINT [User_pkey] PRIMARY KEY ([id]) - ); - - CREATE TABLE [{schema_name}].[Unrelated] ( - id INTEGER IDENTITY, - - CONSTRAINT [Unrelated_pkey] PRIMARY KEY ([id]) - ); - "#, - schema_name = api.schema_name() - ); - - api.raw_cmd(&setup).await; - - let input_dm = indoc! {r#" - generator client { - provider = "prisma-client" - } - - datasource db { - provider = "sqlserver" - url = "bad url" - directUrl = "dummy-url" - } - - model User { - id Int @id - lastupdated DateTime? @updatedAt - lastupdated2 DateTime? @db.DateTime @updatedAt - } - "#}; - - let final_dm = indoc! {r#" - generator client { - provider = "prisma-client" - } - - datasource db { - provider = "sqlserver" - url = "bad url" - directUrl = "dummy-url" - } - - model User { - id Int @id - lastupdated DateTime? @updatedAt @db.DateTime - lastupdated2 DateTime? @updatedAt - } - - model Unrelated { - id Int @id @default(autoincrement()) - } - "#}; - - pretty_assertions::assert_eq!(final_dm, &api.re_introspect_config(input_dm).await.unwrap()); -} diff --git a/schema-engine/sql-migration-tests/src/test_api.rs b/schema-engine/sql-migration-tests/src/test_api.rs index 5bcd9bb1f7e3..61d410a2f35a 100644 --- a/schema-engine/sql-migration-tests/src/test_api.rs +++ b/schema-engine/sql-migration-tests/src/test_api.rs @@ -9,7 +9,10 @@ pub use schema_core::{ pub use test_macros::test_connector; pub use test_setup::{BitFlags, Capabilities, Tags, runtime::run_with_thread_local_runtime as tok}; -use crate::{commands::*, multi_engine_test_api::TestApi as RootTestApi}; +use crate::{ + commands::*, + multi_engine_test_api::{EngineTestApi, TestApi as RootTestApi}, +}; use psl::{ datamodel_connector::NativeTypeInstance, parser_database::{ExtensionTypes, NoExtensionTypes, ScalarFieldType, SourceFile}, @@ -90,6 +93,15 @@ impl TestApi { self.root.connection_info() } + pub fn new_engine_with_connection_strings( + &self, + connection_string: String, + shadow_database_connection_string: Option, + ) -> EngineTestApi { + self.root + .new_engine_with_connection_strings(connection_string, shadow_database_connection_string) + } + pub fn ensure_connection_validity(&mut self) -> ConnectorResult<()> { tok(self.connector.ensure_connection_validity()) } diff --git a/schema-engine/sql-migration-tests/tests/migrations/mssql/multi_schema.rs b/schema-engine/sql-migration-tests/tests/migrations/mssql/multi_schema.rs index 7b9d1f3f3346..00c74567a7ec 100644 --- a/schema-engine/sql-migration-tests/tests/migrations/mssql/multi_schema.rs +++ b/schema-engine/sql-migration-tests/tests/migrations/mssql/multi_schema.rs @@ -1179,7 +1179,6 @@ fn migration_with_shadow_database(api: TestApi) { datasource db {{ provider = "sqlserver" url = "{conn_str}" - shadowDatabaseUrl = "{shadow_str}" schemas = ["one", "two"] }} @@ -1188,6 +1187,8 @@ fn migration_with_shadow_database(api: TestApi) { }} "#}; + let mut engine = api.new_engine_with_connection_strings(conn_str.clone(), Some(shadow_str.clone())); + let namespaces = Namespaces::from_vec(&mut vec![String::from("dbo"), String::from("one"), String::from("two")]); api.raw_cmd("DROP DATABASE IF EXISTS shadow"); @@ -1218,7 +1219,8 @@ fn migration_with_shadow_database(api: TestApi) { let dir = api.create_migrations_directory(); - api.create_migration("init", &dm, &dir) + engine + .create_migration("init", &dm, &dir) .send_sync() .assert_migration_directories_count(1) .assert_migration("init", move |migration| { @@ -1270,7 +1272,8 @@ fn migration_with_shadow_database(api: TestApi) { migration.expect_contents(expected_script) }); - api.apply_migrations(&dir) + engine + .apply_migrations(&dir) .send_sync() .assert_applied_migrations(&["init"]); } diff --git a/schema-engine/sql-migration-tests/tests/migrations/postgres/multi_schema.rs b/schema-engine/sql-migration-tests/tests/migrations/postgres/multi_schema.rs index 0dc82c325e27..ea2829f7882d 100644 --- a/schema-engine/sql-migration-tests/tests/migrations/postgres/multi_schema.rs +++ b/schema-engine/sql-migration-tests/tests/migrations/postgres/multi_schema.rs @@ -1590,7 +1590,6 @@ fn migration_with_shadow_database(api: TestApi) { datasource db {{ provider = "postgresql" url = "{conn_str}" - shadowDatabaseUrl = "{shadow_str}" schemas = ["one", "two"] }} @@ -1600,6 +1599,8 @@ fn migration_with_shadow_database(api: TestApi) { }} "#}; + let mut engine = api.new_engine_with_connection_strings(conn_str.clone(), Some(shadow_str.clone())); + let namespaces = Namespaces::from_vec(&mut vec![String::from("one"), String::from("two")]); api.raw_cmd("DROP DATABASE IF EXISTS shadow"); @@ -1632,7 +1633,8 @@ fn migration_with_shadow_database(api: TestApi) { let dir = api.create_migrations_directory(); - api.create_migration("init", &dm, &dir) + engine + .create_migration("init", &dm, &dir) .send_sync() .assert_migration_directories_count(1) .assert_migration("init", move |migration| { @@ -1669,7 +1671,8 @@ fn migration_with_shadow_database(api: TestApi) { migration.expect_contents(expected_script) }); - api.apply_migrations(&dir) + engine + .apply_migrations(&dir) .send_sync() .assert_applied_migrations(&["init"]); } diff --git a/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_directurl.prisma b/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_directurl.prisma deleted file mode 100644 index 2bb193bd4eda..000000000000 --- a/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_directurl.prisma +++ /dev/null @@ -1,21 +0,0 @@ -// tags=postgres -// exclude=cockroachdb - -datasource testds { - provider = "postgresql" - url = "bad url" - directUrl = env("TEST_DATABASE_URL") -} - -model table { - id String @id - hereBeDragons String @default(dbgenerated()) -} -// Expected Migration: -// -- CreateTable -// CREATE TABLE "table" ( -// "id" TEXT NOT NULL, -// "hereBeDragons" TEXT NOT NULL, -// -// CONSTRAINT "table_pkey" PRIMARY KEY ("id") -// ); From a06eb11979a320a0728d700b06c5afd921691d3a Mon Sep 17 00:00:00 2001 From: "Oleksii (Alexey) Orlenko" Date: Tue, 28 Oct 2025 09:44:45 +0100 Subject: [PATCH 02/23] chore: fix building and testing with `next` branch of prisma (#5667) - Add missing `@prisma/client-runtime-utils` package added on the `next` branch in `prisma/prisma` to the pnpm workspace. - Adapt to breaking changes in the exported driver adapter APIs. /prisma-branch next --- .../executor/src/driver-adapters-manager/better-sqlite3.ts | 4 ++-- .../executor/src/driver-adapters-manager/libsql.ts | 4 ++-- libs/driver-adapters/pnpm-workspace.yaml | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/driver-adapters/executor/src/driver-adapters-manager/better-sqlite3.ts b/libs/driver-adapters/executor/src/driver-adapters-manager/better-sqlite3.ts index 820abe8c1aa1..4b20517387a1 100644 --- a/libs/driver-adapters/executor/src/driver-adapters-manager/better-sqlite3.ts +++ b/libs/driver-adapters/executor/src/driver-adapters-manager/better-sqlite3.ts @@ -1,4 +1,4 @@ -import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3' +import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3' import type { SqlDriverAdapter, SqlMigrationAwareDriverAdapterFactory, @@ -20,7 +20,7 @@ export class BetterSQLite3Manager implements DriverAdaptersManager { private env: EnvForAdapter, { url }: SetupDriverAdaptersInput, ) { - this.#factory = new PrismaBetterSQLite3({ + this.#factory = new PrismaBetterSqlite3({ url, }) } diff --git a/libs/driver-adapters/executor/src/driver-adapters-manager/libsql.ts b/libs/driver-adapters/executor/src/driver-adapters-manager/libsql.ts index 2ebbfe9d9837..fd24409eeed5 100644 --- a/libs/driver-adapters/executor/src/driver-adapters-manager/libsql.ts +++ b/libs/driver-adapters/executor/src/driver-adapters-manager/libsql.ts @@ -1,4 +1,4 @@ -import { PrismaLibSQL } from '@prisma/adapter-libsql' +import { PrismaLibSql } from '@prisma/adapter-libsql' import type { SqlDriverAdapter, SqlMigrationAwareDriverAdapterFactory, @@ -20,7 +20,7 @@ export class LibSQLManager implements DriverAdaptersManager { private env: EnvForAdapter, { url }: SetupDriverAdaptersInput, ) { - this.#factory = new PrismaLibSQL({ + this.#factory = new PrismaLibSql({ url, intMode: 'bigint', }) diff --git a/libs/driver-adapters/pnpm-workspace.yaml b/libs/driver-adapters/pnpm-workspace.yaml index a5e4d6ed64cf..8abd23361327 100644 --- a/libs/driver-adapters/pnpm-workspace.yaml +++ b/libs/driver-adapters/pnpm-workspace.yaml @@ -9,6 +9,7 @@ packages: - '../../../prisma/packages/adapter-mariadb' - '../../../prisma/packages/bundled-js-drivers' - '../../../prisma/packages/client-engine-runtime' + - '../../../prisma/packages/client-runtime-utils' - '../../../prisma/packages/debug' - '../../../prisma/packages/driver-adapter-utils' - './executor' From 5c757101e403a317210e72e8b58e4103930cfc82 Mon Sep 17 00:00:00 2001 From: "Oleksii (Alexey) Orlenko" Date: Tue, 28 Oct 2025 14:37:38 +0100 Subject: [PATCH 03/23] feat!: remove deprecated `metrics` preview feature (#5665) Ref: https://linear.app/prisma-company/issue/TML-1300/remove-metrics-preview-feature /prisma-branch next --- AGENTS.md | 3 +- Cargo.lock | 127 ---- Cargo.toml | 5 - Makefile | 9 +- README.md | 7 - docker-compose.yml | 16 - libs/driver-adapters/Cargo.toml | 1 - .../executor/src/engines/Library.ts | 1 - libs/driver-adapters/src/proxy.rs | 6 - libs/driver-adapters/src/queryable.rs | 8 +- libs/driver-adapters/src/transaction.rs | 16 +- libs/metrics/Cargo.toml | 22 - libs/metrics/src/common.rs | 137 ----- libs/metrics/src/formatters.rs | 152 ----- libs/metrics/src/guards.rs | 31 - libs/metrics/src/instrument.rs | 83 --- libs/metrics/src/lib.rs | 556 ------------------ libs/metrics/src/recorder.rs | 190 ------ libs/metrics/src/registry.rs | 226 ------- libs/query-engine-common/Cargo.toml | 1 - libs/query-engine-common/src/engine.rs | 1 - metrics/grafana/dashboards/dashboards.json | 451 -------------- metrics/grafana/dashboards/prometheus.yml | 22 - metrics/grafana/datasources/all.yml | 9 - metrics/prometheus/prometheus.yml | 9 - psl/psl-core/src/common/preview_features.rs | 8 +- psl/psl/tests/config/generators.rs | 2 +- .../preview_features/metrics.prisma | 16 - .../mysql.prisma | 2 +- quaint/Cargo.toml | 1 - quaint/src/connector.rs | 4 +- quaint/src/connector/mssql/native/mod.rs | 8 +- quaint/src/connector/mysql/native/mod.rs | 8 +- quaint/src/connector/postgres/native/mod.rs | 146 ++--- .../connector/postgres/native/websocket.rs | 4 +- quaint/src/connector/sqlite/native/mod.rs | 8 +- quaint/src/connector/{metrics.rs => trace.rs} | 12 +- quaint/src/connector/transaction.rs | 9 +- quaint/src/pooled.rs | 4 +- quaint/src/pooled/manager.rs | 3 +- query-engine/black-box-tests/Cargo.toml | 1 - query-engine/black-box-tests/README.md | 3 +- .../black-box-tests/tests/black_box_tests.rs | 2 - .../black-box-tests/tests/metrics/mod.rs | 1 - .../tests/metrics/smoke_tests.rs | 130 ---- .../query-engine-tests/Cargo.toml | 1 - .../query-engine-tests/src/utils/metrics.rs | 23 - .../query-engine-tests/src/utils/mod.rs | 1 - .../query-engine-tests/tests/new/metrics.rs | 87 --- .../query-engine-tests/tests/new/mod.rs | 1 - .../tests/new/regressions/prisma_15607.rs | 18 +- .../writes/top_level_mutations/create_many.rs | 57 +- .../create_many_and_return.rs | 71 +-- .../query-tests-setup/Cargo.toml | 3 +- .../query-tests-setup/src/lib.rs | 15 +- .../query-tests-setup/src/runner/mod.rs | 8 - .../mongodb-query-connector/Cargo.toml | 1 - .../src/interface/transaction.rs | 11 +- .../src/root_queries/mod.rs | 9 +- query-engine/core-tests/Cargo.toml | 2 +- query-engine/core/Cargo.toml | 2 - .../core/src/executor/execute_operation.rs | 38 +- .../src/interactive_transactions/manager.rs | 6 - query-engine/core/src/lib.rs | 1 - query-engine/core/src/metrics.rs | 13 - query-engine/query-engine-c-abi/src/engine.rs | 1 - query-engine/query-engine-c-abi/src/logger.rs | 2 +- query-engine/query-engine-node-api/Cargo.toml | 3 +- .../query-engine-node-api/src/engine.rs | 70 +-- .../query-engine-node-api/src/logger.rs | 31 +- .../query-engine-wasm/src/wasm/engine.rs | 5 - .../query-engine-wasm/src/wasm/logger.rs | 2 +- query-engine/query-engine/Cargo.toml | 3 +- query-engine/query-engine/src/cli.rs | 1 - query-engine/query-engine/src/context.rs | 24 +- query-engine/query-engine/src/features.rs | 8 - query-engine/query-engine/src/opt.rs | 9 - query-engine/query-engine/src/server/mod.rs | 33 -- query-engine/query-engine/src/tests/dmmf.rs | 2 - query-engine/query-engine/src/tests/errors.rs | 1 - query-engine/request-handlers/Cargo.toml | 1 - 81 files changed, 192 insertions(+), 2833 deletions(-) delete mode 100644 libs/metrics/Cargo.toml delete mode 100644 libs/metrics/src/common.rs delete mode 100644 libs/metrics/src/formatters.rs delete mode 100644 libs/metrics/src/guards.rs delete mode 100644 libs/metrics/src/instrument.rs delete mode 100644 libs/metrics/src/lib.rs delete mode 100644 libs/metrics/src/recorder.rs delete mode 100644 libs/metrics/src/registry.rs delete mode 100644 metrics/grafana/dashboards/dashboards.json delete mode 100644 metrics/grafana/dashboards/prometheus.yml delete mode 100644 metrics/grafana/datasources/all.yml delete mode 100644 metrics/prometheus/prometheus.yml delete mode 100644 psl/psl/tests/validation/preview_features/metrics.prisma rename quaint/src/connector/{metrics.rs => trace.rs} (81%) delete mode 100644 query-engine/black-box-tests/tests/metrics/mod.rs delete mode 100644 query-engine/black-box-tests/tests/metrics/smoke_tests.rs delete mode 100644 query-engine/connector-test-kit-rs/query-engine-tests/src/utils/metrics.rs delete mode 100644 query-engine/connector-test-kit-rs/query-engine-tests/tests/new/metrics.rs delete mode 100644 query-engine/core/src/metrics.rs diff --git a/AGENTS.md b/AGENTS.md index 60a09d621287..165218467ecd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -20,11 +20,10 @@ Key directories: - `psl/` – Prisma Schema Language parser, validator, config tooling. - `schema-engine/` – Migration/introspection engine plus test suites. - `prisma-fmt/` – Language server & formatter entry point (tests rely on `expect!` snapshots). -- `libs/` – Shared libraries (metrics, value types, test setup). - `schema-engine/sql-migration-tests` / `sql-introspection-tests` – Heavy integration suites (require DBs). - `query-engine/` – Legacy query execution stack (Rust). - `query-compiler/` – New query planner + associated WASM + playground. -- `libs/` – Shared libraries (metrics, value types, driver adapters, test setup). +- `libs/` – Shared libraries (value types, driver adapters, test setup). - `driver-adapters/` – Rust-side adapter utilities for the new query interpreter. Supporting infra: diff --git a/Cargo.lock b/Cargo.lock index 8d4b8ec54edc..6c8037347ede 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,7 +343,6 @@ dependencies = [ "enumflags2", "indoc", "insta", - "prisma-metrics", "query-engine-tests", "query-tests-setup", "regex", @@ -1213,7 +1212,6 @@ dependencies = [ "napi-derive", "panic-utils", "pin-project", - "prisma-metrics", "quaint", "serde", "serde-wasm-bindgen", @@ -1252,12 +1250,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "endian-type" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" - [[package]] name = "enum-as-inner" version = "0.6.0" @@ -2603,39 +2595,6 @@ dependencies = [ "portable-atomic", ] -[[package]] -name = "metrics-exporter-prometheus" -version = "0.15.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4f0c8427b39666bf970460908b213ec09b3b350f20c0c2eabcbba51704a08e6" -dependencies = [ - "base64 0.22.1", - "indexmap 2.10.0", - "metrics", - "metrics-util", - "quanta", - "thiserror 1.0.69", -] - -[[package]] -name = "metrics-util" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4259040465c955f9f2f1a4a8a16dc46726169bca0f88e8fb2dbeced487c3e828" -dependencies = [ - "aho-corasick", - "crossbeam-epoch", - "crossbeam-utils", - "hashbrown 0.14.5", - "indexmap 2.10.0", - "metrics", - "num_cpus", - "ordered-float", - "quanta", - "radix_trie", - "sketches-ddsketch", -] - [[package]] name = "mime" version = "0.3.17" @@ -2793,7 +2752,6 @@ dependencies = [ "mongodb", "mongodb-client", "pretty_assertions", - "prisma-metrics", "prisma-value", "psl", "query-connector", @@ -3015,15 +2973,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "nibble_vec" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" -dependencies = [ - "smallvec", -] - [[package]] name = "nom" version = "7.1.3" @@ -3224,15 +3173,6 @@ dependencies = [ "url", ] -[[package]] -name = "ordered-float" -version = "4.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d501f1a72f71d3c063a6bbc8f7271fa73aa09fe5d6283b6571e2ed176a2537" -dependencies = [ - "num-traits", -] - [[package]] name = "os_str_bytes" version = "6.5.1" @@ -3602,26 +3542,6 @@ dependencies = [ "structopt", ] -[[package]] -name = "prisma-metrics" -version = "0.1.0" -dependencies = [ - "derive_more", - "expect-test", - "futures", - "metrics", - "metrics-exporter-prometheus", - "metrics-util", - "parking_lot", - "pin-project", - "serde", - "serde_json", - "tokio", - "tracing", - "tracing-futures", - "tracing-subscriber", -] - [[package]] name = "prisma-schema-build" version = "0.1.0" @@ -3797,7 +3717,6 @@ dependencies = [ "pin-project", "postgres-native-tls", "postgres-types", - "prisma-metrics", "quaint-test-macros", "quaint-test-setup", "query-template", @@ -3842,21 +3761,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "quanta" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5" -dependencies = [ - "crossbeam-utils", - "libc", - "once_cell", - "raw-cpuid", - "wasi 0.11.0+wasi-snapshot-preview1", - "web-sys", - "winapi", -] - [[package]] name = "query-builder" version = "0.1.0" @@ -3962,7 +3866,6 @@ dependencies = [ "itertools 0.13.0", "lru 0.7.8", "petgraph", - "prisma-metrics", "psl", "query-builder", "query-connector", @@ -3999,7 +3902,6 @@ dependencies = [ "indoc", "mongodb-query-connector", "panic-utils", - "prisma-metrics", "psl", "quaint", "query-connector", @@ -4061,7 +3963,6 @@ dependencies = [ "async-trait", "connection-string", "napi", - "prisma-metrics", "psl", "query-connector", "query-core", @@ -4091,7 +3992,6 @@ dependencies = [ "napi", "napi-build", "napi-derive", - "prisma-metrics", "psl", "quaint", "query-compiler", @@ -4128,7 +4028,6 @@ dependencies = [ "insta", "itertools 0.13.0", "paste", - "prisma-metrics", "prisma-value", "psl", "query-test-macros", @@ -4232,7 +4131,6 @@ dependencies = [ "nom", "panic-utils", "parse-hyperlinks", - "prisma-metrics", "psl", "qe-setup", "quaint", @@ -4282,16 +4180,6 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" -[[package]] -name = "radix_trie" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" -dependencies = [ - "endian-type", - "nibble_vec", -] - [[package]] name = "rand" version = "0.3.23" @@ -4430,15 +4318,6 @@ dependencies = [ "rand_core 0.5.1", ] -[[package]] -name = "raw-cpuid" -version = "11.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ab240315c661615f2ee9f0f2cd32d5a7343a84d5ebcccb99d46e6637565e7b0" -dependencies = [ - "bitflags 2.9.1", -] - [[package]] name = "rayon" version = "1.7.0" @@ -5257,12 +5136,6 @@ version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" -[[package]] -name = "sketches-ddsketch" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85636c14b73d81f541e525f585c0a2109e6744e1565b5c1668e31c70c10ed65c" - [[package]] name = "slab" version = "0.4.8" diff --git a/Cargo.toml b/Cargo.toml index 7b118c188139..0bf573ca8190 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,9 +86,6 @@ log = "0.4" lru = "0.7" lru-cache = "0.1" lsp-types = "0.95" -metrics = "0.23.0" -metrics-util = "0.17.0" -metrics-exporter-prometheus = { version = "0.15.3", default-features = false } mobc = "0.8" mongodb = { git = "https://github.com/prisma/mongo-rust-driver.git", branch = "RUST-1994/happy-eyeballs", features = [ "zstd-compression", @@ -107,7 +104,6 @@ napi-derive = "2" native-tls = "0.2" nom = "7" num_cpus = "1" -parking_lot = "0.12" parse-hyperlinks = "0.23" paste = "1" percent-encoding = "2" @@ -193,7 +189,6 @@ prisma-fmt.path = "./prisma-fmt" build-utils.path = "./libs/build-utils" crosstarget-utils.path = "./libs/crosstarget-utils" panic-utils.path = "./libs/panic-utils" -prisma-metrics.path = "./libs/metrics" prisma-value.path = "./libs/prisma-value" telemetry.path = "./libs/telemetry" user-facing-errors.path = "./libs/user-facing-errors" diff --git a/Makefile b/Makefile index a7c7eca45f2d..f0ee62743d4f 100644 --- a/Makefile +++ b/Makefile @@ -145,7 +145,7 @@ test-qe-st: test-qe-verbose-st: cargo test --package query-engine-tests -- --nocapture --test-threads 1 -# Black-box tests, exercising the query engine HTTP apis (metrics, tracing, etc) +# Black-box tests, exercising the query engine HTTP apis test-qe-black-box: build-qe cargo test --package black-box-tests -- --test-threads 1 @@ -552,10 +552,10 @@ ensure-prisma-present: fi; qe: - cargo run --bin query-engine -- --engine-protocol json --enable-raw-queries --enable-metrics --enable-open-telemetry --enable-telemetry-in-response + cargo run --bin query-engine -- --engine-protocol json --enable-raw-queries --enable-open-telemetry --enable-telemetry-in-response qe-graphql: - cargo run --bin query-engine -- --engine-protocol graphql --enable-playground --enable-raw-queries --enable-metrics --enable-open-telemetry --enable-telemetry-in-response + cargo run --bin query-engine -- --engine-protocol graphql --enable-playground --enable-raw-queries --enable-open-telemetry --enable-telemetry-in-response qe-dmmf: cargo run --bin query-engine -- cli dmmf > dmmf.json @@ -563,9 +563,6 @@ qe-dmmf: qe-dev-mongo_4_4: start-mongodb_4_4 cp $(SCHEMA_EXAMPLES_PATH)/generic_mongo4.prisma $(DEV_SCHEMA_FILE) -show-metrics: - docker compose -f docker-compose.yml up --wait -d --remove-orphans grafana prometheus - ## OpenTelemetry otel: docker compose up --remove-orphans -d otel diff --git a/README.md b/README.md index 4d17304de69d..256b190d7737 100644 --- a/README.md +++ b/README.md @@ -145,13 +145,6 @@ whatever command that starts with `./query-engine` with `cargo run --bin query-e You can also pass `--help` to find out more options to run the engine. -### Metrics - -Running `make show-metrics` will start Prometheus and Grafana with a default metrics dashboard. -Prometheus will scrape the `/metrics` endpoint to collect the engine's metrics - -Navigate to `http://localhost:3000` to view the Grafana dashboard. - ## Schema Engine The _Schema Engine_ does a couple of things: diff --git a/docker-compose.yml b/docker-compose.yml index c4574ccc61e0..d9978ebce6a4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -477,21 +477,5 @@ services: - 14269:14269 - 9411:9411 - prometheus: - image: prom/prometheus - restart: unless-stopped - volumes: - - ${PWD}/metrics/prometheus:/prometheus-data - command: --config.file=/prometheus-data/prometheus.yml - ports: - - 9090:9090 - grafana: - image: grafana/grafana - volumes: - - ${PWD}/metrics/grafana/datasources:/etc/grafana/provisioning/datasources/ - - ${PWD}/metrics/grafana/dashboards:/etc/grafana/provisioning/dashboards/ - ports: - - 3000:3000 - networks: databases: null diff --git a/libs/driver-adapters/Cargo.toml b/libs/driver-adapters/Cargo.toml index 3448588f7d1f..9c1d3c5faeca 100644 --- a/libs/driver-adapters/Cargo.toml +++ b/libs/driver-adapters/Cargo.toml @@ -16,7 +16,6 @@ workspace = true async-trait.workspace = true futures.workspace = true panic-utils.workspace = true -prisma-metrics.workspace = true serde.workspace = true serde_json.workspace = true telemetry.workspace = true diff --git a/libs/driver-adapters/executor/src/engines/Library.ts b/libs/driver-adapters/executor/src/engines/Library.ts index 61bf57495b91..95100f58ff24 100644 --- a/libs/driver-adapters/executor/src/engines/Library.ts +++ b/libs/driver-adapters/executor/src/engines/Library.ts @@ -31,7 +31,6 @@ export type QueryEngineInstance = { traceHeaders: string, requestId: string, ): Promise - metrics(options: string): Promise } export interface QueryEngineConstructor { diff --git a/libs/driver-adapters/src/proxy.rs b/libs/driver-adapters/src/proxy.rs index 0c0818dd7e02..d7a0daa97cfe 100644 --- a/libs/driver-adapters/src/proxy.rs +++ b/libs/driver-adapters/src/proxy.rs @@ -8,7 +8,6 @@ use crate::{ use crate::{conversion::MaybeDefined, queryable::JsQueryable}; use futures::Future; -use prisma_metrics::gauge; use quaint::connector::{AdapterName, AdapterProvider, IsolationLevel}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -161,11 +160,6 @@ impl DriverProxy { .call_as_async(isolation.map(|lvl| lvl.to_string()).into()) .await?; - // Decrement for this gauge is done in JsTransaction::commit/JsTransaction::rollback - // Previously, it was done in JsTransaction::new, similar to the native Transaction. - // However, correct Dispatcher is lost there and increment does not register, so we moved - // it here instead. - gauge!("prisma_client_queries_active").increment(1.0); Ok(Box::new(tx)) } diff --git a/libs/driver-adapters/src/queryable.rs b/libs/driver-adapters/src/queryable.rs index e6af427bfd4c..be4cc6f4d074 100644 --- a/libs/driver-adapters/src/queryable.rs +++ b/libs/driver-adapters/src/queryable.rs @@ -8,7 +8,7 @@ use async_trait::async_trait; use futures::Future; use quaint::connector::{AdapterName, DescribedQuery, ExternalConnectionInfo, ExternalConnector}; use quaint::{ - connector::{IsolationLevel, Transaction, metrics}, + connector::{IsolationLevel, Transaction, trace}, prelude::{Query as QuaintQuery, Queryable as QuaintQueryable, ResultSet, TransactionCapable}, visitor::{self, Visitor}, }; @@ -97,7 +97,7 @@ impl QuaintQueryable for JsBaseQueryable { } async fn query_raw(&self, sql: &str, params: &[quaint::Value<'_>]) -> quaint::Result { - metrics::query("js.query_raw", self.db_system_name, sql, params, move || async move { + trace::query(self.db_system_name, sql, params, move || async move { self.do_query_raw(sql, params).await }) .await @@ -117,7 +117,7 @@ impl QuaintQueryable for JsBaseQueryable { } async fn execute_raw(&self, sql: &str, params: &[quaint::Value<'_>]) -> quaint::Result { - metrics::query("js.execute_raw", self.db_system_name, sql, params, move || async move { + trace::query(self.db_system_name, sql, params, move || async move { self.do_execute_raw(sql, params).await }) .await @@ -129,7 +129,7 @@ impl QuaintQueryable for JsBaseQueryable { async fn raw_cmd(&self, cmd: &str) -> quaint::Result<()> { let params = &[]; - metrics::query("js.raw_cmd", self.db_system_name, cmd, params, move || async move { + trace::query(self.db_system_name, cmd, params, move || async move { self.do_execute_raw(cmd, params).await?; Ok(()) }) diff --git a/libs/driver-adapters/src/transaction.rs b/libs/driver-adapters/src/transaction.rs index 61387ea910c1..60b88de1840c 100644 --- a/libs/driver-adapters/src/transaction.rs +++ b/libs/driver-adapters/src/transaction.rs @@ -1,5 +1,4 @@ use async_trait::async_trait; -use prisma_metrics::gauge; use quaint::{ Value, connector::{DescribedQuery, IsolationLevel, Transaction as QuaintTransaction}, @@ -29,23 +28,13 @@ impl JsTransaction { pub async fn raw_phantom_cmd(&self, cmd: &str) -> quaint::Result<()> { let params = &[]; - quaint::connector::metrics::query( - "js.raw_phantom_cmd", - self.inner.db_system_name, - cmd, - params, - move || async move { Ok(()) }, - ) - .await + quaint::connector::trace::query(self.inner.db_system_name, cmd, params, move || async move { Ok(()) }).await } } #[async_trait] impl QuaintTransaction for JsTransaction { async fn commit(&self) -> quaint::Result<()> { - // increment of this gauge is done in DriverProxy::startTransaction - gauge!("prisma_client_queries_active").decrement(1.0); - let commit_stmt = "COMMIT"; if self.options().use_phantom_query { @@ -59,9 +48,6 @@ impl QuaintTransaction for JsTransaction { } async fn rollback(&self) -> quaint::Result<()> { - // increment of this gauge is done in DriverProxy::startTransaction - gauge!("prisma_client_queries_active").decrement(1.0); - let rollback_stmt = "ROLLBACK"; if self.options().use_phantom_query { diff --git a/libs/metrics/Cargo.toml b/libs/metrics/Cargo.toml deleted file mode 100644 index 2674e70fe381..000000000000 --- a/libs/metrics/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "prisma-metrics" -version = "0.1.0" -edition.workspace = true - -[dependencies] -futures.workspace = true -derive_more.workspace = true -metrics.workspace = true -metrics-util.workspace = true -metrics-exporter-prometheus.workspace = true -serde.workspace = true -serde_json.workspace = true -tracing.workspace = true -tracing-futures.workspace = true -tracing-subscriber.workspace = true -parking_lot.workspace = true -pin-project.workspace = true - -[dev-dependencies] -expect-test.workspace = true -tokio = { workspace = true, features = ["rt-multi-thread"] } diff --git a/libs/metrics/src/common.rs b/libs/metrics/src/common.rs deleted file mode 100644 index 92c76ffba962..000000000000 --- a/libs/metrics/src/common.rs +++ /dev/null @@ -1,137 +0,0 @@ -use std::collections::HashMap; - -use metrics::{Key, Label}; -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize)] -pub(crate) struct KeyLabels { - name: String, - labels: HashMap, -} - -#[derive(Debug)] -pub(crate) enum MetricType { - Counter, - Gauge, - Histogram, // Histograms are cumulative - Description, -} - -#[derive(Debug)] -pub(crate) enum MetricAction { - Increment(u64), - Absolute(u64), - GaugeSet(f64), - GaugeInc(f64), - GaugeDec(f64), - HistRecord(f64), - Description(String), -} - -#[derive(Serialize, Clone)] -pub(crate) struct Histogram { - pub buckets: Vec<(f64, u64)>, - pub sum: f64, - pub count: u64, -} - -#[derive(Serialize, Clone)] -#[serde(untagged)] -pub(crate) enum MetricValue { - Counter(u64), - Gauge(f64), - Histogram(Histogram), -} - -#[derive(Serialize, Clone)] -pub(crate) struct Metric { - pub key: String, - pub labels: HashMap, - pub value: MetricValue, - pub description: String, -} - -impl Metric { - pub(crate) fn renamed( - key: Key, - descriptions: &HashMap, - value: MetricValue, - global_labels: &HashMap, - ) -> Self { - match crate::METRIC_RENAMES.get(key.name()) { - Some((new_key, new_description)) => Self::new( - Key::from_parts(new_key.to_string(), key.labels()), - new_description.to_string(), - value, - global_labels.clone(), - ), - None => { - let description = descriptions.get(key.name()).map(|s| s.to_string()).unwrap_or_default(); - Self::new(key, description, value, global_labels.clone()) - } - } - } - - fn new(key: Key, description: String, value: MetricValue, global_labels: HashMap) -> Self { - let (name, labels) = key.into_parts(); - - let mut labels_map: HashMap = labels - .into_iter() - .map(|label| (label.key().to_string(), label.value().to_string())) - .collect(); - - labels_map.extend(global_labels); - - Self { - key: name.as_str().to_string(), - value, - description, - labels: labels_map, - } - } -} - -// The idea of this snapshot is take from -// https://github.com/metrics-rs/metrics/blob/558a3f93a4bb3958379ae6227c613a222aa813b5/metrics-exporter-prometheus/src/common.rs#L79 -#[derive(Serialize)] -pub(crate) struct Snapshot { - pub counters: Vec, - pub gauges: Vec, - pub histograms: Vec, -} - -impl From for KeyLabels { - fn from(key: Key) -> Self { - let mut kl = KeyLabels { - name: key.name().to_string(), - labels: Default::default(), - }; - - kl.labels - .extend(key.labels().map(|l| (l.key().to_string(), l.value().to_string()))); - - kl - } -} - -impl From for Key { - fn from(kl: KeyLabels) -> Self { - let labels: Vec