@@ -27,7 +27,13 @@ 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 (only for 5.2+)
33+ if django_version >= (5 , 2 ):
34+ self ._create_test_schemas (test_database_name , verbosity )
35+
36+ return test_database_name
3137 except InterfaceError as err :
3238 if err .args [0 ] == '28000' and keepdb :
3339 self .log ('Received error %s, proceeding because keepdb=True' % (
@@ -36,6 +42,32 @@ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
3642 else :
3743 raise err
3844
45+ def _create_test_schemas (self , test_database_name , verbosity ):
46+ """
47+ Create required schemas in test database for Django tests.
48+ """
49+ schemas_to_create = ['inspectdb_special' , 'inspectdb_pascal' ]
50+
51+ # Use a cursor connected to the test database
52+ test_settings = self .connection .settings_dict .copy ()
53+ test_settings ['NAME' ] = test_database_name
54+ test_connection = self .connection .__class__ (test_settings )
55+
56+ try :
57+ with test_connection .cursor () as cursor :
58+ for schema in schemas_to_create :
59+ try :
60+ quoted_schema = self .connection .ops .quote_name (schema )
61+ cursor .execute (f"CREATE SCHEMA { quoted_schema } " )
62+ if verbosity >= 2 :
63+ self .log (f'Created schema { schema } in test database { test_database_name } ' )
64+ except Exception as e :
65+ # Schema might already exist, which is fine
66+ if verbosity >= 2 :
67+ self .log (f'Schema { schema } creation failed (might already exist): { e } ' )
68+ finally :
69+ test_connection .close ()
70+
3971 def _destroy_test_db (self , test_database_name , verbosity ):
4072 """
4173 Internal implementation - remove the test db tables.
0 commit comments