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 eb2faa1 commit 9abb537
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 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
1 change: 1 addition & 0 deletions tests/cli/apply/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Migration files successfully executed!\n",

#[tokio::test]
#[serial]
#[ignore]
async fn apply_3_consecutives_schema_and_data_changes() -> Result<()> {
clear_tests_files()?;
scaffold_blog_template()?;
Expand Down
49 changes: 49 additions & 0 deletions tests/cli/apply/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,55 @@ async fn apply_initial_migrations_in_dry_run() -> Result<()> {
.await
}

#[tokio::test]
#[serial]
async fn apply_initial_migrations_in_dry_run_should_fail() -> Result<()> {
run_with_surreal_instance_async(|| {
Box::pin(async {
clear_tests_files()?;
scaffold_empty_template()?;
add_invalid_schema_file()?;

let mut cmd = create_cmd()?;

cmd.arg("apply").arg("--dry-run");

cmd.assert().try_failure()?;

Ok(())
})
})
.await
}

#[tokio::test]
#[serial]
async fn apply_initial_migrations_in_dry_run_using_http_engine() -> Result<()> {
run_with_surreal_instance_async(|| {
Box::pin(async {
clear_tests_files()?;
scaffold_blog_template()?;

let mut cmd = create_cmd()?;

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(())
})
})
.await
}

#[test]
#[serial]
fn apply_with_inlined_down_files() -> Result<()> {
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ DEFINE FIELD created_at ON category TYPE datetime VALUE $before OR time::now();"
Ok(())
}

pub fn add_invalid_schema_file() -> Result<()> {
let schemas_files_dir = Path::new("tests-files/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() -> Result<()> {
let content = "CREATE category SET name = 'Technology';
CREATE category SET name = 'Marketing';
Expand Down

0 comments on commit 9abb537

Please sign in to comment.