migration-classes.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. Migration Classes
  2. =================
  3. Migration classes must extend ``Doctrine\Migrations\AbstractMigration`` and at a minimum they must implement the ``up``
  4. and ``down`` methods. You can easily generate a blank migration to modify with the following command:
  5. .. code-block:: sh
  6. $ ./vendor/bin/doctrine-migrations generate
  7. Generated new migration class to "/data/doctrine/migrations-docs-example/lib/MyProject/Migrations/Version20180601193057.php"
  8. To run just this migration for testing purposes, you can use migrations:execute --up 'MyProject\Migrations\Version20180601193057'
  9. To revert the migration you can use migrations:execute --down 'MyProject\Migrations\Version20180601193057'
  10. The above command will generate a PHP class with the path to it visible like above. Here is what the blank
  11. migration looks like:
  12. .. code-block:: php
  13. <?php
  14. declare(strict_types=1);
  15. namespace MyProject\Migrations;
  16. use Doctrine\DBAL\Schema\Schema;
  17. use Doctrine\Migrations\AbstractMigration;
  18. /**
  19. * Auto-generated Migration: Please modify to your needs!
  20. */
  21. final class Version20180601193057 extends AbstractMigration
  22. {
  23. public function getDescription() : string
  24. {
  25. return '';
  26. }
  27. public function up(Schema $schema) : void
  28. {
  29. // this up() migration is auto-generated, please modify it to your needs
  30. }
  31. public function down(Schema $schema) : void
  32. {
  33. // this down() migration is auto-generated, please modify it to your needs
  34. }
  35. }
  36. Methods to Implement
  37. --------------------
  38. The ``AbstractMigration`` class provides a few methods you can override to define additional behavior for the migration.
  39. isTransactional
  40. ~~~~~~~~~~~~~~~
  41. Override this method if you want to disable transactions in a migration. It defaults to true.
  42. .. code-block:: php
  43. public function isTransactional() : bool
  44. {
  45. return false;
  46. }
  47. .. note::
  48. Some database platforms like MySQL or Oracle do not support DDL
  49. statements in transactions and may or may not implicitly commit the
  50. transaction opened by this library as soon as they encounter such a
  51. statement, and before running it. Make sure to read the manual of
  52. your database platform to know what is actually happening.
  53. ``isTransactional()`` does not guarantee that statements are wrapped
  54. in a single transaction.
  55. getDescription
  56. ~~~~~~~~~~~~~~
  57. Override this method if you want to provide a description for your migration. The value returned here
  58. will get outputted when you run the ``./vendor/bin/doctrine-migrations status --show-versions`` command.
  59. .. code-block:: php
  60. public function getDescription() : string
  61. {
  62. return 'The description of my awesome migration!';
  63. }
  64. preUp
  65. ~~~~~
  66. This method gets called before the ``up()`` is called.
  67. .. code-block:: php
  68. public function preUp(Schema $schema) : void
  69. {
  70. }
  71. postUp
  72. ~~~~~
  73. This method gets called after the ``up()`` is called.
  74. .. code-block:: php
  75. public function postUp(Schema $schema) : void
  76. {
  77. }
  78. preDown
  79. ~~~~~~~
  80. This method gets called before the ``down()`` is called.
  81. .. code-block:: php
  82. public function preDown(Schema $schema) : void
  83. {
  84. }
  85. postDown
  86. ~~~~~~~~
  87. This method gets called after the ``down()`` is called.
  88. .. code-block:: php
  89. public function postDown(Schema $schema) : void
  90. {
  91. }
  92. Methods to Call
  93. ---------------
  94. The ``AbstractMigration`` class provides a few methods you can call in your migrations to perform various functions.
  95. warnIf
  96. ~~~~~~
  97. Warn with a message if some condition is met.
  98. .. code-block:: php
  99. public function up(Schema $schema) : void
  100. {
  101. $this->warnIf(true, 'Something might be going wrong');
  102. // ...
  103. }
  104. abortIf
  105. ~~~~~~~
  106. Abort the migration if some condition is met:
  107. .. code-block:: php
  108. public function up(Schema $schema) : void
  109. {
  110. $this->abortIf(true, 'Something went wrong. Aborting.');
  111. // ...
  112. }
  113. skipIf
  114. ~~~~~~
  115. Skip the migration if some condition is met.
  116. .. code-block:: php
  117. public function up(Schema $schema) : void
  118. {
  119. $this->skipIf(true, 'Skipping this migration.');
  120. // ...
  121. }
  122. addSql
  123. ~~~~~~
  124. You can use the ``addSql`` method within the ``up`` and ``down`` methods. Internally the ``addSql`` calls are passed
  125. to the executeQuery method in the DBAL. This means that you can use the power of prepared statements easily and that
  126. you don't need to copy paste the same query with different parameters. You can just pass those different parameters
  127. to the addSql method as parameters.
  128. .. code-block:: php
  129. public function up(Schema $schema) : void
  130. {
  131. $users = [
  132. ['name' => 'mike', 'id' => 1],
  133. ['name' => 'jwage', 'id' => 2],
  134. ['name' => 'ocramius', 'id' => 3],
  135. ];
  136. foreach ($users as $user) {
  137. $this->addSql('UPDATE user SET happy = true WHERE name = :name AND id = :id', $user);
  138. }
  139. }
  140. write
  141. ~~~~~
  142. Write some debug information to the console.
  143. .. code-block:: php
  144. public function up(Schema $schema) : void
  145. {
  146. $this->write('Doing some cool migration!');
  147. // ...
  148. }
  149. throwIrreversibleMigrationException
  150. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  151. If a migration cannot be reversed, you can use this exception in the ``down`` method to indicate such.
  152. The ``throwIrreversibleMigrationException`` method accepts an optional message to output.
  153. .. code-block:: php
  154. public function down(Schema $schema) : void
  155. {
  156. $this->throwIrreversibleMigrationException();
  157. // ...
  158. }
  159. :ref:`Next Chapter: Managing Migrations <managing-migrations>`