mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 13:35:21 +02:00
Fix Account Holding validation and synchronization (#1818)
* Fix Account Holding validation and synchronization Fixes #1781 - Add comprehensive validations for Account::Holding - Implement validation to ensure amount matches qty * price - Update Account::Syncer to include qty and price during synchronization - Add database constraints for holding attributes and calculations * Remove database check constraints for Account Holdings Align with project convention of keeping complex validations in ActiveRecord - Remove database-level check constraints for quantity, price, and amount - Maintain database-level null and unique constraints - Prepare for more flexible validation in the model layer
This commit is contained in:
parent
0dc25cda22
commit
cf23673003
6 changed files with 67 additions and 9 deletions
|
@ -0,0 +1,40 @@
|
|||
class AddConstraintsToAccountHoldings < ActiveRecord::Migration[7.2]
|
||||
def up
|
||||
# First, remove any holdings with nil values
|
||||
execute <<-SQL
|
||||
DELETE FROM account_holdings#{' '}
|
||||
WHERE date IS NULL#{' '}
|
||||
OR qty IS NULL#{' '}
|
||||
OR price IS NULL#{' '}
|
||||
OR amount IS NULL#{' '}
|
||||
OR currency IS NULL;
|
||||
SQL
|
||||
|
||||
# Remove any holdings where amount doesn't match qty * price
|
||||
execute <<-SQL
|
||||
DELETE FROM account_holdings#{' '}
|
||||
WHERE ROUND(qty * price, 4) != ROUND(amount, 4);
|
||||
SQL
|
||||
|
||||
# Remove any holdings with negative values
|
||||
execute <<-SQL
|
||||
DELETE FROM account_holdings#{' '}
|
||||
WHERE qty < 0 OR price < 0 OR amount < 0;
|
||||
SQL
|
||||
|
||||
# Now add NOT NULL constraints
|
||||
change_column_null :account_holdings, :date, false
|
||||
change_column_null :account_holdings, :qty, false
|
||||
change_column_null :account_holdings, :price, false
|
||||
change_column_null :account_holdings, :amount, false
|
||||
change_column_null :account_holdings, :currency, false
|
||||
end
|
||||
|
||||
def down
|
||||
change_column_null :account_holdings, :date, true
|
||||
change_column_null :account_holdings, :qty, true
|
||||
change_column_null :account_holdings, :price, true
|
||||
change_column_null :account_holdings, :amount, true
|
||||
change_column_null :account_holdings, :currency, true
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue