Skip to content

Instantly share code, notes, and snippets.

@stovak
Created October 24, 2015 23:52
Show Gist options
  • Save stovak/6e4199a1107435808b8c to your computer and use it in GitHub Desktop.
Save stovak/6e4199a1107435808b8c to your computer and use it in GitHub Desktop.
From 59e8b8ae778d1511af39caa677bebf81cfbe45ca Mon Sep 17 00:00:00 2001
From: Tom Stovall <stovak@anki.com>
Date: Sat, 24 Oct 2015 16:51:31 -0700
Subject: [PATCH] 2488180-mysql-utf8mb4-support-reroll-for-7.41
---
includes/database/mysql/database.inc | 9 +++++----
includes/database/mysql/schema.inc | 11 ++++++++++-
sites/default/default.settings.php | 15 +++++++++++++++
3 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/includes/database/mysql/database.inc b/includes/database/mysql/database.inc
index fdf9271..b1f7503 100644
--- a/includes/database/mysql/database.inc
+++ b/includes/database/mysql/database.inc
@@ -25,7 +25,8 @@ class DatabaseConnection_mysql extends DatabaseConnection {
// MySQL never supports transactional DDL.
$this->transactionalDDLSupport = FALSE;
-
+ // Allow the drupal default charset to be overridden in the settings.php.
+ $charset = (isset($connection_options['charset']) ? $connection_options['charset'] : 'utf8');
$this->connectionOptions = $connection_options;
// The DSN should use either a socket or a host/port.
@@ -39,7 +40,7 @@ class DatabaseConnection_mysql extends DatabaseConnection {
// Character set is added to dsn to ensure PDO uses the proper character
// set when escaping. This has security implications. See
// https://www.drupal.org/node/1201452 for further discussion.
- $dsn .= ';charset=utf8';
+ $dsn .= ';charset=' . $charset;
$dsn .= ';dbname=' . $connection_options['database'];
// Allow PDO options to be overridden.
$connection_options += array(
@@ -63,10 +64,10 @@ class DatabaseConnection_mysql extends DatabaseConnection {
// certain one has been set; otherwise, MySQL defaults to 'utf8_general_ci'
// for UTF-8.
if (!empty($connection_options['collation'])) {
- $this->exec('SET NAMES utf8 COLLATE ' . $connection_options['collation']);
+ $this->exec('SET NAMES ' . $charset . ' COLLATE ' . $connection_options['collation']);
}
else {
- $this->exec('SET NAMES utf8');
+ $this->exec('SET NAMES ' . $charset);
}
// Set MySQL init_commands if not already defined. Default Drupal's MySQL
diff --git a/includes/database/mysql/schema.inc b/includes/database/mysql/schema.inc
index 2a2722e..d84dc26 100644
--- a/includes/database/mysql/schema.inc
+++ b/includes/database/mysql/schema.inc
@@ -81,7 +81,8 @@ class DatabaseSchema_mysql extends DatabaseSchema {
// Provide defaults if needed.
$table += array(
'mysql_engine' => 'InnoDB',
- 'mysql_character_set' => 'utf8',
+ // Allow charset to be overridden in settings.php.
+ 'mysql_character_set' => (isset($info['charset']) ? $info['charset'] : 'utf8'),
);
$sql = "CREATE TABLE {" . $name . "} (\n";
@@ -131,6 +132,14 @@ class DatabaseSchema_mysql extends DatabaseSchema {
protected function createFieldSql($name, $spec) {
$sql = "`" . $name . "` " . $spec['mysql_type'];
+ $info = $this->connection->getConnectionOptions();
+ if (isset($spec['length']) && isset($info['charset'])) {
+ // If the length of the field is set and the collation is utf8mb4
+ // length should be 191 and not 255.
+ if ($spec['length'] >= 192 && $info['charset'] == "utf8mb4") {
+ $spec['length'] = 191;
+ }
+ }
if (in_array($spec['mysql_type'], array('VARCHAR', 'CHAR', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'TEXT'))) {
if (isset($spec['length'])) {
$sql .= '(' . $spec['length'] . ')';
diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php
index 7e36a4a..95e13bd 100644
--- a/sites/default/default.settings.php
+++ b/sites/default/default.settings.php
@@ -126,6 +126,21 @@
* );
* @endcode
*
+ * For handling proper UTF8 including emojis, asian symbols, mathematical
+ * symbols, you may use the collation and charset to utf8mb4:
+ * @code
+ * $databases['default']['default'] = array(
+ * 'driver' => 'mysql',
+ * 'database' => 'databasename',
+ * 'username' => 'username',
+ * 'password' => 'password',
+ * 'host' => 'localhost',
+ * 'prefix' => 'main_',
+ * 'collation' => 'utf8mb4_general_ci',
+ * 'charset' => 'utf8mb4',
+ * );
+ * @endcode
+ *
* You can optionally set prefixes for some or all database table names
* by using the 'prefix' setting. If a prefix is specified, the table
* name will be prepended with its value. Be sure to use valid database
--
2.4.9 (Apple Git-60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment