django-dmf (Django Data Migration Framework)
A Version Controlled, Fully Managed Data Migration Framework for Django that kicks in post Django's built-in Schema Migration.
django-dmf helps to keep Schema Migrations and Data Migrations isolated from each other. This ensures that if one changes, it doesn't impact the other, and both migrations can be independently managed, versioned, and executed.
Features
- Isolated from Schema Migrations: Keeps data manipulation out of Django's
migrationsfiles. - Version Controlled: Each data migration task is versioned. The framework tracks executions in the database and ensures a specific version of a task is never run twice.
- Transactional by Default: Tasks run within an atomic database transaction. If a task fails, the transaction is rolled back safely.
- Retry Mechanism: Configurable retry policies for data migrations, handling transient database locks or network issues.
- Schema Aware: Native support for multi-tenant applications using PostgreSQL schemas (can specify if a task should run in the
publicschema, a custom schema, or both).
Installation
You can install django-dmf from PyPI using pip:
pip install django_dmf
Configuration
- Add
dmto yourINSTALLED_APPSinsettings.py:
INSTALLED_APPS = [
# ...
'dm',
# ...
]
- Run schema migrations to create the necessary database table (
DataMigrationExecution) that keeps track of the data migration statuses:
python manage.py migrate dm
- Configure your Data Migration Settings in
settings.py:
# settings.py
# A list of string paths to your Data Migration Task classes
DATA_MIGRATION_REGISTRY = [
'my_app.dm.tasks.FeatureSwitchDataMigrationTask',
]
# (Optional) Retry configuration
DATA_MIGRATION_TOTAL_RETRIES = 5 # Default is 5
DATA_MIGRATION_RETRY_DELAY_IN_SECONDS = 5 # Default is 5
Usage
1. Create a Data Migration Task
Create a new Python file for your task and inherit from BaseDataMigrationTask.
# my_app/dm/tasks.py
from dm.tasks.dm.base import BaseDataMigrationTask
from my_app.models import UserProfile
class FeatureSwitchDataMigrationTask(BaseDataMigrationTask):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Required: Set a unique version for this task.
# The framework will use this to track execution status.
self.version = 1
# Optional: Multi-tenant schema settings
# self.run_in_public_schema = True
# self.run_in_custom_schema = True
def _run(self):
# Your data migration logic goes here.
# This block is executed within an atomic database transaction.
UserProfile.objects.filter(is_active=None).update(is_active=True)
2. Register the Task
Ensure your task is added to DATA_MIGRATION_REGISTRY in your settings.py.
DATA_MIGRATION_REGISTRY = [
'my_app.dm.tasks.FeatureSwitchDataMigrationTask',
]
3. Run Data Migrations
Use the provided management command to execute pending data migrations:
python manage.py migrate_data
The command will iterate through your registry, check the execution history in the database, and run any tasks (or updated versions of tasks) that haven't been completed successfully.
How it Works
Under the hood, django-dmf creates a DataMigrationExecution record for every task and version it encounters.
IN_PROGRESS: Marked when the task begins.COMPLETED: Marked when_run()completes without raising any exceptions.FAILED: Marked if an exception is raised inside_run(). The database transaction is rolled back, and the framework will optionally retry based on yourDATA_MIGRATION_TOTAL_RETRIESsetting.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for django-dmf 0.0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_dmf-0.0.10.tar.gz | 21.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_dmf-0.0.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 55.5 kB
Release files / django_dmf-0.0.10.tar.gz
| Download URL | django_dmf-0.0.10.tar.gz |
|---|---|
| Size | 21.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e733a66588e0bfb228e9cf22f216f58d8595c0f9c8f88b2cece0468c7ccc9d23
|
|
BLAKE2b-256 checksum How to use checksums |
a1c59dd2ff62dbf321c30c0c0fc954c74796f35bdd2129cfa1e9d49bc8f49b6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / django_dmf-0.0.10-py3-none-any.whl
| Download URL | django_dmf-0.0.10-py3-none-any.whl |
|---|---|
| Size | 33.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67490b976591722ab53c94c14740bd2aa3592f936c0142b5a06a29709a5ef5f6
|
|
BLAKE2b-256 checksum How to use checksums |
1a4eb20e2780188542839b13b480b615084b6d4842a8880872163e8b5d32e69c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|