diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-04-15 02:20:46 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-04-17 11:57:24 +0200 |
| commit | 75410228dfd16e49eb3c0ea30b59b4c0d2ea6b03 (patch) | |
| tree | cb9e6532b57cf60f8eaebcb3860241094e6a5588 /tests/backends/mysql | |
| parent | 8005829bb9d9e0a14d73c9375bb55eb05daa46e1 (diff) | |
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 'tests/backends/mysql')
| -rw-r--r-- | tests/backends/mysql/test_operations.py | 47 |
1 files changed, 16 insertions, 31 deletions
diff --git a/tests/backends/mysql/test_operations.py b/tests/backends/mysql/test_operations.py index f1d9342a7b..a98e8963b7 100644 --- a/tests/backends/mysql/test_operations.py +++ b/tests/backends/mysql/test_operations.py @@ -4,7 +4,7 @@ from django.core.management.color import no_style from django.db import connection from django.test import SimpleTestCase -from ..models import Person, Square, Tag +from ..models import Person, Tag @unittest.skipUnless(connection.vendor == 'mysql', 'MySQL tests.') @@ -13,50 +13,35 @@ class MySQLOperationsTests(SimpleTestCase): # allow_cascade doesn't change statements on MySQL. for allow_cascade in [False, True]: with self.subTest(allow_cascade=allow_cascade): - statements = connection.ops.sql_flush( - no_style(), - [Person._meta.db_table, Tag._meta.db_table], - [], - allow_cascade=allow_cascade, - ) - self.assertEqual(statements[0], 'SET FOREIGN_KEY_CHECKS = 0;') - # The tables are processed in an unordered set. self.assertEqual( - sorted(statements[1:-1]), + connection.ops.sql_flush( + no_style(), + [Person._meta.db_table, Tag._meta.db_table], + allow_cascade=allow_cascade, + ), [ + 'SET FOREIGN_KEY_CHECKS = 0;', 'DELETE FROM `backends_person`;', 'DELETE FROM `backends_tag`;', + 'SET FOREIGN_KEY_CHECKS = 1;', ], ) - self.assertEqual(statements[-1], 'SET FOREIGN_KEY_CHECKS = 1;') def test_sql_flush_sequences(self): # allow_cascade doesn't change statements on MySQL. for allow_cascade in [False, True]: with self.subTest(allow_cascade=allow_cascade): - statements = connection.ops.sql_flush( - no_style(), - [Person._meta.db_table, Square._meta.db_table, Tag._meta.db_table], - [ - { - 'table': Person._meta.db_table, - 'column': Person._meta.pk.db_column, - }, - { - 'table': Tag._meta.db_table, - 'column': Tag._meta.pk.db_column, - }, - ], - allow_cascade=allow_cascade, - ) - self.assertEqual(statements[0], 'SET FOREIGN_KEY_CHECKS = 0;') - # The tables are processed in an unordered set. self.assertEqual( - sorted(statements[1:-1]), + connection.ops.sql_flush( + no_style(), + [Person._meta.db_table, Tag._meta.db_table], + reset_sequences=True, + allow_cascade=allow_cascade, + ), [ - 'DELETE FROM `backends_square`;', + 'SET FOREIGN_KEY_CHECKS = 0;', 'TRUNCATE `backends_person`;', 'TRUNCATE `backends_tag`;', + 'SET FOREIGN_KEY_CHECKS = 1;', ], ) - self.assertEqual(statements[-1], 'SET FOREIGN_KEY_CHECKS = 1;') |
