diff options
| author | Pankrat <lhaehne@gmail.com> | 2016-01-30 21:46:28 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-02-05 09:09:05 -0500 |
| commit | f91a04621ea8d7a657ff755645d823026257e898 (patch) | |
| tree | 2b06f3ce1cbb8e5f04499838154cf715ff76ce5b /docs/howto | |
| parent | 0edb8a146fd6c60f4c3b1ad0a4a89963962f22af (diff) | |
Fixed #25833 -- Added support for non-atomic migrations.
Added the Migration.atomic attribute which can be set to False
for non-atomic migrations.
Diffstat (limited to 'docs/howto')
| -rw-r--r-- | docs/howto/writing-migrations.txt | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/docs/howto/writing-migrations.txt b/docs/howto/writing-migrations.txt index 552035b7c2..adef507fa8 100644 --- a/docs/howto/writing-migrations.txt +++ b/docs/howto/writing-migrations.txt @@ -184,6 +184,53 @@ the respective field according to your needs. migration is running. Objects created after the ``AddField`` and before ``RunPython`` will have their original ``uuid``’s overwritten. +.. _non-atomic-migrations: + +Non-atomic migrations +~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 1.10 + +On databases that support DDL transactions (SQLite and PostgreSQL), migrations +will run inside a transaction by default. For use cases such as performing data +migrations on large tables, you may want to prevent a migration from running in +a transaction by setting the ``atomic`` attribute to ``False``:: + + from django.db import migrations + + class Migration(migrations.Migration): + atomic = False + +Within such a migration, all operations are run without a transaction. It's +possible to execute parts of the migration inside a transaction using +:func:`~django.db.transaction.atomic()` or by passing ``atomic=True`` to +``RunPython``. + +Here's an example of a non-atomic data migration that updates a large table in +smaller batches:: + + import uuid + + from django.db import migrations, transaction + + def gen_uuid(apps, schema_editor): + MyModel = apps.get_model('myapp', 'MyModel') + while MyModel.objects.filter(uuid__isnull=True).exists(): + with transaction.atomic(): + for row in MyModel.objects.filter(uuid__isnull=True)[:1000]: + row.uuid = uuid.uuid4() + row.save() + + class Migration(migrations.Migration): + atomic = False + + operations = [ + migrations.RunPython(gen_uuid), + ] + +The ``atomic`` attribute doesn't have an effect on databases that don't support +DDL transactions (e.g. MySQL, Oracle). + Controlling the order of migrations =================================== |
