custom-configuration.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. Custom Configuration
  2. ====================
  3. It is possible to build a custom configuration where you manually build the ``Doctrine\Migrations\Configuration\Configuration``
  4. instance instead of using YAML, XML, etc. In order to do this, you will need to setup a :ref:`Custom Integration <custom-integration>`.
  5. Once you have your custom integration setup, you can modify it to look like the following:
  6. .. code-block:: php
  7. #!/usr/bin/env php
  8. <?php
  9. require_once __DIR__.'/vendor/autoload.php';
  10. use Doctrine\DBAL\DriverManager;
  11. use Doctrine\Migrations\Configuration\Configuration;
  12. use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
  13. use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
  14. use Doctrine\Migrations\DependencyFactory;
  15. use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
  16. use Doctrine\Migrations\Tools\Console\Command;
  17. use Symfony\Component\Console\Application;
  18. $dbParams = [
  19. 'dbname' => 'migrations_docs_example',
  20. 'user' => 'root',
  21. 'password' => '',
  22. 'host' => 'localhost',
  23. 'driver' => 'pdo_mysql',
  24. ];
  25. $connection = DriverManager::getConnection($dbParams);
  26. $configuration = new Configuration($connection);
  27. $configuration->addMigrationsDirectory('MyProject\Migrations', '/data/doctrine/migrations-docs-example/lib/MyProject/Migrations');
  28. $configuration->setAllOrNothing(true);
  29. $configuration->setCheckDatabasePlatform(false);
  30. $storageConfiguration = new TableMetadataStorageConfiguration();
  31. $storageConfiguration->setTableName('doctrine_migration_versions');
  32. $configuration->setMetadataStorageConfiguration($storageConfiguration);
  33. $dependencyFactory = DependencyFactory::fromConnection(
  34. new ExistingConfiguration($configuration),
  35. new ExistingConnection($connection)
  36. );
  37. $cli = new Application('Doctrine Migrations');
  38. $cli->setCatchExceptions(true);
  39. $cli->addCommands(array(
  40. new Command\DumpSchemaCommand($dependencyFactory),
  41. new Command\ExecuteCommand($dependencyFactory),
  42. new Command\GenerateCommand($dependencyFactory),
  43. new Command\LatestCommand($dependencyFactory),
  44. new Command\ListCommand($dependencyFactory),
  45. new Command\MigrateCommand($dependencyFactory),
  46. new Command\RollupCommand($dependencyFactory),
  47. new Command\StatusCommand($dependencyFactory),
  48. new Command\SyncMetadataCommand($dependencyFactory),
  49. new Command\VersionCommand($dependencyFactory),
  50. ));
  51. $cli->run();
  52. :ref:`Next Chapter: Migrations Events <events>`
  53. It is possible to use multiple entity managers or connections, this is a way to configure your application:
  54. .. code-block:: php
  55. #!/usr/bin/env php
  56. <?php
  57. require_once __DIR__.'/vendor/autoload.php';
  58. use Doctrine\DBAL\DriverManager;
  59. use Doctrine\Migrations\Configuration\Configuration;
  60. use Doctrine\Migrations\Configuration\Configuration\ExistingConfiguration;
  61. use Doctrine\Migrations\Configuration\Connection\ConnectionRegistryConnection;
  62. use Doctrine\Migrations\DependencyFactory;
  63. use Doctrine\Migrations\Tools\Console\Command;
  64. use Symfony\Component\Console\Application;
  65. $connection1 = DriverManager::getConnection([...]);
  66. $connection2 = DriverManager::getConnection([...]);
  67. $connectionRegistry = new class(
  68. 'some_registry',
  69. ['foo' => $connection1, 'bar' => $connection2],
  70. [], // entity managers
  71. 'foo', // default connection
  72. null, // default entity manager
  73. 'Doctrine\Persistence\Proxy' // proxy class
  74. ) extends AbstractManagerRegistry {
  75. // implement abstract methods here
  76. };
  77. $configuration = new Configuration($connection);
  78. $configuration->addMigrationsDirectory('MyProject\Migrations', 'some path');
  79. $configurationLoader = new ExistingConfiguration($configuration);
  80. $connectionLoader = ConnectionRegistryConnection::withSimpleDefault($connectionRegistry);
  81. $dependencyFactory = DependencyFactory::fromConnection(
  82. $configurationLoader,
  83. $connectionLoader
  84. );
  85. $cli = new Application('Doctrine Migrations');
  86. $cli->setCatchExceptions(true);
  87. $cli->addCommands(array(
  88. new Command\MigrateCommand($dependencyFactory),
  89. // more commands here
  90. ));
  91. $cli->run();
  92. With this configuration you can use the ``--conn`` parameter to specify a connection that will be used for running
  93. migrations. If the parameter is not passed, it will fallback to the one passed in the configuration,
  94. and if that is also not provided it will fallback to the default connection name specified when creating
  95. the connection registry.
  96. Custom migration template
  97. -------------------------
  98. When the default generated migrations do not suit your needs, you may provide a custom migration template that will
  99. be used to generate future migrations.
  100. For example, if you don't need a ``down`` migration your template could look like this:
  101. .. code-block:: php
  102. <?php
  103. declare(strict_types=1);
  104. namespace <namespace>;
  105. use Doctrine\DBAL\Schema\Schema;
  106. use Doctrine\Migrations\AbstractMigration;
  107. final class <className> extends AbstractMigration
  108. {
  109. public function up(Schema $schema): void
  110. {
  111. <up>
  112. }
  113. }
  114. Placeholders (words inside ``< >``) are replaced with correct code upon generation. All possible wildcards are:
  115. =============== ===============================================
  116. Placeholder Description
  117. --------------- -----------------------------------------------
  118. ``<namespace>`` Namespace of the class, e.g. ``App\Migrations``
  119. ``<className>`` Classname, e.g. ``Version20210212092730``
  120. ``<up>`` SQL for migrating up
  121. ``<down>`` SQL for migrating down
  122. =============== ===============================================
  123. The custom template needs to be configured.
  124. .. code-block:: php
  125. $configuration->setCustomTemplate(__DIR__ . '/custom_template.tpl');