|
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\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
|
|
12
|
|
|
class VersionsCommand extends AbstractCommand |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
4 |
|
protected function configure() |
|
18
|
|
|
{ |
|
19
|
|
|
$this |
|
20
|
4 |
|
->setName('php-mongodb-migrations:version') |
|
21
|
4 |
|
->setDescription('Manually add and delete migrations from the version collection.') |
|
22
|
4 |
|
->addOption( |
|
23
|
4 |
|
'server', |
|
24
|
4 |
|
's', |
|
25
|
4 |
|
InputOption::VALUE_REQUIRED, |
|
26
|
4 |
|
'The connection string (e.g. mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db)', |
|
27
|
4 |
|
'mongodb://localhost:27017' |
|
28
|
|
|
) |
|
29
|
4 |
|
->addArgument( |
|
30
|
4 |
|
'database', |
|
31
|
4 |
|
InputArgument::REQUIRED, |
|
32
|
4 |
|
'The database to connect to' |
|
33
|
|
|
) |
|
34
|
4 |
|
->addArgument( |
|
35
|
4 |
|
'migration-directories', |
|
36
|
4 |
|
InputArgument::IS_ARRAY, |
|
37
|
4 |
|
'List of directories containing migration classes', |
|
38
|
4 |
|
[] |
|
39
|
|
|
) |
|
40
|
4 |
|
->addOption( |
|
41
|
4 |
|
'id', |
|
42
|
4 |
|
null, |
|
43
|
4 |
|
InputOption::VALUE_REQUIRED, |
|
44
|
4 |
|
'The migration id to add or delete' |
|
45
|
|
|
) |
|
46
|
4 |
|
->addOption( |
|
47
|
4 |
|
'add', |
|
48
|
4 |
|
null, |
|
49
|
4 |
|
InputOption::VALUE_NONE, |
|
50
|
4 |
|
'Add the specified migration id' |
|
51
|
|
|
) |
|
52
|
4 |
|
->addOption( |
|
53
|
4 |
|
'delete', |
|
54
|
4 |
|
null, |
|
55
|
4 |
|
InputOption::VALUE_NONE, |
|
56
|
4 |
|
'Delete the specified migration id' |
|
57
|
|
|
) |
|
58
|
4 |
|
->addOption( |
|
59
|
4 |
|
'all', |
|
60
|
4 |
|
null, |
|
61
|
4 |
|
InputOption::VALUE_NONE, |
|
62
|
4 |
|
'Apply to all the migrations' |
|
63
|
|
|
) |
|
64
|
4 |
|
->setHelp(<<<EOT |
|
65
|
4 |
|
The <info>%command.name%</info> command allows you to manually add, delete or synchronize migrations from the version collection: |
|
66
|
|
|
|
|
67
|
|
|
<info>%command.full_name% DATABASE_NAME MIGRATION_ID /path/to/migrations --add</info> |
|
68
|
|
|
|
|
69
|
|
|
If you want to delete a version you can use the <comment>--delete</comment> option: |
|
70
|
|
|
|
|
71
|
|
|
<info>%command.full_name% DATABASE_NAME MIGRATION_ID /path/to/migrations --delete</info> |
|
72
|
|
|
|
|
73
|
|
|
If you want to synchronize by adding or deleting all migrations available in the version collection you can use the <comment>--all</comment> option: |
|
74
|
|
|
|
|
75
|
|
|
<info>%command.full_name% DATABASE_NAME /path/to/migrations --add --all</info> |
|
76
|
|
|
<info>%command.full_name% DATABASE_NAME /path/to/migrations --delete --all</info> |
|
77
|
|
|
EOT |
|
78
|
|
|
); |
|
79
|
|
|
; |
|
80
|
4 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
6 |
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
|
86
|
|
|
{ |
|
87
|
6 |
|
$id = $input->getOption('id'); |
|
88
|
6 |
|
$directories = $input->getArgument('migration-directories'); |
|
89
|
6 |
|
$migrations = $this->getMigrations($directories); |
|
90
|
5 |
|
$db = $this->connect($input->getOption('server'), $input->getArgument('database')); |
|
91
|
|
|
|
|
92
|
5 |
|
$this->acquireLock($db); |
|
93
|
|
|
|
|
94
|
4 |
|
$add = $input->getOption('add'); |
|
95
|
4 |
|
$delete = $input->getOption('delete'); |
|
96
|
4 |
|
$all = $input->getOption('all'); |
|
97
|
|
|
|
|
98
|
|
|
// Assume add is the default behavior |
|
99
|
4 |
|
if ($add === false && $delete === false) { |
|
100
|
3 |
|
$add = true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
if ($add === true && $delete === true) { |
|
104
|
|
|
throw new Console\Exception\RuntimeException("--add and --delete is not allowed to be set both"); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
if (!$all && $id === null) { |
|
108
|
|
|
throw new Console\Exception\RuntimeException("Specify --all or a single migration id"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
4 |
|
if ($all) { |
|
113
|
3 |
|
$id = null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
4 |
|
if ($id !== null) { |
|
117
|
1 |
|
$migrations = array_filter($migrations, function (MongoDbMigrations\MigrationInterface $migration) use ($id): bool { |
|
118
|
1 |
|
return $migration->getId() === $id; |
|
119
|
1 |
|
}); |
|
120
|
|
|
|
|
121
|
1 |
|
if (count($migrations) === 0) { |
|
122
|
|
|
throw new Console\Exception\RuntimeException("No migration for id '{$id}' found"); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
if ($add) { |
|
127
|
3 |
|
$this->addMigrations($migrations, $db); |
|
128
|
2 |
|
} elseif ($delete) { |
|
129
|
4 |
|
$this->deleteMigrations($migrations, $db); |
|
130
|
|
|
} |
|
131
|
|
|
} catch (\Exception $e) { |
|
132
|
|
|
throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e); |
|
133
|
4 |
|
} finally { |
|
134
|
4 |
|
$this->releaseLock($db); |
|
135
|
|
|
} |
|
136
|
4 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param array $migrations |
|
140
|
|
|
* @param Database $db |
|
141
|
|
|
*/ |
|
142
|
3 |
|
private function addMigrations(array $migrations, Database $db) |
|
143
|
|
|
{ |
|
144
|
3 |
|
$databaseMigrationsCollection = $this->getMigrationsCollection($db); |
|
145
|
|
|
|
|
146
|
3 |
|
foreach ($migrations as $id => $migration) { |
|
147
|
|
|
$migrationInfo = [ |
|
148
|
3 |
|
'migration_id' => $id, |
|
149
|
3 |
|
'migration_class' => get_class($migration), |
|
150
|
3 |
|
'last_execution_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000), |
|
151
|
3 |
|
'run_always' => $migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface, |
|
152
|
|
|
]; |
|
153
|
|
|
|
|
154
|
3 |
|
if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface) { |
|
155
|
2 |
|
$migrationInfo['contexts'] = $migration->getContexts(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
$databaseMigrationsCollection->updateOne( |
|
159
|
3 |
|
['migration_id' => $id], |
|
160
|
3 |
|
['$set' => $migrationInfo], |
|
161
|
3 |
|
['upsert' => true] |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
3 |
|
$addedMigrations = count($migrations); |
|
166
|
3 |
|
$this->output->writeln("<info>✓ Successfully added {$addedMigrations} migrations to version collection</info>"); |
|
167
|
3 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param array $migrations |
|
171
|
|
|
* @param Database $db |
|
172
|
|
|
*/ |
|
173
|
2 |
|
private function deleteMigrations(array $migrations, Database $db) |
|
174
|
|
|
{ |
|
175
|
2 |
|
$databaseMigrationsCollection = $this->getMigrationsCollection($db); |
|
176
|
|
|
|
|
177
|
2 |
|
foreach ($migrations as $id => $migration) { |
|
178
|
2 |
|
$databaseMigrationsCollection->deleteOne(['migration_id' => $id]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
2 |
|
$deletedMigrations = count($migrations); |
|
182
|
2 |
|
$this->output->writeln("<info>✓ Successfully deleted {$deletedMigrations} migrations from version collection</info>"); |
|
183
|
2 |
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|