AbstractMigration.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Doctrine\DBAL\Migrations\Configuration\Configuration,
  22. Doctrine\DBAL\Migrations\Version;
  23. /**
  24. * Abstract class for individual migrations to extend from.
  25. *
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 2.0
  29. * @author Jonathan H. Wage <jonwage@gmail.com>
  30. */
  31. abstract class AbstractMigration
  32. {
  33. /**
  34. * The Migrations Configuration instance for this migration
  35. *
  36. * @var Configuration
  37. */
  38. private $configuration;
  39. /**
  40. * The OutputWriter object instance used for outputting information
  41. *
  42. * @var OutputWriter
  43. */
  44. private $outputWriter;
  45. /**
  46. * The Doctrine\DBAL\Connection instance we are migrating
  47. *
  48. * @var \Doctrine\DBAL\Connection
  49. */
  50. protected $connection;
  51. /**
  52. * Reference to the SchemaManager instance referened by $_connection
  53. *
  54. * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
  55. */
  56. protected $sm;
  57. /**
  58. * Reference to the DatabasePlatform instance referenced by $_conection
  59. *
  60. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  61. */
  62. protected $platform;
  63. /**
  64. * Reference to the Version instance representing this migration
  65. *
  66. * @var Version
  67. */
  68. protected $version;
  69. public function __construct(Version $version)
  70. {
  71. $this->configuration = $version->getConfiguration();
  72. $this->outputWriter = $this->configuration->getOutputWriter();
  73. $this->connection = $this->configuration->getConnection();
  74. $this->sm = $this->connection->getSchemaManager();
  75. $this->platform = $this->connection->getDatabasePlatform();
  76. $this->version = $version;
  77. }
  78. /**
  79. * Get custom migration name
  80. *
  81. * @return string
  82. */
  83. public function getName()
  84. {
  85. }
  86. abstract public function up(Schema $schema);
  87. abstract public function down(Schema $schema);
  88. protected function addSql($sql, array $params = array(), array $types = array())
  89. {
  90. $this->version->addSql($sql, $params, $types);
  91. }
  92. protected function write($message)
  93. {
  94. $this->outputWriter->write($message);
  95. }
  96. protected function throwIrreversibleMigrationException($message = null)
  97. {
  98. if ($message === null) {
  99. $message = 'This migration is irreversible and cannot be reverted.';
  100. }
  101. throw new IrreversibleMigrationException($message);
  102. }
  103. /**
  104. * Print a warning message if the condition evalutes to TRUE.
  105. *
  106. * @param boolean $condition
  107. * @param string $message
  108. */
  109. public function warnIf($condition, $message = '')
  110. {
  111. $message = (strlen($message)) ? $message : 'Unknown Reason';
  112. if ($condition === true) {
  113. $this->outputWriter->write(' <warning>Warning during ' . $this->version->getExecutionState() . ': ' . $message . '</warning>');
  114. }
  115. }
  116. /**
  117. * Abort the migration if the condition evalutes to TRUE.
  118. *
  119. * @param boolean $condition
  120. * @param string $message
  121. *
  122. * @throws AbortMigrationException
  123. */
  124. public function abortIf($condition, $message = '')
  125. {
  126. $message = (strlen($message)) ? $message : 'Unknown Reason';
  127. if ($condition === true) {
  128. throw new AbortMigrationException($message);
  129. }
  130. }
  131. /**
  132. * Skip this migration (but not the next ones) if condition evalutes to TRUE.
  133. *
  134. * @param boolean $condition
  135. * @param string $message
  136. *
  137. * @throws SkipMigrationException
  138. */
  139. public function skipIf($condition, $message = '')
  140. {
  141. $message = (strlen($message)) ? $message : 'Unknown Reason';
  142. if ($condition === true) {
  143. throw new SkipMigrationException($message);
  144. }
  145. }
  146. public function preUp(Schema $schema)
  147. {
  148. }
  149. public function postUp(Schema $schema)
  150. {
  151. }
  152. public function preDown(Schema $schema)
  153. {
  154. }
  155. public function postDown(Schema $schema)
  156. {
  157. }
  158. }