1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\API\Repository\Tests; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\DBAL\DBALException; |
13
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
14
|
|
|
use Doctrine\DBAL\Schema\Schema as DoctrineSchema; |
15
|
|
|
use EzSystems\DoctrineSchema\API\Exception\InvalidConfigurationException; |
16
|
|
|
use EzSystems\DoctrineSchema\Importer\SchemaImporter; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Legacy database Schema Importer for database integration tests. |
21
|
|
|
* |
22
|
|
|
* @uses \EzSystems\DoctrineSchema\Importer\SchemaImporter |
23
|
|
|
* |
24
|
|
|
* @internal For internal use by the Repository test cases. |
25
|
|
|
*/ |
26
|
|
|
final class LegacySchemaImporter |
27
|
|
|
{ |
28
|
|
|
/** @var \Doctrine\DBAL\Connection */ |
29
|
|
|
private $connection; |
30
|
|
|
|
31
|
|
|
public function __construct(Connection $connection) |
32
|
|
|
{ |
33
|
|
|
$this->connection = $connection; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Import database schema from Doctrine Schema Yaml configuration file. |
38
|
|
|
* |
39
|
|
|
* @param string $schemaFilePath Yaml schema configuration file path |
40
|
|
|
* |
41
|
|
|
* @throws \Doctrine\DBAL\ConnectionException |
42
|
|
|
*/ |
43
|
|
|
public function importSchema(string $schemaFilePath): void |
44
|
|
|
{ |
45
|
|
|
if (!file_exists($schemaFilePath)) { |
46
|
|
|
throw new RuntimeException("The schema file path {$schemaFilePath} does not exist"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->connection->beginTransaction(); |
50
|
|
|
$importer = new SchemaImporter(); |
51
|
|
|
try { |
52
|
|
|
$databasePlatform = $this->connection->getDatabasePlatform(); |
53
|
|
|
$schema = $importer->importFromFile($schemaFilePath); |
54
|
|
|
$statements = array_merge( |
55
|
|
|
$this->getDropSqlStatementsForExistingSchema( |
56
|
|
|
$schema, |
57
|
|
|
$databasePlatform, |
58
|
|
|
$this->connection |
59
|
|
|
), |
60
|
|
|
// generate schema DDL queries |
61
|
|
|
$schema->toSql($databasePlatform) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
foreach ($statements as $statement) { |
65
|
|
|
$this->connection->exec($statement); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->connection->commit(); |
69
|
|
|
} catch (InvalidConfigurationException | DBALException $e) { |
70
|
|
|
$this->connection->rollBack(); |
71
|
|
|
throw new RuntimeException($e->getMessage(), 1, $e); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
|
|
private function getDropSqlStatementsForExistingSchema( |
79
|
|
|
DoctrineSchema $newSchema, |
80
|
|
|
AbstractPlatform $databasePlatform, |
81
|
|
|
Connection $connection |
82
|
|
|
): array { |
83
|
|
|
$existingSchema = $connection->getSchemaManager()->createSchema(); |
84
|
|
|
$statements = []; |
85
|
|
|
// reverse table order for clean-up (due to FKs) |
86
|
|
|
$tables = array_reverse($newSchema->getTables()); |
87
|
|
|
// cleanup pre-existing database |
88
|
|
|
foreach ($tables as $table) { |
89
|
|
|
if ($existingSchema->hasTable($table->getName())) { |
90
|
|
|
$statements[] = $databasePlatform->getDropTableSQL($table); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $statements; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|