22 const DB_PREFIX_LENGTH = 5;
29 private $connectionFactory;
36 public function __construct(ConnectionFactory $connectionFactory)
38 $this->connectionFactory = $connectionFactory;
48 public function checkDatabaseTablePrefix(
$prefix)
52 if (
$prefix !==
'' && !preg_match(
'/^([a-zA-Z])([[:alnum:]_]+)$/',
$prefix)) {
53 throw new \InvalidArgumentException(
54 'Please correct the table prefix format, should contain only numbers, letters or underscores.' 55 .
' The first character should be a letter.' 59 if (strlen(
$prefix) > self::DB_PREFIX_LENGTH) {
60 throw new \InvalidArgumentException(
61 'Table prefix length can\'t be more than ' . self::DB_PREFIX_LENGTH .
' characters.' 78 public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass =
'')
90 throw new \Magento\Setup\Exception(
'Database connection failure.');
93 $mysqlVersion =
$connection->fetchOne(
'SELECT version()');
95 if (preg_match(
'/^([0-9\.]+)/', $mysqlVersion, $matches)) {
96 if (isset($matches[1]) && !empty($matches[1])) {
98 throw new \Magento\Setup\Exception(
106 return $this->checkDatabaseName(
$connection, $dbName) && $this->checkDatabasePrivileges(
$connection, $dbName);
117 private function checkDatabaseName(\
Magento\Framework\DB\Adapter\AdapterInterface
$connection, $dbName)
119 $query =
"SHOW DATABASES";
121 foreach ($accessibleDbs as $accessibleDbName) {
122 if ($dbName == $accessibleDbName) {
126 throw new \Magento\Setup\Exception(
127 "Database '{$dbName}' does not exist " 128 .
"or specified database server user does not have privileges to access this database." 140 private function checkDatabasePrivileges(\
Magento\Framework\DB\Adapter\AdapterInterface
$connection, $dbName)
142 $requiredPrivileges = [
151 'CREATE TEMPORARY TABLES',
162 $userPrivilegesQuery =
"SELECT PRIVILEGE_TYPE FROM USER_PRIVILEGES " 163 .
"WHERE REPLACE(GRANTEE, '\'', '') = current_user()";
164 $grantInfo =
$connection->query($userPrivilegesQuery)->fetchAll(\PDO::FETCH_NUM);
165 if (empty(array_diff($requiredPrivileges, $this->parseGrantInfo($grantInfo)))) {
170 $schemaPrivilegesQuery =
"SELECT PRIVILEGE_TYPE FROM SCHEMA_PRIVILEGES " .
171 "WHERE '$dbName' LIKE TABLE_SCHEMA AND REPLACE(GRANTEE, '\'', '') = current_user()";
172 $grantInfo =
$connection->query($schemaPrivilegesQuery)->fetchAll(\PDO::FETCH_NUM);
173 if (empty(array_diff($requiredPrivileges, $this->parseGrantInfo($grantInfo)))) {
177 $errorMessage =
'Database user does not have enough privileges. Please make sure ' 178 . implode(
', ', $requiredPrivileges) .
" privileges are granted to table '{$dbName}'.";
179 throw new \Magento\Setup\Exception($errorMessage);
188 private function parseGrantInfo(array $grantInfo)
191 foreach ($grantInfo as $grantRow) {
const MYSQL_VERSION_REQUIRED