Skip to content

Commit 1fadabe

Browse files
committed
MySQL backend fixes for Python 3.
With Python 2, text sent to and from MySQL is treated as bytes in Python. The database may be recorded by MySQL as having some other encoding (latin1 being the default in some MySQL versions - Roundup does not set an encoding explicitly, unlike in back_postgresql), but as long as MySQL's notion of the connection encoding agrees with its notion of the database encoding, no conversions actually take place and the bytes are stored and returned as-is. With Python 3, text sent to and from MySQL is treated as Python Unicode strings. When the database and connection encoding is latin1, that means the bytes stored in the database under Python 2 are interpreted as latin1 and converted from that to Unicode, producing incorrect results for any non-ASCII characters; furthermore, if trying to store new non-ASCII data in the database under Python 3, any non-latin1 characters produce errors. This patch arranges for both the connection and database character sets to be UTF-8 when using Python 3, and documents a need to export and import the database when moving from Python 2 to Python 3 with this backend.
1 parent 3ae00c0 commit 1fadabe

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

doc/upgrading.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ Python 3 support
4444
Many of the ``.html`` and ``.py`` files from Roundup that are copied
4545
into tracker directories have changed for Python 3 support. If you
4646
wish to move an existing tracker to Python 3, you need to merge in
47-
those changes. If your tracker uses the ``anydbm`` backend, you also
48-
need to export the tracker contents using ``roundup-admin export``
49-
running under Python 2, and them import them using ``roundup-admin
50-
import`` running under Python 3, as for a migration to a different
51-
backend. If using the ``sqlite`` backend, you do not need to export
52-
and import, but need to delete the ``db/otks`` and ``db/sessions``
53-
files when changing Python version.
47+
those changes. If your tracker uses the ``anydbm`` or ``mysql``
48+
backends, you also need to export the tracker contents using
49+
``roundup-admin export`` running under Python 2, and them import them
50+
using ``roundup-admin import`` running under Python 3, as for a
51+
migration to a different backend. If using the ``sqlite`` backend,
52+
you do not need to export and import, but need to delete the
53+
``db/otks`` and ``db/sessions`` files when changing Python version.
54+
If using the ``postgresql`` backend, you do not need to export and
55+
import and no other special database-related steps are needed.
5456

5557
Migrating from 1.5.1 to 1.6.0
5658
=============================

roundup/backends/back_mysql.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from roundup import date, hyperdb, password
3737
from roundup.backends import rdbms_common
3838
import MySQLdb
39-
import os, shutil
39+
import os, shutil, sys
4040
from MySQLdb.constants import ER
4141
import logging
4242

@@ -54,6 +54,8 @@ def connection_dict(config, dbnamestr=None):
5454
del d['password']
5555
if 'port' in d:
5656
d['port'] = int(d['port'])
57+
if sys.version_info[0] > 2:
58+
d['charset'] = 'utf8'
5759
return d
5860

5961
def db_nuke(config):
@@ -90,6 +92,8 @@ def db_create(config):
9092
conn = MySQLdb.connect(**kwargs)
9193
cursor = conn.cursor()
9294
command = "CREATE DATABASE %s"%config.RDBMS_NAME
95+
if sys.version_info[0] > 2:
96+
command += ' CHARACTER SET utf8'
9397
logging.info(command)
9498
cursor.execute(command)
9599
conn.commit()

0 commit comments

Comments
 (0)