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
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Db\Migrations;
use OC\DB\Connection;
use OC\DB\MigrationService;
use OCP\Migration\Attributes\GenericMigrationAttribute;
use OCP\Migration\Attributes\MigrationAttribute;
use OCP\Migration\Exceptions\AttributeException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableCellStyle;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PreviewCommand extends Command {
public function __construct(
private readonly Connection $connection,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
protected function configure() {
$this
->setName('migrations:preview')
->setDescription('Get preview of available DB migrations in case of initiating an upgrade')
->addArgument('version', InputArgument::REQUIRED, 'The destination version number');
parent::configure();
}
public function execute(InputInterface $input, OutputInterface $output): int {
$version = $input->getArgument('version');
$metadata = $this->getMetadata($version);
$parsed = $this->getMigrationsAttributes($metadata);
$table = new Table($output);
$this->displayMigrations($table, 'core', $parsed['core']);
$table->render();
return 0;
}
private function displayMigrations(Table $table, string $appId, array $data): void {
$done = $this->getDoneMigrations($appId);
$done = array_diff($done, ['30000Date20240429122720']);
$table->addRow(
[
new TableCell(
$appId,
[
'colspan' => 2,
'style' => new TableCellStyle(['cellFormat' => '<info>%s</info>'])
]
)
]
)->addRow(new TableSeparator());
foreach($data as $migration => $attributes) {
if (in_array($migration, $done)) {
continue;
}
$attributesStr = [];
/** @var MigrationAttribute[] $attributes */
foreach($attributes as $attribute) {
$definition = '<info>' . $attribute->definition() . "</info>";
$definition .= empty($attribute->getDescription()) ? '' : "\n " . $attribute->getDescription();
$definition .= empty($attribute->getNotes()) ? '' : "\n <comment>" . implode("</comment>\n <comment>", $attribute->getNotes()) . '</comment>';
$attributesStr[] = $definition;
}
$table->addRow([$migration, implode("\n", $attributesStr)]);
}
}
private function getMetadata(string $version): array {
$metadata = json_decode(file_get_contents('/tmp/nextcloud-' . $version . '.metadata'), true);
if (!$metadata) {
throw new \Exception();
}
return $metadata['migrations'] ?? [];
}
private function getDoneMigrations(string $appId): array {
$ms = new MigrationService($appId, $this->connection);
return $ms->getMigratedVersions();
}
private function getMigrationsAttributes(array $metadata): array {
$appsAttributes = [];
foreach (array_keys($metadata['apps']) as $appId) {
$appsAttributes[$appId] = $this->parseMigrations($metadata['apps'][$appId] ?? []);
}
return [
'core' => $this->parseMigrations($metadata['core'] ?? []),
'apps' => $appsAttributes
];
}
private function parseMigrations(array $migrations): array {
$parsed = [];
foreach (array_keys($migrations) as $entry) {
$items = $migrations[$entry];
$parsed[$entry] = [];
foreach ($items as $item) {
try {
$parsed[$entry][] = $this->createAttribute($item);
} catch (AttributeException $e) {
$this->logger->warning(
'exception while trying to create attribute',
['exception' => $e, 'item' => json_encode($item)]
);
$parsed[$entry][] = new GenericMigrationAttribute($item);
}
}
}
return $parsed;
}
/**
* @param array $item
*
* @return MigrationAttribute|null
* @throws AttributeException
*/
private function createAttribute(array $item): ?MigrationAttribute {
$class = $item['class'] ?? '';
$namespace = 'OCP\Migration\Attributes\\';
if (!str_starts_with($class, $namespace)
|| !ctype_alpha(substr($class, strlen($namespace)))) {
throw new AttributeException('class name does not looks valid');
}
try {
$attribute = new $class();
return $attribute->import($item);
} catch (\Error) {
throw new AttributeException('cannot import Attribute');
}
}
}
|