|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Gruberro\MongoDbMigrations\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Gruberro\MongoDbMigrations; |
|
6
|
|
|
use MongoDB; |
|
7
|
|
|
use MongoDB\Database; |
|
8
|
|
|
use Symfony\Component\Console; |
|
9
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractCommand extends Console\Command\Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var OutputInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $output; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
14 |
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
24
|
|
|
{ |
|
25
|
14 |
|
$this->output = $output; |
|
26
|
14 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string[] $directories |
|
30
|
|
|
* @return MongoDbMigrations\MigrationInterface[] |
|
31
|
|
|
*/ |
|
32
|
14 |
|
protected function getMigrations(array $directories): array |
|
33
|
|
|
{ |
|
34
|
14 |
|
foreach ($directories as $directory) { |
|
35
|
14 |
|
if (! is_dir($directory)) { |
|
36
|
2 |
|
throw new Console\Exception\InvalidArgumentException("'{$directory}' is no valid directory"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
14 |
|
$this->output->writeln("<comment>Iterating '{$directory}' for potential migrations classes</comment>", OutputInterface::VERBOSITY_DEBUG); |
|
40
|
14 |
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::LEAVES_ONLY); |
|
41
|
|
|
|
|
42
|
|
|
/** @var \SplFileInfo $file */ |
|
43
|
14 |
|
foreach ($iterator as $file) { |
|
44
|
14 |
|
if ($file->getBasename('.php') === $file->getBasename()) { |
|
45
|
14 |
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
14 |
|
require_once $file->getRealPath(); |
|
49
|
|
|
|
|
50
|
14 |
|
$this->output->writeln("<comment>Loaded potential migration '{$file->getRealPath()}'</comment>", OutputInterface::VERBOSITY_DEBUG); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @var MongoDbMigrations\MigrationInterface[] $migrations */ |
|
55
|
12 |
|
$migrations = []; |
|
56
|
12 |
|
foreach (get_declared_classes() as $className) { |
|
57
|
12 |
|
$reflectionClass = new \ReflectionClass($className); |
|
58
|
|
|
|
|
59
|
12 |
|
if ($reflectionClass->implementsInterface(MongoDbMigrations\MigrationInterface::class)) { |
|
60
|
|
|
/** @var MongoDbMigrations\MigrationInterface $newInstance */ |
|
61
|
12 |
|
$newInstance = $reflectionClass->newInstance(); |
|
62
|
12 |
|
$id = md5($newInstance->getId()); |
|
63
|
|
|
|
|
64
|
12 |
|
if (isset($migrations[$id])) { |
|
65
|
1 |
|
$existingMigrationClass = get_class($migrations[$id]); |
|
66
|
1 |
|
throw new RuntimeException("Found a non unique migration id '{$newInstance->getId()}' in '{$reflectionClass->getName()}', already defined by migration class '{$existingMigrationClass}'"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
12 |
|
$migrations[$id] = $newInstance; |
|
70
|
|
|
|
|
71
|
12 |
|
$this->output->writeln("<comment>Found valid migration class '{$reflectionClass->getName()}'</comment>", OutputInterface::VERBOSITY_DEBUG); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
11 |
|
$migrationClassesCount = count($migrations); |
|
76
|
11 |
|
$this->output->writeln("<info>✓ Found {$migrationClassesCount} valid migration classes</info>", OutputInterface::VERBOSITY_VERBOSE); |
|
77
|
|
|
|
|
78
|
11 |
|
uasort($migrations, function (MongoDbMigrations\MigrationInterface $a, MongoDbMigrations\MigrationInterface $b) { |
|
79
|
11 |
|
return $a->getCreateDate() <=> $b->getCreateDate(); |
|
80
|
11 |
|
}); |
|
81
|
|
|
|
|
82
|
11 |
|
$this->output->writeln("<info>✓ Reordered all migration classes according to their create date</info>", OutputInterface::VERBOSITY_VERBOSE); |
|
83
|
|
|
|
|
84
|
11 |
|
return $migrations; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $server |
|
89
|
|
|
* @param string $database |
|
90
|
|
|
* @return Database |
|
91
|
|
|
*/ |
|
92
|
11 |
|
protected function connect(string $server, string $database): Database |
|
93
|
|
|
{ |
|
94
|
11 |
|
$client = new MongoDB\Client($server); |
|
95
|
11 |
|
$db = $client->selectDatabase($database); |
|
96
|
11 |
|
$this->output->writeln("<info>✓ Successfully established database connection</info>", OutputInterface::VERBOSITY_VERBOSE); |
|
97
|
|
|
|
|
98
|
11 |
|
return $db; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Database $db |
|
103
|
|
|
* |
|
104
|
|
|
* @throws RuntimeException If there is already a running command |
|
105
|
|
|
*/ |
|
106
|
11 |
|
protected function acquireLock(Database $db) |
|
107
|
|
|
{ |
|
108
|
11 |
|
$databaseMigrationsLockCollection = $db->selectCollection('DATABASE_MIGRATIONS_LOCK'); |
|
109
|
11 |
|
$databaseMigrationsLockCollection->createIndex(['locked' => 1]); |
|
110
|
|
|
|
|
111
|
11 |
|
$currentLock = $databaseMigrationsLockCollection->findOneAndReplace(['locked' => ['$exists' => true]], ['locked' => true, 'last_locked_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000)], ['upsert' => true]); |
|
112
|
11 |
|
if ($currentLock !== null && $currentLock->locked === true) { |
|
113
|
2 |
|
throw new RuntimeException('Concurrent migrations are not allowed'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
9 |
|
$this->output->writeln("<info>✓ Successfully acquired migration lock</info>", OutputInterface::VERBOSITY_VERBOSE); |
|
117
|
9 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param Database $db |
|
121
|
|
|
*/ |
|
122
|
9 |
|
protected function releaseLock(Database $db) |
|
123
|
|
|
{ |
|
124
|
9 |
|
$databaseMigrationsLockCollection = $db->selectCollection('DATABASE_MIGRATIONS_LOCK'); |
|
125
|
9 |
|
$databaseMigrationsLockCollection->updateOne(['locked' => true], ['$set' => ['locked' => false]]); |
|
126
|
9 |
|
$this->output->writeln("<info>✓ Successfully released migration lock</info>", OutputInterface::VERBOSITY_VERBOSE); |
|
127
|
9 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param Database $db |
|
131
|
|
|
* @return MongoDB\Collection |
|
132
|
|
|
*/ |
|
133
|
9 |
|
protected function getMigrationsCollection(Database $db): MongoDB\Collection |
|
134
|
|
|
{ |
|
135
|
9 |
|
$databaseMigrationsCollection = $db->selectCollection('DATABASE_MIGRATIONS'); |
|
136
|
9 |
|
$databaseMigrationsCollection->createIndex(['migration_id' => 1], ['unique' => true, 'background' => false]); |
|
137
|
|
|
|
|
138
|
9 |
|
return $databaseMigrationsCollection; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|