-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
20100101000200_MigrateV1ToV2.php
165 lines (137 loc) · 6.03 KB
/
20100101000200_MigrateV1ToV2.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
<?php
use Kaliop\eZMigrationBundle\API\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Kaliop\eZMigrationBundle\API\Value\Migration;
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
use \eZ\Publish\Core\Persistence\Database\SelectQuery;
class MigrateV1ToV2 implements MigrationInterface
{
private $container;
private $dbHandler;
private $activeBundles;
private $legacyTableName;
private $legacyMigrationsDir;
// The API says we have to have a static method, but we like better non-static... :-P
public static function execute(ContainerInterface $container)
{
$migration = new self($container);
$migration->goForIt();
}
private function __construct(ContainerInterface $container)
{
$this->container = $container;
}
private function goForIt()
{
$this->legacyTableName = $this->container->getParameter('kaliop_bundle_migration.table_name');
$this->legacyMigrationsDir = $this->container->get('ez_migration_bundle.helper.config.resolver')->getParameter('kaliop_bundle_migration.version_directory');
$migrationStorageService = $this->container->get('ez_migration_bundle.storage_handler');
$this->dbHandler = $this->container->get('ezpublish.connection');
$this->activeBundles = array();
foreach ($this->container->get('kernel')->getBundles() as $bundle)
{
$this->activeBundles[$bundle->getName()] = $bundle->getPath();
}
/** @var \Kaliop\eZMigrationBundle\Core\Helper\ConsoleIO $io */
$io = $this->container->get('ez_migration_bundle.helper.console_io');
// NB: in theory this could be null!
$output = $io->getOutput();
if (!$this->tableExist($this->legacyTableName))
{
$output->writeln("Nothing to update: v1 database table '{$this->legacyTableName}' not found");
return;
}
$toMigrate = $this->loadLegacyMigrations();
$output->writeln("<info>Found " . count($toMigrate) . ' migration versions in the v1 database table</info>');
// we need to decide of a random time to stamp existing migrations. We use 'now - 1 minute'...
$executionDate = time() - 60;
foreach ($toMigrate as $legacyMigration) {
$name = $legacyMigration['version'];
$path = $this->getLegacyMigrationDefinition($legacyMigration['version'], $legacyMigration['bundle']);
if ($path != false) {
$name = basename($path);
$content = file_get_contents($path);
$md5 = md5($content);
} else {
$path = 'unknown';
$content = '';
$md5 = 'unknown';
}
// take care: what if the migration already exists in the v2 table ???
$existingMigration = $migrationStorageService->loadMigration($name);
if ($existingMigration != null) {
$output->writeln("<info>Info for migration version: {$legacyMigration['bundle']} {$legacyMigration['version']} was already migrated, skipping it</info>");
continue;
}
$migrationDefinition = new MigrationDefinition(
$name, $path, $content, MigrationDefinition::STATUS_PARSED
);
$migrationStorageService->startMigration($migrationDefinition);
$migration = new Migration(
$name,
$md5,
$path,
$executionDate,
Migration::STATUS_DONE
);
$migrationStorageService->endMigration($migration);
// we leave legacy info in the legacy table, in case someone wants to roll back in the future...
//$this->deleteLegacyMigration($legacyMigration['version'], $legacyMigration['bundle']);
$output->writeln("Updated info for migration version: {$legacyMigration['bundle']} {$legacyMigration['version']}");
}
$output->writeln("<info>All known legacy migration versions have been migrated to the v2 database table</info>");
}
private function tableExist($tableName)
{
/** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $sm */
$sm = $this->dbHandler->getConnection()->getSchemaManager();
foreach ($sm->listTables() as $table) {
if ($table->getName() == $tableName) {
return true;
}
}
return false;
}
private function loadLegacyMigrations()
{
/** @var \eZ\Publish\Core\Persistence\Database\SelectQuery $q */
$q = $this->dbHandler->createSelectQuery();
$q->select('version, bundle')
->from($this->legacyTableName)
->orderBy('version', SelectQuery::ASC);
$stmt = $q->prepare();
$stmt->execute();
$results = $stmt->fetchAll();
return $results;
}
private function deleteLegacyMigration($version, $bundle)
{
$this->dbHandler->getConnection()->delete($this->legacyTableName, array('version' => $version, 'bundle' => $bundle));
}
/**
* Attempts to find the migration definition file. If more than one matches, the 1st found is returned
* @param string $version
* @param string $bundle
* @return string|false
*/
private function getLegacyMigrationDefinition($version, $bundle)
{
if (!isset($this->activeBundles[$bundle])) {
return false;
}
$versionsDir = $this->activeBundles[$bundle] . '/' . $this->legacyMigrationsDir;
$versionDefinitions = glob($versionsDir . "/$version*");
if (!is_array($versionDefinitions)) {
return false;
}
foreach ($versionDefinitions as $key => $versionDefinition) {
if (!in_array(pathinfo($versionDefinition, PATHINFO_EXTENSION), array('php', 'yml', 'sql'))) {
unset($versionDefinitions[$key]);
}
}
if (empty($versionDefinitions)) {
return false;
}
return $versionDefinitions[0];
}
}