AbstractMigration.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Migrations;
  20. use Doctrine\DBAL\Schema\Schema;
  21. /**
  22. * Abstract class for individual migrations to extend from.
  23. *
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @link www.doctrine-project.org
  26. * @since 2.0
  27. * @author Jonathan H. Wage <jonwage@gmail.com>
  28. */
  29. abstract class AbstractMigration
  30. {
  31. /**
  32. * Reference to the Version instance representing this migration
  33. *
  34. * @var Version
  35. */
  36. protected $version;
  37. /**
  38. * The Doctrine\DBAL\Connection instance we are migrating
  39. *
  40. * @var \Doctrine\DBAL\Connection
  41. */
  42. protected $connection;
  43. /**
  44. * Reference to the SchemaManager instance referenced by $_connection
  45. *
  46. * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
  47. */
  48. protected $sm;
  49. /**
  50. * Reference to the DatabasePlatform instance referenced by $_connection
  51. *
  52. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  53. */
  54. protected $platform;
  55. /**
  56. * The OutputWriter object instance used for outputting information
  57. *
  58. * @var OutputWriter
  59. */
  60. private $outputWriter;
  61. public function __construct(Version $version)
  62. {
  63. $config = $version->getConfiguration();
  64. $this->version = $version;
  65. $this->connection = $config->getConnection();
  66. $this->sm = $this->connection->getSchemaManager();
  67. $this->platform = $this->connection->getDatabasePlatform();
  68. $this->outputWriter = $config->getOutputWriter();
  69. }
  70. /**
  71. * Indicates the transactional mode of this migration.
  72. * If this function returns true (default) the migration will be executed in one transaction,
  73. * otherwise non-transactional state will be used to execute each of the migration SQLs.
  74. *
  75. * Extending class should override this function to alter the return value
  76. *
  77. * @return bool TRUE by default.
  78. */
  79. public function isTransactional()
  80. {
  81. return true;
  82. }
  83. /**
  84. * Get migration description
  85. *
  86. * @return string
  87. */
  88. public function getDescription()
  89. {
  90. return '';
  91. }
  92. /**
  93. * Print a warning message if the condition evaluates to TRUE.
  94. *
  95. * @param boolean $condition
  96. * @param string $message
  97. */
  98. public function warnIf($condition, $message = '')
  99. {
  100. if ($condition) {
  101. $message = $message ?: 'Unknown Reason';
  102. $this->outputWriter->write(sprintf(
  103. ' <comment>Warning during %s: %s</comment>',
  104. $this->version->getExecutionState(),
  105. $message
  106. ));
  107. }
  108. }
  109. /**
  110. * Abort the migration if the condition evaluates to TRUE.
  111. *
  112. * @param boolean $condition
  113. * @param string $message
  114. *
  115. * @throws AbortMigrationException
  116. */
  117. public function abortIf($condition, $message = '')
  118. {
  119. if ($condition) {
  120. throw new AbortMigrationException($message ?: 'Unknown Reason');
  121. }
  122. }
  123. /**
  124. * Skip this migration (but not the next ones) if condition evaluates to TRUE.
  125. *
  126. * @param boolean $condition
  127. * @param string $message
  128. *
  129. * @throws SkipMigrationException
  130. */
  131. public function skipIf($condition, $message = '')
  132. {
  133. if ($condition) {
  134. throw new SkipMigrationException($message ?: 'Unknown Reason');
  135. }
  136. }
  137. public function preUp(Schema $schema)
  138. {
  139. }
  140. public function postUp(Schema $schema)
  141. {
  142. }
  143. public function preDown(Schema $schema)
  144. {
  145. }
  146. public function postDown(Schema $schema)
  147. {
  148. }
  149. abstract public function up(Schema $schema);
  150. abstract public function down(Schema $schema);
  151. protected function addSql($sql, array $params = [], array $types = [])
  152. {
  153. $this->version->addSql($sql, $params, $types);
  154. }
  155. protected function write($message)
  156. {
  157. $this->outputWriter->write($message);
  158. }
  159. protected function throwIrreversibleMigrationException($message = null)
  160. {
  161. if (null === $message) {
  162. $message = 'This migration is irreversible and cannot be reverted.';
  163. }
  164. throw new IrreversibleMigrationException($message);
  165. }
  166. }