From 2a42e6361881224c7914b48a8faeb75c9e5e6408 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 14 Jul 2025 14:52:22 -0400 Subject: [PATCH 1/2] Fixes #246: Correct database router to ensure changelog is read from active branch --- netbox_branching/database.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/netbox_branching/database.py b/netbox_branching/database.py index 33916e7..2d6f13e 100644 --- a/netbox_branching/database.py +++ b/netbox_branching/database.py @@ -24,9 +24,10 @@ def _get_db(self, model, **hints): return # Bail if the model does not support branching - app_label, model_name = model._meta.label.lower().split('.') - if model_name not in registry['model_features']['branching'].get(app_label, []): - return + if model._meta.label != 'core.ObjectChange': + app_label, model_name = model._meta.label.lower().split('.') + if model_name not in registry['model_features']['branching'].get(app_label, []): + return # Return the schema for the active branch (if any) if branch := active_branch.get(): From 68be468d66a08a5f658e832c386f3826cfc4dae4 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 15 Jul 2025 11:29:15 -0400 Subject: [PATCH 2/2] Fixes #246: Always use active branch (if any) when fetching changelog records --- netbox_branching/database.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/netbox_branching/database.py b/netbox_branching/database.py index 2d6f13e..df4bff2 100644 --- a/netbox_branching/database.py +++ b/netbox_branching/database.py @@ -17,6 +17,9 @@ class BranchAwareRouter: """ connection_prefix = 'schema_' + def _get_connection(self, branch): + return f'{self.connection_prefix}{branch.schema_name}' + def _get_db(self, model, **hints): # Warn & exit if branching support has not yet been initialized if 'branching' not in registry['model_features']: @@ -24,16 +27,22 @@ def _get_db(self, model, **hints): return # Bail if the model does not support branching - if model._meta.label != 'core.ObjectChange': - app_label, model_name = model._meta.label.lower().split('.') - if model_name not in registry['model_features']['branching'].get(app_label, []): - return + app_label, model_name = model._meta.label.lower().split('.') + if model_name not in registry['model_features']['branching'].get(app_label, []): + return # Return the schema for the active branch (if any) if branch := active_branch.get(): - return f'{self.connection_prefix}{branch.schema_name}' + return self._get_connection(branch) def db_for_read(self, model, **hints): + + # Always use the active branch (if any) when retrieving changelog records + if model._meta.label == 'core.ObjectChange': + if branch := active_branch.get(): + return self._get_connection(branch) + return + return self._get_db(model, **hints) def db_for_write(self, model, **hints):