Commit c777245e authored by Thomas Müller's avatar Thomas Müller Committed by Thomas Müller
Browse files

During setup of a mysql database we try to detect if charset 'utf8mb4' can be used

parent 36823b2c
......@@ -130,6 +130,7 @@ class ConnectionFactory {
$eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode));
break;
}
/** @var Connection $connection */
$connection = DriverManager::getConnection(
array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams),
new Configuration(),
......
......@@ -80,11 +80,11 @@ class Setup {
}
static $dbSetupClasses = [
'mysql' => '\OC\Setup\MySQL',
'pgsql' => '\OC\Setup\PostgreSQL',
'oci' => '\OC\Setup\OCI',
'sqlite' => '\OC\Setup\Sqlite',
'sqlite3' => '\OC\Setup\Sqlite',
'mysql' => \OC\Setup\MySQL::class,
'pgsql' => \OC\Setup\PostgreSQL::class,
'oci' => \OC\Setup\OCI::class,
'sqlite' => \OC\Setup\Sqlite::class,
'sqlite3' => \OC\Setup\Sqlite::class,
];
/**
......
......@@ -26,6 +26,7 @@
*/
namespace OC\Setup;
use OC\DB\Connection;
use OC\DB\ConnectionFactory;
use OCP\IDBConnection;
......@@ -36,6 +37,12 @@ class MySQL extends AbstractDatabase {
//check if the database user has admin right
$connection = $this->connect();
// detect mb4
if (is_null($this->config->getSystemValue('mysql.utf8mb4', null)) && $this->supports4ByteCharset($connection)) {
$this->config->setSystemValue('mysql.utf8mb4', true);
$connection = $this->connect();
}
$this->createSpecificUser($username, $connection);
//create the database
......@@ -58,7 +65,7 @@ class MySQL extends AbstractDatabase {
$name = $this->dbName;
$user = $this->dbUser;
//we can't use OC_DB functions here because we need to connect as the administrative user.
$characterSet = \OC::$server->getSystemConfig()->getValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
$characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
$query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET $characterSet COLLATE ${characterSet}_bin;";
$connection->executeUpdate($query);
} catch (\Exception $ex) {
......@@ -184,4 +191,23 @@ class MySQL extends AbstractDatabase {
'dbpassword' => $this->dbPassword,
]);
}
/**
* @param Connection $connection
* @return bool
*/
private function supports4ByteCharset(Connection $connection) {
foreach (['innodb_file_format' => 'Barracuda', 'innodb_large_prefix' => 'ON', 'innodb_file_per_table' => 'ON'] as $var => $val) {
$result = $connection->executeQuery("SHOW VARIABLES LIKE '$var'");
$rows = $result->fetch();
$result->closeCursor();
if ($rows === false) {
return false;
}
if (strcasecmp($rows['Value'], $val) === 0) {
return false;
}
}
return true;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment