mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 05:09:38 +02:00
Start and end balance anchors for historical account balances (#2455)
* Add kind field to valuation * Fix schema conflict * Add kind to valuation * Scaffold opening balance manager * Opening balance manager implementation * Update account import to use opening balance manager + tests * Update account to use opening balance manager * Fix test assertions, usage of current balance manager * Lint fixes * Add Opening Balance manager, add tests to forward calculator * Add credit card to "all cash" designation * Simplify valuation model * Add current balance manager with tests * Add current balance logic to reverse calculator and plaid sync * Tweaks to initial calc logic * Ledger testing helper, tweak assertions for reverse calculator * Update test assertions * Extract balance transformer, simplify calculators * Algo simplifications * Final tweaks to calculators * Cleanup * Fix error, propagate sync errors up to parent * Update migration script, valuation naming
This commit is contained in:
parent
9110ab27d2
commit
c1d98fe73b
35 changed files with 1903 additions and 355 deletions
|
@ -111,4 +111,47 @@ namespace :data_migration do
|
|||
|
||||
puts "✅ Duplicate security migration complete."
|
||||
end
|
||||
|
||||
desc "Migrate account valuation anchors"
|
||||
# 2025-07-10: Set opening_anchor kinds for valuations to support event-sourced ledger model.
|
||||
# Manual accounts get their oldest valuation marked as opening_anchor, which acts as the
|
||||
# starting balance for the account. Current anchors are only used for Plaid accounts.
|
||||
task migrate_account_valuation_anchors: :environment do
|
||||
puts "==> Migrating account valuation anchors..."
|
||||
|
||||
manual_accounts = Account.manual.includes(valuations: :entry)
|
||||
total_accounts = manual_accounts.count
|
||||
accounts_processed = 0
|
||||
opening_anchors_set = 0
|
||||
|
||||
manual_accounts.find_each do |account|
|
||||
accounts_processed += 1
|
||||
|
||||
# Find oldest account entry
|
||||
oldest_entry = account.entries
|
||||
.order("date ASC, created_at ASC")
|
||||
.first
|
||||
|
||||
# Check if it's a valuation that isn't already an anchor
|
||||
if oldest_entry && oldest_entry.valuation?
|
||||
derived_valuation_name = Valuation.build_opening_anchor_name(account.accountable_type)
|
||||
|
||||
Account.transaction do
|
||||
oldest_entry.valuation.update!(kind: "opening_anchor")
|
||||
oldest_entry.update!(name: derived_valuation_name)
|
||||
end
|
||||
opening_anchors_set += 1
|
||||
end
|
||||
|
||||
if accounts_processed % 100 == 0
|
||||
puts "[#{accounts_processed}/#{total_accounts}] Processed #{accounts_processed} accounts..."
|
||||
end
|
||||
rescue => e
|
||||
puts "ERROR processing account #{account.id}: #{e.message}"
|
||||
end
|
||||
|
||||
puts "✅ Account valuation anchor migration complete."
|
||||
puts " Processed: #{accounts_processed} accounts"
|
||||
puts " Opening anchors set: #{opening_anchors_set}"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue