├── .github └── workflows │ ├── build.yml │ ├── codecov.yml │ └── pythonpublish.yml ├── .gitignore ├── LICENSE ├── README.md ├── database_routing └── __init__.py ├── requirements.txt ├── setup.py ├── testproject ├── manage.py ├── testapp │ ├── __init__.py │ ├── migrations │ │ ├── 0001_initial.py │ │ └── __init__.py │ ├── models.py │ └── tests.py └── testproject │ ├── __init__.py │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── tox.ini /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | max-parallel: 4 10 | matrix: 11 | python-version: [3.6, 3.7, 3.8, 3.9, "3.10"] 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Set up Python ${{ matrix.python-version }} 16 | uses: actions/setup-python@v2 17 | with: 18 | python-version: ${{ matrix.python-version }} 19 | - name: Install dependencies 20 | run: | 21 | python -m pip install --upgrade pip 22 | pip install tox tox-gh-actions wheel 23 | sed -i '/Django==.*/d' ./requirements.txt # delete django dependency 24 | - name: Test with tox 25 | run: | 26 | tox 27 | python setup.py sdist bdist_wheel install 28 | -------------------------------------------------------------------------------- /.github/workflows/codecov.yml: -------------------------------------------------------------------------------- 1 | name: codecov 2 | on: [push, pull_request] 3 | jobs: 4 | run: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - uses: actions/checkout@master 8 | - name: Setup Python 9 | uses: actions/setup-python@v2 10 | with: 11 | python-version: '3.x' 12 | - name: Generate coverage report 13 | run: | 14 | pip install -r requirements.txt 15 | pip install coverage 16 | pip install -q -e . 17 | coverage run testproject/manage.py test testproject/ 18 | coverage xml 19 | - name: Upload coverage to Codecov 20 | uses: codecov/codecov-action@v2 21 | with: 22 | token: ${{ secrets.CODECOV_TOKEN }} 23 | file: ./coverage.xml 24 | flags: unittests 25 | name: codecov-umbrella 26 | fail_ci_if_error: true 27 | -------------------------------------------------------------------------------- /.github/workflows/pythonpublish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will upload a Python Package using Twine when a release is created 2 | 3 | name: Upload Python Package 4 | 5 | on: 6 | release: 7 | types: [published] 8 | 9 | jobs: 10 | deploy: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Set up Python 17 | uses: actions/setup-python@v1 18 | with: 19 | python-version: '3.x' 20 | - name: Install dependencies 21 | run: | 22 | python -m pip install --upgrade pip 23 | pip install setuptools wheel twine 24 | - name: Build and publish 25 | env: 26 | TWINE_USERNAME: '__token__' 27 | TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} 28 | run: | 29 | python setup.py sdist bdist_wheel --universal 30 | twine upload dist/* 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | .idea 131 | /testproject/primary.sqlite3 132 | /testproject/replica.sqlite3 133 | /testproject/tag_primary.sqlite3 134 | /testproject/tag_replica.sqlite3 135 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | django-database-routing 2 | =================== 3 | 4 | Provides Primary/Replica database router for Django. 5 | See https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example for example implementation. 6 | 7 | ![build](https://github.com/just-work/django-database-routing/workflows/build/badge.svg?branch=master) 8 | 9 | Configuration 10 | ------------- 11 | 1. Add router to settings.py 12 | ```python 13 | DATABASE_ROUTERS = ['database_routing.PrimaryReplicaRouter'] 14 | 15 | ``` 16 | 2. Configure 'default' and 'replica' connections in `settings.DATABASES` 17 | 3. If needed you can force specific connections for some apps or models: 18 | ```python 19 | PRIMARY_REPLICA_ROUTING = { 20 | 'my_app.MyModel': { 21 | 'read': 'custom_connection', 22 | 'write': 'custom_connection 23 | } 24 | } 25 | 26 | ``` 27 | 28 | Forcing reading from primary 29 | --------------------------- 30 | 31 | When transaction isolation level or replication lag causing bugs in your project, you can force your code 32 | to read all the data from `default` (or primary) database. 33 | 34 | ```python 35 | from database_routing import force_primary_read 36 | @force_primary_read 37 | def do_some_reads_and_updates(): 38 | # All Django ORM queries are going to 'default' database here. 39 | # ... 40 | 41 | ``` 42 | 43 | -------------------------------------------------------------------------------- /database_routing/__init__.py: -------------------------------------------------------------------------------- 1 | import functools 2 | 3 | from django.db import connections 4 | from django.conf import settings 5 | 6 | 7 | class PrimaryReplicaRouter: 8 | """Django database router for Primary/Replica replication scheme support. 9 | 10 | Example configuration: 11 | 12 | PRIMARY_REPLICA_ROUTING = { 13 | 'my_app.MySQLModel': { 14 | 'read': 'mysql_replica', 15 | 'write': 'mysql_default' 16 | }, 17 | 'postgres_app': { 18 | 'read': 'psql_replica', 19 | 'write': 'psql_primary' 20 | } 21 | } 22 | 23 | DATABASE_ROUTERS = ['database_routing.PrimaryReplicaRouter'] 24 | 25 | If model is not present in PRIMARY_REPLICA_ROUTING setting, returns 26 | 'default' connection for write and 'replica' connection for read 27 | """ 28 | _lookup_cache = {} 29 | 30 | default_read = 'replica' 31 | default_write = 'default' 32 | 33 | def get_db_config(self, model): 34 | """ Returns the database configuration for `model`.""" 35 | app_label = model._meta.app_label 36 | model_name = model._meta.model_name 37 | model_label = '%s.%s' % (app_label, model_name) 38 | 39 | if model_label not in self._lookup_cache: 40 | conf = getattr(settings, 'PRIMARY_REPLICA_ROUTING', {}) 41 | 42 | if model_label in conf: 43 | result = conf[model_label] 44 | elif app_label in conf: 45 | result = conf[app_label] 46 | else: 47 | result = {} 48 | self._lookup_cache[model_label] = result 49 | return self._lookup_cache[model_label] 50 | 51 | def db_for_read(self, model, **hints): 52 | db_config = self.get_db_config(model) 53 | return db_config.get('read', self.default_read) 54 | 55 | def db_for_write(self, model, **hints): 56 | db_config = self.get_db_config(model) 57 | return db_config.get('write', self.default_write) 58 | 59 | def allow_syncdb(self, db, model): 60 | """ Schema creation allowed only for write DB.""" 61 | syncdb = self.db_for_write(model) 62 | return db == syncdb 63 | 64 | def allow_relation(self, obj1, obj2, **hints): 65 | """ Relations are allowed only from one database.""" 66 | db_for_write_1 = self.db_for_write(obj1) 67 | db_for_write_2 = self.db_for_write(obj2) 68 | return db_for_write_1 == db_for_write_2 69 | 70 | 71 | class ForcePrimaryRead: 72 | """ Context manager that switches all reads to Primary database. 73 | 74 | """ 75 | 76 | def __enter__(self): 77 | """ Sets Primary as db_for_read 78 | 79 | :return: write-enabled connection 80 | """ 81 | self._prev_read = PrimaryReplicaRouter.default_read 82 | PrimaryReplicaRouter.default_read = PrimaryReplicaRouter.default_write 83 | return connections[PrimaryReplicaRouter.default_read] 84 | 85 | def __exit__(self, exc_type, exc_val, exc_tb): 86 | """ Resets db_for_read to it's previous value.""" 87 | PrimaryReplicaRouter.default_read = self._prev_read 88 | 89 | 90 | def force_primary_read(func): 91 | """ Decorates a func with ForcePrimaryRead context. 92 | 93 | :param func: any callable that needs to do reads from Primary DB 94 | :returns: decorated function 95 | 96 | Example: 97 | 98 | @force_primary_read 99 | def do_some_update(): 100 | # reading obj from Primary database 101 | obj = MyModel.objects.first() 102 | obj.field = 'value' 103 | obj.save() 104 | """ 105 | 106 | @functools.wraps(func) 107 | def wrapper(*args, **kwargs): 108 | with ForcePrimaryRead(): 109 | return func(*args, **kwargs) 110 | 111 | return wrapper 112 | 113 | 114 | def force_primary_read_method(methods=()): 115 | """ Decorates some methods of class with ForcePrimaryRead context. 116 | 117 | :param methods: list-like, names of methods need to be decorated 118 | :returns decorated class 119 | 120 | Example: 121 | 122 | @force_primary_read_methods(methods=['do_some_update']) 123 | class MyModelUpdater(object): 124 | 125 | def do_some_update(self): 126 | # reading obj from Primary database 127 | obj = MyModel.objects.first() 128 | obj.field = 'value' 129 | obj.save() 130 | 131 | def do_other_update(self): 132 | # reading obj from Replica database 133 | obj = MyModel.objects.last() 134 | obj.field = 'dirty' 135 | obj.save() 136 | 137 | """ 138 | 139 | def decorator(cls): 140 | for m in methods: 141 | # decorate methods with force_primary_read decorator 142 | if hasattr(cls, m): 143 | setattr(cls, m, force_primary_read(getattr(cls, m))) 144 | return cls 145 | 146 | return decorator 147 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==4.1 2 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | import subprocess 4 | from setuptools import setup, find_packages # type: ignore 5 | from pathlib import Path 6 | 7 | with open('README.md') as f: 8 | long_description = f.read() 9 | 10 | version_re = re.compile('^Version: (.+)$', re.M) 11 | package_name = 'django-database-routing' 12 | 13 | 14 | def get_version(): 15 | """ 16 | Reads version from git status or PKG-INFO 17 | 18 | https://gist.github.com/pwithnall/7bc5f320b3bdf418265a 19 | """ 20 | d: Path = Path(__file__).absolute().parent 21 | git_dir = d.joinpath('.git') 22 | if git_dir.is_dir(): 23 | # Get the version using "git describe". 24 | cmd = 'git describe --tags --match [0-9]*'.split() 25 | try: 26 | version = subprocess.check_output(cmd).decode().strip() 27 | except subprocess.CalledProcessError: 28 | return None 29 | 30 | # PEP 386 compatibility 31 | if '-' in version: 32 | version = '.post'.join(version.split('-')[:2]) 33 | 34 | # Don't declare a version "dirty" merely because a time stamp has 35 | # changed. If it is dirty, append a ".dev1" suffix to indicate a 36 | # development revision after the release. 37 | with open(os.devnull, 'w') as fd_devnull: 38 | subprocess.call(['git', 'status'], 39 | stdout=fd_devnull, stderr=fd_devnull) 40 | 41 | cmd = 'git diff-index --name-only HEAD'.split() 42 | try: 43 | dirty = subprocess.check_output(cmd).decode().strip() 44 | except subprocess.CalledProcessError: 45 | return None 46 | 47 | if dirty != '': 48 | version += '.dev1' 49 | else: 50 | # Extract the version from the PKG-INFO file. 51 | try: 52 | with open('PKG-INFO') as v: 53 | version = version_re.search(v.read()).group(1) 54 | except FileNotFoundError: 55 | version = None 56 | 57 | return version 58 | 59 | 60 | setup( 61 | name=package_name, 62 | version=get_version() or 'dev', 63 | long_description=long_description, 64 | long_description_content_type='text/markdown', 65 | packages=['database_routing'], 66 | install_requres=[ 67 | 'Django' 68 | ], 69 | url='https://github.com/just-work/django-database-routing', 70 | license='Apache License v2.0', 71 | author='tumbler', 72 | author_email='zimbler@gmail.com', 73 | description='''Provides primary-replica routing 74 | and force primary context manager for Django''', 75 | classifiers=[ 76 | 'Development Status :: 4 - Beta', 77 | 'Environment :: Console', 78 | 'Framework :: Django :: 2.2', 79 | 'Framework :: Django :: 3.0', 80 | 'Framework :: Django :: 3.1', 81 | 'Framework :: Django :: 3.2', 82 | 'Framework :: Django :: 4.0', 83 | 'Framework :: Django :: 4.1', 84 | 'Operating System :: POSIX', 85 | 'Programming Language :: Python :: 3.6', 86 | 'Programming Language :: Python :: 3.7', 87 | 'Programming Language :: Python :: 3.8', 88 | 'Programming Language :: Python :: 3.9', 89 | 'Programming Language :: Python :: 3.10', 90 | ('Topic :: Internet :: WWW/HTTP :: Dynamic Content :: ' 91 | 'Content Management System'), 92 | ] 93 | ) 94 | -------------------------------------------------------------------------------- /testproject/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testproject.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /testproject/testapp/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/just-work/django-database-routing/d5825b8800ba0b02b9cea15e02c5af1e2bbd5f11/testproject/testapp/__init__.py -------------------------------------------------------------------------------- /testproject/testapp/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.8 on 2021-10-27 14:17 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | initial = True 10 | 11 | dependencies = [ 12 | ] 13 | 14 | operations = [ 15 | migrations.CreateModel( 16 | name='Project', 17 | fields=[ 18 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 19 | ('name', models.CharField(max_length=10, unique=True)), 20 | ], 21 | ), 22 | migrations.CreateModel( 23 | name='Tag', 24 | fields=[ 25 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 26 | ('title', models.CharField(max_length=255)), 27 | ], 28 | ), 29 | migrations.CreateModel( 30 | name='Task', 31 | fields=[ 32 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 33 | ('name', models.CharField(max_length=10, unique=True)), 34 | ('project', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='testapp.project')), 35 | ], 36 | ), 37 | migrations.AddField( 38 | model_name='project', 39 | name='tags', 40 | field=models.ManyToManyField(blank=True, to='testapp.Tag'), 41 | ), 42 | ] 43 | -------------------------------------------------------------------------------- /testproject/testapp/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/just-work/django-database-routing/d5825b8800ba0b02b9cea15e02c5af1e2bbd5f11/testproject/testapp/migrations/__init__.py -------------------------------------------------------------------------------- /testproject/testapp/models.py: -------------------------------------------------------------------------------- 1 | from django.contrib.contenttypes.models import ContentType 2 | from django.db import models 3 | 4 | 5 | class Tag(models.Model): 6 | title = models.CharField(max_length=255) 7 | 8 | 9 | class Project(models.Model): 10 | name = models.CharField(max_length=10, unique=True) 11 | tags = models.ManyToManyField(Tag, blank=True) 12 | 13 | 14 | class Task(models.Model): 15 | project = models.ForeignKey(Project, models.PROTECT) 16 | name = models.CharField(max_length=10, unique=True) 17 | 18 | -------------------------------------------------------------------------------- /testproject/testapp/tests.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | 3 | from django.conf import settings 4 | from django.db import models, router as db_router 5 | from django.db.models import Subquery 6 | from django.test import TestCase 7 | 8 | from database_routing import (ForcePrimaryRead, 9 | force_primary_read, 10 | force_primary_read_method, 11 | PrimaryReplicaRouter) 12 | from testapp.models import Project, Tag, Task 13 | 14 | 15 | class DBRoutingTestCase(TestCase): 16 | """ Database router test. """ 17 | 18 | multi_db = True 19 | databases = {'default', 'replica', 'tag_primary', 'tag_replica'} 20 | 21 | def setUp(self) -> None: 22 | self.router = PrimaryReplicaRouter() 23 | 24 | def test_default_primary_write(self): 25 | """ Default primary write test. """ 26 | project = Project.objects.create(name='test', id=1) 27 | 28 | primary_qs = Project.objects.using('default').all() 29 | replica_qs = Project.objects.using('replica').all() 30 | 31 | self.assertEqual(primary_qs.count(), 1) 32 | self.assertEqual(primary_qs.first(), project) 33 | self.assertEqual(replica_qs.count(), 0) 34 | 35 | def test_default_replica_read(self): 36 | """ Default replica read test. """ 37 | project = Project.objects.using('replica').create(name='test', id=1) 38 | 39 | primary_qs = Project.objects.using('default').all() 40 | replica_qs = Project.objects.all() # read from replica 41 | 42 | self.assertEqual(primary_qs.count(), 0) 43 | self.assertEqual(replica_qs.count(), 1) 44 | self.assertEqual(replica_qs.first(), project) 45 | 46 | def test_primary_write__if_defined_for_model(self): 47 | """ Primary write test, overridden for model. """ 48 | tag = Tag.objects.create(title='test', id=1) 49 | 50 | primary_qs = Tag.objects.using('tag_primary').all() 51 | replica_qs = Tag.objects.using('tag_replica').all() 52 | 53 | self.assertEqual(primary_qs.count(), 1) 54 | self.assertEqual(primary_qs.first(), tag) 55 | self.assertEqual(replica_qs.count(), 0) 56 | 57 | def test_replica_read__if_defined_for_model(self): 58 | """ Replica read test, overridden for model.""" 59 | tag = Tag.objects.using('tag_replica').create(title='test', id=1) 60 | 61 | primary_qs = Tag.objects.using('tag_primary').all() 62 | replica_qs = Tag.objects.all() # read from Tag_replica 63 | 64 | self.assertEqual(primary_qs.count(), 0) 65 | self.assertEqual(replica_qs.count(), 1) 66 | self.assertEqual(replica_qs.first(), tag) 67 | 68 | def test_context_manager_force_primary_read(self): 69 | """ Context manager test for reading from the primary. """ 70 | project = Project.objects.using('default').create(name='test', id=1) 71 | 72 | with ForcePrimaryRead(): 73 | primary_count = Project.objects.all().count() 74 | primary_project = Project.objects.get(id=project.id) 75 | 76 | self.assertEqual(primary_count, 1) 77 | self.assertEqual(primary_project, project) 78 | 79 | def test_decorator_force_primary_read(self): 80 | """ Decorator test for reading from primary. """ 81 | project = Project.objects.using('default').create(name='test', id=1) 82 | 83 | @force_primary_read 84 | def read_from_primary(project_id: int) -> (int, Any): 85 | count = Project.objects.all().count() 86 | obj = Project.objects.get(id=project_id) 87 | return count, obj 88 | 89 | primary_count, primary_project = read_from_primary(project.id) 90 | 91 | self.assertEqual(primary_count, 1) 92 | self.assertEqual(primary_project, project) 93 | 94 | def test_class_decorator_force_primary_read(self): 95 | """ Class decorator test for reading from primary. """ 96 | project = Project.objects.using('default').create(name='test', id=1) 97 | 98 | @force_primary_read_method(methods=['read']) 99 | class PrimaryReader: 100 | def read(self, project_id: int) -> (int, Any): 101 | count = Project.objects.all().count() 102 | obj = Project.objects.get(id=project_id) 103 | return count, obj 104 | 105 | primary_count, primary_project = PrimaryReader().read(project.id) 106 | 107 | self.assertEqual(primary_count, 1) 108 | self.assertEqual(primary_project, project) 109 | 110 | def test_allowed_relation__if_from_one_db(self): 111 | """ Test relations, if objects are from one DB. """ 112 | project = Project.objects.create(name='test', id=1) 113 | 114 | Task.objects.create(name='default', project=project) # w/o exception 115 | 116 | def test_denied_relation__if_from_different_db(self): 117 | """ Error relation test if objects are from different DB. """ 118 | project = Project.objects.create(name='primary db', id=1) 119 | tag = Tag.objects.create(title='tag db') 120 | 121 | with self.assertRaises(ValueError): 122 | project.tags.add(tag) 123 | 124 | def test_get_default_db_config(self): 125 | """ Test of DB configuration retrieval. """ 126 | default_config = self.router.get_db_config(Project) 127 | custom_config = self.router.get_db_config(Tag) 128 | 129 | expected = settings.PRIMARY_REPLICA_ROUTING['testapp.tag'] 130 | 131 | self.assertEqual(default_config, {}) 132 | self.assertEqual(custom_config, expected) 133 | 134 | def test_get_db_for_read(self): 135 | """ Test of getting the DB for reading. """ 136 | default_db = self.router.db_for_read(Project) 137 | custom_db = self.router.db_for_read(Tag) 138 | 139 | expected = settings.PRIMARY_REPLICA_ROUTING['testapp.tag']['read'] 140 | 141 | self.assertEqual(default_db, 'replica') 142 | self.assertEqual(custom_db, expected) 143 | 144 | def test_get_db_for_write(self): 145 | """ Test of getting a DB for writing. """ 146 | default_db = self.router.db_for_write(Project) 147 | custom_db = self.router.db_for_write(Tag) 148 | 149 | expected = settings.PRIMARY_REPLICA_ROUTING['testapp.tag']['write'] 150 | 151 | self.assertEqual(default_db, 'default') 152 | self.assertEqual(custom_db, expected) 153 | 154 | def test_allow_syncdb(self): 155 | """ Test of schema creation.""" 156 | write_db = self.router.allow_syncdb('default', Project) 157 | read_db = self.router.allow_syncdb('replica', Project) 158 | custom_write_db = self.router.allow_syncdb('tag_primary', Tag) 159 | custom_read_db = self.router.allow_syncdb('tag_replica', Tag) 160 | 161 | self.assertTrue(write_db) 162 | self.assertFalse(read_db) 163 | self.assertTrue(custom_write_db) 164 | self.assertFalse(custom_read_db) 165 | 166 | def test_allow_relation(self): 167 | """ Relations are allowed only from one DB. """ 168 | allow_relation = db_router.allow_relation(Project, Task) 169 | deny_relation = db_router.allow_relation(Project, Tag) 170 | 171 | self.assertTrue(allow_relation) 172 | self.assertFalse(deny_relation) 173 | 174 | def test_filter_for_diff_db(self): 175 | """ Filter by related field from different DB. """ 176 | project = Project.objects.create(name='primary db', id=1) 177 | Task.objects.create(name='default', project=project) 178 | 179 | with ForcePrimaryRead(): 180 | tasks = Task.objects.filter(project__tags__isnull=True) 181 | tags = tasks.first().project.tags.all() 182 | 183 | self.assertEqual(len(tags), 0) 184 | 185 | def test_values_for_diff_db(self): 186 | """ Getting the values of a related field from different DB. """ 187 | project = Project.objects.create(name='primary db', id=1) 188 | Task.objects.create(name='default', project=project) 189 | 190 | with ForcePrimaryRead(): 191 | values = Project.objects.values('tags__title') 192 | 193 | self.assertEqual(values[0]['tags__title'], None) 194 | 195 | def test_subquery_for_diff_db(self): 196 | """ Test subquery from different DB. """ 197 | tag = Tag.objects.create(title='my tag') 198 | project = Project.objects.create(name='primary db', id=1) 199 | task = Task.objects.create(name='my task', project=project) 200 | 201 | tags = Tag.objects.using('tag_primary').filter( 202 | pk=tag.pk 203 | ).values('title') 204 | tasks = Task.objects.using('default').filter( 205 | pk=task.pk 206 | ).values('name') 207 | 208 | with ForcePrimaryRead(): 209 | project = Project.objects.annotate( 210 | tag_title=Subquery(tags, output_field=models.CharField()), 211 | task_name=Subquery(tasks, output_field=models.CharField()) 212 | ).first() 213 | 214 | self.assertEqual(tags[0]['title'], 'my tag') 215 | self.assertEqual(project.task_name, 'my task') 216 | self.assertEqual(project.tag_title, None) # since different DB 217 | 218 | def test_allow_migrate(self): 219 | """ Migrate are allowed for all DB. """ 220 | self.assertTrue(db_router.allow_migrate('default', 'testapp')) 221 | self.assertTrue(db_router.allow_migrate('replica', 'testapp')) 222 | self.assertTrue(db_router.allow_migrate('tag_primary', 'testapp')) 223 | self.assertTrue(db_router.allow_migrate('tag_replica', 'testapp')) 224 | 225 | def test_allow_migrate_model(self): 226 | """ Migrate model are allowed for all DB. """ 227 | self.assertTrue(db_router.allow_migrate_model('default', Project)) 228 | self.assertTrue(db_router.allow_migrate_model('default', Tag)) 229 | 230 | self.assertTrue(db_router.allow_migrate_model('replica', Project)) 231 | self.assertTrue(db_router.allow_migrate_model('replica', Tag)) 232 | 233 | self.assertTrue(db_router.allow_migrate_model('tag_primary', Project)) 234 | self.assertTrue(db_router.allow_migrate_model('tag_primary', Tag)) 235 | 236 | self.assertTrue(db_router.allow_migrate_model('tag_replica', Project)) 237 | self.assertTrue(db_router.allow_migrate_model('tag_replica', Tag)) 238 | -------------------------------------------------------------------------------- /testproject/testproject/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/just-work/django-database-routing/d5825b8800ba0b02b9cea15e02c5af1e2bbd5f11/testproject/testproject/__init__.py -------------------------------------------------------------------------------- /testproject/testproject/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for testproject project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testproject.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /testproject/testproject/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for testproject project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.2.8. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.0/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | # Quick-start development settings - unsuitable for production 19 | # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ 20 | 21 | # SECURITY WARNING: keep the secret key used in production secret! 22 | SECRET_KEY = 'tpyy!@wn2^#f9qq3lug(f!g&(p!wi3%p$1-tlt(*(*3)-u==_)' 23 | 24 | # SECURITY WARNING: don't run with debug turned on in production! 25 | DEBUG = True 26 | 27 | ALLOWED_HOSTS = [] 28 | 29 | # Application definition 30 | 31 | INSTALLED_APPS = [ 32 | 'django.contrib.admin', 33 | 'django.contrib.auth', 34 | 'django.contrib.contenttypes', 35 | 'django.contrib.sessions', 36 | 'django.contrib.messages', 37 | 'django.contrib.staticfiles', 38 | 39 | 'testapp', 40 | ] 41 | 42 | MIDDLEWARE = [ 43 | 'django.middleware.security.SecurityMiddleware', 44 | 'django.contrib.sessions.middleware.SessionMiddleware', 45 | 'django.middleware.common.CommonMiddleware', 46 | 'django.middleware.csrf.CsrfViewMiddleware', 47 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 48 | 'django.contrib.messages.middleware.MessageMiddleware', 49 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 50 | ] 51 | 52 | ROOT_URLCONF = 'testproject.urls' 53 | 54 | TEMPLATES = [ 55 | { 56 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 57 | 'DIRS': [], 58 | 'APP_DIRS': True, 59 | 'OPTIONS': { 60 | 'context_processors': [ 61 | 'django.template.context_processors.debug', 62 | 'django.template.context_processors.request', 63 | 'django.contrib.auth.context_processors.auth', 64 | 'django.contrib.messages.context_processors.messages', 65 | ], 66 | }, 67 | }, 68 | ] 69 | 70 | WSGI_APPLICATION = 'testproject.wsgi.application' 71 | 72 | # Database 73 | # https://docs.djangoproject.com/en/3.0/ref/settings/#databases 74 | 75 | # noinspection PyUnresolvedReferences 76 | DATABASES = { 77 | 'default': { 78 | 'ENGINE': 'django.db.backends.sqlite3', 79 | 'NAME': os.path.join(BASE_DIR, 'primary.sqlite3'), 80 | }, 81 | 'replica': { 82 | 'ENGINE': 'django.db.backends.sqlite3', 83 | 'NAME': os.path.join(BASE_DIR, 'replica.sqlite3'), 84 | }, 85 | 'tag_primary': { 86 | 'ENGINE': 'django.db.backends.sqlite3', 87 | 'NAME': os.path.join(BASE_DIR, 'tag_primary.sqlite3'), 88 | }, 89 | 'tag_replica': { 90 | 'ENGINE': 'django.db.backends.sqlite3', 91 | 'NAME': os.path.join(BASE_DIR, 'tag_replica.sqlite3'), 92 | } 93 | } 94 | 95 | PRIMARY_REPLICA_ROUTING = { 96 | 'testapp.tag': { 97 | 'read': 'tag_replica', 98 | 'write': 'tag_primary' 99 | } 100 | } 101 | 102 | DATABASE_ROUTERS = ['database_routing.PrimaryReplicaRouter'] 103 | 104 | # Password validation 105 | # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators 106 | 107 | AUTH_PASSWORD_VALIDATORS = [ 108 | { 109 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 110 | }, 111 | { 112 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 113 | }, 114 | { 115 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 116 | }, 117 | { 118 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 119 | }, 120 | ] 121 | 122 | # Internationalization 123 | # https://docs.djangoproject.com/en/3.0/topics/i18n/ 124 | 125 | LANGUAGE_CODE = 'en-us' 126 | 127 | TIME_ZONE = 'UTC' 128 | 129 | USE_I18N = True 130 | 131 | USE_L10N = True 132 | 133 | USE_TZ = True 134 | 135 | # Static files (CSS, JavaScript, Images) 136 | # https://docs.djangoproject.com/en/3.0/howto/static-files/ 137 | 138 | STATIC_URL = '/static/' 139 | 140 | DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' 141 | 142 | # Mocked file storage for tests 143 | DEFAULT_FILE_STORAGE = 'inmemorystorage.InMemoryStorage' 144 | -------------------------------------------------------------------------------- /testproject/testproject/urls.py: -------------------------------------------------------------------------------- 1 | """testproject URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.0/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | ] 22 | -------------------------------------------------------------------------------- /testproject/testproject/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for testproject project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testproject.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = 3 | {py3.6,py3.7,py3.8,py3.9,py3.10}-django2.1 4 | {py3.6,py3.7,py3.8,py3.9,py3.10}-django2.2 5 | {py3.6,py3.7,py3.8,py3.9,py3.10}-django3.0 6 | {py3.6,py3.7,py3.8,py3.9,py3.10}-django3.1 7 | {py3.6,py3.7,py3.8,py3.9,py3.10}-django3.2 8 | {py3.8,py3.9,py3.10}-django4.0 9 | {py3.8,py3.9,py3.10}-django4.1 10 | 11 | [gh-actions] 12 | python = 13 | 3.6: py3.6 14 | 3.7: py3.7 15 | 3.8: py3.8 16 | 3.9: py3.9 17 | 3.10: py3.10 18 | 19 | [testenv] 20 | changedir = ./testproject 21 | basepython = 22 | py3.6: python3.6 23 | py3.7: python3.7 24 | py3.8: python3.8 25 | py3.9: python3.9 26 | py3.10: python3.10 27 | deps = 28 | django2.1: Django~=2.1.0 29 | django2.2: Django~=2.2.0 30 | django3.0: Django~=3.0.0 31 | django3.1: Django~=3.1.0 32 | django3.2: Django~=3.2.0 33 | django4.0: Django~=4.0.0 34 | django4.1: Django~=4.1.0 35 | commands = python manage.py test --------------------------------------------------------------------------------