Commit c8bac4b5 authored by Markus Holtermann's avatar Markus Holtermann Committed by Tim Graham
Browse files

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

Thanks Loïc Bistuer and Tim Graham for the discussion and review.
parent 67d6a8c4
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ class RunSQL(Operation):
    Also accepts a list of operations that represent the state change effected
    by this SQL change, in case it's custom column/table creation/deletion.
    """
    noop = ''

    def __init__(self, sql, reverse_sql=None, state_operations=None):
        self.sql = sql
@@ -100,9 +101,9 @@ class RunSQL(Operation):
    def describe(self):
        return "Raw SQL operation"

    def _run_sql(self, schema_editor, sql):
        if isinstance(sql, (list, tuple)):
            for sql in sql:
    def _run_sql(self, schema_editor, sqls):
        if isinstance(sqls, (list, tuple)):
            for sql in sqls:
                params = None
                if isinstance(sql, (list, tuple)):
                    elements = len(sql)
@@ -111,8 +112,8 @@ class RunSQL(Operation):
                    else:
                        raise ValueError("Expected a 2-tuple but got %d" % elements)
                schema_editor.execute(sql, params=params)
        else:
            statements = schema_editor.connection.ops.prepare_sql_script(sql)
        elif sqls != RunSQL.noop:
            statements = schema_editor.connection.ops.prepare_sql_script(sqls)
            for statement in statements:
                schema_editor.execute(statement, params=None)

@@ -175,3 +176,7 @@ class RunPython(Operation):

    def describe(self):
        return "Raw Python operation"

    @staticmethod
    def noop(apps, schema_editor):
        return None
+16 −0
Original line number Diff line number Diff line
@@ -245,6 +245,14 @@ operation that adds that field and so will try to run it again).
    The ability to pass parameters to the ``sql`` and ``reverse_sql`` queries
    was added.

.. attribute:: RunSQL.noop

    .. versionadded:: 1.8

    Pass the ``RunSQL.noop`` attribute to ``sql`` or ``reverse_sql`` when you
    want the operation not to do anything in the given direction. This is
    especially useful in making the operation reversible.

.. _sqlparse: https://pypi.python.org/pypi/sqlparse

RunPython
@@ -321,6 +329,14 @@ set ``atomic=False``.
    ``schema_editor.connection.alias``, where ``schema_editor`` is the second
    argument to your function).

.. staticmethod:: RunPython.noop

    .. versionadded:: 1.8

    Pass the ``RunPython.noop`` method to ``code`` or ``reverse_code`` when
    you want the operation not to do anything in the given direction. This is
    especially useful in making the operation reversible.

SeparateDatabaseAndState
------------------------

+5 −0
Original line number Diff line number Diff line
@@ -457,6 +457,11 @@ Migrations
* A :ref:`generic mechanism to handle the deprecation of model fields
  <migrations-removing-model-fields>` was added.

* The :attr:`RunPython.noop <django.db.migrations.operations.RunPython.noop>`
  and :meth:`RunSQL.noop() <django.db.migrations.operations.RunSQL.noop>` class
  attribute/method were added to ease in making ``RunPython`` and ``RunSQL``
  operations reversible.

Models
^^^^^^

+1 −1
Original line number Diff line number Diff line
@@ -7,5 +7,5 @@ from django.db import migrations
class Migration(migrations.Migration):

    operations = [
        migrations.RunPython(lambda apps, schema_editor: None)
        migrations.RunPython(migrations.RunPython.noop)
    ]
+1 −1
Original line number Diff line number Diff line
@@ -9,5 +9,5 @@ class Migration(migrations.Migration):
    dependencies = [("migrations", "1_auto")]

    operations = [
        migrations.RunPython(lambda apps, schema_editor: None)
        migrations.RunPython(migrations.RunPython.noop)
    ]
Loading