Skip to content

Commit

Permalink
🐛 ensure apply dry-run works using http engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Jul 17, 2023
1 parent 11c1c53 commit f5f35dc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/surrealdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -103,13 +103,16 @@ pub async fn apply_in_transaction<C: Connection>(
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(())
Expand All @@ -118,6 +121,7 @@ pub async fn apply_in_transaction<C: Connection>(
}
}
TransactionAction::Commit => {
let response = response_result?;
response.check()?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/apply/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
);";

Expand Down
39 changes: 39 additions & 0 deletions tests/cli/apply/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
10 changes: 5 additions & 5 deletions tests/cli/create/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);",
);
Expand All @@ -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",
);
Expand Down Expand Up @@ -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
);",
);
Expand Down Expand Up @@ -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
);",
);
Expand Down Expand Up @@ -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
);",
);
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit f5f35dc

Please sign in to comment.