mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
perf(transactions): add kind
to Transaction
model and remove expensive Transfer joins in aggregations (#2388)
* add kind to transaction model * Basic transfer creator * Fix method naming conflict * Creator form pattern * Remove stale methods * Tweak migration * Remove BaseQuery, write entire query in each class for clarity * Query optimizations * Remove unused exchange rate query lines * Remove temporary cache-warming strategy * Fix test * Update transaction search * Decouple transactions endpoint from IncomeStatement * Clean up transactions controller * Update cursor rules * Cleanup comments, logic in search * Fix totals logic on transactions view * Fix pagination * Optimize search totals query * Default to last 30 days on transactions page if no filters * Decouple transactions list from transfer details * Revert transfer route * Migration reset * Bundle update * Fix matching logic, tests * Remove unused code
This commit is contained in:
parent
7aca5a2277
commit
1aae00f586
49 changed files with 1749 additions and 705 deletions
32
db/migrate/20250616183654_add_kind_to_transactions.rb
Normal file
32
db/migrate/20250616183654_add_kind_to_transactions.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
class AddKindToTransactions < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_column :transactions, :kind, :string, null: false, default: "standard"
|
||||
add_index :transactions, :kind
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
# Update transaction kinds based on transfer relationships
|
||||
execute <<~SQL
|
||||
UPDATE transactions
|
||||
SET kind = CASE
|
||||
WHEN destination_accounts.accountable_type = 'Loan' AND entries.amount > 0 THEN 'loan_payment'
|
||||
WHEN destination_accounts.accountable_type = 'CreditCard' AND entries.amount > 0 THEN 'cc_payment'
|
||||
ELSE 'funds_movement'
|
||||
END
|
||||
FROM transfers t
|
||||
JOIN entries ON (
|
||||
entries.entryable_id = t.inflow_transaction_id OR
|
||||
entries.entryable_id = t.outflow_transaction_id
|
||||
)
|
||||
LEFT JOIN entries inflow_entries ON (
|
||||
inflow_entries.entryable_id = t.inflow_transaction_id
|
||||
AND inflow_entries.entryable_type = 'Transaction'
|
||||
)
|
||||
LEFT JOIN accounts destination_accounts ON destination_accounts.id = inflow_entries.account_id
|
||||
WHERE transactions.id = entries.entryable_id
|
||||
AND entries.entryable_type = 'Transaction'
|
||||
SQL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue