Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SetupDb | |
0.00% |
0 / 18 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| setupDatabase | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| verifyDatabase | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| dbExists | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| cleanDb | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013-2019 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * @author Lionel Lecaque <lionel@taotesting.com> |
| 21 | * @license GPLv2 |
| 22 | * @package tao |
| 23 | */ |
| 24 | |
| 25 | namespace oat\generis\persistence\sql; |
| 26 | |
| 27 | use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 28 | use oat\oatbox\log\LoggerAwareTrait; |
| 29 | use Psr\Log\LoggerAwareInterface; |
| 30 | use Doctrine\DBAL\Exception\ConnectionException; |
| 31 | |
| 32 | class SetupDb implements LoggerAwareInterface |
| 33 | { |
| 34 | use LoggerAwareTrait; |
| 35 | |
| 36 | /** |
| 37 | * Setup the tables for the database |
| 38 | * @param \common_persistence_SqlPersistence $p |
| 39 | * @throws \common_exception_InconsistentData |
| 40 | */ |
| 41 | public function setupDatabase(\common_persistence_SqlPersistence $p) |
| 42 | { |
| 43 | $dbalDriver = $p->getDriver()->getDbalConnection(); |
| 44 | $dbName = $dbalDriver->getDataBase(); |
| 45 | $this->verifyDatabase($p, $dbName); |
| 46 | $this->cleanDb($p); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
| 51 | */ |
| 52 | private function verifyDatabase(\common_persistence_SqlPersistence $p, $dbName) |
| 53 | { |
| 54 | $schemaManager = $p->getSchemaManager()->getDbalSchemaManager(); |
| 55 | if (!$this->dbExists($schemaManager, $dbName)) { |
| 56 | throw new \tao_install_utils_Exception( |
| 57 | 'Unable to find the database, make sure that the db exists and that the db user has the rights ' |
| 58 | . 'to use it.' |
| 59 | ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
| 65 | * @param string $dbName |
| 66 | */ |
| 67 | private function dbExists(AbstractSchemaManager $schemaManager, $dbName) |
| 68 | { |
| 69 | try { |
| 70 | return in_array($dbName, $schemaManager->listDatabases()); |
| 71 | } catch (ConnectionException $e) { |
| 72 | $this->logWarning('Unable to connect to validate dbExists'); |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
| 79 | */ |
| 80 | private function cleanDb(\common_persistence_SqlPersistence $p) |
| 81 | { |
| 82 | $schema = $p->getSchemaManager()->createSchema(); |
| 83 | $queries = $p->getPlatForm()->toDropSql($schema); |
| 84 | foreach ($queries as $query) { |
| 85 | $p->exec($query); |
| 86 | } |
| 87 | } |
| 88 | } |