Skip to content

Commit

Permalink
✨ display error messages from failed applied migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Dec 14, 2023
1 parent fc4007e commit 698ba25
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions src/surrealdb.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;
use color_eyre::eyre::{eyre, ContextCompat, Result};
use itertools::Itertools;
use std::collections::HashMap;
use std::path::Path;
use surrealdb::{
engine::any::{connect, Any},
opt::auth::{Jwt, Root},
Connection, Response, Surreal,
Connection, Surreal,
};

use crate::{
Expand Down Expand Up @@ -92,16 +93,17 @@ async fn set_namespace_and_database(
}

pub async fn get_surrealdb_table_exists<C: Connection>(
client: &Surreal<C>, table: &str,
)->Result<bool> {
client: &Surreal<C>,
table: &str,
) -> Result<bool> {
let tables = get_surrealdb_table_definitions(client).await?;
Ok(tables.contains_key(table))
}

type SurrealdbTableDefinitions = HashMap<String, String>;

pub async fn get_surrealdb_table_definitions<C: Connection>(
client: &Surreal<C>
client: &Surreal<C>,
) -> Result<SurrealdbTableDefinitions> {
let mut response = client.query("INFO FOR DB;").await?;

Expand All @@ -114,8 +116,7 @@ pub async fn get_surrealdb_table_definitions<C: Connection>(
pub async fn list_script_migration_ordered_by_execution_date<C: Connection>(
client: &Surreal<C>,
) -> Result<Vec<ScriptMigration>> {

if get_surrealdb_table_exists(client,SCRIPT_MIGRATION_TABLE_NAME).await? {
if get_surrealdb_table_exists(client, SCRIPT_MIGRATION_TABLE_NAME).await? {
let mut result = list_script_migration(client).await?;
result.sort_by_key(|m| m.executed_at.clone());
Ok(result)
Expand All @@ -139,7 +140,7 @@ pub async fn apply_in_transaction<C: Connection>(

match action {
TransactionAction::Rollback => {
let end_result = response_result.and_then(|response: Response| response.check());
let end_result = response_result.and_then(|response| response.check());

let first_error = end_result.err().context("Error on rollback")?;
let is_rollback_success = first_error
Expand All @@ -153,8 +154,37 @@ pub async fn apply_in_transaction<C: Connection>(
}
}
TransactionAction::Commit => {
let response = response_result?;
response.check()?;
let mut response = response_result?;

let errors = response.take_errors();
if !errors.is_empty() {
const FAILED_TRANSACTION_ERROR: &str =
"The query was not executed due to a failed transaction";

let is_failed_transaction = errors
.values()
.any(|e| e.to_string() == FAILED_TRANSACTION_ERROR);

let initial_error_messages = match is_failed_transaction {
true => {
vec![FAILED_TRANSACTION_ERROR.to_string()]
}
false => vec![],
};

let error_messages = errors
.values()
.map(|e| e.to_string())
.filter(|e| e != FAILED_TRANSACTION_ERROR)
.collect_vec();
let error_messages = initial_error_messages
.into_iter()
.chain(error_messages.into_iter())
.collect_vec();

return Err(eyre!(error_messages.join("\n")));
}

Ok(())
}
}
Expand Down

0 comments on commit 698ba25

Please sign in to comment.