This commit is contained in:
+3
-1
@@ -82,10 +82,12 @@ async fn update_transactions(
|
||||
insertions: &[Insertion],
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<Vec<String>, AppError> {
|
||||
|
||||
|
||||
let insert =
|
||||
transaction::Entity::insert_many(insertions.iter().map(|i| &i.transaction).cloned())
|
||||
.on_conflict(
|
||||
OnConflict::column(transaction::Column::Id)
|
||||
OnConflict::columns([transaction::Column::Id, transaction::Column::IdentityHash])
|
||||
.update_columns(transaction::Column::iter())
|
||||
.to_owned(),
|
||||
)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::hash::Hash;
|
||||
use crate::ingestion::db::Insertion;
|
||||
use anyhow::{anyhow, Context};
|
||||
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
|
||||
@@ -64,6 +65,17 @@ impl MonzoRow {
|
||||
}.into_active_model())
|
||||
}
|
||||
|
||||
/// Compute a hash of this row, returning the number as an i64 to be used as a unique constraint
|
||||
/// in the database.
|
||||
pub fn compute_hash(&self) -> i64 {
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::Hasher;
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
self.hash(&mut hasher);
|
||||
hasher.finish() as i64
|
||||
}
|
||||
|
||||
pub fn into_insertion(self) -> Result<Insertion, anyhow::Error> {
|
||||
let expenditures: Vec<_> = match self.category_split {
|
||||
Some(split) if !split.is_empty() => split
|
||||
@@ -90,6 +102,7 @@ impl MonzoRow {
|
||||
receipt: self.receipt,
|
||||
total_amount: self.total_amount,
|
||||
description: self.description,
|
||||
identity_hash: Some(self.compute_hash()),
|
||||
}.into_active_model(),
|
||||
|
||||
contained_expenditures: expenditures,
|
||||
|
||||
Reference in New Issue
Block a user