From aec8e293bffc27ee58226b6441dc093c84755fa7 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 1 Dec 2025 11:14:06 -0800 Subject: [PATCH] atomic lock on both default and potentially branch database --- netbox/extras/jobs.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/netbox/extras/jobs.py b/netbox/extras/jobs.py index 2418c58e4..f64806701 100644 --- a/netbox/extras/jobs.py +++ b/netbox/extras/jobs.py @@ -44,10 +44,18 @@ class ScriptJob(JobRunner): # A script can modify multiple models so need to do an atomic lock on # both the default database (for non ChangeLogged models) and potentially # any other database (for ChangeLogged models) - with transaction.atomic(using=router.db_for_write(Device)): - script.output = script.run(data, commit) - if not commit: - raise AbortTransaction() + branch_db = router.db_for_write(Device) + with transaction.atomic(using='default'): + # If branch database is different from default, wrap in a second atomic transaction + if branch_db != 'default': + with transaction.atomic(using=branch_db): + script.output = script.run(data, commit) + if not commit: + raise AbortTransaction() + else: + script.output = script.run(data, commit) + if not commit: + raise AbortTransaction() except AbortTransaction: script.log_info(message=_("Database changes have been reverted automatically.")) if script.failed: