-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathManager.php
187 lines (164 loc) · 5.94 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Claroline\MigrationBundle\Manager;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LogLevel;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Claroline\MigrationBundle\Generator\Generator;
use Claroline\MigrationBundle\Generator\Writer;
use Claroline\MigrationBundle\Migrator\Migrator;
/**
* API entry point.
*/
class Manager
{
use LoggerAwareTrait;
private $generator;
private $writer;
private $migrator;
/**
* Constructor.
*
* @param \Claroline\MigrationBundle\Generator\Generator $generator
* @param \Claroline\MigrationBundle\Generator\Writer $writer
* @param \Claroline\MigrationBundle\Migrator\Migrator $migrator
*/
public function __construct(
Generator $generator,
Writer $writer,
Migrator $migrator
)
{
$this->generator = $generator;
$this->migrator = $migrator;
$this->writer = $writer;
}
/**
* Generates bundle migrations classes for all the available driver platforms.
*
* @param \Symfony\Component\HttpKernel\Bundle\Bundle $bundle
* @param \Symfony\Component\HttpKernel\Bundle\Bundle $output
*/
public function generateBundleMigration(Bundle $bundle, $output = null)
{
$platforms = $this->getAvailablePlatforms();
$version = date('YmdHis');
$this->log("Generating migrations classes for '{$bundle->getName()}'...");
if (!$output) $output = $bundle;
foreach ($platforms as $driverName => $platform) {
$queries = $this->generator->generateMigrationQueries($bundle, $platform);
if (count($queries[Generator::QUERIES_UP]) > 0 || count($queries[Generator::QUERIES_DOWN]) > 0) {
$this->log(" - Generating migration class for {$driverName} driver...");
$this->writer->writeMigrationClass($output, $driverName, $version, $queries);
} else {
$this->log('Nothing to generate: database and mapping are synced');
break;
}
}
}
/**
* Returns information about the migration status of a bundle. The return
* value is the same than Migrator::getMigrationStatus().
*
* @param \Symfony\Component\HttpKernel\Bundle\Bundle $bundle
*
* @return array
*/
public function getBundleStatus(Bundle $bundle)
{
return $this->migrator->getMigrationStatus($bundle);
}
/**
* Upgrades a bundle to a specified version. The version can be either an
* explicit version string or a Migrator::VERSION_* constant.
*
* @param \Symfony\Component\HttpKernel\Bundle\Bundle $bundle
* @param string $version
*/
public function upgradeBundle(Bundle $bundle, $version)
{
$this->doMigrate($bundle, $version, Migrator::DIRECTION_UP);
}
/**
* Upgrades a bundle to a specified version. The version can be either an
* explicit version string or a Migrator::VERSION_* constant.
*
* @param \Symfony\Component\HttpKernel\Bundle\Bundle $bundle
* @param string $version
*/
public function downgradeBundle(Bundle $bundle, $version)
{
$this->doMigrate($bundle, $version, Migrator::DIRECTION_DOWN);
}
/**
* Deletes migration classes which are above the current version of a bundle.
*
* @param \Symfony\Component\HttpKernel\Bundle\Bundle $bundle
*/
public function discardUpperMigrations(Bundle $bundle)
{
$drivers = array_keys($this->getAvailablePlatforms());
$currentVersion = $this->migrator->getCurrentVersion($bundle);
$this->log("Deleting migration classes above version {$currentVersion} for '{$bundle->getName()}'...");
$hasDeleted = false;
foreach ($drivers as $driver) {
$deletedVersions = $this->writer->deleteUpperMigrationClasses($bundle, $driver, $currentVersion);
if (count($deletedVersions) > 0) {
$hasDeleted = true;
foreach ($deletedVersions as $version) {
$this->log(" - Deleted {$version} for driver {$driver}");
}
}
}
if (!$hasDeleted) {
$this->log('Nothing to discard: there are no migrations classes above the current version');
}
}
/**
* Returns the list of available driver platforms.
*
* Note: this method is public for testing purposes only
*
* @return array[AbstractPlatform]
*/
public function getAvailablePlatforms()
{
$platforms = array();
foreach ($this->getSupportedDrivers() as $driverName => $driverClass) {
$driver = new $driverClass;
$platforms[$driverName] = $driver->getDatabasePlatform();
}
return $platforms;
}
private function getSupportedDrivers()
{
return array(
'pdo_mysql' => 'Doctrine\DBAL\Driver\PDOMySql\Driver'
);
}
private function doMigrate(Bundle $bundle, $version, $direction)
{
$action = $direction === Migrator::DIRECTION_UP ? 'Ugprading' : 'Downgrading';
$this->log("{$action} bundle '{$bundle->getName()}'...");
$queries = $this->migrator->migrate($bundle, $version, $direction);
$currentVersion = $this->migrator->getCurrentVersion($bundle);
$this->log(
count($queries) === 0 ?
"Nothing to execute: bundle is already at version {$currentVersion}" :
"Done: bundle is now at version {$currentVersion}"
);
}
private function log($message)
{
if ($this->logger) {
$this->logger->log(LogLevel::INFO, $message);
}
}
}