Skip to content

Commit 1b0da5d

Browse files
Merge pull request #168 from netboxlabs/develop
Release v0.5.2
2 parents 7db75df + 232327d commit 1b0da5d

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

docs/changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## v0.5.2
4+
5+
### Bug Fixes
6+
7+
* [#163](https://github.com/netboxlabs/netbox-branching/issues/163) - Ensure changelog records for non-branching models are created in main schema
8+
9+
---
10+
311
## v0.5.1
412

513
### Enhancements

netbox_branching/__init__.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from django.conf import settings
22
from django.core.exceptions import ImproperlyConfigured
33

4-
from netbox.plugins import PluginConfig, get_plugin_config
5-
from netbox.registry import registry
4+
from netbox.plugins import PluginConfig
5+
from .utilities import register_models
66

77

88
class AppConfig(PluginConfig):
99
name = 'netbox_branching'
1010
verbose_name = 'NetBox Branching'
1111
description = 'A git-like branching implementation for NetBox'
12-
version = '0.5.1'
12+
version = '0.5.2'
1313
base_url = 'branching'
1414
min_version = '4.1'
1515
middleware = [
@@ -44,23 +44,8 @@ def ready(self):
4444
"netbox_branching: DATABASE_ROUTERS must contain 'netbox_branching.database.BranchAwareRouter'."
4545
)
4646

47-
# Record all object types which support branching in the NetBox registry
48-
exempt_models = (
49-
*constants.EXEMPT_MODELS,
50-
*get_plugin_config('netbox_branching', 'exempt_models'),
51-
)
52-
branching_models = {}
53-
for app_label, models in registry['model_features']['change_logging'].items():
54-
# Wildcard exclusion for all models in this app
55-
if f'{app_label}.*' in exempt_models:
56-
continue
57-
models = [
58-
model for model in models
59-
if f'{app_label}.{model}' not in exempt_models
60-
]
61-
if models:
62-
branching_models[app_label] = models
63-
registry['model_features']['branching'] = branching_models
47+
# Register models which support branching
48+
register_models()
6449

6550

6651
config = AppConfig

netbox_branching/constants.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
# URL query parameter name
1111
QUERY_PARAM = '_branch'
1212

13-
# Tables which must be replicated within a branch even though their
14-
# models don't directly support branching.
15-
REPLICATE_TABLES = (
16-
'dcim_cablepath',
13+
# Models which do not support change logging, but whose database tables
14+
# must be replicated for each branch to ensure proper functionality
15+
INCLUDE_MODELS = (
16+
'dcim.cablepath',
1717
)
1818

1919
# Models for which branching support is explicitly disabled

netbox_branching/database.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def _get_db(self, model, **hints):
2121
warnings.warn(f"Routing database query for {model} before branching support is initialized.")
2222
return
2323

24+
# Bail if the model does not support branching
25+
app_label, model_name = model._meta.label.lower().split('.')
26+
if model_name not in registry['model_features']['branching'].get(app_label, []):
27+
return
28+
2429
# Return the schema for the active branch (if any)
2530
if branch := active_branch.get():
2631
return f'schema_{branch.schema_name}'

netbox_branching/templates/netbox_branching/branch_archive.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<i class="mdi mdi-alert-circle"></i>
2626
{% blocktrans %}
2727
Are you sure you want to archive the branch {{ branch }}? This will permanently deprovision
28-
its database schema, and it will no longer be possible to automatically rever the branch.
28+
its database schema, and it will no longer be possible to automatically revert the branch.
2929
{% endblocktrans %}
3030
</div>
3131
{% render_field form.confirm %}

netbox_branching/utilities.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import datetime
22
import logging
3+
from collections import defaultdict
34
from contextlib import contextmanager
45
from dataclasses import dataclass
56

67
from django.db.models import ForeignKey, ManyToManyField
78
from django.urls import reverse
89

9-
from .constants import REPLICATE_TABLES
10+
from netbox.plugins import get_plugin_config
11+
from netbox.registry import registry
12+
from .constants import EXEMPT_MODELS, INCLUDE_MODELS
1013
from .contextvars import active_branch
1114

1215
__all__ = (
@@ -19,6 +22,7 @@
1922
'get_tables_to_replicate',
2023
'is_api_request',
2124
'record_applied_change',
25+
'register_models',
2226
'update_object',
2327
)
2428

@@ -80,11 +84,41 @@ def get_branchable_object_types():
8084
return ObjectType.objects.with_feature('branching')
8185

8286

87+
def register_models():
88+
"""
89+
Register all models which support branching in the NetBox registry.
90+
"""
91+
# Compile a list of exempt models (those for which change logging may
92+
# be enabled, but branching is not supported)
93+
exempt_models = (
94+
*EXEMPT_MODELS,
95+
*get_plugin_config('netbox_branching', 'exempt_models'),
96+
)
97+
98+
# Register all models which support change logging and are not exempt
99+
branching_models = defaultdict(list)
100+
for app_label, models in registry['model_features']['change_logging'].items():
101+
# Wildcard exclusion for all models in this app
102+
if f'{app_label}.*' in exempt_models:
103+
continue
104+
for model in models:
105+
if f'{app_label}.{model}' not in exempt_models:
106+
branching_models[app_label].append(model)
107+
108+
# Register additional included models
109+
# TODO: Allow plugins to declare additional models?
110+
for label in INCLUDE_MODELS:
111+
app_label, model = label.split('.')
112+
branching_models[app_label].append(model)
113+
114+
registry['model_features']['branching'] = dict(branching_models)
115+
116+
83117
def get_tables_to_replicate():
84118
"""
85119
Return an ordered list of database tables to replicate when provisioning a new schema.
86120
"""
87-
tables = set(REPLICATE_TABLES)
121+
tables = set()
88122

89123
branch_aware_models = [
90124
ot.model_class() for ot in get_branchable_object_types()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "netboxlabs-netbox-branching"
3-
version = "0.5.1"
3+
version = "0.5.2"
44
description = "A git-like branching implementation for NetBox"
55
readme = "README.md"
66
requires-python = ">=3.10"

0 commit comments

Comments
 (0)