ExportSchema.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Db;
  8. use OC\Core\Command\Base;
  9. use OCP\IDBConnection;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class ExportSchema extends Base {
  14. public function __construct(
  15. protected IDBConnection $connection,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function configure(): void {
  20. $this
  21. ->setName('db:schema:export')
  22. ->setDescription('Export the current database schema')
  23. ->addOption('sql', null, InputOption::VALUE_NONE, 'Dump the SQL statements for creating a copy of the schema');
  24. parent::configure();
  25. }
  26. protected function execute(InputInterface $input, OutputInterface $output): int {
  27. $schema = $this->connection->createSchema();
  28. $sql = $input->getOption('sql');
  29. if ($sql) {
  30. $output->writeln($schema->toSql($this->connection->getDatabasePlatform()));
  31. } else {
  32. $encoder = new SchemaEncoder();
  33. $this->writeArrayInOutputFormat($input, $output, $encoder->encodeSchema($schema, $this->connection->getDatabasePlatform()));
  34. }
  35. return 0;
  36. }
  37. }