Ticket #25371: migration_reverse.patch

File migration_reverse.patch, 2.2 KB (added by Bibhas C Debnath, 9 years ago)

Patch for documentation changes

  • docs/ref/migration-operations.txt

    diff --git a/docs/ref/migration-operations.txt b/docs/ref/migration-operations.txt
    index 8dbebcc..2a8dbfd 100644
    a b queries and parameters in the same way as :ref:`cursor.execute()  
    224224If you want to include literal percent signs in the query, you have to double
    225225them if you are passing parameters.
    226226
     227The ``reverse_sql`` statement is executed when the migration is reversed. So you can
     228reverse the changes done in the first statement::
     229
     230    migrations.RunSQL(
     231        ["INSERT INTO musician (name) VALUES (%s);", ['Reinhardt']],
     232        ["DELETE FROM musician where name=%s;", ['Reinhardt']]
     233    )
     234
    227235The ``state_operations`` argument is so you can supply operations that are
    228236equivalent to the SQL in terms of project state; for example, if you are
    229237manually creating a column, you should pass in a list containing an ``AddField``
    match the operation's place in the project history, and the second is an  
    265273instance of :class:`SchemaEditor
    266274<django.db.backends.base.schema.BaseDatabaseSchemaEditor>`.
    267275
     276The ``reverse_code`` argument is called when running migrations backward.
     277This callable is used so that the migration is reversible by undoing
     278what is being done in the ``code`` argument.
     279
    268280The optional ``hints`` argument will be passed as ``**hints`` to the
    269281:meth:`allow_migrate` method of database routers to assist them in making a
    270282routing decision. See :ref:`topics-db-multi-db-hints` for more details on
    model::  
    294306            Country(name="France", code="fr"),
    295307        ])
    296308
     309    def backwards_func(apps, schema_editor):
     310        # forwards_func creates two Country instance,
     311        # so backwards_func should delete them
     312        Country = apps.get_model("myapp", "Country")
     313        db_alias = schema_editor.connection.alias
     314        Country.objects.using(db_alias).filter(name="USA", code="us").delete()
     315        Country.objects.using(db_alias).filter(name="France", code="fr").delete()
     316
    297317    class Migration(migrations.Migration):
    298318
    299319        dependencies = []
    model::  
    301321        operations = [
    302322            migrations.RunPython(
    303323                forwards_func,
     324                backwards_func
    304325            ),
    305326        ]
    306327
Back to Top