Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors with raising FullResultSet exception and with alter_column_type_sql() and collate_sql() functions #229

Merged
merged 5 commits into from
Feb 21, 2023
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
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