Skip to main content

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.

  1. 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_migrated signal is still triggered.
  2. 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.

  1. Full (full). You can use this if there are no relationships between the public and tenant schemas.
  2. 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.
  3. 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

django_tenants_smart_executor-0.3.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_tenants_smart_executor-0.3.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file django_tenants_smart_executor-0.3.0.tar.gz.

File metadata

File hashes

Hashes for django_tenants_smart_executor-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2693fa32de2321d38e940f59c48bebf225963f9e3c00fa54d438e0728a455a72
MD5 75b0e0e1fd0f2ec99c46230dd1ce87af
BLAKE2b-256 5884fdbe13d8a1b5a370fbbfb338adf109675994e69f6305d1e5beec23a7bca0

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_tenants_smart_executor-0.3.0.tar.gz:

Publisher: build.yml on xelixdev/django-tenants-smart-executor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_tenants_smart_executor-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tenants_smart_executor-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a62ec89f119c72322b992ad8c94074b20f6c14a6149806004cfd4891b543b15a
MD5 2cd30cfb5f82aaa10b3f65d66a67d446
BLAKE2b-256 9188a0e9e94cf4e746ea41bbfc322bb6e77719e0ece382571246caa3f92ed4a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_tenants_smart_executor-0.3.0-py3-none-any.whl:

Publisher: build.yml on xelixdev/django-tenants-smart-executor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.3.1

2 files

This release

0.3.0 This release

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page