summaryrefslogtreecommitdiff
path: root/tests
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 /tests
parent8005829bb9d9e0a14d73c9375bb55eb05daa46e1 (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')
-rw-r--r--tests/backends/base/test_operations.py13
-rw-r--r--tests/backends/mysql/test_operations.py47
-rw-r--r--tests/backends/oracle/test_operations.py29
-rw-r--r--tests/backends/postgresql/test_operations.py42
-rw-r--r--tests/backends/sqlite/test_operations.py24
-rw-r--r--tests/backends/tests.py8
6 files changed, 33 insertions, 130 deletions
diff --git a/tests/backends/base/test_operations.py b/tests/backends/base/test_operations.py
index 5dff48d44e..0485fe8465 100644
--- a/tests/backends/base/test_operations.py
+++ b/tests/backends/base/test_operations.py
@@ -43,7 +43,7 @@ class SimpleDatabaseOperationTests(SimpleTestCase):
def test_sql_flush(self):
msg = 'subclasses of BaseDatabaseOperations must provide a sql_flush() method'
with self.assertRaisesMessage(NotImplementedError, msg):
- self.ops.sql_flush(None, None, None)
+ self.ops.sql_flush(None, None)
def test_pk_default_value(self):
self.assertEqual(self.ops.pk_default_value(), 'DEFAULT')
@@ -154,7 +154,7 @@ class SqlFlushTests(TransactionTestCase):
available_apps = ['backends']
def test_sql_flush_no_tables(self):
- self.assertEqual(connection.ops.sql_flush(no_style(), [], []), [])
+ self.assertEqual(connection.ops.sql_flush(no_style(), []), [])
def test_execute_sql_flush_statements(self):
with transaction.atomic():
@@ -169,12 +169,7 @@ class SqlFlushTests(TransactionTestCase):
sql_list = connection.ops.sql_flush(
no_style(),
[Author._meta.db_table, Book._meta.db_table],
- [
- {
- 'table': Author._meta.db_table,
- 'column': Author._meta.pk.db_column,
- },
- ],
+ reset_sequences=True,
allow_cascade=True,
)
connection.ops.execute_sql_flush(connection.alias, sql_list)
@@ -185,3 +180,5 @@ class SqlFlushTests(TransactionTestCase):
if connection.features.supports_sequence_reset:
author = Author.objects.create(name='F. Scott Fitzgerald')
self.assertEqual(author.pk, 1)
+ book = Book.objects.create(author=author)
+ self.assertEqual(book.pk, 1)
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;')
diff --git a/tests/backends/oracle/test_operations.py b/tests/backends/oracle/test_operations.py
index 97760ecbfe..7722744ecc 100644
--- a/tests/backends/oracle/test_operations.py
+++ b/tests/backends/oracle/test_operations.py
@@ -31,7 +31,6 @@ class OperationsTests(unittest.TestCase):
statements = connection.ops.sql_flush(
no_style(),
[Person._meta.db_table, Tag._meta.db_table],
- [],
)
# The tables and constraints are processed in an unordered set.
self.assertEqual(
@@ -56,7 +55,6 @@ class OperationsTests(unittest.TestCase):
statements = connection.ops.sql_flush(
no_style(),
[Person._meta.db_table, Tag._meta.db_table],
- [],
allow_cascade=True,
)
# The tables and constraints are processed in an unordered set.
@@ -83,16 +81,7 @@ class OperationsTests(unittest.TestCase):
statements = connection.ops.sql_flush(
no_style(),
[Person._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,
- },
- ],
+ reset_sequences=True,
)
# The tables and constraints are processed in an unordered set.
self.assertEqual(
@@ -121,16 +110,7 @@ class OperationsTests(unittest.TestCase):
statements = connection.ops.sql_flush(
no_style(),
[Person._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,
- },
- ],
+ reset_sequences=True,
allow_cascade=True,
)
# The tables and constraints are processed in an unordered set.
@@ -153,6 +133,7 @@ class OperationsTests(unittest.TestCase):
'"BACKENDS__PERSON_ID_1DD5E829_F";',
)
# Sequences.
- self.assertEqual(len(statements[5:]), 2)
+ self.assertEqual(len(statements[5:]), 3)
self.assertIn('BACKENDS_PERSON_SQ', statements[5])
- self.assertIn('BACKENDS_TAG_SQ', statements[6])
+ self.assertIn('BACKENDS_VERYLONGMODELN7BE2_SQ', statements[6])
+ self.assertIn('BACKENDS_TAG_SQ', statements[7])
diff --git a/tests/backends/postgresql/test_operations.py b/tests/backends/postgresql/test_operations.py
index b073f688f4..821bb29cee 100644
--- a/tests/backends/postgresql/test_operations.py
+++ b/tests/backends/postgresql/test_operations.py
@@ -14,7 +14,6 @@ class PostgreSQLOperationsTests(SimpleTestCase):
connection.ops.sql_flush(
no_style(),
[Person._meta.db_table, Tag._meta.db_table],
- [],
),
['TRUNCATE "backends_person", "backends_tag";'],
)
@@ -24,61 +23,28 @@ class PostgreSQLOperationsTests(SimpleTestCase):
connection.ops.sql_flush(
no_style(),
[Person._meta.db_table, Tag._meta.db_table],
- [],
allow_cascade=True,
),
['TRUNCATE "backends_person", "backends_tag" CASCADE;'],
)
def test_sql_flush_sequences(self):
- sequence_reset_sql = (
- "SELECT setval(pg_get_serial_sequence('%s','id'), 1, false);"
- )
self.assertEqual(
connection.ops.sql_flush(
no_style(),
[Person._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,
- },
- ],
+ reset_sequences=True,
),
- [
- 'TRUNCATE "backends_person", "backends_tag";',
- sequence_reset_sql % '"backends_person"',
- sequence_reset_sql % '"backends_tag"',
- ],
+ ['TRUNCATE "backends_person", "backends_tag" RESTART IDENTITY;'],
)
def test_sql_flush_sequences_allow_cascade(self):
- sequence_reset_sql = (
- "SELECT setval(pg_get_serial_sequence('%s','id'), 1, false);"
- )
self.assertEqual(
connection.ops.sql_flush(
no_style(),
[Person._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,
- },
- ],
+ reset_sequences=True,
allow_cascade=True,
),
- [
- 'TRUNCATE "backends_person", "backends_tag" CASCADE;',
- sequence_reset_sql % '"backends_person"',
- sequence_reset_sql % '"backends_tag"',
- ],
+ ['TRUNCATE "backends_person", "backends_tag" RESTART IDENTITY CASCADE;'],
)
diff --git a/tests/backends/sqlite/test_operations.py b/tests/backends/sqlite/test_operations.py
index 34c4d823da..0ee70061f8 100644
--- a/tests/backends/sqlite/test_operations.py
+++ b/tests/backends/sqlite/test_operations.py
@@ -14,7 +14,6 @@ class SQLiteOperationsTests(TestCase):
connection.ops.sql_flush(
no_style(),
[Person._meta.db_table, Tag._meta.db_table],
- [],
),
[
'DELETE FROM "backends_person";',
@@ -26,7 +25,6 @@ class SQLiteOperationsTests(TestCase):
statements = connection.ops.sql_flush(
no_style(),
[Person._meta.db_table, Tag._meta.db_table],
- [],
allow_cascade=True,
)
self.assertEqual(
@@ -47,16 +45,7 @@ class SQLiteOperationsTests(TestCase):
connection.ops.sql_flush(
no_style(),
[Person._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,
- },
- ],
+ reset_sequences=True,
),
[
'DELETE FROM "backends_person";',
@@ -69,16 +58,7 @@ class SQLiteOperationsTests(TestCase):
statements = connection.ops.sql_flush(
no_style(),
[Person._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,
- },
- ],
+ reset_sequences=True,
allow_cascade=True,
)
self.assertEqual(
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index f20d3db5f1..2cbfa2f5a2 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -161,13 +161,7 @@ class LongNameTest(TransactionTestCase):
VLM._meta.db_table,
VLM_m2m._meta.db_table,
]
- sequences = [
- {
- 'column': VLM._meta.pk.column,
- 'table': VLM._meta.db_table
- },
- ]
- sql_list = connection.ops.sql_flush(no_style(), tables, sequences)
+ sql_list = connection.ops.sql_flush(no_style(), tables, reset_sequences=True)
with connection.cursor() as cursor:
for statement in sql_list:
cursor.execute(statement)