1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-18 20:59:39 +02:00
Maybe/db/migrate/20240619125949_rename_accountable_tables.rb
Zach Gollwitzer a947db92b2
Account namespace updates: part 1 (#893)
* Rename accountable types

* Merge conflicts

* Fix broken tests

* Add back sidebar changes
2024-06-20 07:26:25 -04:00

87 lines
2.6 KiB
Ruby

class RenameAccountableTables < ActiveRecord::Migration[7.2]
def change
rename_table :account_depositories, :depositories
rename_table :account_investments, :investments
rename_table :account_credits, :credit_cards
rename_table :account_properties, :properties
rename_table :account_vehicles, :vehicles
rename_table :account_loans, :loans
rename_table :account_cryptos, :cryptos
rename_table :account_other_assets, :other_assets
rename_table :account_other_liabilities, :other_liabilities
reversible do |dir|
dir.up do
update_accountable_types(
'Account::Depository' => 'Depository',
'Account::Investment' => 'Investment',
'Account::Credit' => 'CreditCard',
'Account::Property' => 'Property',
'Account::Vehicle' => 'Vehicle',
'Account::Loan' => 'Loan',
'Account::Crypto' => 'Crypto',
'Account::OtherAsset' => 'OtherAsset',
'Account::OtherLiability' => 'OtherLiability'
)
remove_column :accounts, :classification, :virtual
change_table :accounts do |t|
t.virtual(
:classification,
type: :string,
stored: true,
as: <<-SQL
CASE
WHEN accountable_type IN ('Loan', 'CreditCard', 'OtherLiability')
THEN 'liability'
ELSE 'asset'
END
SQL
)
end
end
dir.down do
update_accountable_types(
'Depository' => 'Account::Depository',
'Investment' => 'Account::Investment',
'CreditCard' => 'Account::Credit',
'Property' => 'Account::Property',
'Vehicle' => 'Account::Vehicle',
'Loan' => 'Account::Loan',
'Crypto' => 'Account::Crypto',
'OtherAsset' => 'Account::OtherAsset',
'OtherLiability' => 'Account::OtherLiability'
)
remove_column :accounts, :classification, :virtual
change_table :accounts do |t|
t.virtual(
:classification,
type: :string,
stored: true,
as: <<-SQL
CASE
WHEN accountable_type IN ('Account::Loan', 'Account::Credit', 'Account::OtherLiability')
THEN 'liability'
ELSE 'asset'
END
SQL
)
end
end
end
end
private
def update_accountable_types(mapping)
Account.reset_column_information
mapping.each do |old_type, new_type|
Account.where(accountable_type: old_type).update_all(accountable_type: new_type)
end
end
end