Skip to content

Commit

Permalink
use diesel insert_or_ignore (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx authored Oct 1, 2024
1 parent 82d0fa9 commit de864d4
Showing 1 changed file with 6 additions and 57 deletions.
63 changes: 6 additions & 57 deletions xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use diesel::{
connection::{AnsiTransactionManager, SimpleConnection, TransactionManager},
prelude::*,
r2d2::{ConnectionManager, Pool, PoolTransactionManager, PooledConnection},
result::{DatabaseErrorKind, Error},
result::Error,
sql_query,
};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
Expand All @@ -53,16 +53,6 @@ struct SqliteVersion {
version: String,
}

pub fn ignore_unique_violation<T>(
result: Result<T, diesel::result::Error>,
) -> Result<(), StorageError> {
match result {
Ok(_) => Ok(()),
Err(Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) => Ok(()),
Err(error) => Err(StorageError::from(error)),
}
}

#[derive(Default, Clone, Debug)]
pub enum StorageOption {
#[default]
Expand Down Expand Up @@ -414,12 +404,13 @@ macro_rules! impl_store_or_ignore {
&self,
into: &$crate::storage::encrypted_store::db_connection::DbConnection,
) -> Result<(), $crate::StorageError> {
let result = into.raw_query(|conn| {
diesel::insert_into($table::table)
into.raw_query(|conn| {
diesel::insert_or_ignore_into($table::table)
.values(self)
.execute(conn)
});
$crate::storage::ignore_unique_violation(result)
.map(|_| ())
})
.map_err($crate::StorageError::from)
}
}
};
Expand Down Expand Up @@ -597,48 +588,6 @@ mod tests {
EncryptedMessageStore::remove_db_files(db_path)
}

#[test]
fn it_returns_ok_when_given_ok_result() {
let result: Result<(), diesel::result::Error> = Ok(());
assert!(
super::ignore_unique_violation(result).is_ok(),
"Expected Ok(()) when given Ok result"
);
}

#[test]
fn it_returns_ok_on_unique_violation_error() {
let result: Result<(), diesel::result::Error> = Err(diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::UniqueViolation,
Box::new("violation".to_string()),
));
assert!(
super::ignore_unique_violation(result).is_ok(),
"Expected Ok(()) when given UniqueViolation error"
);
}

#[test]
fn it_returns_err_on_non_unique_violation_database_errors() {
let result: Result<(), diesel::result::Error> = Err(diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::NotNullViolation,
Box::new("other kind".to_string()),
));
assert!(
super::ignore_unique_violation(result).is_err(),
"Expected Err when given non-UniqueViolation database error"
);
}

#[test]
fn it_returns_err_on_non_database_errors() {
let result: Result<(), diesel::result::Error> = Err(diesel::result::Error::NotFound);
assert!(
super::ignore_unique_violation(result).is_err(),
"Expected Err when given a non-database error"
);
}

// get two connections
// start a transaction
// try to write with second connection
Expand Down

0 comments on commit de864d4

Please sign in to comment.