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: ' . implode(', ', $unsupportedApps) . '');
}
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' => '%s'])
]
)
]
)->addRow(new TableSeparator());
/** @var MigrationAttribute[] $attributes */
foreach($data as $migration => $attributes) {
$attributesStr = [];
if (empty($attributes)) {
$attributesStr[] = '(metadata not set)';
}
foreach($attributes as $attribute) {
$definition = '' . $attribute->definition() . "";
$definition .= empty($attribute->getDescription()) ? '' : "\n " . $attribute->getDescription();
$definition .= empty($attribute->getNotes()) ? '' : "\n " . implode("\n ", $attribute->getNotes()) . '';
$attributesStr[] = $definition;
}
$table->addRow([$migration, implode("\n", $attributesStr)]);
}
}
}