Shorten default string length

- Introduce varcharmax config item, this default eventually should be increased to 255, when MySQL 5.6 support is dropped
- Config item can be kept to retain legacy support
- Only apply to mysql driver, previously was impacting other drivers
- Source true config values, previously was sourcing hard coded "mysql" connection values
This commit is contained in:
Samuel Georges 2019-12-29 11:43:27 +11:00
parent bbed527ecc
commit 160ae441ff
2 changed files with 33 additions and 13 deletions

View File

@ -53,16 +53,17 @@ return [
],
'mysql' => [
'driver' => 'mysql',
'engine' => 'InnoDB',
'host' => 'localhost',
'port' => 3306,
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'driver' => 'mysql',
'engine' => 'InnoDB',
'host' => 'localhost',
'port' => 3306,
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'varcharmax' => 191,
],
'pgsql' => [

View File

@ -1,5 +1,6 @@
<?php namespace System;
use Db;
use App;
use View;
use Event;
@ -84,9 +85,7 @@ class ServiceProvider extends ModuleServiceProvider
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);
}
$this->applyDatabaseDefaultStringLength();
// Fix use of Storage::url() for local disks that haven't been configured correctly
foreach (Config::get('filesystems.disks') as $key => $config) {
@ -564,4 +563,24 @@ class ServiceProvider extends ModuleServiceProvider
{
View::share('appName', Config::get('app.name'));
}
/**
* Fix UTF8MB4 support for old versions of MariaDB (<10.2) and MySQL (<5.7)
*/
protected function applyDatabaseDefaultStringLength()
{
if (Db::getDriverName() !== 'mysql') {
return;
}
$defaultStrLen = Db::getConfig('varcharmax', null);
if ($defaultStrLen === null && Db::getConfig('charset') === 'utf8mb4') {
$defaultStrLen = 191;
}
if ($defaultStrLen !== null) {
Schema::defaultStringLength((int) $defaultStrLen);
}
}
}