summaryrefslogtreecommitdiff
path: root/django/db/backends/mysql/operations.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2020-04-15 02:20:46 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-04-17 11:57:24 +0200
commit75410228dfd16e49eb3c0ea30b59b4c0d2ea6b03 (patch)
treecb9e6532b57cf60f8eaebcb3860241094e6a5588 /django/db/backends/mysql/operations.py
parent8005829bb9d9e0a14d73c9375bb55eb05daa46e1 (diff)
downloaddjango-75410228dfd16e49eb3c0ea30b59b4c0d2ea6b03.tar.gz
Fixed #31473 -- Made sql_flush() use RESTART IDENTITY to reset sequences on PostgreSQL.
The sql_flush() positional argument sequences is replaced by the boolean keyword-only argument reset_sequences. This ensures that the old function signature can't be used by mistake when upgrading Django. When the new argument is True, the sequences of the truncated tables will reset. Using a single boolean value, rather than a list, allows making a binary yes/no choice as to whether to reset all sequences rather than a working on a completely different set.
Diffstat (limited to 'django/db/backends/mysql/operations.py')
-rw-r--r--django/db/backends/mysql/operations.py41
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