Skip to content

Commit

Permalink
Fix errors with raising FullResultSet exception and with alter_column…
Browse files Browse the repository at this point in the history
…_type_sql() and collate_sql() functions (#229)

* fix error with raising fullresultset

* add django4.2 condition

* fix alter_column_type_sql and collate_sql to take 2 additional arguments

* delete argument 'old_rel_collation'

* fix arguments names
  • Loading branch information
maikhanhbui authored Feb 21, 2023
1 parent 0a51811 commit 81018de
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
16 changes: 14 additions & 2 deletions mssql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from django.db.utils import NotSupportedError
if django.VERSION >= (3, 1):
from django.db.models.fields.json import compile_json_path, KeyTransform as json_KeyTransform
if django.VERSION >= (4, 2):
from django.core.exceptions import FullResultSet

def _as_sql_agv(self, compiler, connection):
return self.as_sql(compiler, connection, template='%(function)s(CONVERT(float, %(field)s))')
Expand Down Expand Up @@ -233,8 +235,18 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
# This must come after 'select', 'ordering', and 'distinct' -- see
# docstring of get_from_clause() for details.
from_, f_params = self.get_from_clause()
where, w_params = self.compile(self.where) if self.where is not None else ("", [])
having, h_params = self.compile(self.having) if self.having is not None else ("", [])
if django.VERSION >= (4, 2):
try:
where, w_params = self.compile(self.where) if self.where is not None else ("", [])
except FullResultSet:
where, w_params = "", []
try:
having, h_params = self.compile(self.having) if self.having is not None else ("", [])
except FullResultSet:
having, h_params = "", []
else:
where, w_params = self.compile(self.where) if self.where is not None else ("", [])
having, h_params = self.compile(self.having) if self.having is not None else ("", [])
params = []
result = ['SELECT']

Expand Down
37 changes: 28 additions & 9 deletions mssql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,14 @@ def _alter_column_null_sql(self, model, old_field, new_field):
[],
)

def _alter_column_type_sql(self, model, old_field, new_field, new_type):
new_type = self._set_field_new_type_null_status(old_field, new_type)
return super()._alter_column_type_sql(model, old_field, new_field, new_type)
if django_version >= (4, 2):
def _alter_column_type_sql(self, model, old_field, new_field, new_type, old_collation, new_collation):
new_type = self._set_field_new_type_null_status(old_field, new_type)
return super()._alter_column_type_sql(model, old_field, new_field, new_type, old_collation, new_collation)
else:
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
new_type = self._set_field_new_type_null_status(old_field, new_type)
return super()._alter_column_type_sql(model, old_field, new_field, new_type)

def alter_unique_together(self, model, old_unique_together, new_unique_together):
"""
Expand Down Expand Up @@ -443,7 +448,12 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
post_actions = []
# Type change?
if old_type != new_type:
fragment, other_actions = self._alter_column_type_sql(model, old_field, new_field, new_type)
if django_version >= (4, 2):
fragment, other_actions = self._alter_column_type_sql(
model, old_field, new_field, new_type, old_collation=None, new_collation=None
)
else:
fragment, other_actions = self._alter_column_type_sql(model, old_field, new_field, new_type)
actions.append(fragment)
post_actions.extend(other_actions)
# Drop unique constraint, SQL Server requires explicit deletion
Expand Down Expand Up @@ -683,9 +693,14 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
for old_rel, new_rel in rels_to_update:
rel_db_params = new_rel.field.db_parameters(connection=self.connection)
rel_type = rel_db_params['type']
fragment, other_actions = self._alter_column_type_sql(
new_rel.related_model, old_rel.field, new_rel.field, rel_type
)
if django_version >= (4, 2):
fragment, other_actions = self._alter_column_type_sql(
new_rel.related_model, old_rel.field, new_rel.field, rel_type, old_collation=None, new_collation=None
)
else:
fragment, other_actions = self._alter_column_type_sql(
new_rel.related_model, old_rel.field, new_rel.field, rel_type
)
# Drop related_model indexes, so it can be altered
index_names = self._db_table_constraint_names(old_rel.related_model._meta.db_table, index=True)
for index_name in index_names:
Expand Down Expand Up @@ -1262,8 +1277,12 @@ def add_constraint(self, model, constraint):
(constraint.condition.connector, constraint.name))
super().add_constraint(model, constraint)

def _collate_sql(self, collation):
return ' COLLATE ' + collation
if django_version >= (4, 2):
def _collate_sql(self, collation, old_collation=None, table_name=None):
return ' COLLATE ' + collation if collation else ""
else:
def _collate_sql(self, collation):
return ' COLLATE ' + collation

def _create_index_name(self, table_name, column_names, suffix=""):
index_name = super()._create_index_name(table_name, column_names, suffix)
Expand Down

0 comments on commit 81018de

Please sign in to comment.