Skip to content

Commit 9f9f3fc

Browse files
committed
add support for TEST_CREATE setting
1 parent 97181eb commit 9f9f3fc

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

django_pyodbc/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def __init__(self, *args, **kwargs):
110110
ops[op] = '%s COLLATE %s' % (sql, self.collation)
111111
self.operators.update(ops)
112112

113+
self.test_create = self.settings_dict.get('TEST_CREATE', True)
114+
113115
if _DJANGO_VERSION >= 13:
114116
self.features = DatabaseFeatures(self)
115117
else:

django_pyodbc/creation.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,61 @@ class DatabaseCreation(BaseDatabaseCreation):
5050
#}
5151
})
5252

53+
def _create_test_db(self, verbosity, autoclobber):
54+
settings_dict = self.connection.settings_dict
55+
56+
if self.connection._DJANGO_VERSION >= 13:
57+
test_name = self._get_test_db_name()
58+
else:
59+
if settings_dict['TEST_NAME']:
60+
test_name = settings_dict['TEST_NAME']
61+
else:
62+
from django.db.backends.creation import TEST_DATABASE_PREFIX
63+
test_name = TEST_DATABASE_PREFIX + settings_dict['NAME']
64+
if not settings_dict['TEST_NAME']:
65+
settings_dict['TEST_NAME'] = test_name
66+
67+
if not self.connection.test_create:
68+
# use the existing database instead of creating a new one
69+
if verbosity >= 1:
70+
print("Dropping tables ... ")
71+
72+
self.connection.close()
73+
settings_dict["NAME"] = test_name
74+
cursor = self.connection.cursor()
75+
qn = self.connection.ops.quote_name
76+
sql = "SELECT TABLE_NAME, CONSTRAINT_NAME " \
77+
"FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS " \
78+
"WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'"
79+
for row in cursor.execute(sql).fetchall():
80+
objs = (qn(row[0]), qn(row[1]))
81+
cursor.execute("ALTER TABLE %s DROP CONSTRAINT %s" % objs)
82+
for table in self.connection.introspection.get_table_list(cursor):
83+
if verbosity >= 1:
84+
print("Dropping table %s" % table)
85+
cursor.execute('DROP TABLE %s' % qn(table))
86+
self.connection.connection.commit()
87+
return test_name
88+
89+
return super(DatabaseCreation, self)._create_test_db(verbosity, autoclobber)
90+
5391
def _destroy_test_db(self, test_database_name, verbosity):
5492
"Internal implementation - remove the test db tables."
55-
cursor = self.connection.cursor()
56-
self.connection.connection.autocommit = True
57-
#time.sleep(1) # To avoid "database is being accessed by other users" errors.
58-
cursor.execute("ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE " % \
59-
self.connection.ops.quote_name(test_database_name))
60-
cursor.execute("DROP DATABASE %s" % \
61-
self.connection.ops.quote_name(test_database_name))
93+
if self.connection.test_create:
94+
cursor = self.connection.cursor()
95+
self.connection.connection.autocommit = True
96+
#time.sleep(1) # To avoid "database is being accessed by other users" errors.
97+
cursor.execute("ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE " % \
98+
self.connection.ops.quote_name(test_database_name))
99+
cursor.execute("DROP DATABASE %s" % \
100+
self.connection.ops.quote_name(test_database_name))
101+
else:
102+
if verbosity >= 1:
103+
test_db_repr = ''
104+
if verbosity >= 2:
105+
test_db_repr = " ('%s')" % test_database_name
106+
print("The database is left undestroyed%s." % test_db_repr)
107+
62108
self.connection.close()
63109

64110
def _prepare_for_test_db_ddl(self):

0 commit comments

Comments
 (0)