mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Preserve original transaction names when enriching (#1556)
* Preserve original transaction name * Remove stale method * Fix tests
This commit is contained in:
parent
68617514b0
commit
7be6a372bf
22 changed files with 100 additions and 76 deletions
37
db/migrate/20241218132503_add_enriched_name_field.rb
Normal file
37
db/migrate/20241218132503_add_enriched_name_field.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
class AddEnrichedNameField < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_column :account_entries, :enriched_name, :string
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
execute <<-SQL
|
||||
UPDATE account_entries ae
|
||||
SET name = CASE ae.entryable_type
|
||||
WHEN 'Account::Trade' THEN
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM account_trades t
|
||||
WHERE t.id = ae.entryable_id AND t.qty < 0
|
||||
) THEN 'Sell trade'
|
||||
ELSE 'Buy trade'
|
||||
END
|
||||
WHEN 'Account::Transaction' THEN
|
||||
CASE
|
||||
WHEN ae.amount > 0 THEN 'Expense'
|
||||
ELSE 'Income'
|
||||
END
|
||||
WHEN 'Account::Valuation' THEN 'Balance update'
|
||||
ELSE 'Unknown entry'
|
||||
END
|
||||
WHERE name IS NULL
|
||||
SQL
|
||||
|
||||
change_column_null :account_entries, :name, false
|
||||
end
|
||||
|
||||
dir.down do
|
||||
change_column_null :account_entries, :name, true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue