Skip to content

Commit 40da1f9

Browse files
authored
Drop django_mysql.utils.connection_is_mariadb (#913)
1 parent 0779ae1 commit 40da1f9

File tree

12 files changed

+28
-50
lines changed

12 files changed

+28
-50
lines changed

HISTORY.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ History
66

77
* Support MariaDB 10.7.
88

9+
* Drop ``django_mysql.utils.connection_is_mariadb``. On Django 3.0+ you can
10+
simply check:
11+
12+
.. code-block:: python
13+
14+
connection.vendor == "mysql" and connection.mysql_is_mariadb
15+
916
4.5.0 (2022-01-23)
1017
------------------
1118

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ action, or get started with :doc:`installation`. Otherwise, take your pick:
2828
locks
2929
status
3030
management_commands/index
31-
utilities
3231
test_utilities
3332
exceptions
3433
contributing

docs/utilities.rst

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/django_mysql/models/fields/dynamic.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from django_mysql.checks import mysql_connections
2525
from django_mysql.models.lookups import DynColHasKey
2626
from django_mysql.typing import DeconstructResult
27-
from django_mysql.utils import connection_is_mariadb
2827

2928
try:
3029
import mariadb_dyncol
@@ -102,7 +101,7 @@ def _check_mariadb_version(self) -> list[checks.CheckMessage]:
102101
errors = []
103102

104103
any_conn_works = any(
105-
(hasattr(conn, "mysql_version") and connection_is_mariadb(conn))
104+
(conn.vendor == "mysql" and conn.mysql_is_mariadb)
106105
for _alias, conn in mysql_connections()
107106
)
108107

@@ -122,9 +121,7 @@ def _check_character_set(self) -> list[checks.CheckMessage]:
122121

123122
conn = None
124123
for _alias, check_conn in mysql_connections():
125-
if hasattr(check_conn, "mysql_version") and connection_is_mariadb(
126-
check_conn
127-
):
124+
if check_conn.vendor == "mysql" and check_conn.mysql_is_mariadb:
128125
conn = check_conn
129126
break
130127

src/django_mysql/models/functions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from django.db.models import Func, IntegerField, JSONField, TextField, Value
1212
from django.db.models.sql.compiler import SQLCompiler
1313

14-
from django_mysql.utils import connection_is_mariadb
15-
1614
ExpressionArgument = Union[
1715
Expression,
1816
str, # column reference handled by Django
@@ -291,7 +289,7 @@ def as_sql(
291289
if connection.vendor != "mysql": # pragma: no cover
292290
raise AssertionError("JSONValue only supports MySQL/MariaDB")
293291
json_string = json.dumps(self._data, allow_nan=False)
294-
if connection_is_mariadb(connection):
292+
if connection.vendor == "mysql" and connection.mysql_is_mariadb:
295293
# MariaDB doesn't support explicit cast to JSON.
296294
return "JSON_EXTRACT(%s, '$')", (json_string,)
297295
else:

src/django_mysql/utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ def format_duration(total_seconds: int) -> str:
9393
return "".join(out)
9494

9595

96-
def connection_is_mariadb(connection: BaseDatabaseWrapper) -> bool:
97-
return connection.vendor == "mysql" and connection.mysql_is_mariadb
98-
99-
10096
def settings_to_cmd_args(settings_dict: dict[str, Any]) -> list[str]:
10197
"""
10298
Copied from django 1.8 MySQL backend DatabaseClient - where the runshell

tests/testapp/models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
SizedBinaryField,
3434
SizedTextField,
3535
)
36-
from django_mysql.utils import connection_is_mariadb
3736

3837

3938
class EnumModel(Model):
@@ -122,7 +121,7 @@ class DynamicModel(Model):
122121
@classmethod
123122
def check(cls, **kwargs):
124123
# Disable the checks on MySQL so that checks tests don't fail
125-
if not (connection_is_mariadb(connection)):
124+
if not connection.mysql_is_mariadb:
126125
return []
127126
return super().check(**kwargs)
128127

@@ -136,7 +135,7 @@ class SpeclessDynamicModel(Model):
136135
@classmethod
137136
def check(cls, **kwargs):
138137
# Disable the checks on MySQL so that checks tests don't fail
139-
if not (connection_is_mariadb(connection)):
138+
if not connection.mysql_is_mariadb:
140139
return []
141140
return super().check(**kwargs)
142141

tests/testapp/test_dynamicfield.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
import pytest
99
from django.core import serializers
1010
from django.core.exceptions import FieldError
11-
from django.db import connection, models
11+
from django.db import connection, connections, models
1212
from django.db.migrations.writer import MigrationWriter
1313
from django.db.models import CharField, Transform
1414
from django.test import TestCase
1515
from django.test.utils import isolate_apps
1616

1717
from django_mysql.models import DynamicField
18-
from django_mysql.utils import connection_is_mariadb
1918
from tests.testapp.models import DynamicModel, SpeclessDynamicModel
2019

2120

2221
class DynColTestCase(TestCase):
2322
@classmethod
2423
def setUpClass(cls):
25-
if not (connection_is_mariadb(connection)):
24+
if not connection.mysql_is_mariadb:
2625
raise SkipTest("Dynamic Columns require MariaDB")
2726
super().setUpClass()
2827

@@ -286,14 +285,18 @@ class TestCheck(DynColTestCase):
286285

287286
databases = ["default", "other"]
288287

289-
@mock.patch("django_mysql.models.fields.dynamic.connection_is_mariadb")
290-
def test_db_not_mariadb(self, is_mariadb):
291-
is_mariadb.return_value = False
292-
288+
def test_db_not_mariadb(self):
293289
class Valid(models.Model):
294290
field = DynamicField()
295291

296-
errors = Valid.check()
292+
mock_default = mock.patch.object(
293+
connections["default"], "mysql_is_mariadb", False
294+
)
295+
mock_other = mock.patch.object(connections["other"], "mysql_is_mariadb", False)
296+
297+
with mock_default, mock_other:
298+
errors = Valid.check()
299+
297300
assert len(errors) == 1
298301
assert errors[0].id == "django_mysql.E013"
299302
assert "MariaDB is required" in errors[0].msg

tests/testapp/test_functions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
UpdateXML,
3838
XMLExtractValue,
3939
)
40-
from django_mysql.utils import connection_is_mariadb
4140
from tests.testapp.models import Alphabet, Author, DynamicModel, JSONModel
4241
from tests.testapp.test_dynamicfield import DynColTestCase
4342
from tests.testapp.utils import print_all_queries
@@ -604,7 +603,7 @@ def test_json_array_append(self):
604603
class RegexpFunctionTests(TestCase):
605604
def setUp(self):
606605
super().setUp()
607-
if not connection_is_mariadb(connection):
606+
if not connection.mysql_is_mariadb:
608607
raise SkipTest("MariaDB is required")
609608

610609
def test_regex_instr(self):

tests/testapp/test_locks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from django_mysql.exceptions import TimeoutError
1313
from django_mysql.locks import Lock, TableLock
1414
from django_mysql.models import Model
15-
from django_mysql.utils import connection_is_mariadb
1615
from tests.testapp.models import (
1716
AgedCustomer,
1817
Alphabet,
@@ -30,7 +29,7 @@ class LockTests(TestCase):
3029
def setUpClass(cls):
3130
super().setUpClass()
3231

33-
cls.supports_lock_info = connection_is_mariadb(connection)
32+
cls.supports_lock_info = connection.mysql_is_mariadb
3433
if cls.supports_lock_info:
3534
with connection.cursor() as cursor:
3635
cursor.execute(
@@ -237,7 +236,7 @@ def is_locked(self, connection_name, table_name):
237236
if rows:
238237
assert len(rows) == 1
239238
return rows[0][2] > 0
240-
else:
239+
else: # pragma: no cover
241240
# MySQL 8+ closes the table really quickly. If it's closed,
242241
# it's not locked.
243242
return False

0 commit comments

Comments
 (0)