Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for invalid accountable data #1086

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.