Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Issue #38 Multiple entity managers support #46

Open
wants to merge 1 commit into from
@pvanliefland
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? NA
Fixed tickets #38
License MIT

This PR is an attempt to solve #38 by allowing to specify a different migrations config per entity manager. This feature is targeted at people who use multiple databases for a single Symfony2 project.

As an example :

doctrine_migrations:
    em1:
        dir_name: %kernel.root_dir%/DoctrineMigrations/Em1
        namespace: Application\Migrations\Em1
    em2:
        dir_name: %kernel.root_dir%/DoctrineMigrations/Em2
        namespace: Application\Migrations\Em2

Notes :

  • This additional level of nesting in the config is totally optional, the standard way still works
  • An exception is thrown in 2 cases :
    • If the config file is defined on a per-entity manager basis but no --em option is provided
    • If there is no config entry defined for the provided --em option

Thoughts ?

@richsage

+1

@odino
Collaborator

+1

@ocramius what do you think? I guess a lot of people might be blocked by a simple feature like this one

@Ocramius
Owner

@odino the idea seems good, but I'm not maintaining nor know how this bundle was thought :)

@odino
Collaborator

ok, maybe @stof? :)

@bendavies

@stof can you at least give some feedback?

@odino
Collaborator

+1 :)

@newneon

Hi @pvanliefland I've incorporated your changes and added the feature to use a --configuration setting that points to a parameter entry of Symfony (and if that does not exist, still points to a file). We needed this, because we do use multiple entity managers and do not want to use fixed paths in the --configuration parameter (...production uses different paths :/ )

The branch is available here: https://github.com/cruisetravel/DoctrineMigrationsBundle and could be used as a PR, depening on the findings of @stof

Example configuration settings in parameters.yml:

doctrine_migrations:
  configs:
    content:
      file: /vagrant/app/config/migrations/content/configuration.yml
      em: content
@pvanliefland pvanliefland Issue #38 Multiple entity managers support
Added the possibility to configure migrations depending on
the entity manager
e86c137
@natelenart

+1.

Where's this at? I just hacked essentially the same fix into my apps and was going to submit a PR until I saw this one.

@richsage

Some feedback would be great on this issue. I'm using the patch in production and it works great - would be good if it could get merged in! @stof ?

@nikolajosipovic

A bit confused about this issue status. Would need the multiple databases & entity managers feature as well. Any idea whether that would be implemented any time soon?

@drgomesp

Can someone provide a feedback on the merge of this PR?

@jbbrunsveld

+1 Need multiple database support as well...

@nikolajosipovic

@stof Would be very nice to have the multiple em support within the migrations bundle, could it be merged in?

@odino
Collaborator

just so you know, this would be a good candidate for being integrated after 1.0.0 is released (see the updates in #80)

@sveriger

Works for me.

But:
The exception "If the config file is defined on a per-entity manager basis but no --em option is provided" is a little bit confusing, in case one of the entity managers is the default one.

I also run into problems by using "schema_filters". This could be documented as well.

@pvanliefland

Well I'm ok to update this PR, but I need clear instructions from the bundle maintainer. @odino, is it you? What do you think ?

@odino
Collaborator

@pvanliefland I already upvoted this before turning maintainer of this bundle :) so Im very happy to see this into master soon

next release will be an RC version then we go for 1.0 -- since development here is quite chilled it shouldnt take long. After that, this should go into master for people to test it.

I would also default to the default em if none is specified, can you do that change? :)

Cheers

@pvanliefland

Ok, I will take care of it in the next few days.

@cyk
cyk commented

Looking forward to this feature! Thank you. In the interim, I'm extending an abstract class that skips the migration if its entity manager's db doesn't match the connections db.

@boteeka

@pvanliefland, @odino Any progress on this one?

@odino
Collaborator

fyi #80

@pvanliefland would you be able to rebase this from the latest master?

@dmaicher

We also need this :+1:

@dvaes

please :)

@polidog

please :)

@tomcyr

+1 :)

@rivaros

This pull request will probably never be merged, as some other fixes were already merged into the trunk.
For example if you need to support multiple databases with migrations, you have to specify configuration file as a parameter.

php app/console doctrine:migrations:status --em={entity_manager} --configuration={path_to_yml_file}

where configuration file looks like:

name: Doctrine Postgres Migrations
migrations_namespace: Application\Migrations
table_name: migration_versions
migrations_directory: PostgreSqlMigrations

Migration directory is searched relatively to configuration file.
The whole approach is far not ideal, but this is what doctrine/migrations & symfony/doctrine-migrations-bundle authors decided to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 2, 2013
  1. @pvanliefland

    Issue #38 Multiple entity managers support

    pvanliefland authored
    Added the possibility to configure migrations depending on
    the entity manager
