New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating DoctrineMigrationsBundle for Doctrine Migrations 2.0 #228
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
composer.lock | ||
/vendor/ | ||
/.phpcs-cache | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
build: | ||
nodes: | ||
analysis: | ||
environment: | ||
php: | ||
version: 7.1 | ||
cache: | ||
disabled: false | ||
directories: | ||
- ~/.composer/cache | ||
project_setup: | ||
override: true | ||
tests: | ||
override: | ||
- php-scrutinizer-run | ||
- phpcs-run | ||
dependencies: | ||
override: | ||
- composer install --ignore-platform-reqs --no-interaction | ||
|
||
tools: | ||
external_code_coverage: | ||
timeout: 600 | ||
|
||
build_failure_conditions: | ||
- 'elements.rating(<= C).new.exists' # No new classes/methods with a rating of C or worse allowed | ||
- 'issues.label("coding-style").new.exists' # No new coding style issues allowed | ||
- 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity | ||
- 'project.metric_change("scrutinizer.test_coverage", < 0)' # Code Coverage decreased from previous inspection | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\MigrationsBundle\Command; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand as BaseCommand; | ||
use Doctrine\DBAL\Migrations\Configuration\AbstractFileConfiguration; | ||
use Doctrine\DBAL\Migrations\Configuration\Configuration; | ||
use Doctrine\Migrations\Configuration\AbstractFileConfiguration; | ||
use Doctrine\Migrations\Configuration\Configuration; | ||
use Doctrine\Migrations\Version\Version; | ||
use ErrorException; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use function error_get_last; | ||
use function is_dir; | ||
use function method_exists; | ||
use function mkdir; | ||
use function preg_match; | ||
use function sprintf; | ||
use function str_replace; | ||
|
||
/** | ||
* Base class for Doctrine console commands to extend from. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
abstract class DoctrineCommand extends BaseCommand | ||
{ | ||
public static function configureMigrations(ContainerInterface $container, Configuration $configuration) | ||
public static function configureMigrations(ContainerInterface $container, Configuration $configuration) : void | ||
{ | ||
if (!$configuration->getMigrationsDirectory()) { | ||
$dir = $configuration->getMigrationsDirectory(); | ||
|
||
if (empty($dir)) { | ||
$dir = $container->getParameter('doctrine_migrations.dir_name'); | ||
if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) { | ||
|
||
if (! is_dir($dir) && ! @mkdir($dir, 0777, true) && ! is_dir($dir)) { | ||
$error = error_get_last(); | ||
throw new \ErrorException($error['message']); | ||
|
||
throw new ErrorException(sprintf( | ||
'Failed to create directory "%s" with message "%s"', | ||
$dir, | ||
$error['message'] | ||
)); | ||
} | ||
|
||
$configuration->setMigrationsDirectory($dir); | ||
} else { | ||
$dir = $configuration->getMigrationsDirectory(); | ||
// class Kernel has method getKernelParameters with some of the important path parameters | ||
$pathPlaceholderArray = array('kernel.root_dir', 'kernel.cache_dir', 'kernel.logs_dir'); | ||
$pathPlaceholderArray = ['kernel.root_dir', 'kernel.cache_dir', 'kernel.logs_dir']; | ||
|
||
foreach ($pathPlaceholderArray as $pathPlaceholder) { | ||
if ($container->hasParameter($pathPlaceholder) && preg_match('/\%'.$pathPlaceholder.'\%/', $dir)) { | ||
$dir = str_replace('%'.$pathPlaceholder.'%', $container->getParameter($pathPlaceholder), $dir); | ||
if (! $container->hasParameter($pathPlaceholder) || ! preg_match('/\%' . $pathPlaceholder . '\%/', $dir)) { | ||
continue; | ||
} | ||
|
||
$dir = str_replace('%' . $pathPlaceholder . '%', $container->getParameter($pathPlaceholder), $dir); | ||
} | ||
if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) { | ||
|
||
if (! is_dir($dir) && ! @mkdir($dir, 0777, true) && ! is_dir($dir)) { | ||
$error = error_get_last(); | ||
throw new \ErrorException($error['message']); | ||
|
||
throw new ErrorException(sprintf( | ||
'Failed to create directory "%s" with message "%s"', | ||
$dir, | ||
$error['message'] | ||
)); | ||
} | ||
|
||
$configuration->setMigrationsDirectory($dir); | ||
} | ||
if (!$configuration->getMigrationsNamespace()) { | ||
|
||
if (empty($configuration->getMigrationsNamespace())) { | ||
$configuration->setMigrationsNamespace($container->getParameter('doctrine_migrations.namespace')); | ||
} | ||
if (!$configuration->getName()) { | ||
|
||
if (empty($configuration->getName())) { | ||
$configuration->setName($container->getParameter('doctrine_migrations.name')); | ||
} | ||
|
||
// For backward compatibility, need use a table from parameters for overwrite the default configuration | ||
if (!($configuration instanceof AbstractFileConfiguration) || !$configuration->getMigrationsTableName()) { | ||
if (! ($configuration instanceof AbstractFileConfiguration) || empty($configuration->getMigrationsTableName())) { | ||
$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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these ones should check for an explicit config file, as done for the table name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure I understand what you mean. You mean how the others have an |
||
$configuration->setAllOrNothing($container->getParameter('doctrine_migrations.all_or_nothing')); | ||
|
||
// Migrations is not register from configuration loader | ||
if (!($configuration instanceof AbstractFileConfiguration)) { | ||
$configuration->registerMigrationsFromDirectory($configuration->getMigrationsDirectory()); | ||
if (! ($configuration instanceof AbstractFileConfiguration)) { | ||
$migrationsDirectory = $configuration->getMigrationsDirectory(); | ||
|
||
if ($migrationsDirectory !== null) { | ||
$configuration->registerMigrationsFromDirectory($migrationsDirectory); | ||
} | ||
} | ||
|
||
if (method_exists($configuration, 'getCustomTemplate') && !$configuration->getCustomTemplate()) { | ||
if (method_exists($configuration, 'getCustomTemplate') && empty($configuration->getCustomTemplate())) { | ||
$configuration->setCustomTemplate($container->getParameter('doctrine_migrations.custom_template')); | ||
} | ||
|
||
$organizeMigrations = $container->getParameter('doctrine_migrations.organize_migrations'); | ||
|
||
switch ($organizeMigrations) { | ||
case Configuration::VERSIONS_ORGANIZATION_BY_YEAR: | ||
$configuration->setMigrationsAreOrganizedByYear(true); | ||
|
@@ -81,18 +121,19 @@ public static function configureMigrations(ContainerInterface $container, Config | |
} | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* @param array $versions | ||
* @param Version[] $versions | ||
* | ||
* Injects the container to migrations aware of it | ||
*/ | ||
private static function injectContainerToMigrations(ContainerInterface $container, array $versions) | ||
private static function injectContainerToMigrations(ContainerInterface $container, array $versions) : void | ||
{ | ||
foreach ($versions as $version) { | ||
$migration = $version->getMigration(); | ||
if ($migration instanceof ContainerAwareInterface) { | ||
$migration->setContainer($container); | ||
if (! ($migration instanceof ContainerAwareInterface)) { | ||
continue; | ||
} | ||
|
||
$migration->setContainer($container); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\MigrationsBundle\Command\Helper; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper as BaseDoctrineCommandHelper; | ||
use Doctrine\Bundle\DoctrineBundle\Registry; | ||
use Doctrine\DBAL\Sharding\PoolingShardConnection; | ||
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; | ||
use LogicException; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use function count; | ||
use function sprintf; | ||
|
||
/** | ||
* Provides some helper and convenience methods to configure doctrine commands in the context of bundles | ||
* and multiple connections/entity managers. | ||
* | ||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> | ||
*/ | ||
abstract class DoctrineCommandHelper extends BaseDoctrineCommandHelper | ||
{ | ||
public static function setApplicationHelper(Application $application, InputInterface $input) | ||
public static function setApplicationHelper(Application $application, InputInterface $input) : void | ||
{ | ||
$container = $application->getKernel()->getContainer(); | ||
$doctrine = $container->get('doctrine'); | ||
|
||
/** @var Registry $doctrine */ | ||
$doctrine = $container->get('doctrine'); | ||
|
||
$managerNames = $doctrine->getManagerNames(); | ||
|
||
if ($input->getOption('db') || empty($managerNames)) { | ||
if ($input->getOption('db') !== null || count($managerNames) === 0) { | ||
self::setApplicationConnection($application, $input->getOption('db')); | ||
} else { | ||
self::setApplicationEntityManager($application, $input->getOption('em')); | ||
} | ||
|
||
if ($input->getOption('shard')) { | ||
$connection = $application->getHelperSet()->get('db')->getConnection(); | ||
if (!$connection instanceof PoolingShardConnection) { | ||
if (empty($managerNames)) { | ||
throw new \LogicException(sprintf("Connection '%s' must implement shards configuration.", $input->getOption('db'))); | ||
} else { | ||
throw new \LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $input->getOption('em'))); | ||
} | ||
if ($input->getOption('shard') === null) { | ||
return; | ||
} | ||
|
||
/** @var ConnectionHelper $dbHelper */ | ||
$dbHelper = $application->getHelperSet()->get('db'); | ||
|
||
$connection = $dbHelper->getConnection(); | ||
|
||
if (! $connection instanceof PoolingShardConnection) { | ||
if (count($managerNames) === 0) { | ||
throw new LogicException(sprintf( | ||
"Connection '%s' must implement shards configuration.", | ||
$input->getOption('db') | ||
)); | ||
} | ||
|
||
$connection->connect($input->getOption('shard')); | ||
throw new LogicException(sprintf( | ||
"Connection of EntityManager '%s' must implement shards configuration.", | ||
$input->getOption('em') | ||
)); | ||
} | ||
|
||
$connection->connect($input->getOption('shard')); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should cache only
$HOME/.composer/cache/files
, to avoid invalidating the Travis cache due to changes in the packagist metadata (cached in$HOME/.composer/cache/repo
). Packagist changes too often (every few minutes) to avoi this invalidation.