From d693a086def21d843c03ec3f5b5e697ed2463d45 Mon Sep 17 00:00:00 2001 From: Frantisek Holop Date: Fri, 22 May 2020 11:40:34 +0200 Subject: Fixed #31615 -- Made migrations skip extension operations if not needed. - Don't try to create an existing extension. - Don't try to drop a nonexistent extension. --- tests/postgres_tests/test_operations.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'tests/postgres_tests') diff --git a/tests/postgres_tests/test_operations.py b/tests/postgres_tests/test_operations.py index 0a9d8040ef..8cc9a2b66e 100644 --- a/tests/postgres_tests/test_operations.py +++ b/tests/postgres_tests/test_operations.py @@ -12,7 +12,8 @@ from . import PostgreSQLTestCase try: from django.contrib.postgres.operations import ( - AddIndexConcurrently, CreateExtension, RemoveIndexConcurrently, + AddIndexConcurrently, BloomExtension, CreateExtension, + RemoveIndexConcurrently, ) from django.contrib.postgres.indexes import BrinIndex, BTreeIndex except ImportError: @@ -180,9 +181,33 @@ class CreateExtensionTests(PostgreSQLTestCase): with CaptureQueriesContext(connection) as captured_queries: with connection.schema_editor(atomic=False) as editor: operation.database_forwards(self.app_label, editor, project_state, new_state) - self.assertIn('CREATE EXTENSION', captured_queries[0]['sql']) + self.assertEqual(len(captured_queries), 4) + self.assertIn('CREATE EXTENSION', captured_queries[1]['sql']) # Reversal. with CaptureQueriesContext(connection) as captured_queries: with connection.schema_editor(atomic=False) as editor: operation.database_backwards(self.app_label, editor, new_state, project_state) - self.assertIn('DROP EXTENSION', captured_queries[0]['sql']) + self.assertEqual(len(captured_queries), 2) + self.assertIn('DROP EXTENSION', captured_queries[1]['sql']) + + def test_create_existing_extension(self): + operation = BloomExtension() + project_state = ProjectState() + new_state = project_state.clone() + # Don't create an existing extension. + with CaptureQueriesContext(connection) as captured_queries: + with connection.schema_editor(atomic=False) as editor: + operation.database_forwards(self.app_label, editor, project_state, new_state) + self.assertEqual(len(captured_queries), 3) + self.assertIn('SELECT', captured_queries[0]['sql']) + + def test_drop_nonexistent_extension(self): + operation = CreateExtension('tablefunc') + project_state = ProjectState() + new_state = project_state.clone() + # Don't drop a nonexistent extension. + with CaptureQueriesContext(connection) as captured_queries: + with connection.schema_editor(atomic=False) as editor: + operation.database_backwards(self.app_label, editor, project_state, new_state) + self.assertEqual(len(captured_queries), 1) + self.assertIn('SELECT', captured_queries[0]['sql']) -- cgit v1.3