|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Gruberro\MongoDbMigrations\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Gruberro\MongoDbMigrations; |
|
6
|
|
|
use MongoDB; |
|
7
|
|
|
use Symfony\Component\Console; |
|
8
|
|
|
|
|
9
|
|
|
class MigrationsCommand extends AbstractCommand |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
7 |
|
protected function configure() |
|
15
|
|
|
{ |
|
16
|
|
|
$this |
|
17
|
7 |
|
->setName('php-mongodb-migrations:migrate') |
|
18
|
7 |
|
->setDescription('Execute all open migrations') |
|
19
|
7 |
|
->addOption( |
|
20
|
7 |
|
'server', |
|
21
|
7 |
|
's', |
|
22
|
7 |
|
Console\Input\InputOption::VALUE_REQUIRED, |
|
23
|
7 |
|
'The connection string (e.g. mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db)', |
|
24
|
7 |
|
'mongodb://localhost:27017' |
|
25
|
|
|
) |
|
26
|
7 |
|
->addOption( |
|
27
|
7 |
|
'contexts', |
|
28
|
7 |
|
'c', |
|
29
|
7 |
|
Console\Input\InputOption::VALUE_IS_ARRAY | Console\Input\InputOption::VALUE_REQUIRED, |
|
30
|
7 |
|
'A list of contexts evaluated with each migration of type ContextualMigrationInterface', |
|
31
|
7 |
|
[] |
|
32
|
|
|
) |
|
33
|
7 |
|
->addArgument( |
|
34
|
7 |
|
'database', |
|
35
|
7 |
|
Console\Input\InputArgument::REQUIRED, |
|
36
|
7 |
|
'The database to connect to' |
|
37
|
|
|
) |
|
38
|
7 |
|
->addArgument( |
|
39
|
7 |
|
'migration-directories', |
|
40
|
7 |
|
Console\Input\InputArgument::IS_ARRAY, |
|
41
|
7 |
|
'List of directories containing migration classes', |
|
42
|
7 |
|
[] |
|
43
|
|
|
) |
|
44
|
|
|
; |
|
45
|
7 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
8 |
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int |
|
51
|
|
|
{ |
|
52
|
8 |
|
$directories = $input->getArgument('migration-directories'); |
|
53
|
8 |
|
$migrations = $this->getMigrations($directories); |
|
54
|
6 |
|
$db = $this->connect($input->getOption('server'), $input->getArgument('database')); |
|
55
|
|
|
|
|
56
|
6 |
|
$this->acquireLock($db); |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
5 |
|
$databaseMigrationsCollection = $this->getMigrationsCollection($db); |
|
60
|
5 |
|
$progress = new Console\Helper\ProgressBar($output, count($migrations)); |
|
61
|
|
|
|
|
62
|
5 |
|
switch ($output->getVerbosity()) { |
|
63
|
5 |
|
case $output::VERBOSITY_VERBOSE: |
|
64
|
|
|
$format = 'verbose'; |
|
65
|
|
|
break; |
|
66
|
5 |
|
case $output::VERBOSITY_VERY_VERBOSE: |
|
67
|
|
|
$format = 'very_verbose'; |
|
68
|
|
|
break; |
|
69
|
5 |
|
case $output::VERBOSITY_DEBUG: |
|
70
|
|
|
$format = 'debug'; |
|
71
|
|
|
break; |
|
72
|
|
|
default: |
|
73
|
5 |
|
$format = 'normal'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
$progress->setFormat($format); |
|
77
|
5 |
|
$progress->start(); |
|
78
|
5 |
|
$executedMigrations = 0; |
|
79
|
|
|
|
|
80
|
5 |
|
foreach ($migrations as $id => $migration) { |
|
81
|
5 |
|
$progress->advance(); |
|
82
|
|
|
|
|
83
|
5 |
|
if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface && $input->getOption('contexts') !== []) { |
|
84
|
4 |
|
if ($migration->getContexts() === []) { |
|
85
|
1 |
|
throw new \InvalidArgumentException('An empty context specification is not allowed'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
if (array_intersect($migration->getContexts(), $input->getOption('contexts')) === []) { |
|
89
|
2 |
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
if (!$migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface) { |
|
94
|
5 |
|
if ($databaseMigrationsCollection->countDocuments(['migration_id' => $id]) > 0) { |
|
95
|
1 |
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
5 |
|
$migration->execute($db); |
|
100
|
5 |
|
$executedMigrations++; |
|
101
|
|
|
|
|
102
|
|
|
$migrationInfo = [ |
|
103
|
5 |
|
'migration_id' => $id, |
|
104
|
5 |
|
'migration_class' => get_class($migration), |
|
105
|
5 |
|
'last_execution_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000), |
|
106
|
5 |
|
'run_always' => $migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface, |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
5 |
|
if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface) { |
|
110
|
2 |
|
$migrationInfo['contexts'] = $migration->getContexts(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
5 |
|
$databaseMigrationsCollection->updateOne( |
|
114
|
5 |
|
['migration_id' => $id], |
|
115
|
5 |
|
['$set' => $migrationInfo], |
|
116
|
5 |
|
['upsert' => true] |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
$progress->finish(); |
|
121
|
3 |
|
$output->writeln(''); |
|
122
|
|
|
|
|
123
|
3 |
|
$output->writeln("<info>✓ Successfully executed {$executedMigrations} migrations</info>"); |
|
124
|
2 |
|
} catch (\Exception $e) { |
|
125
|
2 |
|
throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e); |
|
126
|
3 |
|
} finally { |
|
127
|
5 |
|
$this->releaseLock($db); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
3 |
|
return 0; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|