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
166
167
168
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Migration;
use OC\DB\Connection;
use OC\DB\MigrationService;
use OC\Migration\Exceptions\AttributeException;
use OCP\App\IAppManager;
use OCP\Migration\Attributes\GenericMigrationAttribute;
use OCP\Migration\Attributes\MigrationAttribute;
use Psr\Log\LoggerInterface;
use ReflectionClass;
/**
* Helps managing DB Migrations' Metadata
*
* @since 30.0.0
*/
class MetadataManager {
public function __construct(
private readonly IAppManager $appManager,
private readonly Connection $connection,
private readonly LoggerInterface $logger,
) {
}
/**
* We get all migrations from an app (or 'core'), and
* for each migration files we extract its attributes
*
* @param string $appId
*
* @return array<string, MigrationAttribute[]>
* @since 30.0.0
*/
public function extractMigrationAttributes(string $appId): array {
$ms = new MigrationService($appId, $this->connection);
$metadata = [];
foreach($ms->getAvailableVersions() as $version) {
$metadata[$version] = [];
$class = new ReflectionClass($ms->createInstance($version));
$attributes = $class->getAttributes();
foreach ($attributes as $attribute) {
$item = $attribute->newInstance();
if ($item instanceof MigrationAttribute) {
$metadata[$version][] = $item;
}
}
}
return $metadata;
}
/**
* convert direct data from release metadata into a list of Migrations' Attribute
*
* @param array<array-key, array<array-key, array>> $metadata
* @param bool $filterKnownMigrations ignore metadata already done in local instance
*
* @return array{apps: array<array-key, array<string, MigrationAttribute[]>>, core: array<string, MigrationAttribute[]>}
* @since 30.0.0
*/
public function getMigrationsAttributesFromReleaseMetadata(
array $metadata,
bool $filterKnownMigrations = false
): array {
$appsAttributes = [];
foreach (array_keys($metadata['apps']) as $appId) {
if ($filterKnownMigrations && !$this->appManager->isInstalled($appId)) {
continue; // if not interested and app is not installed
}
$done = ($filterKnownMigrations) ? $this->getKnownMigrations($appId) : [];
$appsAttributes[$appId] = $this->parseMigrations($metadata['apps'][$appId] ?? [], $done);
}
$done = ($filterKnownMigrations) ? $this->getKnownMigrations('core') : [];
return [
'core' => $this->parseMigrations($metadata['core'] ?? [], $done),
'apps' => $appsAttributes
];
}
/**
* returns list of installed apps that does not support migrations metadata (yet)
*
* @param array<array-key, array<array-key, array>> $metadata
*
* @return string[]
* @since 30.0.0
*/
public function getUnsupportedApps(array $metadata): array {
return array_values(array_diff($this->appManager->getInstalledApps(), array_keys($metadata['apps'])));
}
/**
* convert raw data to a list of MigrationAttribute
*
* @param array $migrations
* @param array $ignoreMigrations
*
* @return array<string, MigrationAttribute[]>
*/
private function parseMigrations(array $migrations, array $ignoreMigrations = []): array {
$parsed = [];
foreach (array_keys($migrations) as $entry) {
if (in_array($entry, $ignoreMigrations)) {
continue;
}
$parsed[$entry] = [];
foreach ($migrations[$entry] 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;
}
/**
* returns migrations already done
*
* @param string $appId
*
* @return array
* @throws \Exception
*/
private function getKnownMigrations(string $appId): array {
$ms = new MigrationService($appId, $this->connection);
return $ms->getMigratedVersions();
}
/**
* generate (deserialize) a MigrationAttribute from a serialized version
*
* @param array $item
*
* @return MigrationAttribute
* @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($item['table'] ?? '');
return $attribute->import($item);
} catch (\Error) {
throw new AttributeException('cannot import Attribute');
}
}
}
|