1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #31331 -- Added DatabaseWrapper.sql_mode to MySQL.

This commit is contained in:
Mariusz Felisiak 2020-03-03 08:30:37 +01:00
parent 7b8fa1653f
commit a19505eb2e
3 changed files with 17 additions and 6 deletions

View File

@ -364,3 +364,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
@cached_property @cached_property
def mysql_is_mariadb(self): def mysql_is_mariadb(self):
return 'mariadb' in self.mysql_server_info.lower() return 'mariadb' in self.mysql_server_info.lower()
@cached_property
def sql_mode(self):
with self.cursor() as cursor:
cursor.execute('SELECT @@sql_mode')
sql_mode = cursor.fetchone()
return set(sql_mode[0].split(',') if sql_mode else ())

View File

@ -10,11 +10,7 @@ class DatabaseValidation(BaseDatabaseValidation):
return issues return issues
def _check_sql_mode(self, **kwargs): def _check_sql_mode(self, **kwargs):
with self.connection.cursor() as cursor: if not (self.connection.sql_mode & {'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES'}):
cursor.execute("SELECT @@sql_mode")
sql_mode = cursor.fetchone()
modes = set(sql_mode[0].split(',') if sql_mode else ())
if not (modes & {'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES'}):
return [checks.Warning( return [checks.Warning(
"MySQL Strict Mode is not set for database connection '%s'" % self.connection.alias, "MySQL Strict Mode is not set for database connection '%s'" % self.connection.alias,
hint="MySQL's Strict Mode fixes many data integrity problems in MySQL, " hint="MySQL's Strict Mode fixes many data integrity problems in MySQL, "

View File

@ -2,7 +2,7 @@ import unittest
from unittest import mock from unittest import mock
from django.core.checks.database import check_database_backends from django.core.checks.database import check_database_backends
from django.db import connection from django.db import connection, connections
from django.test import TestCase from django.test import TestCase
@ -18,6 +18,12 @@ class DatabaseCheckTests(TestCase):
@unittest.skipUnless(connection.vendor == 'mysql', 'Test only for MySQL') @unittest.skipUnless(connection.vendor == 'mysql', 'Test only for MySQL')
def test_mysql_strict_mode(self): def test_mysql_strict_mode(self):
def _clean_sql_mode():
for alias in self.databases:
if hasattr(connections[alias], 'sql_mode'):
del connections[alias].sql_mode
_clean_sql_mode()
good_sql_modes = [ good_sql_modes = [
'STRICT_TRANS_TABLES,STRICT_ALL_TABLES', 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES',
'STRICT_TRANS_TABLES', 'STRICT_TRANS_TABLES',
@ -29,6 +35,7 @@ class DatabaseCheckTests(TestCase):
return_value=(response,) return_value=(response,)
): ):
self.assertEqual(check_database_backends(databases=self.databases), []) self.assertEqual(check_database_backends(databases=self.databases), [])
_clean_sql_mode()
bad_sql_modes = ['', 'WHATEVER'] bad_sql_modes = ['', 'WHATEVER']
for response in bad_sql_modes: for response in bad_sql_modes:
@ -40,3 +47,4 @@ class DatabaseCheckTests(TestCase):
result = check_database_backends(databases=self.databases) result = check_database_backends(databases=self.databases)
self.assertEqual(len(result), 2) self.assertEqual(len(result), 2)
self.assertEqual([r.id for r in result], ['mysql.W002', 'mysql.W002']) self.assertEqual([r.id for r in result], ['mysql.W002', 'mysql.W002'])
_clean_sql_mode()