The personal finance app for everyone (by everyone)
Ruby
Avoid Account model in Trade Republic bond migration (#4807)
Juan José Mata5c0eaaa279ff
diff --git a/db/migrate/20261008090000_rescale_trade_republic_bond_holding_prices.rb b/db/migrate/20261008090000_rescale_trade_republic_bond_holding_prices.rbindex 2f48e94c71..f3e0d50555 100644--- a/db/migrate/20261008090000_rescale_trade_republic_bond_holding_prices.rb+++ b/db/migrate/20261008090000_rescale_trade_republic_bond_holding_prices.rb@@ -30,17 +30,21 @@ class RescaleTradeRepublicBondHoldingPrices < ActiveRecord::Migration[8.1]SHARED_BOND_TICKER = "BOND"SHARED_BOND_MIC = "XHAM"+ class HoldingRecord < ActiveRecord::Base+ self.table_name = "holdings"+ end+def upbond_security_ids = Security.where(ticker: SHARED_BOND_TICKER, exchange_operating_mic: SHARED_BOND_MIC).pluck(:id)item_ids = []TradeRepublicAccount.where(kind: "portfolio").includes(:account_provider).find_each do |trade_republic_account|- account = trade_republic_account.current_account- provider_id = trade_republic_account.account_provider&.id- next if account.nil? || provider_id.nil?+ # Account can require columns added by later migrations. Read its ID+ # from the provider link without loading the application model.+ provider = trade_republic_account.account_provider+ next unless provider- snapshots = account.holdings- .where(account_provider_id: provider_id)+ snapshots = HoldingRecord.where(account_id: provider.account_id, account_provider_id: provider.id).where("external_id LIKE ?", "#{snapshot_prefix(trade_republic_account)}%")bond_positions = Array(trade_republic_account.raw_positions_payload).map { |position| position.to_h.with_indifferent_access }.select { |position| position[:isin].present? && position[:category].to_s == "interest_products" }@@ -55,7 +59,7 @@ class RescaleTradeRepublicBondHoldingPrices < ActiveRecord::Migration[8.1]payload_rescaled = rescale_payload_prices(trade_republic_account)next if rescaled.zero? && !payload_rescaled- say "Rescaled #{rescaled} Trade Republic bond holdings on account #{account.id}"+ say "Rescaled #{rescaled} Trade Republic bond holdings on account #{provider.account_id}"item_ids << trade_republic_account.trade_republic_item_idenddiff --git a/test/migrations/rescale_trade_republic_bond_holding_prices_migration_test.rb b/test/migrations/rescale_trade_republic_bond_holding_prices_migration_test.rbindex 283435fc42..916389523a 100644--- a/test/migrations/rescale_trade_republic_bond_holding_prices_migration_test.rb+++ b/test/migrations/rescale_trade_republic_bond_holding_prices_migration_test.rb@@ -30,6 +30,25 @@ class RescaleTradeRepublicBondHoldingPricesMigrationTest < ActiveSupport::TestCa@tr_account.reloadend+ teardown { Account.reset_column_information }++ test "rescales holdings before the account logo selection column exists" do+ holding = create_snapshot(BOND_ISIN, Date.current, qty: "2677.95", price: "84.04", cost_basis: "0.93")++ # This later column is absent on upgrades, but present in CI's schema dump.+ # PostgreSQL restores it when the test transaction rolls back.+ Account.connection.remove_column(:accounts, :logo_selection)+ Account.reset_column_information++ assert_difference -> { @item.syncs.count }, 1 do+ run_migration+ end++ assert_equal BigDecimal("0.8404"), holding.reload.price+ assert_equal BigDecimal("2250.5492"), holding.amount+ assert_equal "0.8404", @tr_account.reload.raw_positions_payload.first["price"]+ end+test "rescales bond snapshots stored at percent of par" doolder = create_snapshot(BOND_ISIN, 3.days.ago.to_date, qty: "2677.95", price: "83.10", cost_basis: "0.93")latest = create_snapshot(BOND_ISIN, Date.current, qty: "2677.95", price: "84.04", cost_basis: "0.93")@@ -88,6 +107,16 @@ class RescaleTradeRepublicBondHoldingPricesMigrationTest < ActiveSupport::TestCaassert_equal "101.20", prices[INTEREST_ISIN]end+ test "leaves unlinked portfolio payloads alone" do+ @tr_account.account_provider.destroy!++ assert_no_difference -> { @item.syncs.count } do+ run_migration+ end++ assert_equal "84.04", @tr_account.reload.raw_positions_payload.first["price"]+ end+test "schedules a sync only for items with rescaled holdings" docreate_snapshot(BOND_ISIN, Date.current, qty: "2677.95", price: "84.04", cost_basis: "0.93")