From f5f35dc1652133f6d741db3bf4bdbb34a197f6e5 Mon Sep 17 00:00:00 2001 From: Odonno Date: Mon, 17 Jul 2023 22:12:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20ensure=20apply=20dry-run=20works?= =?UTF-8?q?=20using=20http=20engine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/surrealdb.rs | 14 +++++++++----- tests/cli/apply/e2e.rs | 4 ++-- tests/cli/apply/up.rs | 39 +++++++++++++++++++++++++++++++++++++++ tests/cli/create/event.rs | 10 +++++----- tests/helpers/io.rs | 13 +++++++++++++ 5 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/surrealdb.rs b/src/surrealdb.rs index f57e3c9..2a455e6 100644 --- a/src/surrealdb.rs +++ b/src/surrealdb.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Context, Result}; use surrealdb::{ engine::any::{connect, Any}, opt::auth::Root, - Connection, Surreal, + Connection, Response, Surreal, }; use crate::{ @@ -103,13 +103,16 @@ pub async fn apply_in_transaction( action: TransactionAction, ) -> Result<()> { let query = format_transaction(inner_query.to_owned(), &action); - let response = client.query(query).await?; + let response_result = client.query(query).await; match action { TransactionAction::Rollback => { - let first_error = response.check().err().context("Error on rollback")?; - let is_rollback_success = first_error.to_string() - == "The query was not executed due to a cancelled transaction"; + let end_result = response_result.and_then(|response: Response| response.check()); + + let first_error = end_result.err().context("Error on rollback")?; + let is_rollback_success = first_error + .to_string() + .contains("The query was not executed due to a cancelled transaction"); if is_rollback_success { Ok(()) @@ -118,6 +121,7 @@ pub async fn apply_in_transaction( } } TransactionAction::Commit => { + let response = response_result?; response.check()?; Ok(()) } diff --git a/tests/cli/apply/e2e.rs b/tests/cli/apply/e2e.rs index 380af71..878a959 100644 --- a/tests/cli/apply/e2e.rs +++ b/tests/cli/apply/e2e.rs @@ -638,7 +638,7 @@ const INITIAL_DEFINITION_EVENTS: &str = "DEFINE TABLE publish_post SCHEMALESS DEFINE FIELD post_id ON publish_post TYPE record(post); DEFINE FIELD created_at ON publish_post TYPE datetime VALUE $before OR time::now(); -DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( UPDATE post SET status = \"PUBLISHED\" WHERE id = $after.post_id ); DEFINE TABLE unpublish_post SCHEMALESS @@ -649,7 +649,7 @@ DEFINE TABLE unpublish_post SCHEMALESS DEFINE FIELD post_id ON unpublish_post TYPE record(post); DEFINE FIELD created_at ON unpublish_post TYPE datetime VALUE $before OR time::now(); -DEFINE EVENT unpublish_post ON TABLE unpublish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT unpublish_post ON TABLE unpublish_post WHEN $event == \"CREATE\" THEN ( UPDATE post SET status = \"DRAFT\" WHERE id = $after.post_id );"; diff --git a/tests/cli/apply/up.rs b/tests/cli/apply/up.rs index 0e821f9..f9643cd 100644 --- a/tests/cli/apply/up.rs +++ b/tests/cli/apply/up.rs @@ -223,6 +223,45 @@ async fn apply_initial_migrations_in_dry_run() -> Result<()> { Ok(()) } +#[tokio::test] +async fn apply_initial_migrations_in_dry_run_should_fail() -> Result<()> { + let temp_dir = TempDir::new()?; + + scaffold_empty_template(&temp_dir)?; + add_invalid_schema_file(&temp_dir)?; + + let mut cmd = create_cmd(&temp_dir)?; + + cmd.arg("apply").arg("--dry-run"); + + cmd.assert().try_failure()?; + + Ok(()) +} + +#[tokio::test] +async fn apply_initial_migrations_in_dry_run_using_http_engine() -> Result<()> { + let temp_dir = TempDir::new()?; + + scaffold_blog_template(&temp_dir)?; + + let mut cmd = create_cmd(&temp_dir)?; + + cmd.arg("apply") + .arg("--dry-run") + .arg("--address") + .arg("http://localhost:8000"); + + cmd.assert() + .try_success() + .and_then(|assert| assert.try_stdout(""))?; + + let is_empty = is_surreal_db_empty(None, None).await?; + ensure!(is_empty, "SurrealDB should be empty"); + + Ok(()) +} + #[test] fn apply_with_inlined_down_files() -> Result<()> { let temp_dir = TempDir::new()?; diff --git a/tests/cli/create/event.rs b/tests/cli/create/event.rs index e12adf6..d80b658 100644 --- a/tests/cli/create/event.rs +++ b/tests/cli/create/event.rs @@ -29,7 +29,7 @@ fn create_event_file() -> Result<()> { DEFINE FIELD post_id ON publish_post; DEFINE FIELD created_at ON publish_post; -DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( # TODO );", ); @@ -56,7 +56,7 @@ fn create_event_file_dry_run() -> Result<()> { DEFINE FIELD post_id ON publish_post; DEFINE FIELD created_at ON publish_post; -DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( # TODO );\n", ); @@ -93,7 +93,7 @@ fn create_event_file_with_schemafull_table_from_config() -> Result<()> { DEFINE FIELD post_id ON publish_post; DEFINE FIELD created_at ON publish_post; -DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( # TODO );", ); @@ -127,7 +127,7 @@ fn create_event_file_with_schemaless_table_from_invalid_config() -> Result<()> { DEFINE FIELD post_id ON publish_post; DEFINE FIELD created_at ON publish_post; -DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( # TODO );", ); @@ -161,7 +161,7 @@ fn create_event_file_with_schemafull_table_from_cli_arg() -> Result<()> { DEFINE FIELD post_id ON publish_post; DEFINE FIELD created_at ON publish_post; -DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == "CREATE" THEN ( +DEFINE EVENT publish_post ON TABLE publish_post WHEN $event == \"CREATE\" THEN ( # TODO );", ); diff --git a/tests/helpers/io.rs b/tests/helpers/io.rs index c38e0f8..d2bb499 100644 --- a/tests/helpers/io.rs +++ b/tests/helpers/io.rs @@ -200,6 +200,19 @@ DEFINE FIELD created_at ON category TYPE datetime VALUE $before OR time::now();" Ok(()) } +pub fn add_invalid_schema_file(path: &Path) -> Result<()> { + let schemas_files_dir = path.join("schemas"); + + if schemas_files_dir.exists() { + let schema_file = schemas_files_dir.join("table.surql"); + const CONTENT: &str = "DEFINE TABLE table SCHEMANONE;"; + + fs::write(schema_file, CONTENT)?; + } + + Ok(()) +} + pub fn add_category_migration_file(path: &Path) -> Result<()> { let content = "CREATE category SET name = 'Technology'; CREATE category SET name = 'Marketing';