@@ -314,7 +314,7 @@ def savepoint_rollback_sql(self, sid):
314
314
"""
315
315
return "ROLLBACK TRANSACTION %s" % sid
316
316
317
- def sql_flush (self , style , tables , sequences , allow_cascade = False ):
317
+ def sql_flush (self , style , tables , * , reset_sequences = False , allow_cascade = False ):
318
318
"""
319
319
Returns a list of SQL statements required to remove all data from
320
320
the given database tables (without actually removing the tables
@@ -329,14 +329,21 @@ def sql_flush(self, style, tables, sequences, allow_cascade=False):
329
329
The `allow_cascade` argument determines whether truncation may cascade
330
330
to tables with foreign keys pointing the tables being truncated.
331
331
"""
332
- if tables :
333
- # Cannot use TRUNCATE on tables that are referenced by a FOREIGN KEY
334
- # So must use the much slower DELETE
335
- from django .db import connections
336
- cursor = connections [self .connection .alias ].cursor ()
337
- # Try to minimize the risks of the braindeaded inconsistency in
338
- # DBCC CHEKIDENT(table, RESEED, n) behavior.
339
- seqs = []
332
+ if not tables :
333
+ return []
334
+
335
+ # Cannot use TRUNCATE on tables that are referenced by a FOREIGN KEY
336
+ # So must use the much slower DELETE
337
+ from django .db import connections
338
+ cursor = connections [self .connection .alias ].cursor ()
339
+ # Try to minimize the risks of the braindeaded inconsistency in
340
+ # DBCC CHEKIDENT(table, RESEED, n) behavior.
341
+ seqs = []
342
+ if reset_sequences :
343
+ sequences = [
344
+ sequence
345
+ for sequence in self .connection .introspection .sequence_list ()
346
+ ]
340
347
for seq in sequences :
341
348
cursor .execute ("SELECT COUNT(*) FROM %s" % self .quote_name (seq ["table" ]))
342
349
rowcnt = cursor .fetchone ()[0 ]
@@ -347,37 +354,36 @@ def sql_flush(self, style, tables, sequences, allow_cascade=False):
347
354
elem ['start_id' ] = 1
348
355
elem .update (seq )
349
356
seqs .append (elem )
350
- COLUMNS = "TABLE_NAME, CONSTRAINT_NAME"
351
- WHERE = "CONSTRAINT_TYPE not in ('PRIMARY KEY','UNIQUE')"
352
- cursor .execute (
353
- "SELECT {} FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE {}" .format (COLUMNS , WHERE ))
354
- fks = cursor .fetchall ()
355
- sql_list = ['ALTER TABLE %s NOCHECK CONSTRAINT %s;' %
356
- (self .quote_name (fk [0 ]), self .quote_name (fk [1 ])) for fk in fks ]
357
- sql_list .extend (['%s %s %s;' % (style .SQL_KEYWORD ('DELETE' ), style .SQL_KEYWORD ('FROM' ),
358
- style .SQL_FIELD (self .quote_name (table ))) for table in tables ])
359
-
360
- if self .connection .to_azure_sql_db and self .connection .sql_server_version < 2014 :
361
- warnings .warn ("Resetting identity columns is not supported "
362
- "on this versios of Azure SQL Database." ,
363
- RuntimeWarning )
364
- else :
365
- # Then reset the counters on each table.
366
- sql_list .extend (['%s %s (%s, %s, %s) %s %s;' % (
367
- style .SQL_KEYWORD ('DBCC' ),
368
- style .SQL_KEYWORD ('CHECKIDENT' ),
369
- style .SQL_FIELD (self .quote_name (seq ["table" ])),
370
- style .SQL_KEYWORD ('RESEED' ),
371
- style .SQL_FIELD ('%d' % seq ['start_id' ]),
372
- style .SQL_KEYWORD ('WITH' ),
373
- style .SQL_KEYWORD ('NO_INFOMSGS' ),
374
- ) for seq in seqs ])
375
-
376
- sql_list .extend (['ALTER TABLE %s CHECK CONSTRAINT %s;' %
377
- (self .quote_name (fk [0 ]), self .quote_name (fk [1 ])) for fk in fks ])
378
- return sql_list
357
+
358
+ COLUMNS = "TABLE_NAME, CONSTRAINT_NAME"
359
+ WHERE = "CONSTRAINT_TYPE not in ('PRIMARY KEY','UNIQUE')"
360
+ cursor .execute (
361
+ "SELECT {} FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE {}" .format (COLUMNS , WHERE ))
362
+ fks = cursor .fetchall ()
363
+ sql_list = ['ALTER TABLE %s NOCHECK CONSTRAINT %s;' %
364
+ (self .quote_name (fk [0 ]), self .quote_name (fk [1 ])) for fk in fks ]
365
+ sql_list .extend (['%s %s %s;' % (style .SQL_KEYWORD ('DELETE' ), style .SQL_KEYWORD ('FROM' ),
366
+ style .SQL_FIELD (self .quote_name (table ))) for table in tables ])
367
+
368
+ if self .connection .to_azure_sql_db and self .connection .sql_server_version < 2014 :
369
+ warnings .warn ("Resetting identity columns is not supported "
370
+ "on this versios of Azure SQL Database." ,
371
+ RuntimeWarning )
379
372
else :
380
- return []
373
+ # Then reset the counters on each table.
374
+ sql_list .extend (['%s %s (%s, %s, %s) %s %s;' % (
375
+ style .SQL_KEYWORD ('DBCC' ),
376
+ style .SQL_KEYWORD ('CHECKIDENT' ),
377
+ style .SQL_FIELD (self .quote_name (seq ["table" ])),
378
+ style .SQL_KEYWORD ('RESEED' ),
379
+ style .SQL_FIELD ('%d' % seq ['start_id' ]),
380
+ style .SQL_KEYWORD ('WITH' ),
381
+ style .SQL_KEYWORD ('NO_INFOMSGS' ),
382
+ ) for seq in seqs ])
383
+
384
+ sql_list .extend (['ALTER TABLE %s CHECK CONSTRAINT %s;' %
385
+ (self .quote_name (fk [0 ]), self .quote_name (fk [1 ])) for fk in fks ])
386
+ return sql_list
381
387
382
388
def start_transaction_sql (self ):
383
389
"""
0 commit comments