@@ -27,7 +27,12 @@ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
2727 # so we can proceed if we're keeping the DB anyway.
2828 # https://github.com/microsoft/mssql-django/issues/61
2929 try :
30- return super ()._create_test_db (verbosity , autoclobber , keepdb )
30+ test_database_name = super ()._create_test_db (verbosity , autoclobber , keepdb )
31+
32+ # Create required schemas for Django tests
33+ self ._create_test_schemas (test_database_name , verbosity )
34+
35+ return test_database_name
3136 except InterfaceError as err :
3237 if err .args [0 ] == '28000' and keepdb :
3338 self .log ('Received error %s, proceeding because keepdb=True' % (
@@ -36,6 +41,32 @@ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
3641 else :
3742 raise err
3843
44+ def _create_test_schemas (self , test_database_name , verbosity ):
45+ """
46+ Create required schemas in test database for Django tests.
47+ """
48+ schemas_to_create = ['inspectdb_special' , 'inspectdb_pascal' ]
49+
50+ # Use a cursor connected to the test database
51+ test_settings = self .connection .settings_dict .copy ()
52+ test_settings ['NAME' ] = test_database_name
53+ test_connection = self .connection .__class__ (test_settings )
54+
55+ try :
56+ with test_connection .cursor () as cursor :
57+ for schema in schemas_to_create :
58+ try :
59+ quoted_schema = self .connection .ops .quote_name (schema )
60+ cursor .execute (f"CREATE SCHEMA { quoted_schema } " )
61+ if verbosity >= 2 :
62+ self .log (f'Created schema { schema } in test database { test_database_name } ' )
63+ except Exception as e :
64+ # Schema might already exist, which is fine
65+ if verbosity >= 2 :
66+ self .log (f'Schema { schema } creation failed (might already exist): { e } ' )
67+ finally :
68+ test_connection .close ()
69+
3970 def _destroy_test_db (self , test_database_name , verbosity ):
4071 """
4172 Internal implementation - remove the test db tables.
0 commit comments