This page is out of date. Refresh to see the latest.
View
30 Command/DoctrineCommand.php
@@ -27,19 +27,37 @@
*/
abstract class DoctrineCommand extends BaseCommand
{
- public static function configureMigrations(ContainerInterface $container, Configuration $configuration)
+ public static function configureMigrations(ContainerInterface $container, Configuration $configuration, $em)
{
- $dir = $container->getParameter('doctrine_migrations.dir_name');
+ if ($container->hasParameter('doctrine_migrations.default_entity_manager')) {
+ $configurationPrefix = 'doctrine_migrations.default_entity_manager';
+ } elseif ($container->hasParameter('doctrine_migrations.' . $em)) {
+ $configurationPrefix = 'doctrine_migrations.' . $em;
+ } else {
+ if (null === $em) {
+ $message = 'There is no doctrine migrations configuration available for the default entity manager';
+ } else {
+ $message = sprintf(
+ 'There is no doctrine migrations configuration available for the %s entity manager',
+ $em
+ );
+ }
+ throw new \InvalidArgumentException($message);
+ }
+
+ $containerParameters = $container->getParameter($configurationPrefix);
+
+ $dir = $containerParameters['dir_name'];
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
- $configuration->setMigrationsNamespace($container->getParameter('doctrine_migrations.namespace'));
+ $configuration->setMigrationsNamespace($containerParameters['namespace']);
$configuration->setMigrationsDirectory($dir);
$configuration->registerMigrationsFromDirectory($dir);
- $configuration->setName($container->getParameter('doctrine_migrations.name'));
- $configuration->setMigrationsTableName($container->getParameter('doctrine_migrations.table_name'));
-
+ $configuration->setName($containerParameters['name']);
+ $configuration->setMigrationsTableName($containerParameters['table_name']);
+
self::injectContainerToMigrations($container, $configuration->getMigrations());
}
View
6 Command/MigrationsDiffDoctrineCommand.php
@@ -44,7 +44,11 @@ public function execute(InputInterface $input, OutputInterface $output)
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
- DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
+ DoctrineCommand::configureMigrations(
+ $this->getApplication()->getKernel()->getContainer(),
+ $configuration,
+ $input->getOption('em')
+ );
parent::execute($input, $output);
}
View
6 Command/MigrationsExecuteDoctrineCommand.php
@@ -43,7 +43,11 @@ public function execute(InputInterface $input, OutputInterface $output)
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
- DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
+ DoctrineCommand::configureMigrations(
+ $this->getApplication()->getKernel()->getContainer(),
+ $configuration,
+ $input->getOption('em')
+ );
parent::execute($input, $output);
}
View
6 Command/MigrationsGenerateDoctrineCommand.php
@@ -43,7 +43,11 @@ public function execute(InputInterface $input, OutputInterface $output)
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
- DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
+ DoctrineCommand::configureMigrations(
+ $this->getApplication()->getKernel()->getContainer(),
+ $configuration,
+ $input->getOption('em')
+ );
parent::execute($input, $output);
}
View
6 Command/MigrationsMigrateDoctrineCommand.php
@@ -43,7 +43,11 @@ public function execute(InputInterface $input, OutputInterface $output)
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
- DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
+ DoctrineCommand::configureMigrations(
+ $this->getApplication()->getKernel()->getContainer(),
+ $configuration,
+ $input->getOption('em')
+ );
parent::execute($input, $output);
}
View
6 Command/MigrationsStatusDoctrineCommand.php
@@ -43,7 +43,11 @@ public function execute(InputInterface $input, OutputInterface $output)
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
- DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
+ DoctrineCommand::configureMigrations(
+ $this->getApplication()->getKernel()->getContainer(),
+ $configuration,
+ $input->getOption('em')
+ );
parent::execute($input, $output);
}
View
6 Command/MigrationsVersionDoctrineCommand.php
@@ -43,7 +43,11 @@ public function execute(InputInterface $input, OutputInterface $output)
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
$configuration = $this->getMigrationConfiguration($input, $output);
- DoctrineCommand::configureMigrations($this->getApplication()->getKernel()->getContainer(), $configuration);
+ DoctrineCommand::configureMigrations(
+ $this->getApplication()->getKernel()->getContainer(),
+ $configuration,
+ $input->getOption('em')
+ );
parent::execute($input, $output);
}
View
17 DependencyInjection/Configuration.php
@@ -35,6 +35,23 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root('doctrine_migrations', 'array');
$rootNode
+ ->beforeNormalization()
+ ->ifTrue(function($v) {
+ if(empty($v)) {
+ return true;
+ }
+ $firstConfigValue = array_shift($v);
+
+ return !is_array($firstConfigValue);
+ })
+ ->then(function($v) {
+ $v = array('default_entity_manager' => $v);
+
+ return $v;
+ })
+ ->end()
+ ->useAttributeAsKey('name')
+ ->prototype('array')
->children()
->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end()
->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end()
Something went wrong with that request. Please try again.