Skip to content

Commit

Permalink
Adapt to version handling changes in core interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Feb 15, 2022
1 parent 474c21b commit 4c44950
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
7 changes: 7 additions & 0 deletions lib/ExportDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public function copyFolder(Folder $folder, string $destinationPath): bool {
return true;
}

/**
* {@inheritDoc}
*/
public function setMigratorVersions(array $versions): bool {
return $this->addFileContents("migrator_versions.json", json_encode($versions));
}

/**
* {@inheritDoc}
*/
Expand Down
15 changes: 15 additions & 0 deletions lib/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class ImportSource implements IImportSource {

private string $path;

/**
* @var ?array<string, int>
*/
private ?array $migratorVersions = null;

public function __construct(string $path) {
$this->path = $path;
$this->archive = new ZIP($this->path);
Expand Down Expand Up @@ -84,6 +89,16 @@ public function copyToFolder(Folder $destination, string $sourcePath): bool {
return true;
}

/**
* {@inheritDoc}
*/
public function getMigratorVersions(): array {
if ($this->migratorVersions === null) {
$this->migratorVersions = json_decode($this->getFileContents("migrator_versions.json"), true, 512, JSON_THROW_ON_ERROR);
}
return $this->migratorVersions;
}

/**
* {@inheritDoc}
*/
Expand Down
25 changes: 9 additions & 16 deletions lib/Service/UserMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
class UserMigrationService {
use TMigratorBasicVersionHandling;

protected bool $mandatory = true;

protected IRootFolder $root;

protected IConfig $config;
Expand Down Expand Up @@ -145,7 +147,7 @@ public function export(IUser $user, ?OutputInterface $output = null): string {
$migrator->export($user, $exportDestination, $output);
$migratorVersions[$migrator::class] = $migrator->getVersion();
}
if ($exportDestination->addFileContents("migrator_versions.json", json_encode($migratorVersions)) === false) {
if ($exportDestination->setMigratorVersions($migratorVersions) === false) {
throw new UserMigrationException("Could not export user information.");
}

Expand All @@ -166,25 +168,18 @@ public function import(string $path, ?OutputInterface $output = null): void {
if ($context === null) {
throw new UserMigrationException("Failed to get context");
}
// TODO move this to ImportSource so that migrators can query the versions as well
$migrationVersions = json_decode($importSource->getFileContents("migrator_versions.json"), true, 512, JSON_THROW_ON_ERROR);
$migratorVersions = $importSource->getMigratorVersions();

if (!isset($migrationVersions[static::class])) {
throw new UserMigrationException("Cannot find version for main class ".static::class);
} elseif (!$this->canImport($migrationVersions[static::class])) {
throw new UserMigrationException("Version ${migrationVersions[static::class]} for main class ".static::class." is not compatible");
if (!$this->canImport($importSource, $migratorVersions[static::class] ?? null)) {
throw new UserMigrationException("Version ${$migratorVersions[static::class]} for main class ".static::class." is not compatible");
}

// Check versions
foreach ($context->getUserMigrators() as $migratorRegistration) {
/** @var IMigrator $migrator */
$migrator = $this->container->get($migratorRegistration->getService());
if (isset($migrationVersions[$migrator::class])) {
if (!$migrator->canImport($migrationVersions[$migrator::class])) {
throw new UserMigrationException("Version ${migrationVersions[$migrator::class]} for migrator ".$migrator::class." is not compatible");
}
} else {
$output->writeln("No input data for migrator ".$migrator::class.", ignoring");
if (!$migrator->canImport($importSource, $migratorVersions[$migrator::class] ?? null)) {
throw new UserMigrationException("Version ${$migratorVersions[$migrator::class]} for migrator ".$migrator::class." is not compatible");
}
}

Expand All @@ -197,9 +192,7 @@ public function import(string $path, ?OutputInterface $output = null): void {
foreach ($context->getUserMigrators() as $migratorRegistration) {
/** @var IMigrator $migrator */
$migrator = $this->container->get($migratorRegistration->getService());
if (isset($migrationVersions[$migrator::class])) {
$migrator->import($user, $importSource, $output);
}
$migrator->import($user, $importSource, $output, $migratorVersions[$migrator::class] ?? null);
}

$uid = $user->getUID();
Expand Down

0 comments on commit 4c44950

Please sign in to comment.