Setup Doctrine2 Migrations and cli-config.php

After composer requiring doctrine/migrations, you need to edit your cli-config.php. Here’s mine, with the new stuff added in bold.

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use XYZ\XYZService;
use XYZ\Test\XYZTesting;

// This is just a dependency injection container
$container = XYZTesting::getContainer();

/* Edit these details to suit

$container['db.credentials'] = array(
    'driver' => 'pdo_mysql',
    'dbname' => 'twg',
    'user' => 'dbuser',
    'password' => '123',
];
 */

// this is just my service which returns the entitymanager (requires the DIC above)
$svc = new XYZService($container);
$em = $svc->getEntityManager();
$helperSet = ConsoleRunner::createHelperSet($em);

// Add Doctrine Migration commands
$cli = ConsoleRunner::createApplication($helperSet,[
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
    new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand(),
]);

return $cli->run();

Note that I have removed the last line, return ConsoleRunner::createHelperSet($entityManager); and replaced it with return $cli->run(); Now if you type doctrine:

$ doctrine
Doctrine Command Line Interface version 2.5.1

Usage:
command [options] [arguments]

Options:
-h, --help            Display this help message
-q, --quiet           Do not output any message
-V, --version         Display this application version
--ansi            Force ANSI output
--no-ansi         Disable ANSI output
-n, --no-interaction  Do not ask any interactive question
-v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
help                            Displays help for a command
list                            Lists commands
dbal
dbal:import                     Import SQL file(s) directly to Database.
dbal:run-sql                    Executes arbitrary SQL directly from the command line.
migrations
migrations:diff                 Generate a migration by comparing your current database to your mapping information.
migrations:execute              Execute a single migration version up or down manually.
migrations:generate             Generate a blank migration class.
migrations:migrate              Execute a migration to a specified version or the latest available version.
migrations:status               View the status of a set of migrations.
migrations:version              Manually add and delete migration versions from the version table.
orm
orm:clear-cache:metadata        Clear all metadata cache of the various cache drivers.
orm:clear-cache:query           Clear all query cache of the various cache drivers.
orm:clear-cache:result          Clear all result cache of the various cache drivers.
orm:convert-d1-schema           Converts Doctrine 1.X schema into a Doctrine 2.X schema.
orm:convert-mapping             Convert mapping information between supported formats.
orm:convert:d1-schema           Converts Doctrine 1.X schema into a Doctrine 2.X schema.
orm:convert:mapping             Convert mapping information between supported formats.
orm:ensure-production-settings  Verify that Doctrine is properly configured for a production environment.
orm:generate-entities           Generate entity classes and method stubs from your mapping information.
orm:generate-proxies            Generates proxy classes for entity classes.
orm:generate-repositories       Generate repository classes from your mapping information.
orm:generate:entities           Generate entity classes and method stubs from your mapping information.
orm:generate:proxies            Generates proxy classes for entity classes.
orm:generate:repositories       Generate repository classes from your mapping information.
orm:info                        Show basic information about all mapped entities
orm:mapping:describe            Display information about mapped objects
orm:run-dql                     Executes arbitrary DQL directly from the command line.
orm:schema-tool:create          Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.
orm:schema-tool:drop            Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output.
orm:schema-tool:update          Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata.
orm:validate-schema             Validate the mapping files.

Loads more commands! Now you can start migrating your database properly and safely 🙂 Have fun!

Update
After having run my migrations, it turns out it dumps the migration files in your doc root! After a bit of faffing about, I got the config working, so here is the full cli-config.php:

<?php

use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use XYZ\XYZService;
use XYZ\Test\XYZTesting;

$container = XYZTesting::getContainer();

/* Edit these details to suit

$container['db.credentials'] = array(
    'driver' => 'pdo_mysql',
    'dbname' => 'XYZ',
    'user' => 'dbuser',
    'password' => '123',
];
 */

// Fetch the entity Manager
$svc = new XYZService($container);
$em = $svc->getEntityManager();

// Create the helperset
$helperSet = ConsoleRunner::createHelperSet($em);

/** Migrations setup */

$configuration = new Configuration($em->getConnection());
$configuration->setMigrationsDirectory('migrations');

$diff = new DiffCommand();
$exec = new ExecuteCommand();
$gen = new GenerateCommand();
$migrate = new MigrateCommand();
$status = new StatusCommand();
$ver = new VersionCommand();

$diff->setMigrationConfiguration($configuration);


$cli = ConsoleRunner::createApplication($helperSet,[
    $diff, $exec, $gen, $migrate, $status, $ver
]);

return $cli->run();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s