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 EzSystems\PlatformInstallerBundle\Installer; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\DBAL\DBALException; |
13
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
14
|
|
|
use Doctrine\DBAL\Schema\Schema; |
15
|
|
|
use EzSystems\DoctrineSchema\API\Builder\SchemaBuilder; |
16
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* New variant of CleanInstaller, which uses SchemaBuilder. |
20
|
|
|
*/ |
21
|
|
|
class CoreInstaller extends DbBasedInstaller implements Installer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \EzSystems\DoctrineSchema\API\Builder\SchemaBuilder |
25
|
|
|
*/ |
26
|
|
|
protected $schemaBuilder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Doctrine\DBAL\Connection $db |
30
|
|
|
* @param \EzSystems\DoctrineSchema\API\Builder\SchemaBuilder $schemaBuilder |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Connection $db, SchemaBuilder $schemaBuilder) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($db); |
35
|
|
|
|
36
|
|
|
$this->schemaBuilder = $schemaBuilder; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Import Schema using event-driven Schema Builder API from eZ Systems DoctrineSchema Bundle. |
41
|
|
|
* |
42
|
|
|
* If you wish to extend schema, implement your own EventSubscriber |
43
|
|
|
* |
44
|
|
|
* @see \EzSystems\DoctrineSchema\API\Event\SchemaBuilderEvent |
45
|
|
|
* @see \EzSystems\PlatformInstallerBundle\Event\Subscriber\BuildSchemaSubscriber |
46
|
|
|
* |
47
|
|
|
* @throws \Doctrine\DBAL\DBALException |
48
|
|
|
*/ |
49
|
|
|
public function importSchema() |
50
|
|
|
{ |
51
|
|
|
// note: schema is built using Schema Builder event-driven API |
52
|
|
|
$schema = $this->schemaBuilder->buildSchema(); |
53
|
|
|
$databasePlatform = $this->db->getDatabasePlatform(); |
54
|
|
|
$queries = array_merge( |
55
|
|
|
$this->getDropSqlStatementsForExistingSchema($schema, $databasePlatform), |
56
|
|
|
// generate schema DDL queries |
57
|
|
|
$schema->toSql($databasePlatform) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$queriesCount = count($queries); |
61
|
|
|
$this->output->writeln( |
62
|
|
|
sprintf( |
63
|
|
|
'<info>Executing %d queries on database <comment>%s</comment> (<comment>%s</comment>)</info>', |
64
|
|
|
$queriesCount, |
65
|
|
|
$this->db->getDatabase(), |
66
|
|
|
$databasePlatform->getName() |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
$progressBar = new ProgressBar($this->output); |
70
|
|
|
$progressBar->start($queriesCount); |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$this->db->beginTransaction(); |
74
|
|
|
foreach ($queries as $query) { |
75
|
|
|
$this->db->exec($query); |
76
|
|
|
$progressBar->advance(1); |
77
|
|
|
} |
78
|
|
|
$this->db->commit(); |
79
|
|
|
} catch (DBALException $e) { |
80
|
|
|
$this->db->rollBack(); |
81
|
|
|
throw $e; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$progressBar->finish(); |
85
|
|
|
// go to the next line after ProgressBar::finish |
86
|
|
|
$this->output->writeln(''); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws \Doctrine\DBAL\DBALException |
91
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
92
|
|
|
*/ |
93
|
|
|
public function importData() |
94
|
|
|
{ |
95
|
|
|
$this->runQueriesFromFile($this->getKernelSQLFileForDBMS('cleandata.sql')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $newSchema |
100
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $databasePlatform |
101
|
|
|
* |
102
|
|
|
* @return string[] |
103
|
|
|
*/ |
104
|
|
|
protected function getDropSqlStatementsForExistingSchema( |
105
|
|
|
Schema $newSchema, |
106
|
|
|
AbstractPlatform $databasePlatform |
107
|
|
|
): array { |
108
|
|
|
$existingSchema = $this->db->getSchemaManager()->createSchema(); |
109
|
|
|
$statements = []; |
110
|
|
|
// reverse table order for clean-up (due to FKs) |
111
|
|
|
$tables = array_reverse($newSchema->getTables()); |
112
|
|
|
// cleanup pre-existing database |
113
|
|
|
foreach ($tables as $table) { |
114
|
|
|
if ($existingSchema->hasTable($table->getName())) { |
115
|
|
|
$statements[] = $databasePlatform->getDropTableSQL($table); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $statements; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Handle optional import of binary files to var folder. |
124
|
|
|
*/ |
125
|
|
|
public function importBinaries() |
126
|
|
|
{ |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function createConfiguration() |
133
|
|
|
{ |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|