Django Tenants Smart Executor
When using django-tenants, running migrations becomes quite a problem time-wise, especially as your list of tenants grows and the number of your apps and models increases. This is the case even if there are actually no migrations to run.
This package provides two methods of speeding up migrations for django-tenants.
- The first method is to skip running the migration on a schema if there are no migrations to run. If there are no migrations to run, the
schema_migratedsignal is still triggered. - The second method is to skip applying the state part of migrations in a schema when running a migration from the other schema. For example, if running a migration in the public schema, applying the tenant app state migrations is not necessary.
Installation
Install using pip (or your package manager of choice):
pip install django-tenants-smart-executor
Skipping schema migration if there are no migrations to run
Configure the GET_EXECUTOR_FUNCTION in your settings.py:
GET_EXECUTOR_FUNCTION = "django_tenants_smart_executor.load_executor"
This will tell django-tenants to use the executor from this package, which will skip the migrations if there is nothing to run.
When you run migrations when all tenants are migrated, the output will look something like this:
$ python manage.py migrate_schemas
No migrations needed for schema public, only triggering signals
No migrations needed for schema test, only triggering signals
The multiprocessing executor is also supported.
$ python manage.py migrate_schemas --executor multiprocessing
No migrations needed for schema public, only triggering signals
No migrations needed for schema test, only triggering signals
Skipping state migration on schema
By default, this functionality is off. If you want to use it, you need to use the GET_EXECUTOR_FUNCTION configuration mentioned above.
There are three possible modes for this functionality, configured by the SMART_EXECUTOR_LIMIT_STATE_TO_SCHEMA setting.
- Full (
full). You can use this if there are no relationships between the public and tenant schemas. - Public (
public). This will skip the state migrations of tenant apps when running on public schema. This is safe to use when there are no relationships from the tenant schema to the public schema. - Tenant (
tenant). This will skip the state migrations of public apps when running on tenant schema. This is safe to use when there are no relationships from the public schema to the tenant schema.
On top of this, you can further configure exceptions with SMART_EXECUTOR_LIMIT_STATE_EXCEPTIONS_MAP. That option should be a dictionary of boolean (whether it's in the tenant schema) to iterables of apps which should be migrated regardless of the mode.
For example, with the following settings, the only public app that will have state migrations applied in the tenant schemas will be account. There will be no state migrations for tenant apps performed in public schema.
SMART_EXECUTOR_LIMIT_STATE_TO_SCHEMA = "full"
SMART_EXECUTOR_LIMIT_STATE_EXCEPTIONS_MAP: dict[bool, set[str]] = {
True: { # in tenant schema, do migrate these public apps
"account",
},
False: set(), # in public schema, there are no exceptions for any tenant apps
}
Then, you will need to replace every single django.db.migrations import with django_tenants_smart_executor.migrations in your migration files. This is to use the modified version which can skip the state migration. All public classes and functions from the module are reexported, so you can do a simple find & replace.
The ruff rule TID251 is useful for enforcing this.
[ruff.lint.flake8-tidy-imports.banned-api]
"django.db.migrations" = { msg = "Use django_tenants_smart_executor.migrations" }
When to use this
Because this option is disabling state operations, you should only run this when applying migrations, not when creating new migrations or linting them using various tools. You may want to only run this in tests to speed up your tests, but in theory applying in production should be fine as well. Definitely do not contigure this to run when running makemigrations.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_tenants_smart_executor-0.3.1.tar.gz.
File metadata
- Download URL: django_tenants_smart_executor-0.3.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06611430dfd3065452ca82f80f1061819fcde31bb629458627025334b771feaa
|
|
| MD5 |
ed3dc333da5ace4d0111e3aa44f4aab4
|
|
| BLAKE2b-256 |
80c994130a79d01a356fd91afa2444fe055212f1e9d14e64b34e4fa4be6e6499
|
Provenance
The following attestation bundles were made for django_tenants_smart_executor-0.3.1.tar.gz:
Publisher:
build.yml on xelixdev/django-tenants-smart-executor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_tenants_smart_executor-0.3.1.tar.gz -
Subject digest:
06611430dfd3065452ca82f80f1061819fcde31bb629458627025334b771feaa - Sigstore transparency entry: 2615498452
- Sigstore integration time:
-
Permalink:
xelixdev/django-tenants-smart-executor@91c329ff57c479956bad81fbcb0c0f3dd8b2ffaa -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/xelixdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@91c329ff57c479956bad81fbcb0c0f3dd8b2ffaa -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_tenants_smart_executor-0.3.1-py3-none-any.whl.
File metadata
- Download URL: django_tenants_smart_executor-0.3.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c1ab5a4c1031bc10bce46fdb7424ef32d9aadf3bebf5034a9aa4c70d71cc09d
|
|
| MD5 |
fad97e0ece2cdde0b044972f6458d981
|
|
| BLAKE2b-256 |
19def5179d217372f1aeaeda697a7c9ac6b6ebdfe99417f8e4c3d35bb3ffac48
|
Provenance
The following attestation bundles were made for django_tenants_smart_executor-0.3.1-py3-none-any.whl:
Publisher:
build.yml on xelixdev/django-tenants-smart-executor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_tenants_smart_executor-0.3.1-py3-none-any.whl -
Subject digest:
4c1ab5a4c1031bc10bce46fdb7424ef32d9aadf3bebf5034a9aa4c70d71cc09d - Sigstore transparency entry: 2615498494
- Sigstore integration time:
-
Permalink:
xelixdev/django-tenants-smart-executor@91c329ff57c479956bad81fbcb0c0f3dd8b2ffaa -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/xelixdev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@91c329ff57c479956bad81fbcb0c0f3dd8b2ffaa -
Trigger Event:
push
-
Statement type: