comparison roundup/backends/back_mysql.py @ 5543:bc3e00a3d24b

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.
author Joseph Myers <jsm@polyomino.org.uk>
date Sun, 16 Sep 2018 16:19:20 +0000
parents 0db3779702d9
children ee2e8f8d6648
comparison
equal deleted inserted replaced
5542:29346d92d80c 5543:bc3e00a3d24b
34 __docformat__ = 'restructuredtext' 34 __docformat__ = 'restructuredtext'
35 35
36 from roundup import date, hyperdb, password 36 from roundup import date, hyperdb, password
37 from roundup.backends import rdbms_common 37 from roundup.backends import rdbms_common
38 import MySQLdb 38 import MySQLdb
39 import os, shutil 39 import os, shutil, sys
40 from MySQLdb.constants import ER 40 from MySQLdb.constants import ER
41 import logging 41 import logging
42 42
43 isolation_levels = \ 43 isolation_levels = \
44 { 'read uncommitted': 'READ UNCOMMITTED' 44 { 'read uncommitted': 'READ UNCOMMITTED'
52 if 'password' in d: 52 if 'password' in d:
53 d['passwd'] = d['password'] 53 d['passwd'] = d['password']
54 del d['password'] 54 del d['password']
55 if 'port' in d: 55 if 'port' in d:
56 d['port'] = int(d['port']) 56 d['port'] = int(d['port'])
57 if sys.version_info[0] > 2:
58 d['charset'] = 'utf8'
57 return d 59 return d
58 60
59 def db_nuke(config): 61 def db_nuke(config):
60 """Clear all database contents and drop database itself""" 62 """Clear all database contents and drop database itself"""
61 if db_exists(config): 63 if db_exists(config):
88 """Create the database.""" 90 """Create the database."""
89 kwargs = connection_dict(config) 91 kwargs = connection_dict(config)
90 conn = MySQLdb.connect(**kwargs) 92 conn = MySQLdb.connect(**kwargs)
91 cursor = conn.cursor() 93 cursor = conn.cursor()
92 command = "CREATE DATABASE %s"%config.RDBMS_NAME 94 command = "CREATE DATABASE %s"%config.RDBMS_NAME
95 if sys.version_info[0] > 2:
96 command += ' CHARACTER SET utf8'
93 logging.info(command) 97 logging.info(command)
94 cursor.execute(command) 98 cursor.execute(command)
95 conn.commit() 99 conn.commit()
96 conn.close() 100 conn.close()
97 101

Roundup Issue Tracker: http://roundup-tracker.org/