11from django .conf import settings
2- from django .db .backends .base .creation import BaseDatabaseCreation
2+ from django .db .backends .base .creation import TEST_DATABASE_PREFIX , BaseDatabaseCreation
33
44
55class DatabaseCreation (BaseDatabaseCreation ):
66 def _execute_create_test_db (self , cursor , parameters , keepdb = False ):
77 # Close the connection (which may point to the non-test database) so
88 # that a new connection to the test database can be established later.
99 self .connection .close_pool ()
10+ # Use a test _key_vault_namespace. This assumes the key vault database
11+ # is the same as the encrypted database so that _destroy_test_db() can
12+ # reset the collection by dropping it.
13+ opts = self .connection .settings_dict ["OPTIONS" ].get ("auto_encryption_opts" )
14+ if opts :
15+ self .connection .settings_dict ["OPTIONS" ][
16+ "auto_encryption_opts"
17+ ]._key_vault_namespace = TEST_DATABASE_PREFIX + opts ._key_vault_namespace
1018 if not keepdb :
1119 self ._destroy_test_db (parameters ["dbname" ], verbosity = 0 )
1220
@@ -24,3 +32,9 @@ def destroy_test_db(self, old_database_name=None, verbosity=1, keepdb=False, suf
2432 super ().destroy_test_db (old_database_name , verbosity , keepdb , suffix )
2533 # Close the connection to the test database.
2634 self .connection .close_pool ()
35+ # Restore the original _key_vault_namespace.
36+ opts = self .connection .settings_dict ["OPTIONS" ].get ("auto_encryption_opts" )
37+ if opts :
38+ self .connection .settings_dict ["OPTIONS" ][
39+ "auto_encryption_opts"
40+ ]._key_vault_namespace = opts ._key_vault_namespace [len (TEST_DATABASE_PREFIX ) :]
0 commit comments