Skip to content

Fix startup errors in Django 3.1 #64

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

Merged
merged 5 commits into from
Oct 16, 2020
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
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ jobs:
tox_env:
- "py36-django22"
- "py36-django30"
- "py36-django31"

- "py37-django22"
- "py37-django30"
- "py37-django31"

- "py38-django30"
- "py38-django31"

include:
- python: "3.6"
Expand All @@ -54,15 +57,24 @@ jobs:
- python: "3.6"
tox_env: "py36-django30"

- python: "3.6"
tox_env: "py36-django31"

- python: "3.7"
tox_env: "py37-django22"

- python: "3.7"
tox_env: "py37-django30"

- python: "3.7"
tox_env: "py37-django31"

- python: "3.8"
tox_env: "py38-django30"

- python: "3.8"
tox_env: "py38-django31"


steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,25 @@ matrix:

- { before_install: *linux_before_install, python: "3.6", os: linux, env: TOX_ENV=py36-django22 }
- { before_install: *linux_before_install, python: "3.6", os: linux, env: TOX_ENV=py36-django30 }
- { before_install: *linux_before_install, python: "3.6", os: linux, env: TOX_ENV=py36-django31 }

- { before_install: *linux_before_install, python: "3.7", os: linux, env: TOX_ENV=py37-django22 }
- { before_install: *linux_before_install, python: "3.7", os: linux, env: TOX_ENV=py37-django30 }
- { before_install: *linux_before_install, python: "3.7", os: linux, env: TOX_ENV=py37-django31 }

- { before_install: *linux_before_install, python: "3.8", os: linux, env: TOX_ENV=py38-django30 }
- { before_install: *linux_before_install, python: "3.8", os: linux, env: TOX_ENV=py38-django31 }

- { before_install: *win_before_install, language: sh, python: "3.6", os: windows, env: TOX_ENV=py36-django22 }
- { before_install: *win_before_install, language: sh, python: "3.6", os: windows, env: TOX_ENV=py36-django30 }
- { before_install: *win_before_install, language: sh, python: "3.6", os: windows, env: TOX_ENV=py36-django31 }

- { before_install: *win_before_install, language: sh, python: "3.7", os: windows, env: TOX_ENV=py37-django22 }
- { before_install: *win_before_install, language: sh, python: "3.7", os: windows, env: TOX_ENV=py37-django30 }
- { before_install: *win_before_install, language: sh, python: "3.7", os: windows, env: TOX_ENV=py37-django31 }

- { before_install: *win_before_install, language: sh, python: "3.8", os: windows, env: TOX_ENV=py38-django30 }
- { before_install: *win_before_install, language: sh, python: "3.8", os: windows, env: TOX_ENV=py38-django31 }



Expand Down
13 changes: 10 additions & 3 deletions sql_server/pyodbc/creation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import binascii
import os

import django
from django.db.backends.base.creation import BaseDatabaseCreation


class DatabaseCreation(BaseDatabaseCreation):
@property
def cursor(self):
if django.VERSION >= (3, 1):
return self.connection._nodb_cursor

return self.connection._nodb_connection.cursor

def _destroy_test_db(self, test_database_name, verbosity):
"""
Expand All @@ -14,7 +21,7 @@ def _destroy_test_db(self, test_database_name, verbosity):
# ourselves. Connect to the previous database (not the test database)
# to do so, because it's not allowed to delete a database while being
# connected to it.
with self.connection._nodb_connection.cursor() as cursor:
with self.cursor() as cursor:
to_azure_sql_db = self.connection.to_azure_sql_db
if not to_azure_sql_db:
cursor.execute("ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
Expand All @@ -36,7 +43,7 @@ def enable_clr(self):
This function will not fail if current user doesn't have
permissions to enable clr, and clr is already enabled
"""
with self._nodb_connection.cursor() as cursor:
with self.cursor() as cursor:
# check whether clr is enabled
cursor.execute('''
SELECT value FROM sys.configurations
Expand Down Expand Up @@ -86,7 +93,7 @@ def install_regex_clr(self, database_name):

self.enable_clr()

with self._nodb_connection.cursor() as cursor:
with self.cursor() as cursor:
for s in sql:
cursor.execute(s)

Expand Down
1 change: 1 addition & 0 deletions sql_server/pyodbc/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
requires_literal_defaults = True
requires_sqlparse_for_splitting = False
supports_boolean_expr_in_select_clause = False
supports_deferrable_unique_constraints = False
supports_ignore_conflicts = False
supports_index_on_text_field = False
supports_paramstyle_pyformat = False
Expand Down
19 changes: 19 additions & 0 deletions sql_server/pyodbc/operations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import datetime
import uuid
import warnings
import django

from django.conf import settings
from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.models import Exists, ExpressionWrapper
from django.db.models.expressions import RawSQL
from django.db.models.sql.where import WhereNode
from django.utils import timezone
from django.utils.encoding import force_str

Expand Down Expand Up @@ -440,3 +444,18 @@ def time_trunc_sql(self, lookup_type, field_name):
elif lookup_type == 'second':
sql = "CONVERT(time, SUBSTRING(CONVERT(varchar, %s, 114), 0, 9))" % field_name
return sql

def conditional_expression_supported_in_where_clause(self, expression):
"""
Following "Moved conditional expression wrapping to the Exact lookup" in django 3.1
https://github.com/django/django/commit/37e6c5b79bd0529a3c85b8c478e4002fd33a2a1d
"""
if django.VERSION >= (3, 1):
if isinstance(expression, (Exists, WhereNode)):
return True
if isinstance(expression, ExpressionWrapper) and expression.conditional:
return self.conditional_expression_supported_in_where_clause(expression.expression)
if isinstance(expression, RawSQL) and expression.conditional:
return True
return False
return True
2 changes: 2 additions & 0 deletions sql_server/pyodbc/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,15 @@ def create_unique_name(*args, **kwargs):
name=name,
columns=columns,
condition=' WHERE ' + condition,
deferrable=''
) if self.connection.features.supports_partial_indexes else None
else:
return Statement(
self.sql_create_unique,
table=table,
name=name,
columns=columns,
deferrable=''
)

def _create_index_sql(self, model, fields, *, name=None, suffix='', using='',
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
envlist =
{py36,py37}-django22,
{py36,py37,py38}-django30,
{py36,py37,py38}-django31,

[testenv]
passenv =
Expand All @@ -19,4 +20,5 @@ commands =
deps =
django22: django==2.2.*
django30: django>=3.0a1,<3.1
django31: django>=3.1,<3.2
dj-database-url==0.5.0