Loading django/db/migrations/operations/special.py +10 −5 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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) Loading @@ -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) Loading Loading @@ -175,3 +176,7 @@ class RunPython(Operation): def describe(self): return "Raw Python operation" @staticmethod def noop(apps, schema_editor): return None docs/ref/migration-operations.txt +16 −0 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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 ------------------------ Loading docs/releases/1.8.txt +5 −0 Original line number Diff line number Diff line Loading @@ -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 ^^^^^^ Loading tests/migrations/test_migrations_squashed_complex/1_auto.py +1 −1 Original line number Diff line number Diff line Loading @@ -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) ] tests/migrations/test_migrations_squashed_complex/2_auto.py +1 −1 Original line number Diff line number Diff line Loading @@ -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
django/db/migrations/operations/special.py +10 −5 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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) Loading @@ -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) Loading Loading @@ -175,3 +176,7 @@ class RunPython(Operation): def describe(self): return "Raw Python operation" @staticmethod def noop(apps, schema_editor): return None
docs/ref/migration-operations.txt +16 −0 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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 ------------------------ Loading
docs/releases/1.8.txt +5 −0 Original line number Diff line number Diff line Loading @@ -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 ^^^^^^ Loading
tests/migrations/test_migrations_squashed_complex/1_auto.py +1 −1 Original line number Diff line number Diff line Loading @@ -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) ]
tests/migrations/test_migrations_squashed_complex/2_auto.py +1 −1 Original line number Diff line number Diff line Loading @@ -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) ]