1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-21 14:19:39 +02:00
Maybe/db/migrate/20241218132503_add_enriched_name_field.rb
Zach Gollwitzer 7be6a372bf
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions
Preserve original transaction names when enriching (#1556)
* Preserve original transaction name

* Remove stale method

* Fix tests
2024-12-19 10:16:09 -05:00

37 lines
1 KiB
Ruby

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