SchemaUpdateCommand.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. declare(strict_types=1);
  3. /** @noinspection PhpUnused */
  4. namespace App\Commands\Doctrine;
  5. use App\Service\Utils\Path;
  6. use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
  7. use Doctrine\ORM\Tools\SchemaTool;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. /**
  12. * Overrides the default doctrine:schema:update command.
  13. */
  14. class SchemaUpdateCommand extends UpdateCommand
  15. {
  16. protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui): int
  17. {
  18. $output->writeln('-- Executing pre-update scripts');
  19. // Lists schema extensions scripts in the '/sql/schema-extensions' dir
  20. $schemaExtensionsDir = PathUtils::join(PathUtils::getProjectDir(), 'sql', 'schema-extensions');
  21. $scripts = $this->fileUtils->list($schemaExtensionsDir, '*.sql');
  22. sort($scripts);
  23. // Execute those scripts in alphabetical order
  24. $em = $this->getEntityManager($input);
  25. $conn = $em->getConnection();
  26. foreach ($scripts as $script) {
  27. $sql = $this->fileUtils->getFileContent($script);
  28. $conn->executeQuery($sql);
  29. }
  30. $output->writeln(sprintf('-- Database successfully updated, %s scripts executed', count($scripts)));
  31. $output->writeln('<!> Operation interrupted: use the d:s:u command on the current version to update the DB tables');
  32. // parent::executeSchemaCommand($input, $output, $schemaTool, $metadatas, $ui);
  33. return 0;
  34. }
  35. }