Skip to content

Commit

Permalink
Fix for invalid accountable data (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgoll authored Aug 15, 2024
1 parent 1f6f55c commit acf3564
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
27 changes: 2 additions & 25 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Account < ApplicationRecord

delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy

delegate :value, :series, to: :accountable

class << self
def by_group(period: Period.all, currency: Money.default_currency.iso_code)
grouped_accounts = { assets: ValueGroup.new("Assets", currency), liabilities: ValueGroup.new("Liabilities", currency) }
Expand Down Expand Up @@ -73,31 +75,6 @@ def create_with_optional_start_balance!(attributes:, start_date: nil, start_bala
end
end

# Start of temporary fix for #1068
# ==========================================================================

# TODO: Both `series` and `value` methods are a temporary fix for #1068, which appears to be a data corruption issue.
# Every account should have an accountable no matter what, but some self hosted instances seem to have missing accountables.
# When this is fixed, we can add this back to `delegate :value, :series, to: :accountable`
def series(period: Period.all, currency: self.currency)
if accountable.present?
accountable.series(period: period, currency: currency)
else
TimeSeries.new([])
end
end

def value
if accountable.present?
accountable.value
else
balance_money
end
end

# ==========================================================================
# End of temporary fix for #1068

def alert
latest_sync = syncs.latest
[ latest_sync&.error, *latest_sync&.warnings ].compact.first
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20240813170608_fix_invalid_accountable_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class FixInvalidAccountableData < ActiveRecord::Migration[7.2]
def up
Account.all.each do |account|
unless account.accountable
puts "Generating new accountable for id=#{account.id}, name=#{account.name}, type=#{account.accountable_type}"
new_accountable = Accountable.from_type(account.accountable_type).new
account.update!(accountable: new_accountable)
end
end
end

def down
# Not reversible
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acf3564

Please sign in to comment.