Skip to content

Commit e495ebf

Browse files
committed
37 add branching support when creating COT
1 parent 0db00de commit e495ebf

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

netbox_custom_objects/field_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from netbox.tables.columns import BooleanColumn
3232

3333
from netbox_custom_objects.constants import APP_LABEL
34-
from netbox_custom_objects.utilities import generate_model
34+
from netbox_custom_objects.utilities import generate_model, get_schema_connection
3535

3636

3737
class LazyForeignKey(ForeignKey):

netbox_custom_objects/models.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# from django.contrib.contenttypes.management import create_contenttypes
1313
from django.contrib.contenttypes.models import ContentType
1414
from django.core.validators import RegexValidator, ValidationError
15-
from django.db import connection, IntegrityError, models, transaction
15+
from django.db import connection, connections, IntegrityError, models, transaction
1616
from django.db.models import Q
1717
from django.db.models.functions import Lower
1818
from django.db.models.signals import pre_delete, post_save
@@ -52,7 +52,7 @@
5252

5353
from netbox_custom_objects.constants import APP_LABEL, RESERVED_FIELD_NAMES
5454
from netbox_custom_objects.field_types import FIELD_TYPE_CLASS
55-
from netbox_custom_objects.utilities import generate_model
55+
from netbox_custom_objects.utilities import generate_model, get_schema_connection
5656

5757

5858
class UniquenessConstraintTestError(Exception):
@@ -549,12 +549,11 @@ def create_model(self):
549549

550550
# Ensure the ContentType exists and is immediately available
551551
features = get_model_features(model)
552-
self.object_type.features = features + ['branching']
553-
self.object_type.public = True
554552
self.object_type.features = features
553+
self.object_type.public = True
555554
self.object_type.save()
556555

557-
with connection.schema_editor() as schema_editor:
556+
with get_schema_connection().schema_editor() as schema_editor:
558557
schema_editor.create_model(model)
559558

560559
self.register_custom_object_search_index(model)
@@ -588,7 +587,7 @@ def delete(self, *args, **kwargs):
588587
# TODO: Remove this disconnect/reconnect after ObjectType has been exempted from handle_deleted_object
589588
pre_delete.disconnect(handle_deleted_object)
590589
object_type.delete()
591-
with connection.schema_editor() as schema_editor:
590+
with get_schema_connection().schema_editor() as schema_editor:
592591
schema_editor.delete_model(model)
593592
pre_delete.connect(handle_deleted_object)
594593

@@ -936,7 +935,7 @@ def clean(self):
936935

937936
try:
938937
with transaction.atomic():
939-
with connection.schema_editor() as test_schema_editor:
938+
with get_schema_connection().schema_editor() as test_schema_editor:
940939
test_schema_editor.alter_field(model, old_field, model_field)
941940
# If we get here, the constraint was applied successfully
942941
# Now raise a custom exception to rollback the test transaction
@@ -1371,7 +1370,7 @@ def save(self, *args, **kwargs):
13711370
model = self.custom_object_type.get_model()
13721371
model_field.contribute_to_class(model, self.name)
13731372

1374-
with connection.schema_editor() as schema_editor:
1373+
with get_schema_connection().schema_editor() as schema_editor:
13751374
if self._state.adding:
13761375
schema_editor.add_field(model, model_field)
13771376
if self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
@@ -1489,7 +1488,7 @@ def delete(self, *args, **kwargs):
14891488
model = self.custom_object_type.get_model()
14901489
model_field.contribute_to_class(model, self.name)
14911490

1492-
with connection.schema_editor() as schema_editor:
1491+
with get_schema_connection().schema_editor() as schema_editor:
14931492
if self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
14941493
apps = model._meta.apps
14951494
through_model = apps.get_model(APP_LABEL, self.through_model_name)

netbox_custom_objects/utilities.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import warnings
22

33
from django.apps import apps
4+
from django.db import connection, connections
45

56
from netbox_custom_objects.constants import APP_LABEL
67

8+
# Import branching components if available
9+
try:
10+
from netbox_branching.contextvars import active_branch
11+
from netbox_branching.database import BranchAwareRouter
12+
BRANCHING_AVAILABLE = True
13+
except ImportError:
14+
BRANCHING_AVAILABLE = False
15+
716
__all__ = (
817
"AppsProxy",
918
"generate_model",
19+
"get_schema_connection",
1020
"get_viewname",
1121
)
1222

@@ -59,6 +69,20 @@ def __getattr__(self, attr):
5969
return getattr(apps, attr)
6070

6171

72+
def get_schema_connection():
73+
"""
74+
Get the appropriate database connection for schema operations.
75+
Uses BranchAwareRouter logic if branching is available and there's an active branch.
76+
"""
77+
if BRANCHING_AVAILABLE:
78+
if branch := active_branch.get():
79+
router = BranchAwareRouter()
80+
branch_connection_name = router._get_connection(branch)
81+
return connections[branch_connection_name]
82+
83+
return connection
84+
85+
6286
def get_viewname(model, action=None, rest_api=False):
6387
"""
6488
Return the view name for the given model and action, if valid.

0 commit comments

Comments
 (0)