Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored get_sql_sequence_reset() to DatabaseOperations.sequence_re…

…set_sql(). Refs #5106

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5964 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 147e99a08a4c5983f8b031917ace99e5a4827033 1 parent aaed6e0
Adrian Holovaty authored
4  django/core/management/commands/loaddata.py
@@ -15,7 +15,7 @@ class Command(BaseCommand):
15 15
     def handle(self, *fixture_labels, **options):
16 16
         from django.db.models import get_apps
17 17
         from django.core import serializers
18  
-        from django.db import connection, transaction, backend
  18
+        from django.db import connection, transaction
19 19
         from django.conf import settings
20 20
 
21 21
         self.style = no_style()
@@ -105,7 +105,7 @@ def handle(self, *fixture_labels, **options):
105 105
                                 (format, fixture_name, humanize(fixture_dir))
106 106
 
107 107
         if count[0] > 0:
108  
-            sequence_sql = backend.get_sql_sequence_reset(self.style, models)
  108
+            sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
109 109
             if sequence_sql:
110 110
                 if verbosity > 1:
111 111
                     print "Resetting sequences"
4  django/core/management/commands/sqlsequencereset.py
@@ -5,5 +5,5 @@ class Command(AppCommand):
5 5
     output_transaction = True
6 6
 
7 7
     def handle_app(self, app, **options):
8  
-        from django.db import backend, models
9  
-        return '\n'.join(backend.get_sql_sequence_reset(self.style, models.get_models(app)))
  8
+        from django.db import connection, models
  9
+        return '\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app)))
10  django/db/backends/__init__.py
@@ -150,3 +150,13 @@ def sql_flush(self, style, tables, sequences):
150 150
         color_style() or no_style() in django.core.management.color.
151 151
         """
152 152
         raise NotImplementedError()
  153
+
  154
+    def sequence_reset_sql(self, style, model_list):
  155
+        """
  156
+        Returns a list of the SQL statements required to reset sequences for
  157
+        the given models.
  158
+
  159
+        The `style` argument is a Style object as returned by either
  160
+        color_style() or no_style() in django.core.management.color.
  161
