Skip to content

Commit

Permalink
Fix issues with MySQL utf8mb4 support
Browse files Browse the repository at this point in the history
Fixes: octobercms#1927. Related: laravel/framework#17508. Issue occurs when database configuration related to full support for the utf8mb4 charset is incorrect; MySQL > 5.7 & MariaDB > 10.2 doesn't have this issue because they default to the correct configuration values; this fix solves the issue for older versions of MySQL and MariaDB without requiring database server configuration changes.

The root cause of the issue with the utf8mb4 encoding is that both InnoDB and MyISAM have too low of an index key prefix limit (767 bytes and 1000 bytes respectively) to properly store 255 4-byte characters; which would take 1024 bytes. See the docs on InnoDB limitations: https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html

In MySQL >= 5.7 & MariaDB >= 10.2 this limit has been bumped to 3076 bytes by the changing of the default value of the `innodb_large_prefix` configuration property (introduced in MySQL 5.5) to true; which is what bumps up the limit. In order to manually set that property to true on earlier versions, `innodb_file_format` must be set to `BARRACUDA` and `row_format` must be `DYNAMIC` or `COMPRESSED`. See http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/ for more information. 

This change fixes the issue by changing the default string length to 191 (total of 764 bytes, within the older size limit) when the MySQL database config is detected to be using the utf8mb4 charset.
  • Loading branch information
LukeTowers authored Feb 17, 2018
1 parent 708b1e3 commit bd9f885
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Backend\Classes\WidgetManager;
use October\Rain\Support\ModuleServiceProvider;
use October\Rain\Router\Helper as RouterHelper;
use Illuminate\Support\Facades\Schema;

class ServiceProvider extends ModuleServiceProvider
{
Expand Down Expand Up @@ -84,6 +85,11 @@ public function register()
*/
public function boot()
{
// Fix UTF8MB4 support for MariaDB < 10.2 and MySQL < 5.7
if (Config::get('database.connections.mysql.charset') === 'utf8mb4') {
Schema::defaultStringLength(191);
}

/*
* Boot plugins
*/
Expand Down

0 comments on commit bd9f885

Please sign in to comment.