|
| 1 | +class AddStartEndColumnsToBalances < ActiveRecord::Migration[7.2] |
| 2 | + def up |
| 3 | + # Add new columns for balance tracking |
| 4 | + add_column :balances, :start_cash_balance, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 5 | + add_column :balances, :start_non_cash_balance, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 6 | + |
| 7 | + # Flow tracking columns (absolute values) |
| 8 | + add_column :balances, :cash_inflows, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 9 | + add_column :balances, :cash_outflows, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 10 | + add_column :balances, :non_cash_inflows, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 11 | + add_column :balances, :non_cash_outflows, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 12 | + |
| 13 | + # Market value changes |
| 14 | + add_column :balances, :net_market_flows, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 15 | + |
| 16 | + # Manual adjustments from valuations |
| 17 | + add_column :balances, :cash_adjustments, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 18 | + add_column :balances, :non_cash_adjustments, :decimal, precision: 19, scale: 4, null: false, default: 0.0 |
| 19 | + |
| 20 | + # Flows factor determines *how* the flows affect the balance. |
| 21 | + # Inflows increase asset accounts, while inflows decrease liability accounts (reducing debt via "payment") |
| 22 | + add_column :balances, :flows_factor, :integer, null: false, default: 1 |
| 23 | + |
| 24 | + # Add generated columns |
| 25 | + change_table :balances do |t| |
| 26 | + t.virtual :start_balance, type: :decimal, precision: 19, scale: 4, stored: true, |
| 27 | + as: "start_cash_balance + start_non_cash_balance" |
| 28 | + |
| 29 | + t.virtual :end_cash_balance, type: :decimal, precision: 19, scale: 4, stored: true, |
| 30 | + as: "start_cash_balance + ((cash_inflows - cash_outflows) * flows_factor) + cash_adjustments" |
| 31 | + |
| 32 | + t.virtual :end_non_cash_balance, type: :decimal, precision: 19, scale: 4, stored: true, |
| 33 | + as: "start_non_cash_balance + ((non_cash_inflows - non_cash_outflows) * flows_factor) + net_market_flows + non_cash_adjustments" |
| 34 | + |
| 35 | + # Postgres doesn't support generated columns depending on other generated columns, |
| 36 | + # but we want the integrity of the data to happen at the DB level, so this is the full formula. |
| 37 | + # Formula: (cash components) + (non-cash components) |
| 38 | + t.virtual :end_balance, type: :decimal, precision: 19, scale: 4, stored: true, |
| 39 | + as: <<~SQL.squish |
| 40 | + ( |
| 41 | + start_cash_balance + |
| 42 | + ((cash_inflows - cash_outflows) * flows_factor) + |
| 43 | + cash_adjustments |
| 44 | + ) + ( |
| 45 | + start_non_cash_balance + |
| 46 | + ((non_cash_inflows - non_cash_outflows) * flows_factor) + |
| 47 | + net_market_flows + |
| 48 | + non_cash_adjustments |
| 49 | + ) |
| 50 | + SQL |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def down |
| 55 | + # Remove generated columns first (PostgreSQL requirement) |
| 56 | + remove_column :balances, :start_balance |
| 57 | + remove_column :balances, :end_cash_balance |
| 58 | + remove_column :balances, :end_non_cash_balance |
| 59 | + remove_column :balances, :end_balance |
| 60 | + |
| 61 | + # Remove new columns |
| 62 | + remove_column :balances, :start_cash_balance |
| 63 | + remove_column :balances, :start_non_cash_balance |
| 64 | + remove_column :balances, :cash_inflows |
| 65 | + remove_column :balances, :cash_outflows |
| 66 | + remove_column :balances, :non_cash_inflows |
| 67 | + remove_column :balances, :non_cash_outflows |
| 68 | + remove_column :balances, :net_market_flows |
| 69 | + remove_column :balances, :cash_adjustments |
| 70 | + remove_column :balances, :non_cash_adjustments |
| 71 | + end |
| 72 | +end |
0 commit comments