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.
This commit is contained in:
Jon Dufresne 2020-04-15 02:20:46 -07:00 committed by Mariusz Felisiak
parent 8005829bb9
commit 75410228df
13 changed files with 92 additions and 176 deletions

View file

@ -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;'],
)