Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make sqlmigrate ignore the RunPython operation

  • Loading branch information...
commit 8a3e543f268ba027a631274d1cfe44db64ad9025 1 parent 3b810c5
Andrew Godwin authored September 25, 2013
4  django/db/migrations/executor.py
@@ -71,9 +71,9 @@ def collect_sql(self, plan):
71 71
             with self.connection.schema_editor(collect_sql=True) as schema_editor:
72 72
                 project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
73 73
                 if not backwards:
74  
-                    migration.apply(project_state, schema_editor)
  74
+                    migration.apply(project_state, schema_editor, collect_sql=True)
75 75
                 else:
76  
-                    migration.unapply(project_state, schema_editor)
  76
+                    migration.unapply(project_state, schema_editor, collect_sql=True)
77 77
                 statements.extend(schema_editor.collected_sql)
78 78
         return statements
79 79
 
23  django/db/migrations/migration.py
@@ -67,7 +67,7 @@ def mutate_state(self, project_state):
67 67
             operation.state_forwards(self.app_label, new_state)
68 68
         return new_state
69 69
 
70  
-    def apply(self, project_state, schema_editor):
  70
+    def apply(self, project_state, schema_editor, collect_sql=False):
71 71
         """
72 72
         Takes a project_state representing all migrations prior to this one
73 73
         and a schema_editor for a live database and applies the migration
@@ -77,6 +77,14 @@ def apply(self, project_state, schema_editor):
77 77
         Migrations.
78 78
         """
79 79
         for operation in self.operations:
  80
+            # If this operation cannot be represented as SQL, place a comment
  81
+            # there instead
  82
+            if collect_sql and not operation.reduces_to_sql:
  83
+                schema_editor.collected_sql.append("--")
  84
+                schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:")
  85
+                schema_editor.collected_sql.append("-- %s" % operation.describe())
  86
+                schema_editor.collected_sql.append("--")
  87
+                continue
80 88
             # Get the state after the operation has run
81 89
             new_state = project_state.clone()
82 90
             operation.state_forwards(self.app_label, new_state)
@@ -86,7 +94,7 @@ def apply(self, project_state, schema_editor):
86 94
             project_state = new_state
87 95
         return project_state
88 96
 
89  
-    def unapply(self, project_state, schema_editor):
  97
+    def unapply(self, project_state, schema_editor, collect_sql=False):
90 98
         """
91 99
         Takes a project_state representing all migrations prior to this one
92 100
         and a schema_editor for a live database and applies the migration
@@ -95,8 +103,17 @@ def unapply(self, project_state, schema_editor):
95 103
         # We need to pre-calculate the stack of project states
96 104
         to_run = []
97 105
         for operation in self.operations:
  106
+            # If this operation cannot be represented as SQL, place a comment
  107
+            # there instead
  108
+            if collect_sql and not operation.reduces_to_sql:
  109
+                schema_editor.collected_sql.append("--")
  110
+                schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:")
  111
+                schema_editor.collected_sql.append("-- %s" % operation.describe())
  112
+                schema_editor.collected_sql.append("--")
  113
+                continue
  114
+            # If it's irreversible, error out
98 115
             if not operation.reversible:
99  
-                raise Migration.IrreversibleError("Operation %s in %s is not reversible" % (operation, sekf))
  116
+                raise Migration.IrreversibleError("Operation %s in %s is not reversible" % (operation, self))
100 117
             new_state = project_state.clone()
101 118
             operation.state_forwards(self.app_label, new_state)
102 119
             to_run.append((operation, project_state, new_state))
3  django/db/migrations/operations/special.py
@@ -132,3 +132,6 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
132 132
 
133 133
     def database_backwards(self, app_label, schema_editor, from_state, to_state):
134 134
         raise NotImplementedError("You cannot reverse this operation")
  135
+
  136
+    def describe(self):
  137
+        return "Raw Python operation"

0 notes on commit 8a3e543

Please sign in to comment.
Something went wrong with that request. Please try again.