2
0

MigrationDiffCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Command\Migration;
  3. use PhpDevCommunity\Console\Command\CommandInterface;
  4. use PhpDevCommunity\Console\InputInterface;
  5. use PhpDevCommunity\Console\Option\CommandOption;
  6. use PhpDevCommunity\Console\Output\ConsoleOutput;
  7. use PhpDevCommunity\Console\OutputInterface;
  8. use PhpDevCommunity\FileSystem\Tools\FileExplorer;
  9. use PhpDevCommunity\PaperORM\Entity\EntityInterface;
  10. use PhpDevCommunity\PaperORM\Migration\PaperMigration;
  11. use PhpDevCommunity\PaperORM\Tools\EntityExplorer;
  12. class MigrationDiffCommand implements CommandInterface
  13. {
  14. private PaperMigration $paperMigration;
  15. private ?string $defaultEntitiesDir;
  16. public function __construct(PaperMigration $paperMigration, ?string $defaultEntitiesDir = null)
  17. {
  18. $this->paperMigration = $paperMigration;
  19. $this->defaultEntitiesDir = $defaultEntitiesDir;
  20. }
  21. public function getName(): string
  22. {
  23. return 'paper:migration:diff';
  24. }
  25. public function getDescription(): string
  26. {
  27. return 'Generate a migration diff for the SQL database';
  28. }
  29. public function getOptions(): array
  30. {
  31. return [
  32. new CommandOption('entities-dir', null, 'The directory where the entities are', false)
  33. ];
  34. }
  35. public function getArguments(): array
  36. {
  37. return [];
  38. }
  39. public function execute(InputInterface $input, OutputInterface $output): void
  40. {
  41. $io = ConsoleOutput::create($output);
  42. $entitiesDir = $this->defaultEntitiesDir;
  43. $printOutput = $input->getOptionValue('verbose');
  44. if ($input->hasOption('entities-dir')) {
  45. $entitiesDir = $input->getOptionValue('entities-dir');
  46. }
  47. if ($entitiesDir === null) {
  48. throw new \LogicException('The --entities-dir option is required');
  49. }
  50. $platform = $this->paperMigration->getEntityManager()->getPlatform();
  51. $io->title('Starting migration diff on ' . $platform->getDatabaseName());
  52. $io->list([
  53. 'Database : ' . $platform->getDatabaseName(),
  54. 'Entities directory : ' . $entitiesDir
  55. ]);
  56. $entities = EntityExplorer::getEntities($entitiesDir);
  57. $io->title('Number of entities detected: ' . count($entities));
  58. $io->listKeyValues($entities);
  59. $file = $this->paperMigration->generateMigrationFromEntities($entities);
  60. if ($file === null) {
  61. $io->info('No migration file was generated — all entities are already in sync with the database schema.');
  62. return;
  63. }
  64. if ($printOutput === true) {
  65. $splFile = new \SplFileObject($file);
  66. $lines = [];
  67. while (!$splFile->eof()) {
  68. $lines[] = $splFile->fgets();
  69. }
  70. unset($splFile);
  71. $io->listKeyValues($lines);
  72. }
  73. $io->success('✅ Migration file successfully generated: ' . $file);
  74. }
  75. }