Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mssql/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
SQL_AUTOFIELD = -777555
SQL_BIGAUTOFIELD = -777444


def get_schema_name():
return getattr(settings, 'SCHEMA_TO_INSPECT', 'SCHEMA_NAME()')


class DatabaseIntrospection(BaseDatabaseIntrospection):
# Map type codes to Django Field types.
data_types_reverse = {
Expand Down Expand Up @@ -66,7 +68,8 @@ def get_table_list(self, cursor):
"""
Returns a list of table and view names in the current database.
"""
sql = f'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = {get_schema_name()}'
sql = 'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s' % (
get_schema_name())
cursor.execute(sql)
types = {'BASE TABLE': 't', 'VIEW': 'v'}
return [TableInfo(row[0], types.get(row[1]))
Expand Down Expand Up @@ -114,7 +117,7 @@ def get_table_description(self, cursor, table_name, identity_check=True):
""" % (table_name, column[0])
cursor.execute(sql)
collation_name = cursor.fetchone()
column.append(collation_name[0] if collation_name else '')
column.append(collation_name[0] if collation_name else '')
else:
column.append('')

Expand Down
9 changes: 5 additions & 4 deletions mssql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
result = cursor.fetchall()
columns_to_recreate_index = ', '.join(['%s' % self.quote_name(column[0]) for column in result])
filter_definition = result[0][1]
sql_restore_index += f'CREATE UNIQUE INDEX {index_name} ON {model._meta.db_table} ({columns_to_recreate_index}) WHERE {filter_definition};'
sql_restore_index += 'CREATE UNIQUE INDEX %s ON %s (%s) WHERE %s;' % (
index_name, model._meta.db_table, columns_to_recreate_index, filter_definition)
self.execute(self._db_table_delete_constraint_sql(
self.sql_delete_index, model._meta.db_table, index_name))
self.execute(self._rename_field_sql(model._meta.db_table, old_field, new_field, new_type))
Expand Down Expand Up @@ -453,7 +454,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
(old_field.db_index or not new_field.db_index) and
new_field.db_index or
((indexes_dropped and sorted(indexes_dropped) == sorted([index.name for index in model._meta.indexes])) or
(indexes_dropped and sorted(indexes_dropped) == sorted(auto_index_names)))
(indexes_dropped and sorted(indexes_dropped) == sorted(auto_index_names)))
):
create_index_sql_statement = self._create_index_sql(model, [new_field])
if create_index_sql_statement.__str__() not in [sql.__str__() for sql in self.deferred_sql]:
Expand Down Expand Up @@ -580,8 +581,8 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
for columns in index_columns:
create_index_sql_statement = self._create_index_sql(model, columns)
if (create_index_sql_statement.__str__()
not in [sql.__str__() for sql in self.deferred_sql] + [statement[0].__str__() for statement in post_actions]
):
not in [sql.__str__() for sql in self.deferred_sql] + [statement[0].__str__() for statement in post_actions]
):
self.execute(create_index_sql_statement)

# Type alteration on primary key? Then we need to alter the column
Expand Down