+        """
  162
+        return [] # No sequence reset required by default.
5  django/db/backends/ado_mssql/base.py
@@ -109,11 +109,6 @@ def get_start_transaction_sql():
109 109
 def get_tablespace_sql(tablespace, inline=False):
110 110
     return "ON %s" % quote_name(tablespace)
111 111
 
112  
-def get_sql_sequence_reset(style, model_list):
113  
-    "Returns a list of the SQL statements to reset sequences for the given models."
114  
-    # No sequence reset required
115  
-    return []
116  
-
117 112
 OPERATOR_MAPPING = {
118 113
     'exact': '= %s',
119 114
     'iexact': 'LIKE %s',
1  django/db/backends/dummy/base.py
@@ -44,6 +44,5 @@ def close(self):
44 44
 dictfetchmany = complain
45 45
 dictfetchall = complain
46 46
 get_start_transaction_sql = complain
47  
-get_sql_sequence_reset = complain
48 47
 
49 48
 OPERATOR_MAPPING = {}
5  django/db/backends/mysql/base.py
@@ -191,11 +191,6 @@ def quote_name(name):
191 191
 def get_start_transaction_sql():
192 192
     return "BEGIN;"
193 193
 
194  
-def get_sql_sequence_reset(style, model_list):
195  
-    "Returns a list of the SQL statements to reset sequences for the given models."
196  
-    # No sequence reset required
197  
-    return []
198  
-
199 194
 OPERATOR_MAPPING = {
200 195
     'exact': '= %s',
201 196
     'iexact': 'LIKE %s',
5  django/db/backends/mysql_old/base.py
@@ -210,11 +210,6 @@ def quote_name(name):
210 210
 def get_start_transaction_sql():
211 211
     return "BEGIN;"
212 212
 
213  
-def get_sql_sequence_reset(style, model_list):
214  
-    "Returns a list of the SQL statements to reset sequences for the given models."
215  
-    # No sequence reset required
216  
-    return []
217  
-
218 213
 OPERATOR_MAPPING = {
219 214
     'exact': '= %s',
220 215
     'iexact': 'LIKE %s',
35  django/db/backends/oracle/base.py
@@ -95,6 +95,23 @@ def sql_flush(self, style, tables, sequences):
95 95
         else:
96 96
             return []
97 97
 
  98
+    def sequence_reset_sql(self, style, model_list):
  99
+        from django.db import models
  100
+        output = []
  101
+        query = _get_sequence_reset_sql()
  102
+        for model in model_list:
  103
+            for f in model._meta.fields:
  104
+                if isinstance(f, models.AutoField):
  105
+                    sequence_name = get_sequence_name(model._meta.db_table)
  106
+                    output.append(query % {'sequence':sequence_name,
  107
+                                           'table':model._meta.db_table})
  108
+                    break # Only one AutoField is allowed per model, so don't bother continuing.
  109
+            for f in model._meta.many_to_many:
  110
+                sequence_name = get_sequence_name(f.m2m_db_table())
  111
+                output.append(query % {'sequence':sequence_name,
  112
+                                       'table':f.m2m_db_table()})
  113
+        return output
  114
+
98 115
 class DatabaseWrapper(BaseDatabaseWrapper):
99 116
     ops = DatabaseOperations()
100 117
 
@@ -244,24 +261,6 @@ def get_sequence_name(table):
244 261
     name_length = DatabaseOperations().max_name_length() - 3
245 262
     return '%s_SQ' % util.truncate_name(table, name_length).upper()
246 263
 
247  
-def get_sql_sequence_reset(style, model_list):
248  
-    "Returns a list of the SQL statements to reset sequences for the given models."
249  
-    from django.db import models
250  
-    output = []
251  
-    query = _get_sequence_reset_sql()
252  
-    for model in model_list:
253  
-        for f in model._meta.fields:
254  
-            if isinstance(f, models.AutoField):
255  
-                sequence_name = get_sequence_name(model._meta.db_table)
256  
-                output.append(query % {'sequence':sequence_name,
257  
-                                       'table':model._meta.db_table})
258  
-                break # Only one AutoField is allowed per model, so don't bother continuing.
259  
-        for f in model._meta.many_to_many:
260  
-            sequence_name = get_sequence_name(f.m2m_db_table())
261  
-            output.append(query % {'sequence':sequence_name,
262  
-                                   'table':f.m2m_db_table()})
263  
-    return output
264  
-
265 264
 def get_trigger_name(table):
266 265
     name_length = DatabaseOperations().max_name_length() - 3
267 266
     return '%s_TR' % util.truncate_name(table, name_length).upper()
59  django/db/backends/postgresql/base.py
@@ -124,6 +124,35 @@ def sql_flush(self, style, tables, sequences):
124 124
         else:
125 125
             return []
126 126
 
  127
+    def sequence_reset_sql(self, style, model_list):
  128
+        from django.db import models
  129
+        output = []
  130
+        for model in model_list:
  131
+            # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
  132
+            # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
  133
+            # if there are records (as the max pk value is already in use), otherwise set it to false.
  134
+            for f in model._meta.fields:
  135
+                if isinstance(f, models.AutoField):
  136
+                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  137
+                        (style.SQL_KEYWORD('SELECT'),
  138
+                        style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
  139
+                        style.SQL_FIELD(quote_name(f.column)),
  140
+                        style.SQL_FIELD(quote_name(f.column)),
  141
+                        style.SQL_KEYWORD('IS NOT'),
  142
+                        style.SQL_KEYWORD('FROM'),
  143
+                        style.SQL_TABLE(quote_name(model._meta.db_table))))
  144
+                    break # Only one AutoField is allowed per model, so don't bother continuing.
  145
+            for f in model._meta.many_to_many:
  146
+                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  147
+                    (style.SQL_KEYWORD('SELECT'),
  148
+                    style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
  149
+                    style.SQL_FIELD(quote_name('id')),
  150
+                    style.SQL_FIELD(quote_name('id')),
  151
+                    style.SQL_KEYWORD('IS NOT'),
  152
+                    style.SQL_KEYWORD('FROM'),
  153
+                    style.SQL_TABLE(f.m2m_db_table())))
  154
+        return output
  155
+
127 156
 class DatabaseWrapper(BaseDatabaseWrapper):
128 157
     ops = DatabaseOperations()
129 158
 
@@ -185,36 +214,6 @@ def dictfetchall(cursor):
185 214
 def get_start_transaction_sql():
186 215
     return "BEGIN;"
187 216
 
188  
-def get_sql_sequence_reset(style, model_list):
189  
-    "Returns a list of the SQL statements to reset sequences for the given models."
190  
-    from django.db import models
191  
-    output = []
192  
-    for model in model_list:
193  
-        # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
194  
-        # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
195  
-        # if there are records (as the max pk value is already in use), otherwise set it to false.
196  
-        for f in model._meta.fields:
197  
-            if isinstance(f, models.AutoField):
198  
-                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
199  
-                    (style.SQL_KEYWORD('SELECT'),
200  
-                    style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
201  
-                    style.SQL_FIELD(quote_name(f.column)),
202  
-                    style.SQL_FIELD(quote_name(f.column)),
203  
-                    style.SQL_KEYWORD('IS NOT'),
204  
-                    style.SQL_KEYWORD('FROM'),
205  
-                    style.SQL_TABLE(quote_name(model._meta.db_table))))
206  
-                break # Only one AutoField is allowed per model, so don't bother continuing.
207  
-        for f in model._meta.many_to_many:
208  
-            output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
209  
-                (style.SQL_KEYWORD('SELECT'),
210  
-                style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
211  
-                style.SQL_FIELD(quote_name('id')),
212  
-                style.SQL_FIELD(quote_name('id')),
213  
-                style.SQL_KEYWORD('IS NOT'),
214  
-                style.SQL_KEYWORD('FROM'),
215  
-                style.SQL_TABLE(f.m2m_db_table())))
216  
-    return output
217  
-
218 217
 def typecast_string(s):
219 218
     """
