-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split read-only and read/write API by the db backends (#37)
- Loading branch information
Showing
3 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import random | ||
from nopayloaddb.middleware import get_current_request | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class ReadWriteRouter: | ||
def db_for_read(self, model, **hints): | ||
"""Route read queries to one of the read databases.""" | ||
db = random.choice(['read_db_1', 'read_db_2']) | ||
# Check if request information is available | ||
request = get_current_request() | ||
if request and request.method == 'GET': | ||
return db | ||
return 'default' | ||
|
||
def db_for_write(self, model, **hints): | ||
"""Route write queries to the default database.""" | ||
return 'default' | ||
|
||
def allow_relation(self, obj1, obj2, **hints): | ||
"""Allow any relation if both objects are in the same database.""" | ||
db_set = {'default', 'read_db_1', 'read_db_2'} | ||
if obj1._state.db in db_set and obj2._state.db in db_set: | ||
return True | ||
return None | ||
|
||
def allow_migrate(self, db, app_label, model_name=None, **hints): | ||
"""Ensure migrations only run on the default database.""" | ||
return db == 'default' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from threading import local | ||
|
||
# Local storage for storing the current request | ||
_request_local = local() | ||
|
||
def get_current_request(): | ||
return getattr(_request_local, 'request', None) | ||
|
||
class RequestMiddleware: | ||
"""Middleware to store the current request for use in the DB router.""" | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
# Save the request in the local thread storage | ||
_request_local.request = request | ||
response = self.get_response(request) | ||
# Clear the request after processing to avoid conflicts | ||
_request_local.request = None | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters