diff options
Diffstat (limited to 'django/db/backends/mysql/operations.py')
-rw-r--r-- | django/db/backends/mysql/operations.py | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 9d69ba1152..d01e3bef6b 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -193,29 +193,30 @@ class DatabaseOperations(BaseDatabaseOperations): ] return 'RETURNING %s' % ', '.join(columns), () - def sql_flush(self, style, tables, sequences, allow_cascade=False): + def sql_flush(self, style, tables, *, reset_sequences=False, allow_cascade=False): if not tables: return [] + sql = ['SET FOREIGN_KEY_CHECKS = 0;'] - tables = set(tables) - with_sequences = set(s['table'] for s in sequences) - # It's faster to TRUNCATE tables that require a sequence reset since - # ALTER TABLE AUTO_INCREMENT is slower than TRUNCATE. - sql.extend( - '%s %s;' % ( - style.SQL_KEYWORD('TRUNCATE'), - style.SQL_FIELD(self.quote_name(table_name)), - ) for table_name in tables.intersection(with_sequences) - ) - # Otherwise issue a simple DELETE since it's faster than TRUNCATE - # and preserves sequences. - sql.extend( - '%s %s %s;' % ( - style.SQL_KEYWORD('DELETE'), - style.SQL_KEYWORD('FROM'), - style.SQL_FIELD(self.quote_name(table_name)), - ) for table_name in tables.difference(with_sequences) - ) + if reset_sequences: + # It's faster to TRUNCATE tables that require a sequence reset + # since ALTER TABLE AUTO_INCREMENT is slower than TRUNCATE. + sql.extend( + '%s %s;' % ( + style.SQL_KEYWORD('TRUNCATE'), + style.SQL_FIELD(self.quote_name(table_name)), + ) for table_name in tables + ) + else: + # Otherwise issue a simple DELETE since it's faster than TRUNCATE + # and preserves sequences. + sql.extend( + '%s %s %s;' % ( + style.SQL_KEYWORD('DELETE'), + style.SQL_KEYWORD('FROM'), + style.SQL_FIELD(self.quote_name(table_name)), + ) for table_name in tables + ) sql.append('SET FOREIGN_KEY_CHECKS = 1;') return sql |