220 219
     Cast all returned strings to unicode strings.
59  django/db/backends/postgresql_psycopg2/base.py
@@ -86,6 +86,35 @@ def sql_flush(self, style, tables, sequences):
86 86
         else:
87 87
             return []
88 88
 
  89
+    def sequence_reset_sql(self, style, model_list):
  90
+        from django.db import models
  91
+        output = []
  92
+        for model in model_list:
  93
+            # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
  94
+            # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
  95
+            # if there are records (as the max pk value is already in use), otherwise set it to false.
  96
+            for f in model._meta.fields:
  97
+                if isinstance(f, models.AutoField):
  98
+                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  99
+                        (style.SQL_KEYWORD('SELECT'),
  100
+                        style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
  101
+                        style.SQL_FIELD(quote_name(f.column)),
  102
+                        style.SQL_FIELD(quote_name(f.column)),
  103
+                        style.SQL_KEYWORD('IS NOT'),
  104
+                        style.SQL_KEYWORD('FROM'),
  105
+                        style.SQL_TABLE(quote_name(model._meta.db_table))))
  106
+                    break # Only one AutoField is allowed per model, so don't bother continuing.
  107
+            for f in model._meta.many_to_many:
  108
+                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  109
+                    (style.SQL_KEYWORD('SELECT'),
  110
+                    style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
  111
+                    style.SQL_FIELD(quote_name('id')),
  112
+                    style.SQL_FIELD(quote_name('id')),
  113
+                    style.SQL_KEYWORD('IS NOT'),
  114
+                    style.SQL_KEYWORD('FROM'),
  115
+                    style.SQL_TABLE(f.m2m_db_table())))
  116
+        return output
  117
+
89 118
 class DatabaseWrapper(BaseDatabaseWrapper):
90 119
     ops = DatabaseOperations()
91 120
 
@@ -139,36 +168,6 @@ def quote_name(name):
139 168
 def get_start_transaction_sql():
140 169
     return "BEGIN;"
141 170
 
142  
-def get_sql_sequence_reset(style, model_list):
143  
-    "Returns a list of the SQL statements to reset sequences for the given models."
144  
-    from django.db import models
145  
-    output = []
146  
-    for model in model_list:
147  
-        # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
148  
-        # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
149  
-        # if there are records (as the max pk value is already in use), otherwise set it to false.
150  
-        for f in model._meta.fields:
151  
-            if isinstance(f, models.AutoField):
152  
-                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
153  
-                    (style.SQL_KEYWORD('SELECT'),
154  
-                    style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
155  
-                    style.SQL_FIELD(quote_name(f.column)),
156  
-                    style.SQL_FIELD(quote_name(f.column)),
157  
-                    style.SQL_KEYWORD('IS NOT'),
158  
-                    style.SQL_KEYWORD('FROM'),
159  
-                    style.SQL_TABLE(quote_name(model._meta.db_table))))
160  
-                break # Only one AutoField is allowed per model, so don't bother continuing.
161  
-        for f in model._meta.many_to_many:
162  
-            output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
163  
-                (style.SQL_KEYWORD('SELECT'),
164  
-                style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
165  
-                style.SQL_FIELD(quote_name('id')),
166  
-                style.SQL_FIELD(quote_name('id')),
167  
-                style.SQL_KEYWORD('IS NOT'),
168  
-                style.SQL_KEYWORD('FROM'),
169  
-                style.SQL_TABLE(f.m2m_db_table())))
170  
-    return output
171  
-
172 171
 OPERATOR_MAPPING = {
173 172
     'exact': '= %s',
174 173
     'iexact': 'ILIKE %s',
5  django/db/backends/sqlite3/base.py
@@ -134,11 +134,6 @@ def _sqlite_extract(lookup_type, dt):
134 134
 def get_start_transaction_sql():
135 135
     return "BEGIN;"
136 136
 
137  
-def get_sql_sequence_reset(style, model_list):
138  
-    "Returns a list of the SQL statements to reset sequences for the given models."
139  
-    # No sequence reset required
140  
-    return []
141  
-
142 137
 def _sqlite_date_trunc(lookup_type, dt):
143 138
     try:
144 139
         dt = util.typecast_timestamp(dt)

0 notes on commit 147e99a

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