Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 141 additions & 41 deletions .rot/snapshot.json

Large diffs are not rendered by default.

1,326 changes: 512 additions & 814 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ members = ["app", "db", "shared", "tui"]
resolver = "2"

[workspace.dependencies]
anyhow = "1.0.103"
anyhow = "1.0.104"
chrono = "0.4.45"
diesel = { version = "2.3.10", default-features = false, features = [
diesel = { version = "2.3.13", default-features = false, features = [
"chrono",
"returning_clauses_for_sqlite_3_35",
"sqlite",
] }
rex-app = "0.2.9"
rex-db = "0.2.9"
rex-shared = "0.2.9"
rex-app = "0.3.0"
rex-db = "0.3.0"
rex-shared = "0.3.0"
strum = "0.28.0"
strum_macros = "0.28"

Expand Down
10 changes: 3 additions & 7 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
[package]
name = "rex-app"
version = "0.2.9"
version = "0.3.0"
edition = "2024"
description = """
Core functionality for Rex
"""
description = "Core functionality for Rex"
homepage = "https://github.com/TheRustyPickle/Rex"
repository = "https://github.com/TheRustyPickle/Rex"
license = "MIT"
Expand All @@ -13,14 +11,12 @@ license = "MIT"
anyhow.workspace = true
chrono.workspace = true
diesel.workspace = true
log = "0.4.34"
rex-db.workspace = true
rex-shared.workspace = true
strsim = "0.11.1"
strum.workspace = true
strum_macros.workspace = true
thiserror = "2.0.17"

thiserror = "2.0.21"

[lints.clippy]
too_many_arguments = { level = "allow" }
9 changes: 9 additions & 0 deletions app/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use rex_db::models::{FetchNature, FullRecurringTx, NewRecurringTx, Recurrenc
use rex_db::{Cache, ConnCache, get_connection, get_connection_no_migrations};
use std::collections::{HashMap, HashSet};

use crate::migration::migrate_to_v1;
use crate::modifier::{
activity_delete_tx, activity_edit_tx, activity_new_tx, activity_search_tx,
activity_swap_position, add_new_recurring_tx, add_new_tx, add_new_tx_methods, advance_date,
Expand Down Expand Up @@ -566,4 +567,12 @@ impl DbConn {
let db_conn = MutDbConn::new(&mut self.conn, &self.cache);
Stepper::new(db_conn)
}

pub fn initiate_v1_migration(&mut self) -> Result<()> {
self.conn.transaction::<(), Error, _>(|conn| {
let mut db_conn = MutDbConn::new(conn, &self.cache);

migrate_to_v1(&mut db_conn)
})
}
}
5 changes: 5 additions & 0 deletions app/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod old_db;
pub mod v1;

pub use old_db::*;
pub(crate) use v1::*;
File renamed without changes.
34 changes: 34 additions & 0 deletions app/src/migration/v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anyhow::Result;
use chrono::Datelike;
use rex_db::ConnCache;
use rex_db::models::{Balance, Tx};
use std::collections::HashMap;

use crate::modifier::tidy_balances;

/// Do the V1 migration of the balances. Tidies every single balance and the final balance. This
/// removes the defensive balance update in the balance fetching.
pub(crate) fn migrate_to_v1(conn: &mut impl ConnCache) -> Result<()> {
// If no tx, nothing to tidy
let Some(oldest_tx_date) = Tx::get_lowest_date(conn)? else {
return Ok(());
};

let oldest_month = oldest_tx_date.date().with_day(1).unwrap();

tidy_balances(oldest_month, conn)?;

// Tidied from the oldest tx to the newest balance, so the latest monthly balances are now
// assumed to be absolutely correct.
let latest_balances: HashMap<i32, i64> = Balance::get_balance_highest_date(conn)?
.into_iter()
.map(|balance| (balance.method_id, balance.balance))
.collect();

for (method_id, mut final_balance) in Balance::get_final_balance(conn)? {
final_balance.balance = latest_balances.get(&method_id).copied().unwrap_or(0);
final_balance.update_final_balance(conn)?;
}

Ok(())
}
4 changes: 2 additions & 2 deletions app/src/modifier/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::modifier::first_occurrence_on_or_after;
use crate::utils::{month_name_to_num, parse_amount_nature_cent, weekday_name_to_num};

pub(crate) fn tidy_balances(date: NaiveDate, db_conn: &mut impl ConnCache) -> Result<()> {
let tx = Balance::get_highest_date(db_conn)?;
let balance = Balance::get_highest_date(db_conn)?;

let max_date = NaiveDate::from_ymd_opt(tx.year, tx.month as u32, 1).unwrap();
let max_date = NaiveDate::from_ymd_opt(balance.year, balance.month as u32, 1).unwrap();

tidy_recursive(max_date, date, db_conn)?;

Expand Down
6 changes: 5 additions & 1 deletion app/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn split_tags(input: &str) -> Vec<String> {
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.map(std::string::ToString::to_string)
.collect()
}

Expand Down Expand Up @@ -107,11 +107,13 @@ pub fn num_to_weekday_name(num: i32) -> Result<&'static str> {
}
}

#[must_use]
pub fn month_year_to_unique(month: i32, year: i32) -> i32 {
year * 100 + month
}

/// Returns what percentage each of the two values is of their combined total
#[must_use]
pub fn get_percentages(value1: f64, value2: f64) -> (f64, f64) {
if value1 == 0.0 && value2 == 0.0 {
return (0.0, 0.0);
Expand Down Expand Up @@ -144,6 +146,7 @@ pub fn parse_amount_nature_cent(amount: &str) -> Result<Option<AmountNature>> {
Ok(Some(res))
}

#[must_use]
pub fn compare_change(current: Dollar, previous: Dollar) -> String {
match current.cent().percent_change(previous.cent()) {
None => "∞".to_string(),
Expand All @@ -152,6 +155,7 @@ pub fn compare_change(current: Dollar, previous: Dollar) -> String {
}
}

#[must_use]
pub fn compare_change_opt(current: Dollar, previous: Option<Dollar>) -> String {
match previous {
None => "∞".to_string(),
Expand Down
24 changes: 0 additions & 24 deletions app/src/views/tx_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub(crate) fn get_txs(
) -> Result<TxViewGroup> {
let txs = FullTx::get_txs(date, nature, db_conn)?;

let current_balance = Balance::get_balance(date, nature, db_conn)?;

let last_balance = Balance::get_last_balance(date, nature, db_conn)?;

let mut last_balance = last_balance
Expand Down Expand Up @@ -66,28 +64,6 @@ pub(crate) fn get_txs(
all_tx_views.push(tx_view);
}

// Only monthly views are allowed to rewrite the stored balances
if nature != FetchNature::Monthly {
return Ok(TxViewGroup(all_tx_views));
}

let mut to_insert_balance = Vec::new();

for mut balance in current_balance {
let method_id = balance.method_id;
let last_balance = *last_balance.get(&method_id).unwrap();

if last_balance != balance.balance {
balance.balance = last_balance.value();
to_insert_balance.push(balance);
}
}

for to_insert in to_insert_balance {
to_insert.insert(db_conn)?;
log::info!("Inserted balance: {to_insert:#?}");
}

Ok(TxViewGroup(all_tx_views))
}

Expand Down
Loading
Loading