Skip to content

Commit

Permalink
Work on performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcharnock committed May 28, 2024
1 parent dbebf01 commit 9408f9f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
16 changes: 9 additions & 7 deletions hordak/migrations/0041_auto_20240528_2107.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def forward(apps, schema_editor):
RETURNS TRIGGER AS
$$
BEGIN
-- Set empty string codes to be NULL
UPDATE hordak_account SET code = NULL where code = '';
-- Update all children's full account codes
-- Only do this if the account code has been changed
IF OLD.code != NEW.code OR (OLD.code IS NULL) != (NEW.code IS NULL) THEN
-- Useful for debugging, check the postgres logs to see output
-- RAISE WARNING 'Left %%, Right %%, tree %%, %%|%% -> %%|%%', NEW.lft, NEW.rght, NEW.tree_id, OLD.name, OLD.code, NEW.name, NEW.code;
-- Set full code to the combination of the parent account's codes
IF NEW.code != OLD.code THEN
UPDATE
hordak_account AS a
SET
Expand All @@ -32,16 +34,16 @@ def forward(apps, schema_editor):
-- sane and readable. We simply compare the stringified version of the array
-- against one that has NULL values replaced with '*'. If they match then we are free of any NULL
-- values.
WHEN array_to_string(array_agg(code), '', '') = array_to_string(array_agg(code), '', '*')
WHEN array_to_string(array_agg(nullif(code, '')), '', '') = array_to_string(array_agg(nullif(code, '')), '', '*')
THEN string_agg(code, '' order by lft)
ELSE NULL
END
FROM hordak_account AS a2
WHERE a2.lft <= a.lft AND a2.rght >= a.rght AND a.tree_id = a2.tree_id
)
-- Only update this account and the accounts below it in the tree
WHERE a.lft >= NEW.lft AND a.rght <= NEW.rght AND a.tree_id = NEW.tree_id
;
WHERE a.lft >= NEW.lft AND a.rght <= NEW.rght AND a.tree_id = NEW.tree_id
;
END IF;
Expand Down
9 changes: 5 additions & 4 deletions hordak/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from hordak.utilities.currency import Balance


Empty = object()


class DataProvider(object):
"""Utility methods for providing data to test cases"""

Expand Down Expand Up @@ -36,7 +39,7 @@ def account(
name=None,
parent=None,
type=Account.TYPES.income,
code=None,
code=Empty,
currencies=("EUR",),
**kwargs
):
Expand All @@ -46,9 +49,7 @@ def account(
Account
"""
name = name or "Account {}".format(Account.objects.count() + 1)
code = (
code if code is not None else Account.objects.filter(parent=parent).count()
)
code = Account.objects.filter(parent=parent).count() if code is Empty else code

return Account.objects.create(
name=name,
Expand Down

0 comments on commit 9408f9f

Please sign in to comment.