1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +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:
Zach Gollwitzer 2025-06-20 13:31:58 -04:00 committed by GitHub
parent 7aca5a2277
commit 1aae00f586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
49 changed files with 1749 additions and 705 deletions

View 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

11
db/schema.rb generated
View file

@ -30,7 +30,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_18_120703) do
t.decimal "balance", precision: 19, scale: 4
t.string "currency"
t.boolean "is_active", default: true, null: false
t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY (ARRAY[('Loan'::character varying)::text, ('CreditCard'::character varying)::text, ('OtherLiability'::character varying)::text])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true
t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY ((ARRAY['Loan'::character varying, 'CreditCard'::character varying, 'OtherLiability'::character varying])::text[])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true
t.uuid "import_id"
t.uuid "plaid_account_id"
t.boolean "scheduled_for_deletion", default: false
@ -216,12 +216,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_18_120703) do
t.boolean "excluded", default: false
t.string "plaid_id"
t.jsonb "locked_attributes", default: {}
t.index ["account_id", "date"], name: "index_entries_on_account_id_and_date"
t.index ["account_id"], name: "index_entries_on_account_id"
t.index ["amount"], name: "index_entries_on_amount"
t.index ["date"], name: "index_entries_on_date"
t.index ["entryable_id", "entryable_type"], name: "index_entries_on_entryable"
t.index ["excluded"], name: "index_entries_on_excluded"
t.index ["import_id"], name: "index_entries_on_import_id"
end
@ -232,7 +227,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_18_120703) do
t.date "date", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["date", "from_currency", "to_currency"], name: "index_exchange_rates_on_date_and_currencies"
t.index ["from_currency", "to_currency", "date"], name: "index_exchange_rates_on_base_converted_date_unique", unique: true
t.index ["from_currency"], name: "index_exchange_rates_on_from_currency"
t.index ["to_currency"], name: "index_exchange_rates_on_to_currency"
@ -691,7 +685,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_18_120703) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type"], name: "index_taggings_on_taggable_id_and_type"
t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable"
end
@ -734,7 +727,9 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_18_120703) do
t.uuid "category_id"
t.uuid "merchant_id"
t.jsonb "locked_attributes", default: {}
t.string "kind", default: "standard", null: false
t.index ["category_id"], name: "index_transactions_on_category_id"
t.index ["kind"], name: "index_transactions_on_kind"
t.index ["merchant_id"], name: "index_transactions_on_merchant_id"
end