From 29f98c51f61dc797cf55fc51d1061394968f7389 Mon Sep 17 00:00:00 2001 From: jkomyno Date: Fri, 14 Jul 2023 20:38:14 +0200 Subject: [PATCH] feat(schema-engine): add back getDatabaseVersion RPC support --- schema-engine/cli/tests/cli_tests.rs | 24 +++++++++++++++++++ schema-engine/core/src/api.rs | 2 +- schema-engine/core/src/rpc.rs | 2 +- schema-engine/core/src/state.rs | 4 ++-- .../methods/getDatabaseVersion.toml | 1 + 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/schema-engine/cli/tests/cli_tests.rs b/schema-engine/cli/tests/cli_tests.rs index 9c293818e0a0..96ba205fc1a1 100644 --- a/schema-engine/cli/tests/cli_tests.rs +++ b/schema-engine/cli/tests/cli_tests.rs @@ -334,6 +334,30 @@ fn basic_jsonrpc_roundtrip_works(_api: TestApi) { }); } +#[test_connector(tags(Postgres))] +fn basic_jsonrpc_roundtrip_works_with_dynamic_url(_api: TestApi) { + let url = std::env::var("TEST_DATABASE_URL").unwrap(); + let command = Command::new(schema_engine_bin_path()); + + let params = format!( + r#"{{ "jsonrpc": "2.0", "method": "getDatabaseVersion", "params": {{ "datasource": {{ "tag": "ConnectionString", "url": "{url}" }} }}, "id": 1 }}"# + ); + + with_child_process(command, |process| { + let stdin = process.stdin.as_mut().unwrap(); + let mut stdout = BufReader::new(process.stdout.as_mut().unwrap()); + + for _ in 0..2 { + writeln!(stdin, "{}", ¶ms).unwrap(); + + let mut response = String::new(); + stdout.read_line(&mut response).unwrap(); + + assert!(response.contains("PostgreSQL") || response.contains("CockroachDB")); + } + }); +} + #[test] fn introspect_sqlite_empty_database() { let tmpdir = tempfile::tempdir().unwrap(); diff --git a/schema-engine/core/src/api.rs b/schema-engine/core/src/api.rs index 4148ef3ae518..b9e4ee39bbff 100644 --- a/schema-engine/core/src/api.rs +++ b/schema-engine/core/src/api.rs @@ -6,7 +6,7 @@ use crate::{commands, json_rpc::types::*, CoreResult}; #[async_trait::async_trait] pub trait GenericApi: Send + Sync + 'static { /// Return the database version as a string. - async fn version(&self) -> CoreResult; + async fn version(&self, params: GetDatabaseVersionInput) -> CoreResult; /// Apply all the unapplied migrations from the migrations folder. async fn apply_migrations(&self, input: ApplyMigrationsInput) -> CoreResult; diff --git a/schema-engine/core/src/rpc.rs b/schema-engine/core/src/rpc.rs index 57a24508fd9b..326f7328bf6d 100644 --- a/schema-engine/core/src/rpc.rs +++ b/schema-engine/core/src/rpc.rs @@ -35,7 +35,7 @@ async fn run_command( DIAGNOSE_MIGRATION_HISTORY => render(executor.diagnose_migration_history(params.parse()?).await), ENSURE_CONNECTION_VALIDITY => render(executor.ensure_connection_validity(params.parse()?).await), EVALUATE_DATA_LOSS => render(executor.evaluate_data_loss(params.parse()?).await), - GET_DATABASE_VERSION => render(executor.version().await), + GET_DATABASE_VERSION => render(executor.version(params.parse()?).await), INTROSPECT => render(executor.introspect(params.parse()?).await), LIST_MIGRATION_DIRECTORIES => render(executor.list_migration_directories(params.parse()?).await), MARK_MIGRATION_APPLIED => render(executor.mark_migration_applied(params.parse()?).await), diff --git a/schema-engine/core/src/state.rs b/schema-engine/core/src/state.rs index 0b98db0a7b28..944ab4d1d037 100644 --- a/schema-engine/core/src/state.rs +++ b/schema-engine/core/src/state.rs @@ -183,8 +183,8 @@ impl EngineState { #[async_trait::async_trait] impl GenericApi for EngineState { - async fn version(&self) -> CoreResult { - self.with_default_connector(Box::new(|connector| connector.version())) + async fn version(&self, params: GetDatabaseVersionInput) -> CoreResult { + self.with_connector_from_datasource_param(¶ms.datasource, Box::new(|connector| connector.version())) .await } diff --git a/schema-engine/json-rpc-api-build/methods/getDatabaseVersion.toml b/schema-engine/json-rpc-api-build/methods/getDatabaseVersion.toml index c64e5b9f6d16..c1955e442fa8 100644 --- a/schema-engine/json-rpc-api-build/methods/getDatabaseVersion.toml +++ b/schema-engine/json-rpc-api-build/methods/getDatabaseVersion.toml @@ -4,6 +4,7 @@ requestShape = "getDatabaseVersionInput" responseShape = "getDatabaseVersionOutput" [recordShapes.getDatabaseVersionInput] +fields.datasource.shape = "DatasourceParam" [recordShapes.getDatabaseVersionOutput.fields.version] shape = "string"