Refs #31615 -- Added EXISTS clauses to extension operations.

This commit is contained in:
Adam Johnson 2020-06-02 23:18:15 +01:00 committed by Mariusz Felisiak
parent 06c8565a46
commit 22a59c01c0
2 changed files with 4 additions and 4 deletions

View File

@ -23,7 +23,7 @@ class CreateExtension(Operation):
return return
if not self.extension_exists(schema_editor, self.name): if not self.extension_exists(schema_editor, self.name):
schema_editor.execute( schema_editor.execute(
'CREATE EXTENSION %s' % schema_editor.quote_name(self.name) 'CREATE EXTENSION IF NOT EXISTS %s' % schema_editor.quote_name(self.name)
) )
# Clear cached, stale oids. # Clear cached, stale oids.
get_hstore_oids.cache_clear() get_hstore_oids.cache_clear()
@ -38,7 +38,7 @@ class CreateExtension(Operation):
return return
if self.extension_exists(schema_editor, self.name): if self.extension_exists(schema_editor, self.name):
schema_editor.execute( schema_editor.execute(
'DROP EXTENSION %s' % schema_editor.quote_name(self.name) 'DROP EXTENSION IF EXISTS %s' % schema_editor.quote_name(self.name)
) )
# Clear cached, stale oids. # Clear cached, stale oids.
get_hstore_oids.cache_clear() get_hstore_oids.cache_clear()

View File

@ -183,13 +183,13 @@ class CreateExtensionTests(PostgreSQLTestCase):
with connection.schema_editor(atomic=False) as editor: with connection.schema_editor(atomic=False) as editor:
operation.database_forwards(self.app_label, editor, project_state, new_state) operation.database_forwards(self.app_label, editor, project_state, new_state)
self.assertEqual(len(captured_queries), 4) self.assertEqual(len(captured_queries), 4)
self.assertIn('CREATE EXTENSION', captured_queries[1]['sql']) self.assertIn('CREATE EXTENSION IF NOT EXISTS', captured_queries[1]['sql'])
# Reversal. # Reversal.
with CaptureQueriesContext(connection) as captured_queries: with CaptureQueriesContext(connection) as captured_queries:
with connection.schema_editor(atomic=False) as editor: with connection.schema_editor(atomic=False) as editor:
operation.database_backwards(self.app_label, editor, new_state, project_state) operation.database_backwards(self.app_label, editor, new_state, project_state)
self.assertEqual(len(captured_queries), 2) self.assertEqual(len(captured_queries), 2)
self.assertIn('DROP EXTENSION', captured_queries[1]['sql']) self.assertIn('DROP EXTENSION IF EXISTS', captured_queries[1]['sql'])
def test_create_existing_extension(self): def test_create_existing_extension(self):
operation = BloomExtension() operation = BloomExtension()