GenerateMetadataCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Db\Migrations;
  8. use OC\Migration\MetadataManager;
  9. use OCP\App\IAppManager;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. /**
  14. * @since 30.0.0
  15. */
  16. class GenerateMetadataCommand extends Command {
  17. public function __construct(
  18. private readonly MetadataManager $metadataManager,
  19. private readonly IAppManager $appManager,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure(): void {
  24. $this->setName('migrations:generate-metadata')
  25. ->setHidden(true)
  26. ->setDescription('Generate metadata from DB migrations - internal and should not be used');
  27. parent::configure();
  28. }
  29. public function execute(InputInterface $input, OutputInterface $output): int {
  30. $output->writeln(
  31. json_encode(
  32. [
  33. 'migrations' => $this->extractMigrationMetadata()
  34. ],
  35. JSON_PRETTY_PRINT
  36. )
  37. );
  38. return 0;
  39. }
  40. private function extractMigrationMetadata(): array {
  41. return [
  42. 'core' => $this->extractMigrationMetadataFromCore(),
  43. 'apps' => $this->extractMigrationMetadataFromApps()
  44. ];
  45. }
  46. private function extractMigrationMetadataFromCore(): array {
  47. return $this->metadataManager->extractMigrationAttributes('core');
  48. }
  49. /**
  50. * get all apps and extract attributes
  51. *
  52. * @return array
  53. * @throws \Exception
  54. */
  55. private function extractMigrationMetadataFromApps(): array {
  56. $allApps = $this->appManager->getAllAppsInAppsFolders();
  57. $metadata = [];
  58. foreach ($allApps as $appId) {
  59. // We need to load app before being able to extract Migrations
  60. // If app was not enabled before, we will disable it afterward.
  61. $alreadyLoaded = $this->appManager->isInstalled($appId);
  62. if (!$alreadyLoaded) {
  63. $this->appManager->loadApp($appId);
  64. }
  65. $metadata[$appId] = $this->metadataManager->extractMigrationAttributes($appId);
  66. if (!$alreadyLoaded) {
  67. $this->appManager->disableApp($appId);
  68. }
  69. }
  70. return $metadata;
  71. }
  72. }