mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-21 22:29:38 +02:00
Fix for invalid accountable data (#1086)
This commit is contained in:
parent
1f6f55c4a8
commit
acf3564a86
3 changed files with 18 additions and 26 deletions
|
@ -28,6 +28,8 @@ class Account < ApplicationRecord
|
||||||
|
|
||||||
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
|
||||||
|
|
||||||
|
delegate :value, :series, to: :accountable
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def by_group(period: Period.all, currency: Money.default_currency.iso_code)
|
def by_group(period: Period.all, currency: Money.default_currency.iso_code)
|
||||||
grouped_accounts = { assets: ValueGroup.new("Assets", currency), liabilities: ValueGroup.new("Liabilities", currency) }
|
grouped_accounts = { assets: ValueGroup.new("Assets", currency), liabilities: ValueGroup.new("Liabilities", currency) }
|
||||||
|
@ -73,31 +75,6 @@ class Account < ApplicationRecord
|
||||||
end
|
end
|
||||||
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
|
def alert
|
||||||
latest_sync = syncs.latest
|
latest_sync = syncs.latest
|
||||||
[ latest_sync&.error, *latest_sync&.warnings ].compact.first
|
[ latest_sync&.error, *latest_sync&.warnings ].compact.first
|
||||||
|
|
15
db/migrate/20240813170608_fix_invalid_accountable_data.rb
Normal file
15
db/migrate/20240813170608_fix_invalid_accountable_data.rb
Normal file
|
@ -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
db/schema.rb
generated
2
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.2].define(version: 2024_08_07_153618) do
|
ActiveRecord::Schema[7.2].define(version: 2024_08_13_170608) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pgcrypto"
|
enable_extension "pgcrypto"
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue