-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46476 from nextcloud/enh/noid/migration-attributes
Migration Attributes
- Loading branch information
Showing
28 changed files
with
1,618 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
41 changes: 41 additions & 0 deletions
41
apps/testing/lib/Migration/Version30000Date20240102030405.php
This file contains 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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Testing\Migration; | ||
|
||
use Closure; | ||
use OCP\Migration\Attributes\AddColumn; | ||
use OCP\Migration\Attributes\AddIndex; | ||
use OCP\Migration\Attributes\ColumnType; | ||
use OCP\Migration\Attributes\CreateTable; | ||
use OCP\Migration\Attributes\DropColumn; | ||
use OCP\Migration\Attributes\DropIndex; | ||
use OCP\Migration\Attributes\DropTable; | ||
use OCP\Migration\Attributes\IndexType; | ||
use OCP\Migration\Attributes\ModifyColumn; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
#[DropTable(table: 'old_table')] | ||
#[CreateTable(table: 'new_table', description: 'Table is used to store things, but also to get more things', notes: ['this is a notice', 'and another one, if really needed'])] | ||
#[AddColumn(table: 'my_table')] | ||
#[AddColumn(table: 'my_table', name: 'another_field')] | ||
#[AddColumn(table: 'other_table', name: 'last_one', type: ColumnType::DATE)] | ||
#[AddIndex(table: 'my_table')] | ||
#[AddIndex(table: 'my_table', type: IndexType::PRIMARY)] | ||
#[DropColumn(table: 'other_table')] | ||
#[DropColumn(table: 'other_table', name: 'old_column', description: 'field is not used anymore and replaced by \'last_one\'')] | ||
#[DropIndex(table: 'other_table')] | ||
#[ModifyColumn(table: 'other_table')] | ||
#[ModifyColumn(table: 'other_table', name: 'this_field')] | ||
#[ModifyColumn(table: 'other_table', name: 'this_field', type: ColumnType::BIGINT)] | ||
class Version30000Date20240102030405 extends SimpleMigrationStep { | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { | ||
return null; | ||
} | ||
} |
This file contains 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,83 @@ | ||
<?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 OCP\App\IAppManager; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
class GenerateMetadataCommand extends Command { | ||
public function __construct( | ||
private readonly MetadataManager $metadataManager, | ||
private readonly IAppManager $appManager, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void { | ||
$this->setName('migrations:generate-metadata') | ||
->setHidden(true) | ||
->setDescription('Generate metadata from DB migrations - internal and should not be used'); | ||
|
||
parent::configure(); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int { | ||
$output->writeln( | ||
json_encode( | ||
[ | ||
'migrations' => $this->extractMigrationMetadata() | ||
], | ||
JSON_PRETTY_PRINT | ||
) | ||
); | ||
|
||
return 0; | ||
} | ||
|
||
private function extractMigrationMetadata(): array { | ||
return [ | ||
'core' => $this->extractMigrationMetadataFromCore(), | ||
'apps' => $this->extractMigrationMetadataFromApps() | ||
]; | ||
} | ||
|
||
private function extractMigrationMetadataFromCore(): array { | ||
return $this->metadataManager->extractMigrationAttributes('core'); | ||
} | ||
|
||
/** | ||
* get all apps and extract attributes | ||
* | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
private function extractMigrationMetadataFromApps(): array { | ||
$allApps = \OC_App::getAllApps(); | ||
$metadata = []; | ||
foreach ($allApps as $appId) { | ||
// We need to load app before being able to extract Migrations | ||
// If app was not enabled before, we will disable it afterward. | ||
$alreadyLoaded = $this->appManager->isInstalled($appId); | ||
if (!$alreadyLoaded) { | ||
$this->appManager->loadApp($appId); | ||
} | ||
$metadata[$appId] = $this->metadataManager->extractMigrationAttributes($appId); | ||
if (!$alreadyLoaded) { | ||
$this->appManager->disableApp($appId); | ||
} | ||
} | ||
return $metadata; | ||
} | ||
} |
This file contains 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,102 @@ | ||
<?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 readonly MetadataManager $metadataManager, | ||
private readonly 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(); | ||
|
||
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 = []; | ||
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 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 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 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
Oops, something went wrong.