Migrating multiple databases using doctrine2 with Symfony2

…衆ロ難τιáo~ 提交于 2019-11-28 07:04:35

You can provide an entityManager using --em=name option in the migration task. I also add this piece of code, to avoid executing of the migration on another db by mistake:

    $parameters = $this->connection->getParams();
    $this->skipIf(
        $parameters['dbname'] != "my_db_name"
        'This is the other DB\'s migration, pass a correct --em parameter'
    );

I haven't found any other way to check the EM, so I can't help you if your databases have same names.

Also note, that you should add the skipIf to all your migrations, so you can migrate without worry in both you databases.

This question is a little old, but it came up first when I was asking the same thing. I found my answer in the Doctrine migrations configuration docs. Let's say you have connections for two databases, each with their own entity managers (mapped here with XML, not annotations, and not auto mapped so the schema configs can live in the same config/doctrine path):

# config.yml
doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver: '%database_driver%'
            ...
        special:
            driver: '%special_database_driver%'
orm:
    entity_managers:
        default:
            auto_mapping: false
            mappings:
                base:
                    type: xml
                    dir: '%kernel.root_dir%/../src/MyBundle/Resources/config/doctrine/base'
                    prefix: MyBundle\Entity
                    alias: Base
                    is_bundle: false
        special:
            auto_mapping: false
            connection: special
            mappings:
                special:
                    type: xml
                    dir: '%kernel.root_dir%/../src/MyBundle/Resources/config/doctrine/special'
                    prefix: MyBundle\Special
                    alias: Special
                    is_bundle: false

Then you do not include the doctrine_migrations configurations in config.yml. Instead, create a configuration file for each one:

# src/MyBundle/Resources/config/migrations/base.yml
name: BaseMigrations
migrations_namespace: MyBundle\Migrations\Base
table_name: Migrations
migrations_directory: src/MyBundle/Migrations/Base

# src/MyBundle/Resources/config/migrations/special.yml
name: SpecialMigrations
migrations_namespace: MyBundle\Migrations\Special
table_name: Migrations
migrations_directory: src/MyBundle/Migrations/Special

Then, whenever you run any migration command, specify both the entity manager and configuration:

bin/console doctrine:migrations:status --env=dev --em=special --configuration=src/MyBundle/Resources/config/migrations/special.yml

It's a bit to remember if running by hand, so you might want to wrap them up in your own command to make life easy (e.g. something like bin/console my:migrations:status --env=dev --db=special). It's also not an issue if you have a deploy bash script, like:

#!/bin/bash
ENVIRONMENT="$1"

# Run migrations for a configuration
function runMigrations()
{
    local CONFIG="$1"
    local MANAGER="$2"
    local STATUS="$(bin/console doctrine:migrations:status --env=${ENVIRONMENT} --configuration=${CONFIG} --em=${MANAGER})"

    case "${STATUS}" in
        *"Already at latest version"*)
            # Do nothing
            ;;
        *)
            runNextMigration $CONFIG $MANAGER
            ;;
    esac
}

# Run the next migration for a configuration
function runNextMigration()
{
    local CONFIG="$1"
    local MANAGER="$2"
    bin/console doctrine:migrations:migrate next --env=$ENVIRONMENT --configuration=$CONFIG --em=$MANAGER

    runMigrations $CONFIG $MANAGER
}

runMigrations "src/MyBundle/Resources/config/migrations/base.yml" "default"
runMigrations "src/MyBundle/Resources/config/migrations/special.yml" "special"
Andrey Mussatov

The similar problem and solution: Symfony2 - Change Migration Directory

You can create another migration folder for second DB and put migrations inside.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!