Skip to content

Commit

Permalink
feat(schema-engine): add back getDatabaseVersion RPC support
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Jul 14, 2023
1 parent 0b746fe commit 29f98c5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
24 changes: 24 additions & 0 deletions schema-engine/cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "{}", &params).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();
Expand Down
2 changes: 1 addition & 1 deletion schema-engine/core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>;
async fn version(&self, params: GetDatabaseVersionInput) -> CoreResult<String>;

/// Apply all the unapplied migrations from the migrations folder.
async fn apply_migrations(&self, input: ApplyMigrationsInput) -> CoreResult<ApplyMigrationsOutput>;
Expand Down
2 changes: 1 addition & 1 deletion schema-engine/core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions schema-engine/core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ impl EngineState {

#[async_trait::async_trait]
impl GenericApi for EngineState {
async fn version(&self) -> CoreResult<String> {
self.with_default_connector(Box::new(|connector| connector.version()))
async fn version(&self, params: GetDatabaseVersionInput) -> CoreResult<String> {
self.with_connector_from_datasource_param(&params.datasource, Box::new(|connector| connector.version()))
.await
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ requestShape = "getDatabaseVersionInput"
responseShape = "getDatabaseVersionOutput"

[recordShapes.getDatabaseVersionInput]
fields.datasource.shape = "DatasourceParam"

[recordShapes.getDatabaseVersionOutput.fields.version]
shape = "string"

0 comments on commit 29f98c5

Please sign in to comment.