Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion netbox_branching/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand All @@ -30,9 +33,16 @@ def _get_db(self, model, **hints):

# 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):
Expand Down