From 10d5d1266e1b925e6dbcd0c632f9981ee082f852 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 9 Jun 2014 14:26:53 -0400 Subject: [PATCH] Implement utf8mb4 charset. --- bitshift/database/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bitshift/database/__init__.py b/bitshift/database/__init__.py index 23b190c..61b27b6 100644 --- a/bitshift/database/__init__.py +++ b/bitshift/database/__init__.py @@ -3,6 +3,7 @@ Subpackage with classes and functions to handle communication with the MySQL database backend, which manages the search index. """ +import codecs import os import mmh3 @@ -24,10 +25,17 @@ class Database(object): def _connect(self): """Establish a connection to the database.""" + try: + codecs.lookup("utf8mb4") + except LookupError: + utf8 = codecs.lookup("utf8") + codecs.register(lambda name: utf8 if name == "utf8mb4" else None) + root = os.path.dirname(os.path.abspath(__file__)) default_file = os.path.join(root, ".my.cnf") - return oursql.connect(db="bitshift", read_default_file=default_file, - autoping=True, autoreconnect=True) + return oursql.connect( + db="bitshift", read_default_file=default_file, autoping=True, + autoreconnect=True, charset="utf8mb4") def _migrate(self, cursor, current): """Migrate the database to the latest schema version."""