Rollup.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations;
  4. use Doctrine\Migrations\Exception\RollupFailed;
  5. use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
  6. use Doctrine\Migrations\Version\ExecutionResult;
  7. use Doctrine\Migrations\Version\Version;
  8. use function count;
  9. /**
  10. * The Rollup class is responsible for deleting all previously executed migrations from the versions table
  11. * and marking the freshly dumped schema migration (that was created with SchemaDumper) as migrated.
  12. *
  13. * @internal
  14. */
  15. class Rollup
  16. {
  17. /** @var MigrationsRepository */
  18. private $migrationRepository;
  19. /** @var MetadataStorage */
  20. private $metadataStorage;
  21. public function __construct(
  22. MetadataStorage $metadataStorage,
  23. MigrationsRepository $migrationRepository
  24. ) {
  25. $this->migrationRepository = $migrationRepository;
  26. $this->metadataStorage = $metadataStorage;
  27. }
  28. /**
  29. * @throws RollupFailed
  30. */
  31. public function rollup(): Version
  32. {
  33. $versions = $this->migrationRepository->getMigrations();
  34. if (count($versions) === 0) {
  35. throw RollupFailed::noMigrationsFound();
  36. }
  37. if (count($versions) > 1) {
  38. throw RollupFailed::tooManyMigrations();
  39. }
  40. $this->metadataStorage->reset();
  41. $result = new ExecutionResult($versions->getItems()[0]->getVersion());
  42. $this->metadataStorage->complete($result);
  43. return $result->getVersion();
  44. }
  45. }