@davoodmehraban89 / Aram / commits / d8a5572
Fork 0

fix(mapping-coverage): prevent diagnostic reads from autoflushing pending writes

davoodmehraban89 committed Oct 23, 2026 at 02:47 UTC d8a5572903ef14c9aa2593adfcb53b86ee6505b2
1 file changed +35 -33
backend/src/aram_backend/mapping_coverage.py
+35 -33
@@ -32,40 +32,42 @@ def inspect_mapping_coverage(session: Session, *, year_id: int) -> MappingCovera
32 """
33 if type(year_id) is not int or year_id <= 0:
34 raise MappingValidationError("year_id must be a positive integer")
35 - if session.get(FiscalYear, year_id) is None:
36 - raise MappingValidationError("fiscal year does not exist")
35 + # A diagnostic must not flush unrelated pending writes in the caller's Session.
36 + with session.no_autoflush:
37 + if session.get(FiscalYear, year_id) is None:
38 + raise MappingValidationError("fiscal year does not exist")
39
38 - accounts = session.scalars(
39 - select(AccountVersion)
40 - .where(AccountVersion.year_id == year_id)
41 - .order_by(AccountVersion.account_code, AccountVersion.version.desc(), AccountVersion.id.desc())
42 - ).all()
43 - current_accounts: dict[str, AccountVersion] = {}
44 - for account in accounts:
45 - current_accounts.setdefault(account.account_code, account)
40 + accounts = session.scalars(
41 + select(AccountVersion)
42 + .where(AccountVersion.year_id == year_id)
43 + .order_by(AccountVersion.account_code, AccountVersion.version.desc(), AccountVersion.id.desc())
44 + ).all()
45 + current_accounts: dict[str, AccountVersion] = {}
46 + for account in accounts:
47 + current_accounts.setdefault(account.account_code, account)
48
47 - rules = session.scalars(
48 - select(MappingRule)
49 - .where(MappingRule.year_id == year_id)
50 - .order_by(MappingRule.rule_key, MappingRule.version.desc(), MappingRule.id.desc())
51 - ).all()
52 - current_rules: dict[str, MappingRule] = {}
53 - for rule in rules:
54 - current_rules.setdefault(rule.rule_key, rule)
49 + rules = session.scalars(
50 + select(MappingRule)
51 + .where(MappingRule.year_id == year_id)
52 + .order_by(MappingRule.rule_key, MappingRule.version.desc(), MappingRule.id.desc())
53 + ).all()
54 + current_rules: dict[str, MappingRule] = {}
55 + for rule in rules:
56 + current_rules.setdefault(rule.rule_key, rule)
57
56 - targets: dict[str, set[int]] = {code: set() for code in current_accounts}
57 - orphan_keys: list[str] = []
58 - for key, rule in current_rules.items():
59 - if rule.account_code not in targets:
60 - orphan_keys.append(key)
61 - else:
62 - targets[rule.account_code].add(rule.target_note_id)
58 + targets: dict[str, set[int]] = {code: set() for code in current_accounts}
59 + orphan_keys: list[str] = []
60 + for key, rule in current_rules.items():
61 + if rule.account_code not in targets:
62 + orphan_keys.append(key)
63 + else:
64 + targets[rule.account_code].add(rule.target_note_id)
65
64 - mapped = tuple(sorted(code for code, notes in targets.items() if len(notes) == 1))
65 - unmapped = tuple(sorted(code for code, notes in targets.items() if not notes))
66 - ambiguous = tuple(sorted(code for code, notes in targets.items() if len(notes) > 1))
67 - return MappingCoverage(
68 - year_id=year_id, total_accounts=len(current_accounts),
69 - mapped_accounts=mapped, unmapped_accounts=unmapped,
70 - ambiguous_accounts=ambiguous, orphan_rule_keys=tuple(sorted(orphan_keys)),
71 - )
66 + mapped = tuple(sorted(code for code, notes in targets.items() if len(notes) == 1))
67 + unmapped = tuple(sorted(code for code, notes in targets.items() if not notes))
68 + ambiguous = tuple(sorted(code for code, notes in targets.items() if len(notes) > 1))
69 + return MappingCoverage(
70 + year_id=year_id, total_accounts=len(current_accounts),
71 + mapped_accounts=mapped, unmapped_accounts=unmapped,
72 + ambiguous_accounts=ambiguous, orphan_rule_keys=tuple(sorted(orphan_keys)),
73 + )