Fixed test failures on MySQL.

Some tests failed when the time zone definitions were loaded in MySQL
and pytz wasn't installed. This setup isn't supported.
This commit is contained in:
Aymeric Augustin 2013-05-08 12:57:35 +02:00
parent de8aa3a9a9
commit 1fff8daf88
3 changed files with 17 additions and 2 deletions

View File

@ -30,6 +30,11 @@ if (version < (1, 2, 1) or (version[:3] == (1, 2, 1) and
from MySQLdb.converters import conversions, Thing2Literal from MySQLdb.converters import conversions, Thing2Literal
from MySQLdb.constants import FIELD_TYPE, CLIENT from MySQLdb.constants import FIELD_TYPE, CLIENT
try:
import pytz
except ImportError:
pytz = None
from django.conf import settings from django.conf import settings
from django.db import utils from django.db import utils
from django.db.backends import * from django.db.backends import *
@ -186,6 +191,15 @@ class DatabaseFeatures(BaseDatabaseFeatures):
@cached_property @cached_property
def has_zoneinfo_database(self): def has_zoneinfo_database(self):
# MySQL accepts full time zones names (eg. Africa/Nairobi) but rejects
# abbreviations (eg. EAT). When pytz isn't installed and the current
# time zone is LocalTimezone (the only sensible value in this
# context), the current time zone name will be an abbreviation. As a
# consequence, MySQL cannot perform time zone conversions reliably.
if pytz is None:
return False
# Test if the time zone definitions are installed.
cursor = self.connection.cursor() cursor = self.connection.cursor()
cursor.execute("SELECT 1 FROM mysql.time_zone LIMIT 1") cursor.execute("SELECT 1 FROM mysql.time_zone LIMIT 1")
return cursor.fetchone() is not None return cursor.fetchone() is not None

View File

@ -1092,7 +1092,7 @@ class SQLDateTimeCompiler(SQLCompiler):
if datetime is None: if datetime is None:
raise ValueError("Database returned an invalid value " raise ValueError("Database returned an invalid value "
"in QuerySet.dates(). Are time zone " "in QuerySet.dates(). Are time zone "
"definitions installed?") "definitions and pytz installed?")
datetime = datetime.replace(tzinfo=None) datetime = datetime.replace(tzinfo=None)
datetime = timezone.make_aware(datetime, self.query.tzinfo) datetime = timezone.make_aware(datetime, self.query.tzinfo)
yield datetime yield datetime

View File

@ -627,7 +627,8 @@ object. If it's ``None``, Django uses the :ref:`current time zone
- SQLite: install pytz_ — conversions are actually performed in Python. - SQLite: install pytz_ — conversions are actually performed in Python.
- PostgreSQL: no requirements (see `Time Zones`_). - PostgreSQL: no requirements (see `Time Zones`_).
- Oracle: no requirements (see `Choosing a Time Zone File`_). - Oracle: no requirements (see `Choosing a Time Zone File`_).
- MySQL: load the time zone tables with `mysql_tzinfo_to_sql`_. - MySQL: install pytz_ and load the time zone tables with
`mysql_tzinfo_to_sql`_.
.. _pytz: http://pytz.sourceforge.net/ .. _pytz: http://pytz.sourceforge.net/
.. _Time Zones: http://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-TIMEZONES .. _Time Zones: http://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-TIMEZONES