Skip to content

Commit 5b565a4

Browse files
authored
Merge pull request #31173 from nextcloud/enhancement/user_migration-version-handling
Add version handling to IMigrator
2 parents 67f192e + 07940f9 commit 5b565a4

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@
547547
'OCP\\UserMigration\\IExportDestination' => $baseDir . '/lib/public/UserMigration/IExportDestination.php',
548548
'OCP\\UserMigration\\IImportSource' => $baseDir . '/lib/public/UserMigration/IImportSource.php',
549549
'OCP\\UserMigration\\IMigrator' => $baseDir . '/lib/public/UserMigration/IMigrator.php',
550+
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => $baseDir . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
550551
'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php',
551552
'OCP\\UserStatus\\IProvider' => $baseDir . '/lib/public/UserStatus/IProvider.php',
552553
'OCP\\UserStatus\\IUserStatus' => $baseDir . '/lib/public/UserStatus/IUserStatus.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
576576
'OCP\\UserMigration\\IExportDestination' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IExportDestination.php',
577577
'OCP\\UserMigration\\IImportSource' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IImportSource.php',
578578
'OCP\\UserMigration\\IMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IMigrator.php',
579+
'OCP\\UserMigration\\TMigratorBasicVersionHandling' => __DIR__ . '/../../..' . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php',
579580
'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php',
580581
'OCP\\UserStatus\\IProvider' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IProvider.php',
581582
'OCP\\UserStatus\\IUserStatus' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IUserStatus.php',

lib/public/UserMigration/IExportDestination.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public function addFileAsStream(string $path, $stream): bool;
6666
*/
6767
public function copyFolder(Folder $folder, string $destinationPath): bool;
6868

69+
/**
70+
* @param array<string,int> $versions Migrators and their versions.
71+
*
72+
* @since 24.0.0
73+
*/
74+
public function setMigratorVersions(array $versions): bool;
75+
6976
/**
7077
* Called after export is complete
7178
*

lib/public/UserMigration/IImportSource.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ public function getFileAsStream(string $path);
6363
*/
6464
public function copyToFolder(Folder $destination, string $sourcePath): bool;
6565

66+
/**
67+
* @return array<string,int> Migrators and their versions from the export archive.
68+
*
69+
* @since 24.0.0
70+
*/
71+
public function getMigratorVersions(): array;
72+
73+
/**
74+
* @return ?int Version for this migrator from the export archive. Null means migrator missing.
75+
*
76+
* @param class-string<IMigrator> $migrator
77+
*
78+
* @since 24.0.0
79+
*/
80+
public function getMigratorVersion(string $migrator): ?int;
81+
6682
/**
6783
* Called after import is complete
6884
*

lib/public/UserMigration/IMigrator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @copyright 2022 Christopher Ng <[email protected]>
77
*
88
* @author Christopher Ng <[email protected]>
9+
* @author Côme Chilliet <[email protected]>
910
*
1011
* @license GNU AGPL version 3 or any later version
1112
*
@@ -55,4 +56,21 @@ public function import(
5556
IImportSource $importSource,
5657
OutputInterface $output
5758
): void;
59+
60+
/**
61+
* Returns the version of the export format for this migrator
62+
*
63+
* @since 24.0.0
64+
*/
65+
public function getVersion(): int;
66+
67+
/**
68+
* Checks whether it is able to import a version of the export format for this migrator
69+
* Use $importSource->getMigratorVersion(static::class) to get the version from the archive
70+
*
71+
* @since 24.0.0
72+
*/
73+
public function canImport(
74+
IImportSource $importSource
75+
): bool;
5876
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Côme Chilliet <[email protected]>
7+
*
8+
* @author Côme Chilliet <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\UserMigration;
28+
29+
/**
30+
* Basic version handling: we can import older versions but not newer ones
31+
*/
32+
trait TMigratorBasicVersionHandling {
33+
protected int $version = 1;
34+
35+
protected bool $mandatory = false;
36+
37+
/**
38+
* {@inheritDoc}
39+
* @since 24.0.0
40+
*/
41+
public function getVersion(): int {
42+
return $this->version;
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
* @since 24.0.0
48+
*/
49+
public function canImport(
50+
IImportSource $importSource
51+
): bool {
52+
$version = $importSource->getMigratorVersion(static::class);
53+
if ($version === null) {
54+
return !$this->mandatory;
55+
}
56+
return ($this->version >= $version);
57+
}
58+
}

0 commit comments

Comments
 (0)