Add identity_hash column
Some checks failed
Build and Publish Docker Container / build (push) Failing after 9m27s
Some checks failed
Build and Publish Docker Container / build (push) Failing after 9m27s
This commit is contained in:
parent
f19f861297
commit
3df05b2d9c
@ -16,6 +16,8 @@ pub struct Model {
|
|||||||
pub notes: Option<String>,
|
pub notes: Option<String>,
|
||||||
pub receipt: Option<String>,
|
pub receipt: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
|
#[sea_orm(unique)]
|
||||||
|
pub identity_hash: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
pub use sea_orm_migration::prelude::*;
|
pub use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
pub mod m20230904_141851_create_monzo_tables;
|
pub mod m20230904_141851_create_monzo_tables;
|
||||||
|
mod m20240529_195030_add_transaction_identity_hash;
|
||||||
|
|
||||||
pub struct Migrator;
|
pub struct Migrator;
|
||||||
|
|
||||||
@ -11,6 +12,9 @@ impl MigratorTrait for Migrator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||||
vec![Box::new(m20230904_141851_create_monzo_tables::Migration)]
|
vec![
|
||||||
|
Box::new(m20230904_141851_create_monzo_tables::Migration),
|
||||||
|
Box::new(m20240529_195030_add_transaction_identity_hash::Migration),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,34 @@
|
|||||||
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
|
#[derive(DeriveMigrationName)]
|
||||||
|
pub struct Migration;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl MigrationTrait for Migration {
|
||||||
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager.alter_table(
|
||||||
|
TableAlterStatement::new()
|
||||||
|
.table(Transaction::Table)
|
||||||
|
.add_column(
|
||||||
|
ColumnDef::new(Transaction::IdentityHash)
|
||||||
|
.big_integer()
|
||||||
|
.unique_key(),
|
||||||
|
).to_owned()
|
||||||
|
).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager.alter_table(
|
||||||
|
TableAlterStatement::new()
|
||||||
|
.table(Transaction::Table)
|
||||||
|
.drop_column(Transaction::IdentityHash)
|
||||||
|
.to_owned()
|
||||||
|
).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DeriveIden)]
|
||||||
|
enum Transaction {
|
||||||
|
Table,
|
||||||
|
IdentityHash,
|
||||||
|
}
|
||||||
@ -82,10 +82,12 @@ async fn update_transactions(
|
|||||||
insertions: &[Insertion],
|
insertions: &[Insertion],
|
||||||
tx: &DatabaseTransaction,
|
tx: &DatabaseTransaction,
|
||||||
) -> Result<Vec<String>, AppError> {
|
) -> Result<Vec<String>, AppError> {
|
||||||
|
|
||||||
|
|
||||||
let insert =
|
let insert =
|
||||||
transaction::Entity::insert_many(insertions.iter().map(|i| &i.transaction).cloned())
|
transaction::Entity::insert_many(insertions.iter().map(|i| &i.transaction).cloned())
|
||||||
.on_conflict(
|
.on_conflict(
|
||||||
OnConflict::column(transaction::Column::Id)
|
OnConflict::columns([transaction::Column::Id, transaction::Column::IdentityHash])
|
||||||
.update_columns(transaction::Column::iter())
|
.update_columns(transaction::Column::iter())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use std::hash::Hash;
|
||||||
use crate::ingestion::db::Insertion;
|
use crate::ingestion::db::Insertion;
|
||||||
use anyhow::{anyhow, Context};
|
use anyhow::{anyhow, Context};
|
||||||
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
|
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
|
||||||
@ -64,6 +65,17 @@ impl MonzoRow {
|
|||||||
}.into_active_model())
|
}.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> {
|
pub fn into_insertion(self) -> Result<Insertion, anyhow::Error> {
|
||||||
let expenditures: Vec<_> = match self.category_split {
|
let expenditures: Vec<_> = match self.category_split {
|
||||||
Some(split) if !split.is_empty() => split
|
Some(split) if !split.is_empty() => split
|
||||||
@ -90,6 +102,7 @@ impl MonzoRow {
|
|||||||
receipt: self.receipt,
|
receipt: self.receipt,
|
||||||
total_amount: self.total_amount,
|
total_amount: self.total_amount,
|
||||||
description: self.description,
|
description: self.description,
|
||||||
|
identity_hash: Some(self.compute_hash()),
|
||||||
}.into_active_model(),
|
}.into_active_model(),
|
||||||
|
|
||||||
contained_expenditures: expenditures,
|
contained_expenditures: expenditures,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user