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.1.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.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_tenants_smart_executor-0.3.1.tar.gz
Algorithm Hash digest
SHA256 06611430dfd3065452ca82f80f1061819fcde31bb629458627025334b771feaa
MD5 ed3dc333da5ace4d0111e3aa44f4aab4
BLAKE2b-256 80c994130a79d01a356fd91afa2444fe055212f1e9d14e64b34e4fa4be6e6499

See more details on using hashes here.

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

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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tenants_smart_executor-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c1ab5a4c1031bc10bce46fdb7424ef32d9aadf3bebf5034a9aa4c70d71cc09d
MD5 fad97e0ece2cdde0b044972f6458d981
BLAKE2b-256 19def5179d217372f1aeaeda697a7c9ac6b6ebdfe99417f8e4c3d35bb3ffac48

See more details on using hashes here.

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

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

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 files

0.3.0

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