Skip to content

Commit 79026b8

Browse files
Fixes #246: Correct database router to ensure changelog is read from active branch (#301)
* Fixes #246: Correct database router to ensure changelog is read from active branch * Fixes #246: Always use active branch (if any) when fetching changelog records
1 parent 76f41ea commit 79026b8

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

netbox_branching/database.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class BranchAwareRouter:
1717
"""
1818
connection_prefix = 'schema_'
1919

20+
def _get_connection(self, branch):
21+
return f'{self.connection_prefix}{branch.schema_name}'
22+
2023
def _get_db(self, model, **hints):
2124
# Warn & exit if branching support has not yet been initialized
2225
if 'branching' not in registry['model_features']:
@@ -30,9 +33,16 @@ def _get_db(self, model, **hints):
3033

3134
# Return the schema for the active branch (if any)
3235
if branch := active_branch.get():
33-
return f'{self.connection_prefix}{branch.schema_name}'
36+
return self._get_connection(branch)
3437

3538
def db_for_read(self, model, **hints):
39+
40+
# Always use the active branch (if any) when retrieving changelog records
41+
if model._meta.label == 'core.ObjectChange':
42+
if branch := active_branch.get():
43+
return self._get_connection(branch)
44+
return
45+
3646
return self._get_db(model, **hints)
3747

3848
def db_for_write(self, model, **hints):

0 commit comments

Comments
 (0)