AbstractMigration.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\DBALException;
  6. use Doctrine\DBAL\Platforms\AbstractPlatform;
  7. use Doctrine\DBAL\Schema\AbstractSchemaManager;
  8. use Doctrine\DBAL\Schema\Schema;
  9. use Doctrine\Migrations\Exception\AbortMigration;
  10. use Doctrine\Migrations\Exception\IrreversibleMigration;
  11. use Doctrine\Migrations\Exception\MigrationException;
  12. use Doctrine\Migrations\Exception\SkipMigration;
  13. use Doctrine\Migrations\Query\Query;
  14. use Psr\Log\LoggerInterface;
  15. use function sprintf;
  16. /**
  17. * The AbstractMigration class is for end users to extend from when creating migrations. Extend this class
  18. * and implement the required up() and down() methods.
  19. */
  20. abstract class AbstractMigration
  21. {
  22. /** @var Connection */
  23. protected $connection;
  24. /** @var AbstractSchemaManager */
  25. protected $sm;
  26. /** @var AbstractPlatform */
  27. protected $platform;
  28. /** @var LoggerInterface */
  29. private $logger;
  30. /** @var Query[] */
  31. private $plannedSql = [];
  32. public function __construct(Connection $connection, LoggerInterface $logger)
  33. {
  34. $this->connection = $connection;
  35. $this->sm = $this->connection->getSchemaManager();
  36. $this->platform = $this->connection->getDatabasePlatform();
  37. $this->logger = $logger;
  38. }
  39. /**
  40. * Indicates the transactional mode of this migration.
  41. *
  42. * If this function returns true (default) the migration will be executed
  43. * in one transaction, otherwise non-transactional state will be used to
  44. * execute each of the migration SQLs.
  45. *
  46. * Extending class should override this function to alter the return value.
  47. */
  48. public function isTransactional(): bool
  49. {
  50. return true;
  51. }
  52. public function getDescription(): string
  53. {
  54. return '';
  55. }
  56. public function warnIf(bool $condition, string $message = 'Unknown Reason'): void
  57. {
  58. if (! $condition) {
  59. return;
  60. }
  61. $this->logger->warning($message, ['migration' => $this]);
  62. }
  63. /**
  64. * @throws AbortMigration
  65. */
  66. public function abortIf(bool $condition, string $message = 'Unknown Reason'): void
  67. {
  68. if ($condition) {
  69. throw new AbortMigration($message);
  70. }
  71. }
  72. /**
  73. * @throws SkipMigration
  74. */
  75. public function skipIf(bool $condition, string $message = 'Unknown Reason'): void
  76. {
  77. if ($condition) {
  78. throw new SkipMigration($message);
  79. }
  80. }
  81. /**
  82. * @throws MigrationException|DBALException
  83. */
  84. public function preUp(Schema $schema): void
  85. {
  86. }
  87. /**
  88. * @throws MigrationException|DBALException
  89. */
  90. public function postUp(Schema $schema): void
  91. {
  92. }
  93. /**
  94. * @throws MigrationException|DBALException
  95. */
  96. public function preDown(Schema $schema): void
  97. {
  98. }
  99. /**
  100. * @throws MigrationException|DBALException
  101. */
  102. public function postDown(Schema $schema): void
  103. {
  104. }
  105. /**
  106. * @throws MigrationException|DBALException
  107. */
  108. abstract public function up(Schema $schema): void;
  109. /**
  110. * @throws MigrationException|DBALException
  111. */
  112. public function down(Schema $schema): void
  113. {
  114. $this->abortIf(true, sprintf('No down() migration implemented for "%s"', static::class));
  115. }
  116. /**
  117. * @param mixed[] $params
  118. * @param mixed[] $types
  119. */
  120. protected function addSql(
  121. string $sql,
  122. array $params = [],
  123. array $types = []
  124. ): void {
  125. $this->plannedSql[] = new Query($sql, $params, $types);
  126. }
  127. /**
  128. * @return Query[]
  129. */
  130. public function getSql(): array
  131. {
  132. return $this->plannedSql;
  133. }
  134. protected function write(string $message): void
  135. {
  136. $this->logger->notice($message, ['migration' => $this]);
  137. }
  138. /**
  139. * @throws IrreversibleMigration
  140. */
  141. protected function throwIrreversibleMigrationException(?string $message = null): void
  142. {
  143. if ($message === null) {
  144. $message = 'This migration is irreversible and cannot be reverted.';
  145. }
  146. throw new IrreversibleMigration($message);
  147. }
  148. }