Skip to content

Commit 517a630

Browse files
author
Alexander Paliarush
committed
MAGETWO-45069: Can't change Database Name after fail during installation process Unknown database 'magento2'
- Added DB name validation
1 parent 22629ab commit 517a630

File tree

2 files changed

+86
-24
lines changed

2 files changed

+86
-24
lines changed

setup/src/Magento/Setup/Test/Unit/Validator/DbValidatorTest.php

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,35 @@ public function testCheckDatabaseConnection()
4747
->expects($this->atLeastOnce())
4848
->method('query')
4949
->willReturn($pdo);
50-
$pdo->expects($this->once())
50+
51+
$listOfPrivileges = [
52+
['SELECT'],
53+
['INSERT'],
54+
['UPDATE'],
55+
['DELETE'],
56+
['CREATE'],
57+
['DROP'],
58+
['REFERENCES'],
59+
['INDEX'],
60+
['ALTER'],
61+
['CREATE TEMPORARY TABLES'],
62+
['LOCK TABLES'],
63+
['EXECUTE'],
64+
['CREATE VIEW'],
65+
['SHOW VIEW'],
66+
['CREATE ROUTINE'],
67+
['ALTER ROUTINE'],
68+
['EVENT'],
69+
['TRIGGER'],
70+
];
71+
$accessibleDbs = ['some_db', 'name', 'another_db'];
72+
73+
$pdo->expects($this->atLeastOnce())
5174
->method('fetchAll')
52-
->willReturn(
75+
->willReturnMap(
5376
[
54-
['SELECT'],
55-
['INSERT'],
56-
['UPDATE'],
57-
['DELETE'],
58-
['CREATE'],
59-
['DROP'],
60-
['REFERENCES'],
61-
['INDEX'],
62-
['ALTER'],
63-
['CREATE TEMPORARY TABLES'],
64-
['LOCK TABLES'],
65-
['EXECUTE'],
66-
['CREATE VIEW'],
67-
['SHOW VIEW'],
68-
['CREATE ROUTINE'],
69-
['ALTER ROUTINE'],
70-
['EVENT'],
71-
['TRIGGER'],
77+
[\PDO::FETCH_COLUMN, 0, $accessibleDbs],
78+
[\PDO::FETCH_NUM, null, $listOfPrivileges]
7279
]
7380
);
7481
$this->assertEquals(true, $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password'));
@@ -90,7 +97,41 @@ public function testCheckDatabaseConnectionNotEnoughPrivileges()
9097
->expects($this->atLeastOnce())
9198
->method('query')
9299
->willReturn($pdo);
93-
$pdo->expects($this->atLeastOnce())->method('fetchAll')->willReturn([['SELECT']]);
100+
$listOfPrivileges = [['SELECT']];
101+
$accessibleDbs = ['some_db', 'name', 'another_db'];
102+
103+
$pdo->expects($this->atLeastOnce())
104+
->method('fetchAll')
105+
->willReturnMap(
106+
[
107+
[\PDO::FETCH_COLUMN, 0, $accessibleDbs],
108+
[\PDO::FETCH_NUM, null, $listOfPrivileges]
109+
]
110+
);
111+
$this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password');
112+
}
113+
114+
/**
115+
* @expectedException \Magento\Setup\Exception
116+
* @expectedExceptionMessage Database 'name' does not exist or specified database server user does not have
117+
*/
118+
public function testCheckDatabaseConnectionDbNotAccessible()
119+
{
120+
$this->connection
121+
->expects($this->once())
122+
->method('fetchOne')
123+
->with('SELECT version()')
124+
->willReturn('5.6.0-0ubuntu0.12.04.1');
125+
$pdo = $this->getMockForAbstractClass('Zend_Db_Statement_Interface', [], '', false);
126+
$this->connection
127+
->expects($this->atLeastOnce())
128+
->method('query')
129+
->willReturn($pdo);
130+
$accessibleDbs = ['some_db', 'another_db'];
131+
132+
$pdo->expects($this->atLeastOnce())
133+
->method('fetchAll')
134+
->willReturn($accessibleDbs);
94135
$this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password');
95136
}
96137

setup/src/Magento/Setup/Validator/DbValidator.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DbValidator
3030

3131
/**
3232
* Constructor
33-
*
33+
*
3434
* @param ConnectionFactory $connectionFactory
3535
*/
3636
public function __construct(ConnectionFactory $connectionFactory)
@@ -103,7 +103,28 @@ public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '')
103103
}
104104
}
105105

106-
return $this->checkDatabasePrivileges($connection, $dbName);
106+
return $this->checkDatabaseName($connection, $dbName) && $this->checkDatabasePrivileges($connection, $dbName);
107+
}
108+
109+
/**
110+
* Checks if specified database exists and visible to current user
111+
*
112+
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
113+
* @param string $dbName
114+
* @return bool
115+
* @throws \Magento\Setup\Exception
116+
*/
117+
private function checkDatabaseName(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $dbName)
118+
{
119+
$query = "SHOW DATABASES";
120+
$accessibleDbs = $connection->query($query)->fetchAll(\PDO::FETCH_COLUMN, 0);
121+
foreach ($accessibleDbs as $accessibleDbName) {
122+
if ($dbName == $accessibleDbName) {
123+
return true;
124+
}
125+
}
126+
throw new \Magento\Setup\Exception("Database '{$dbName}' does not exist "
127+
."or specified database server user does not have privileges to access this database.");
107128
}
108129

109130
/**
@@ -154,7 +175,7 @@ private function checkDatabasePrivileges(\Magento\Framework\DB\Adapter\AdapterIn
154175
}
155176

156177
$errorMessage = 'Database user does not have enough privileges. Please make sure '
157-
. implode(', ', $requiredPrivileges) . " privileges are granted to table '$dbName'.";
178+
. implode(', ', $requiredPrivileges) . " privileges are granted to table '{$dbName}'.";
158179
throw new \Magento\Setup\Exception($errorMessage);
159180
}
160181

0 commit comments

Comments
 (0)