Opened 7 years ago

Closed 7 years ago

#24098 closed New feature (fixed)

Add utility function to mark RunPython operations as noops

Reported by: Markus Holtermann Owned by: Tim Graham <timograham@…>
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


When adding a field to a model and propagating new values with a RunPython operation in migrations, there is often no reason for a backwards function to call. Same goes for the other direction.

However, if no backwards callable is specified, the respective migration isn't reversible. And the forwards callable is required.

I propose to add a class attribute that effectively provides a no-op but makes the code a bit more expressive:

class RunPython(Operation):
    # ...
    noop = lambda apps, schema_editor: None

Hence the migration could look like

from django.db import migrations

def forwards(apps, schema_editor):
    # Do some stuff

class Migration(migrations.Migration):
    operations = [
        # Current way
        migrations.RunPython(forwards, lambda apps, schema_editor: None)
        # New approach
        migrations.RunPython(forwards, migrations.RunPython.noop)

Change History (4)

comment:1 Changed 7 years ago by Loic Bistuer

Triage Stage: UnreviewedAccepted

As discussed on IRC we should do the same thing for RunSQL.

An empty string '' should work well to represent a provided no-op query but we can also alias it to RunSQL.noop for consistency with RunPython.

comment:2 Changed 7 years ago by Markus Holtermann

Has patch: set

comment:3 Changed 7 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:4 Changed 7 years ago by Tim Graham <timograham@…>

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In c8bac4b556cf4716dc9003e5da48060cb72ba7cb:

Fixed #24098 -- Added no-op attributes to RunPython and RunSQL

Thanks Loïc Bistuer and Tim Graham for the discussion and review.

Note: See TracTickets for help on using tickets.
Back to Top