configuration.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. Configuration
  2. =============
  3. So you are ready to start configuring your migrations? We just need to provide
  4. a few bits of information for the console application in order to get started.
  5. Migrations Configuration
  6. ------------------------
  7. First we need to configure information about your migrations. In ``/data/doctrine/migrations-docs-example``
  8. go ahead and create a folder to store your migrations in:
  9. .. code-block:: sh
  10. $ mkdir -p lib/MyProject/Migrations
  11. Now, in the root of your project place a file named ``migrations.php``, ``migrations.yml``,
  12. ``migrations.xml`` or ``migrations.json`` and place the following contents:
  13. .. configuration-block::
  14. .. code-block:: php
  15. <?php
  16. return [
  17. 'table_storage' => [
  18. 'table_name' => 'doctrine_migration_versions',
  19. 'version_column_name' => 'version',
  20. 'version_column_length' => 1024,
  21. 'executed_at_column_name' => 'executed_at',
  22. 'execution_time_column_name' => 'execution_time',
  23. ],
  24. 'migrations_paths' => [
  25. 'MyProject\Migrations' => '/data/doctrine/migrations/lib/MyProject/Migrations',
  26. 'MyProject\Component\Migrations' => './Component/MyProject/Migrations',
  27. ],
  28. 'all_or_nothing' => true,
  29. 'check_database_platform' => true,
  30. 'organize_migrations' => 'none',
  31. 'connection' => null,
  32. 'em' => null,
  33. ];
  34. .. code-block:: yaml
  35. table_storage:
  36. table_name: doctrine_migration_versions
  37. version_column_name: version
  38. version_column_length: 1024
  39. executed_at_column_name: executed_at
  40. execution_time_column_name: execution_time
  41. migrations_paths:
  42. 'MyProject\Migrations': /data/doctrine/migrations/lib/MyProject/Migrations
  43. 'MyProject\Component\Migrations': ./Component/MyProject/Migrations
  44. all_or_nothing: true
  45. check_database_platform: true
  46. organize_migrations: none
  47. connection: null
  48. em: null
  49. .. code-block:: xml
  50. <?xml version="1.0" encoding="UTF-8"?>
  51. <doctrine-migrations xmlns="http://doctrine-project.org/schemas/migrations/configuration/3.0"
  52. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  53. xsi:schemaLocation="http://doctrine-project.org/schemas/migrations/configuration/3.0
  54. http://doctrine-project.org/schemas/migrations/configuration-3.0.xsd">
  55. <connection>default</connection>
  56. <em>default</em>
  57. <storage>
  58. <table-storage
  59. table-name="doctrine_migration_versions"
  60. version-column-name="version"
  61. version-column-length="1024"
  62. executed-at-column-name="executed_at"
  63. execution-time-column-name="execution_time"
  64. />
  65. </storage>
  66. <migrations-paths>
  67. <path namespace="MyProject\Migrations">/data/doctrine/migrations/lib/MyProject/Migrations</path>
  68. <path namespace="MyProject\Component\Migrations">./Component/MyProject/Migrations</path>
  69. </migrations-paths>
  70. <all-or-nothing>true</all-or-nothing>
  71. <check-database-platform>true</check-database-platform>
  72. <organize_migrations>none</organize_migrations>
  73. </doctrine-migrations>
  74. .. code-block:: json
  75. {
  76. "table_storage": {
  77. "table_name": "doctrine_migration_versions",
  78. "version_column_name": "version",
  79. "version_column_length": 1024,
  80. "executed_at_column_name": "executed_at",
  81. "execution_time_column_name": "execution_time"
  82. },
  83. "migrations_paths": {
  84. "MyProject\Migrations": "/data/doctrine/migrations/lib/MyProject/Migrations",
  85. "MyProject\Component\Migrations": "./Component/MyProject/Migrations"
  86. },
  87. "all_or_nothing": true,
  88. "check_database_platform": true,
  89. "organize_migrations": "none"
  90. "connection": null,
  91. "em": null
  92. }
  93. Please note that if you want to use the YAML configuration option, you will need to install the ``symfony/yaml`` package with composer:
  94. .. code-block:: sh
  95. composer require symfony/yaml
  96. Here are details about what each configuration option does:
  97. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  98. | Name | Required | Default | Description |
  99. +============================+============+==============================+==================================================================================+
  100. | migrations_paths<string, string> | yes | null | The PHP namespace your migration classes are located under and the path to a directory where to look for migration classes. |
  101. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  102. | table_storage | no | | Used by doctrine migrations to track the currently executed migrations |
  103. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  104. | all_or_nothing | no | false | Whether or not to wrap multiple migrations in a single transaction. |
  105. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  106. | migrations | no | [] | Manually specify the array of migration versions instead of finding migrations. |
  107. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  108. | check_database_platform | no | true | Whether to add a database platform check at the beginning of the generated code. |
  109. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  110. | organize_migrations | no | ``none`` | Whether to organize migration classes under year (``year``) or year and month (``year_and_month``) subdirectories. |
  111. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  112. | connection | no | null | The named connection to use (available only when ConnectionRegistryConnection is used). |
  113. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  114. | em | no | null | The named entity manager to use (available only when ManagerRegistryEntityManager is used). |
  115. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  116. Here the possible options for ``table_storage``:
  117. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  118. | Name | Required | Default | Description |
  119. +============================+============+==============================+==================================================================================+
  120. | table_name | no | doctrine_migration_versions | The name of the table to track executed migrations in. |
  121. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  122. | version_column_name | no | version | The name of the column which stores the version name. |
  123. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  124. | version_column_length | no | 1024 | The length of the column which stores the version name. |
  125. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  126. | executed_at_column_name | no | executed_at | The name of the column which stores the date that a migration was executed. |
  127. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  128. | execution_time_column_name | no | execution_time | The name of the column which stores how long a migration took (milliseconds). |
  129. +----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
  130. Manually Providing Migrations
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. If you don't want to rely on Doctrine finding your migrations, you can explicitly specify the array of migration
  133. classes using the ``migrations`` configuration setting:
  134. .. configuration-block::
  135. .. code-block:: php
  136. <?php
  137. return [
  138. // ..
  139. 'migrations' => [
  140. 'MyProject\Migrations\NewMigration',
  141. ],
  142. ];
  143. .. code-block:: yaml
  144. // ...
  145. migrations:
  146. - "MyProject\Migrations\NewMigration"
  147. .. code-block:: xml
  148. <?xml version="1.0" encoding="UTF-8"?>
  149. <doctrine-migrations xmlns="http://doctrine-project.org/schemas/migrations/configuration"
  150. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  151. xsi:schemaLocation="http://doctrine-project.org/schemas/migrations/configuration
  152. http://doctrine-project.org/schemas/migrations/configuration.xsd">
  153. // ...
  154. <migrations>
  155. <migration class="MyProject\Migrations\NewMigration" />
  156. </migrations>
  157. </doctrine-migrations>
  158. .. code-block:: json
  159. {
  160. // ...
  161. "migrations": [
  162. "DoctrineMigrations\NewMigration"
  163. ]
  164. }
  165. All or Nothing Transaction
  166. --------------------------
  167. .. note::
  168. This only works if your database supports transactions for DDL statements.
  169. When using the ``all_or_nothing`` option, multiple migrations ran at the same time will be wrapped in a single
  170. transaction. If one migration fails, all migrations will be rolled back
  171. From the Command Line
  172. ~~~~~~~~~~~~~~~~~~~~~
  173. You can also set this option from the command line with the ``migrate`` command and the ``--all-or-nothing`` option:
  174. .. code-block:: sh
  175. $ ./vendor/bin/doctrine-migrations migrate --all-or-nothing
  176. If you have it enabled at the configuration level and want to change it for an individual migration you can
  177. pass a value of ``0`` or ``1`` to ``--all-or-nothing``.
  178. .. code-block:: sh
  179. $ ./vendor/bin/doctrine-migrations migrate --all-or-nothing=0
  180. Connection Configuration
  181. ------------------------
  182. Now that we've configured our migrations, the next thing we need to configure is how the migrations console
  183. application knows how to get the connection to use for the migrations:
  184. Simple
  185. ~~~~~~
  186. The simplest configuration is to put a ``migrations-db.php`` file in the root of your
  187. project and return an array of connection information that can be passed to the DBAL:
  188. .. code-block:: php
  189. <?php
  190. return [
  191. 'dbname' => 'migrations_docs_example',
  192. 'user' => 'root',
  193. 'password' => '',
  194. 'host' => 'localhost',
  195. 'driver' => 'pdo_mysql',
  196. ];
  197. You will need to make sure the ``migrations_docs_example`` database exists. If you are using MySQL you can create it with
  198. the following command:
  199. .. code-block:: sh
  200. $ mysqladmin create migrations_docs_example
  201. If you have already a DBAL connection available in your application, ``migrations-db.php`` can return it directly:
  202. .. code-block:: php
  203. <?php
  204. use Doctrine\DBAL\DriverManager;
  205. return DriverManager::getConnection([
  206. 'dbname' => 'migrations_docs_example',
  207. 'user' => 'root',
  208. 'password' => '',
  209. 'host' => 'localhost',
  210. 'driver' => 'pdo_mysql',
  211. ]);
  212. Advanced
  213. ~~~~~~~~
  214. If you require a more advanced configuration and you want to get the connection to use
  215. from your existing application setup then you can use this method of configuration.
  216. In the root of your project, place a file named ``cli-config.php`` with the following
  217. contents. It can also be placed in a folder named ``config`` if you prefer to keep it
  218. out of the root of your project.
  219. .. code-block:: php
  220. <?php
  221. require 'vendor/autoload.php';
  222. use Doctrine\DBAL\DriverManager;
  223. use Doctrine\Migrations\Configuration\Configuration\PhpFile;
  224. use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
  225. use Doctrine\Migrations\DependencyFactory;
  226. $config = new PhpFile('migrations.php'); // Or use one of the Doctrine\Migrations\Configuration\Configuration\* loaders
  227. $conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
  228. return DependencyFactory::fromConnection($config, new ExistingConnection($conn));
  229. The above setup assumes you are not using the ORM. If you want to use the ORM, first require it in your project
  230. with composer:
  231. .. code-block:: sh
  232. composer require doctrine/orm
  233. Now update your ``cli-config.php`` in the root of your project to look like the following:
  234. .. code-block:: php
  235. <?php
  236. require 'vendor/autoload.php';
  237. use Doctrine\ORM\EntityManager;
  238. use Doctrine\ORM\Tools\Setup;
  239. use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
  240. use Doctrine\Migrations\DependencyFactory;
  241. $config = new PhpFile('migrations.php'); // Or use one of the Doctrine\Migrations\Configuration\Configuration\* loaders
  242. $paths = [__DIR__.'/lib/MyProject/Entities'];
  243. $isDevMode = true;
  244. $ORMconfig = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
  245. $entityManager = EntityManager::create(['driver' => 'pdo_sqlite', 'memory' => true], $ORMconfig);
  246. return DependencyFactory::fromEntityManager($config, new ExistingEntityManager($entityManager));
  247. Make sure to create the directory where your ORM entities will be located:
  248. .. code-block:: sh
  249. $ mkdir lib/MyProject/Entities
  250. :ref:`Next Chapter: Migration Classes <migration-classes>`