-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[stable29] Migration Attributes #46889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?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\Migration\MetadataManager; | ||
| use OC\Updater\ReleaseMetadata; | ||
| use OCP\Migration\Attributes\MigrationAttribute; | ||
| 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; | ||
|
|
||
| /** | ||
| * @since 30.0.0 | ||
| */ | ||
| class PreviewCommand extends Command { | ||
| private bool $initiated = false; | ||
| public function __construct( | ||
| private MetadataManager $metadataManager, | ||
| private ReleaseMetadata $releaseMetadata, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| $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'); | ||
| if (filter_var($version, FILTER_VALIDATE_URL)) { | ||
| $metadata = $this->releaseMetadata->downloadMetadata($version); | ||
| } elseif (str_starts_with($version, '/')) { | ||
| $metadata = json_decode(file_get_contents($version), true, flags: JSON_THROW_ON_ERROR); | ||
| } else { | ||
| $metadata = $this->releaseMetadata->getMetadata($version); | ||
| } | ||
|
|
||
| $parsed = $this->metadataManager->getMigrationsAttributesFromReleaseMetadata($metadata['migrations'] ?? [], true); | ||
|
|
||
| $table = new Table($output); | ||
| $this->displayMigrations($table, 'core', $parsed['core'] ?? []); | ||
| foreach ($parsed['apps'] as $appId => $migrations) { | ||
| if (!empty($migrations)) { | ||
| $this->displayMigrations($table, $appId, $migrations); | ||
| } | ||
| } | ||
| $table->render(); | ||
|
|
||
| $unsupportedApps = $this->metadataManager->getUnsupportedApps($metadata['migrations']); | ||
| if (!empty($unsupportedApps)) { | ||
| $output->writeln(''); | ||
| $output->writeln('Those apps are not supporting metadata yet and might initiate migrations on upgrade: <info>' . implode(', ', $unsupportedApps) . '</info>'); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private function displayMigrations(Table $table, string $appId, array $data): void { | ||
| if (empty($data)) { | ||
| return; | ||
| } | ||
|
|
||
| if ($this->initiated) { | ||
| $table->addRow(new TableSeparator()); | ||
| } | ||
| $this->initiated = true; | ||
|
|
||
| $table->addRow( | ||
| [ | ||
| new TableCell( | ||
| $appId, | ||
| [ | ||
| 'colspan' => 2, | ||
| 'style' => new TableCellStyle(['cellFormat' => '<info>%s</info>']) | ||
| ] | ||
| ) | ||
| ] | ||
| )->addRow(new TableSeparator()); | ||
|
|
||
| /** @var MigrationAttribute[] $attributes */ | ||
| foreach($data as $migration => $attributes) { | ||
| $attributesStr = []; | ||
| if (empty($attributes)) { | ||
| $attributesStr[] = '<comment>(metadata not set)</comment>'; | ||
| } | ||
| 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)]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OC\Migration\Exceptions; | ||
|
|
||
| use Exception; | ||
|
|
||
| /** | ||
| * @since 30.0.0 | ||
| */ | ||
| class AttributeException extends Exception { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| <?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; | ||
|
|
||
| /** | ||
| * Helps managing DB Migrations' Metadata | ||
| * | ||
| * @since 30.0.0 | ||
| */ | ||
| class MetadataManager { | ||
| public function __construct( | ||
| private IAppManager $appManager, | ||
| private Connection $connection, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * 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'); | ||
| } | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
lib/private/Updater/Exceptions/ReleaseMetadataException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OC\Updater\Exceptions; | ||
|
|
||
| use Exception; | ||
|
|
||
| /** | ||
| * @since 30.0.0 | ||
| */ | ||
| class ReleaseMetadataException extends Exception { | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.