PageRenderTime 65ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/en/reference/managing-migrations.rst

https://github.com/doctrine/migrations
ReStructuredText | 374 lines | 241 code | 133 blank | 0 comment | 0 complexity | a26143eaec62ffce25bc8ae0078bae18 MD5 | raw file
  1. Managing Migrations
  2. ===================
  3. Managing migrations with Doctrine is easy. You can execute migrations from the console and easily revert them. You also
  4. have the option to write the SQL for a migration to a file instead of executing it from PHP.
  5. Status
  6. ------
  7. Now that we have a new migration created, run the ``status`` command with the ``--show-versions`` option to see
  8. that the new migration is registered and ready to be executed:
  9. .. code-block:: sh
  10. $ ./vendor/bin/doctrine-migrations status --show-versions
  11. == Configuration
  12. >> Name: My Project Migrations
  13. >> Database Driver: pdo_mysql
  14. >> Database Host: localhost
  15. >> Database Name: migrations_docs_example
  16. >> Configuration Source: /data/doctrine/migrations-docs-example/migrations.php
  17. >> Version Table Name: doctrine_migration_versions
  18. >> Version Column Name: version
  19. >> Migrations Namespace: MyProject\Migrations
  20. >> Migrations Directory: /data/doctrine/migrations-docs-example/lib/MyProject/Migrations
  21. >> Previous Version: Already at first version
  22. >> Current Version: 0
  23. >> Next Version: 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057)
  24. >> Latest Version: 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057)
  25. >> Executed Migrations: 0
  26. >> Executed Unavailable Migrations: 0
  27. >> Available Migrations: 1
  28. >> New Migrations: 1
  29. == Available Migration Versions
  30. >> 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057) not migrated This is my example migration.
  31. As you can see we have a new migration version available and it is ready to be executed. The problem
  32. is, it does not have anything in it so nothing would be executed! Let's add some code to it and add a new table:
  33. .. code-block:: php
  34. <?php
  35. declare(strict_types=1);
  36. namespace MyProject\Migrations;
  37. use Doctrine\DBAL\Schema\Schema;
  38. use Doctrine\Migrations\AbstractMigration;
  39. /**
  40. * Auto-generated Migration: Please modify to your needs!
  41. */
  42. final class Version20180601193057 extends AbstractMigration
  43. {
  44. public function getDescription(): string
  45. {
  46. return 'This is my example migration.';
  47. }
  48. public function up(Schema $schema): void
  49. {
  50. $this->addSql('CREATE TABLE example_table (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
  51. }
  52. public function down(Schema $schema): void
  53. {
  54. $this->addSql('DROP TABLE example_table');
  55. }
  56. }
  57. Dry Run
  58. -------
  59. Now we are ready to give it a test! First lets just do a dry-run to make sure it produces the SQL we expect:
  60. .. code-block:: sh
  61. $ ./vendor/bin/doctrine-migrations migrate --dry-run
  62. My Project Migrations
  63. Executing dry run of migration up to MyProject\Migrations\Version20180601193057 from 0
  64. ++ migrating MyProject\Migrations\Version20180601193057
  65. -> CREATE TABLE example_table (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))
  66. ++ migrated (took 60.9ms, used 8M memory)
  67. ------------------------
  68. ++ finished in 69.4ms
  69. ++ used 8M memory
  70. ++ 1 migrations executed
  71. ++ 1 sql queries
  72. Executing Multiple Migrations
  73. -----------------------------
  74. Everything looks good so we can remove the ``--dry-run`` option and actually execute the migration.
  75. .. note::
  76. The ``migrate`` command will execute multiple migrations if there are multiple new unexecuted migration versions
  77. available. It will attempt to go from the current version to the latest version available.
  78. .. code-block:: sh
  79. $ ./vendor/bin/doctrine-migrations migrate
  80. My Project Migrations
  81. WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)y
  82. Migrating up to MyProject\Migrations\Version20180601193057 from 0
  83. ++ migrating MyProject\Migrations\Version20180601193057
  84. -> CREATE TABLE example_table (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))
  85. ++ migrated (took 47.7ms, used 8M memory)
  86. ------------------------
  87. ++ finished in 49.1ms
  88. ++ used 8M memory
  89. ++ 1 migrations executed
  90. ++ 1 sql queries
  91. Executing Single Migrations
  92. ---------------------------
  93. You may want to just execute a single migration up or down. You can do this with the ``execute`` command:
  94. .. code-block:: sh
  95. $ ./vendor/bin/doctrine-migrations execute MyProject\Migrations\Version20180601193057 --down
  96. WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)y
  97. ++ migrating MyProject\Migrations\Version20180601193057
  98. -> DROP TABLE example_table
  99. ++ migrated (took 42.6ms, used 8M memory)
  100. No Interaction
  101. --------------
  102. Alternately, if you wish to run the migrations in an unattended mode, we can add the ``--no-interaction`` option and then
  103. execute the migrations without any extra prompting from Doctrine.
  104. .. code-block:: sh
  105. $ ./vendor/bin/doctrine-migrations migrate --no-interaction
  106. My Project Migrations
  107. Migrating up to MyProject\Migrations\Version20180601193057 from 0
  108. ++ migrating MyProject\Migrations\Version20180601193057
  109. -> CREATE TABLE example_table (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))
  110. ++ migrated (took 46.5ms, used 8M memory)
  111. ------------------------
  112. ++ finished in 47.3ms
  113. ++ used 8M memory
  114. ++ 1 migrations executed
  115. ++ 1 sql queries
  116. By checking the status again after using either method you will see everything is updated:
  117. .. code-block:: sh
  118. $ ./vendor/bin/doctrine-migrations status --show-versions
  119. == Configuration
  120. >> Name: My Project Migrations
  121. >> Database Driver: pdo_mysql
  122. >> Database Host: localhost
  123. >> Database Name: migrations_docs_example
  124. >> Configuration Source: /data/doctrine/migrations-docs-example/migrations.php
  125. >> Version Table Name: doctrine_migration_versions
  126. >> Version Column Name: version
  127. >> Migrations Namespace: MyProject\Migrations
  128. >> Migrations Directory: /data/doctrine/migrations-docs-example/lib/MyProject/Migrations
  129. >> Previous Version: 0
  130. >> Current Version: 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057)
  131. >> Next Version: Already at latest version
  132. >> Latest Version: 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057)
  133. >> Executed Migrations: 1
  134. >> Executed Unavailable Migrations: 0
  135. >> Available Migrations: 1
  136. >> New Migrations: 0
  137. == Available Migration Versions
  138. >> 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057) migrated (executed at 2018-06-01 17:08:44) This is my example migration.
  139. Reverting Migrations
  140. --------------------
  141. The ``migrate`` command optionally accepts a version or version alias to migrate to. By default it will try to migrate up
  142. from the current version to the latest version. If you pass a version that is older than the current version,
  143. it will migrate down. To rollback to the the first version you can use the ``first`` version alias:
  144. .. code-block:: sh
  145. $ ./vendor/bin/doctrine-migrations migrate first
  146. My Project Migrations
  147. WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)y
  148. Migrating down to 0 from MyProject\Migrations\Version20180601193057
  149. -- reverting MyProject\Migrations\Version20180601193057
  150. -> DROP TABLE example_table
  151. -- reverted (took 38.4ms, used 8M memory)
  152. ------------------------
  153. ++ finished in 39.5ms
  154. ++ used 8M memory
  155. ++ 1 migrations executed
  156. ++ 1 sql queries
  157. Now if you run the ``status`` command again, you will see that the database is back to the way it was before:
  158. .. code-block:: sh
  159. $ ./vendor/bin/doctrine-migrations status --show-versions
  160. == Configuration
  161. >> Name: My Project Migrations
  162. >> Database Driver: pdo_mysql
  163. >> Database Host: localhost
  164. >> Database Name: migrations_docs_example
  165. >> Configuration Source: /data/doctrine/migrations-docs-example/migrations.php
  166. >> Version Table Name: doctrine_migration_versions
  167. >> Version Column Name: version
  168. >> Migrations Namespace: MyProject\Migrations
  169. >> Migrations Directory: /data/doctrine/migrations-docs-example/lib/MyProject/Migrations
  170. >> Previous Version: Already at first version
  171. >> Current Version: 0
  172. >> Next Version: 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057)
  173. >> Latest Version: 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057)
  174. >> Executed Migrations: 0
  175. >> Executed Unavailable Migrations: 0
  176. >> Available Migrations: 1
  177. >> New Migrations: 1
  178. == Available Migration Versions
  179. >> 2018-06-01 19:30:57 (MyProject\Migrations\Version20180601193057) not migrated This is my example migration.
  180. Version Aliases
  181. ---------------
  182. You can use version aliases when executing migrations. This is for your convenience so you don't have to always know
  183. the version number. The following aliases are available:
  184. - ``first`` - Migrate down to before the first version.
  185. - ``prev`` - Migrate down to before the previous version.
  186. - ``next`` - Migrate up to the next version.
  187. - ``latest`` - Migrate up to the latest version.
  188. Here is an example where we migrate to the latest version and then revert back to the first:
  189. .. code-block:: bash
  190. $ ./vendor/bin/doctrine-migrations migrate latest
  191. $ ./vendor/bin/doctrine-migrations migrate first
  192. Writing Migration SQL Files
  193. ---------------------------
  194. You can optionally choose to not execute a migration directly on a database from PHP and instead output all the SQL
  195. statement to a file. This is possible by using the ``--write-sql`` option:
  196. .. code-block:: sh
  197. $ ./vendor/bin/doctrine-migrations migrate --write-sql
  198. My Project Migrations
  199. Executing dry run of migration up to MyProject\Migrations\Version20180601193057 from 0
  200. ++ migrating MyProject\Migrations\Version20180601193057
  201. -> CREATE TABLE example_table (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))
  202. ++ migrated (took 55ms, used 8M memory)
  203. ------------------------
  204. ++ finished in 60.7ms
  205. ++ used 8M memory
  206. ++ 1 migrations executed
  207. ++ 1 sql queries
  208. -- Migrating from 0 to MyProject\Migrations\Version20180601193057
  209. Writing migration file to "/data/doctrine/migrations-docs-example/doctrine_migration_20180601172528.sql"
  210. Now if you have a look at the ``doctrine_migration_20180601172528.sql`` file you will see the would be
  211. executed SQL outputted in a nice format:
  212. .. code-block:: sh
  213. $ cat doctrine_migration_20180601172528.sql
  214. -- Doctrine Migration File Generated on 2018-06-01 17:25:28
  215. -- Version MyProject\Migrations\Version20180601193057
  216. CREATE TABLE example_table (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id));
  217. INSERT INTO doctrine_migration_versions (version, executed_at) VALUES ('MyProject\Migrations\Version20180601193057', CURRENT_TIMESTAMP);
  218. The ``--write-sql`` option also accepts an optional value for where to write the sql file. It can be a relative path
  219. to a file that will write to the current working directory:
  220. .. code-block:: sh
  221. $ ./vendor/bin/doctrine-migrations migrate --write-sql=migration.sql
  222. Or it can be an absolute path to the file:
  223. .. code-block:: sh
  224. $ ./vendor/bin/doctrine-migrations migrate --write-sql=/path/to/migration.sql
  225. Or it can be a directory and it will write the default filename to it:
  226. .. code-block:: sh
  227. $ ./vendor/bin/doctrine-migrations migrate --write-sql=/path/to/directory
  228. Managing the Version Table
  229. --------------------------
  230. Sometimes you may need to manually mark a migration as migrated or not. You can use the ``version`` command for this.
  231. .. caution::
  232. Use caution when using the ``version`` command. If you delete a version from the table and then run the ``migrate``
  233. command, that migration version will be executed again.
  234. .. code-block:: sh
  235. $ ./vendor/bin/doctrine-migrations version 'MyProject\Migrations\Version20180601193057' --add
  236. Or you can delete that version:
  237. .. code-block:: sh
  238. $ ./vendor/bin/doctrine-migrations version 'MyProject\Migrations\Version20180601193057' --delete
  239. This command does not actually execute any migrations, it just adds or deletes the version from the version table where
  240. we track whether or not a migration version has been executed or not.
  241. :ref:`Next Chapter: Generating Migrations <generating-migrations>`