NEW Allow setting sql_mode via config (#9721)

This commit is contained in:
Bernard Hamlin 2020-10-22 15:01:30 +13:00 committed by GitHub
parent 8c0ff67c55
commit f00f64120d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View File

@ -28,6 +28,10 @@ Use [phpinfo()](http://php.net/manual/en/function.phpinfo.php) to inspect your c
* SQL Server ([third party module](https://addons.silverstripe.org/add-ons/silverstripe/mssql), community supported)
* SQLite ([third party module](https://addons.silverstripe.org/add-ons/silverstripe/sqlite3), community supported)
### Connection mode (sql_mode) when using MySQL server >=5.7.5
In MySQL versions >=5.7.5, the `ANSI` sql_mode setting behaves differently and includes the `ONLY_FULL_GROUP_BY` setting. It is generally recommended to leave this setting as-is because it results in deterministic SQL. However, for some advanced cases, the sql_mode can be configured on the database connection via the configuration API (see `MySQLDatabase::$sql_mode` for more details.) This setting is only available in Silverstripe CMS 4.7 and later.
## Webserver Configuration
### Overview

View File

@ -5,6 +5,7 @@
- [Experimental support for PHP 8](#experimental-support-for-php-8)
- [Support for Symfony 4 Components](#support-for-symfony-4-components)
- [Default MySQL collation updated](#default-mysql-collation-updated)
- [MySQL connection mode configurable](#mysql-connection-mode-now-configurable)
- [Flysystem dependency shifted](#flysystem-dependency-shifted)
## New features
@ -59,6 +60,10 @@ You can rectify this by upgrading MySQL, enabling the `innodb_large_prefix` sett
reducing the size of affected fields. If none of these solutions are currently suitable, you can
remove the new collation configuration to default back to the previous default collation.
### MySQL connection mode now configurable
In MySQL versions >=5.7.5, the `ANSI` sql_mode setting behaves differently and includes the `ONLY_FULL_GROUP_BY` setting. It is generally recommended to leave this setting as-is because it results in deterministic SQL. However, for some advanced cases, the sql_mode can now be configured on the database connection via the configuration API (see `MySQLDatabase::$sql_mode` for more details.)
### Flysystem dependency shifted
Previously the Flysystem package was pulled in via the `silverstripe/framework` module, but only
@ -71,4 +76,3 @@ An edgecase exists where a project can update to `silverstripe/framework 4.7.0`
`silverstripe/assets 1.6.x`, and lose the Flysystem dependency entirely. The best way to avoid this
is by ensuring you update all core modules to the new minor release at once, ideally through a core
recipe like `silverstripe/recipe-core`.

View File

@ -49,6 +49,15 @@ class MySQLDatabase extends Database implements TransactionManager
*/
private static $charset = 'utf8';
/**
* SQL Mode used on connections to MySQL. Defaults to ANSI. For basic ORM
* compatibility, this setting must always include ANSI or ANSI_QUOTES.
*
* @config
* @var string
*/
private static $sql_mode = 'ANSI';
/**
* Cache for getTransactionManager()
*
@ -84,8 +93,8 @@ class MySQLDatabase extends Database implements TransactionManager
// Notify connector of parameters
$this->connector->connect($parameters);
// This is important!
$this->setSQLMode('ANSI');
// Set sql_mode
$this->setSQLMode(static::config()->get('sql_mode'));
if (isset($parameters['timezone'])) {
$this->selectTimezone($parameters['timezone']);