Script run does not use database routing #11871

Closed
opened 2025-12-29 21:51:02 +01:00 by adam · 0 comments
Owner

Originally created by @arthanson on GitHub (Nov 25, 2025).

Originally assigned to: @arthanson on GitHub.

NetBox Edition

NetBox Community

NetBox Version

v4.4.6

Python Version

3.10

Steps to Reproduce

The easiest way to see this is with branching:

  1. Have a custom script that modifies some data (example script below).
  2. Create a branch and activate the branch
  3. Run the script without the "Commit Changes" box checked

You can see the issue here: https://github.com/netbox-community/netbox/blob/main/netbox/extras/jobs.py#L45

                with transaction.atomic():
                    script.output = script.run(data, commit)
                    if not commit:
                        raise AbortTransaction()

In all other places in the codebase we use the correct with transaction.atomic(using=router.db_for_write(model)):

See NetBox branching issue: #270

from typing import Any

from extras.scripts import Script, ObjectVar
from dcim.models import Device


class BranchDummy(Script):
    class Meta:
        name = "Branch Dummy"
        description = "Test for branching"
        commit_default = True
        scheduling_enabled = False

    device = ObjectVar(
        model=Device,
        label="Device",
    )

    def run(self, data: Any, commit: bool) -> str:  # type: ignore
        device = data["device"]

        device.name = f"Branch Dummy {device.name}"

        device.full_clean()
        device.save()

        self.log_info(f"Device [{device.name}]({device.get_absolute_url()}) updated")

Expected Behavior

The script should revert the changes, so no changes are made. This is the case if you run it on the main branch.

Observed Behavior

The changes are not reversed.

Originally created by @arthanson on GitHub (Nov 25, 2025). Originally assigned to: @arthanson on GitHub. ### NetBox Edition NetBox Community ### NetBox Version v4.4.6 ### Python Version 3.10 ### Steps to Reproduce The easiest way to see this is with branching: 1. Have a custom script that modifies some data (example script below). 2. Create a branch and activate the branch 3. Run the script without the "Commit Changes" box checked You can see the issue here: https://github.com/netbox-community/netbox/blob/main/netbox/extras/jobs.py#L45 ``` with transaction.atomic(): script.output = script.run(data, commit) if not commit: raise AbortTransaction() ``` In all other places in the codebase we use the correct `with transaction.atomic(using=router.db_for_write(model)):` See NetBox branching issue: [#270](https://github.com/netboxlabs/netbox-branching/issues/270) ``` from typing import Any from extras.scripts import Script, ObjectVar from dcim.models import Device class BranchDummy(Script): class Meta: name = "Branch Dummy" description = "Test for branching" commit_default = True scheduling_enabled = False device = ObjectVar( model=Device, label="Device", ) def run(self, data: Any, commit: bool) -> str: # type: ignore device = data["device"] device.name = f"Branch Dummy {device.name}" device.full_clean() device.save() self.log_info(f"Device [{device.name}]({device.get_absolute_url()}) updated") ``` ### Expected Behavior The script should revert the changes, so no changes are made. This is the case if you run it on the main branch. ### Observed Behavior The changes are not reversed.
adam added the type: bugstatus: acceptednetboxseverity: low labels 2025-12-29 21:51:02 +01:00
adam closed this issue 2025-12-29 21:51:02 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11871