From f91a04621ea8d7a657ff755645d823026257e898 Mon Sep 17 00:00:00 2001 From: Pankrat Date: Sat, 30 Jan 2016 21:46:28 +0100 Subject: Fixed #25833 -- Added support for non-atomic migrations. Added the Migration.atomic attribute which can be set to False for non-atomic migrations. --- docs/howto/writing-migrations.txt | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'docs/howto') 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 =================================== -- cgit v1.3