From ae343fe3bdd5d43f0d931761ce1746d7aed61e80 Mon Sep 17 00:00:00 2001 From: "Jonathan H. Wage" Date: Tue, 5 Jun 2018 03:17:40 +0100 Subject: [PATCH] Add new configuration options. --- Command/DoctrineCommand.php | 5 ++ DependencyInjection/Configuration.php | 4 ++ Resources/doc/index.rst | 86 ++++++++++++++++++--------- Tests/Command/DoctrineCommandTest.php | 24 ++++++++ 4 files changed, 92 insertions(+), 27 deletions(-) diff --git a/Command/DoctrineCommand.php b/Command/DoctrineCommand.php index 38261db..977bebe 100644 --- a/Command/DoctrineCommand.php +++ b/Command/DoctrineCommand.php @@ -81,6 +81,11 @@ public static function configureMigrations(ContainerInterface $container, Config $configuration->setMigrationsTableName($container->getParameter('doctrine_migrations.table_name')); } + $configuration->setMigrationsColumnName($container->getParameter('doctrine_migrations.column_name')); + $configuration->setMigrationsColumnLength($container->getParameter('doctrine_migrations.column_length')); + $configuration->setMigrationsExecutedAtColumnName($container->getParameter('doctrine_migrations.executed_at_column_name')); + $configuration->setAllOrNothing($container->getParameter('doctrine_migrations.all_or_nothing')); + // Migrations is not register from configuration loader if (! ($configuration instanceof AbstractFileConfiguration)) { $migrationsDirectory = $configuration->getMigrationsDirectory(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ef9b0d0..29b8a4a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -36,6 +36,10 @@ public function getConfigTreeBuilder() : TreeBuilder ->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end() ->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end() ->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end() + ->scalarNode('column_name')->defaultValue('version')->end() + ->scalarNode('column_length')->defaultValue(255)->end() + ->scalarNode('executed_at_column_name')->defaultValue('executed_at')->end() + ->scalarNode('all_or_nothing')->defaultValue(false)->end() ->scalarNode('name')->defaultValue('Application Migrations')->end() ->scalarNode('custom_template')->defaultValue(null)->end() ->scalarNode('organize_migrations')->defaultValue(false) diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst index 95b9d2d..c6c2bac 100644 --- a/Resources/doc/index.rst +++ b/Resources/doc/index.rst @@ -54,12 +54,16 @@ You can configure the path, namespace, table_name, name, organize_migrations and # app/config/config.yml doctrine_migrations: - dir_name: "%kernel.root_dir%/DoctrineMigrations" - namespace: Application\Migrations - table_name: migration_versions + dir_name: "%kernel.root_dir%/Migrations" + namespace: "DoctrineMigrations" + table_name: "migration_versions" + column_name: "version" + column_length: 14 + executed_at_column_name: "executed_at" name: Application Migrations organize_migrations: false # Version >= 1.2, possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false custom_template: ~ # Version >= 1.2, path to your custom migrations template + all_or_nothing: false Usage ----- @@ -87,18 +91,25 @@ the ``status`` command: .. code-block:: bash - php app/console doctrine:migrations:status + $ php app/console doctrine:migrations:status == Configuration >> Name: Application Migrations + >> Database Driver: pdo_mysql + >> Database Host: 127.0.0.1 + >> Database Name: symfony_migrations >> Configuration Source: manually configured >> Version Table Name: migration_versions - >> Migrations Namespace: Application\Migrations - >> Migrations Directory: /path/to/project/app/DoctrineMigrations + >> Version Column Name: version + >> Migrations Namespace: DoctrineMigrations + >> Migrations Directory: /path/to/project/app/Migrations + >> Previous Version: Already at first version >> Current Version: 0 + >> Next Version: Already at latest version >> Latest Version: 0 >> Executed Migrations: 0 + >> Executed Unavailable Migrations: 0 >> Available Migrations: 0 >> New Migrations: 0 @@ -109,25 +120,41 @@ for you. .. code-block:: bash $ php app/console doctrine:migrations:generate - Generated new migration class to "/path/to/project/app/DoctrineMigrations/Version20100621140655.php" + Generated new migration class to "/path/to/project/app/Migrations/Version20180605025653.php" + + To run just this migration for testing purposes, you can use migrations:execute --up 20180605025653 + + To revert the migration you can use migrations:execute --down 20180605025653 Have a look at the newly generated migration class and you will see something like the following:: - namespace Application\Migrations; + declare(strict_types=1); + + namespace DoctrineMigrations; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; - class Version20100621140655 extends AbstractMigration + /** + * Auto-generated Migration: Please modify to your needs! + */ + final class Version20180605025653 extends AbstractMigration { + public function getDescription() : string + { + return ''; + } + public function up(Schema $schema) : void { + // this up() migration is auto-generated, please modify it to your needs } public function down(Schema $schema) : void { + // this down() migration is auto-generated, please modify it to your needs } } @@ -141,27 +168,34 @@ migration to execute: == Configuration - >> Name: Application Migrations - >> Configuration Source: manually configured - >> Version Table Name: migration_versions - >> Migrations Namespace: Application\Migrations - >> Migrations Directory: /path/to/project/app/DoctrineMigrations - >> Current Version: 0 - >> Latest Version: 2010-06-21 14:06:55 (20100621140655) - >> Executed Migrations: 0 - >> Available Migrations: 1 - >> New Migrations: 1 + >> Name: Application Migrations + >> Database Driver: pdo_mysql + >> Database Host: 127.0.0.1 + >> Database Name: symfony_migrations + >> Configuration Source: manually configured + >> Version Table Name: migration_versions + >> Version Column Name: version + >> Migrations Namespace: DoctrineMigrations + >> Migrations Directory: /path/to/project/app/Migrations + >> Previous Version: Already at first version + >> Current Version: 0 + >> Next Version: 2018-06-05 02:56:53 (20180605025653) + >> Latest Version: 2018-06-05 02:56:53 (20180605025653) + >> Executed Migrations: 0 + >> Executed Unavailable Migrations: 0 + >> Available Migrations: 1 + >> New Migrations: 1 - == Migration Versions + == Available Migration Versions - >> 2010-06-21 14:06:55 (20100621140655) not migrated + >> 2018-06-05 02:56:53 (20180605025653) not migrated Now you can add some migration code to the ``up()`` and ``down()`` methods and finally migrate when you're ready: .. code-block:: bash - $ php app/console doctrine:migrations:migrate 20100621140655 + $ php app/console doctrine:migrations:migrate 20180605025653 For more information on how to write the migrations themselves (i.e. how to fill in the ``up()`` and ``down()`` methods), see the official Doctrine Migrations @@ -196,7 +230,6 @@ You can skip single migrations by explicitely adding them to the ``migration_ver Doctrine will then assume that this migration has already been run and will ignore it. - Generating Migrations Automatically ----------------------------------- @@ -228,13 +261,12 @@ for Doctrine's ORM: * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ - protected $id; + private $id; /** * @ORM\Column(type="string", length=255) */ - protected $name; - } + private $name; .. code-block:: yaml @@ -406,6 +438,6 @@ This ignores the tables on the DBAL level and they will be ignored by the diff c Note that if you have multiple connections configured then the ``schema_filter`` configuration will need to be placed per-connection. -.. _documentation: http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/index.html +.. _documentation: https://www.doctrine-project.org/projects/doctrine-migrations/en/current/index.html .. _DoctrineMigrationsBundle: https://github.com/doctrine/DoctrineMigrationsBundle .. _`Doctrine Database Migrations`: https://github.com/doctrine/migrations diff --git a/Tests/Command/DoctrineCommandTest.php b/Tests/Command/DoctrineCommandTest.php index 77fe28d..53e2849 100644 --- a/Tests/Command/DoctrineCommandTest.php +++ b/Tests/Command/DoctrineCommandTest.php @@ -48,10 +48,30 @@ public function testConfigureMigrations() : void ->method('setMigrationsTableName') ->with('test'); + $configurationMock->expects($this->once()) + ->method('setMigrationsColumnName') + ->with('version'); + + $configurationMock->expects($this->once()) + ->method('setMigrationsColumnLength') + ->with(255); + + $configurationMock->expects($this->once()) + ->method('setMigrationsColumnLength') + ->with(255); + + $configurationMock->expects($this->once()) + ->method('setMigrationsExecutedAtColumnName') + ->with('executed_at'); + $configurationMock->expects($this->once()) ->method('setMigrationsAreOrganizedByYear') ->with(true); + $configurationMock->expects($this->once()) + ->method('setAllOrNothing') + ->with(false); + DoctrineCommand::configureMigrations($this->getContainer(), $configurationMock); } @@ -62,8 +82,12 @@ private function getContainer() : ContainerBuilder 'doctrine_migrations.namespace' => 'test', 'doctrine_migrations.name' => 'test', 'doctrine_migrations.table_name' => 'test', + 'doctrine_migrations.column_name' => 'version', + 'doctrine_migrations.column_length' => 255, + 'doctrine_migrations.executed_at_column_name' => 'executed_at', 'doctrine_migrations.organize_migrations' => Configuration::VERSIONS_ORGANIZATION_BY_YEAR, 'doctrine_migrations.custom_template' => null, + 'doctrine_migrations.all_or_nothing' => false, ])); } }