WIP: Defer Odoo writes to transaction.on_commit to prevent orphans #521

Draft
tobru wants to merge 1 commit from fix-org-creation-race into main
Owner

Inverts the create-organization flow so Django becomes the source of truth: BillingEntity and Organization rows are saved synchronously with NULL Odoo IDs, and the Odoo res.partner / sale.order writes are scheduled via transaction.on_commit. If Django rolls back, the on_commit callbacks are discarded and no Odoo orphan is created.

  • BillingEntity: split into create_django (no Odoo) and sync_to_odoo (idempotent). create_from_data kept as a thin wrapper.
  • Organization: create_organization no longer touches Odoo; new sync_to_odoo method handles sale.order create / default-sale-order lookup / GTC chatter (idempotent).
  • OrganizationCreateView wraps the scheduled sync in _safe_odoo_sync so Odoo failure no longer 500s the user; the dashboard shows a banner when Odoo IDs are still NULL.
  • OSB API mirrors the on_commit pattern but lets exceptions propagate so the existing 500-on-Odoo-failure contract with Exoscale is preserved.
  • sync_odoo gains --resync-missing to rerun sync_to_odoo on rows that the on_commit callback failed to populate; idempotency guards make re-runs safe.
Inverts the create-organization flow so Django becomes the source of truth: BillingEntity and Organization rows are saved synchronously with NULL Odoo IDs, and the Odoo res.partner / sale.order writes are scheduled via transaction.on_commit. If Django rolls back, the on_commit callbacks are discarded and no Odoo orphan is created. - BillingEntity: split into create_django (no Odoo) and sync_to_odoo (idempotent). create_from_data kept as a thin wrapper. - Organization: create_organization no longer touches Odoo; new sync_to_odoo method handles sale.order create / default-sale-order lookup / GTC chatter (idempotent). - OrganizationCreateView wraps the scheduled sync in _safe_odoo_sync so Odoo failure no longer 500s the user; the dashboard shows a banner when Odoo IDs are still NULL. - OSB API mirrors the on_commit pattern but lets exceptions propagate so the existing 500-on-Odoo-failure contract with Exoscale is preserved. - sync_odoo gains --resync-missing to rerun sync_to_odoo on rows that the on_commit callback failed to populate; idempotency guards make re-runs safe.
defer Odoo writes to transaction.on_commit to prevent orphans
All checks were successful
Tests / test (push) Successful in 52s
d14d826d9f
Inverts the create-organization flow so Django becomes the source of
truth: BillingEntity and Organization rows are saved synchronously with
NULL Odoo IDs, and the Odoo res.partner / sale.order writes are
scheduled via transaction.on_commit. If Django rolls back, the on_commit
callbacks are discarded and no Odoo orphan is created.

- BillingEntity: split into create_django (no Odoo) and sync_to_odoo
  (idempotent). create_from_data kept as a thin wrapper.
- Organization: create_organization no longer touches Odoo; new
  sync_to_odoo method handles sale.order create / default-sale-order
  lookup / GTC chatter (idempotent).
- OrganizationCreateView wraps the scheduled sync in _safe_odoo_sync so
  Odoo failure no longer 500s the user; the dashboard shows a banner
  when Odoo IDs are still NULL.
- OSB API mirrors the on_commit pattern but lets exceptions propagate so
  the existing 500-on-Odoo-failure contract with Exoscale is preserved.
- sync_odoo gains --resync-missing to rerun sync_to_odoo on rows that
  the on_commit callback failed to populate; idempotency guards make
  re-runs safe.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
All checks were successful
Tests / test (push) Successful in 52s
This pull request has changes conflicting with the target branch.
  • src/servala/core/models/organization.py
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix-org-creation-race:fix-org-creation-race
git switch fix-org-creation-race

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch main
git merge --no-ff fix-org-creation-race
git switch fix-org-creation-race
git rebase main
git switch main
git merge --ff-only fix-org-creation-race
git switch fix-org-creation-race
git rebase main
git switch main
git merge --no-ff fix-org-creation-race
git switch main
git merge --squash fix-org-creation-race
git switch main
git merge --ff-only fix-org-creation-race
git switch main
git merge fix-org-creation-race
git push origin main
Sign in to join this conversation.
No description provided.