5

I currently have a single database / entity manager symfony2 (2.1) app working just fine, including doctrine migrations. I am in the process of adding a second database connection + entity manager and am having trouble getting doctrine migrations to do what I want.

Essentially, I want most of my entities to live in my first, 'default' database, and have several new entities / tables in my second database - and then have doctrine migrations manage any changes to the schemas. The documentation seems to imply you can set a 'schema_filter' on the connection to achieve this.

(The use case is this: there are several installs of our app for white-labelled resellers, each with their own primary database. However, we are implementing interactive help, which will be written by us in a CMS / blog type interface and available, via this second database, to all installs.)

So far my doctrine configuration looks like this (from app/config/config.yml):

    dbal:
        default:
            driver:   %database_driver%
            host:     %database_host%
            port:     %database_port%
            dbname:   %database_name%
            user:     %database_user%
            password: %database_password%
            charset:  UTF8
            schema_filter: ~^(?!help_)~
        cross_site:
            driver:   %crossite_database_driver%
            host:     %crossite_database_host%
            port:     %crossite_database_port%
            dbname:   %crossite_database_name%
            user:     %crossite_database_user%
            password: %crossite_database_password%
            charset:  UTF8
            schema_filter: ~^help_~

orm:
    auto_generate_proxy_classes: %kernel.debug%
    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                <most of the bundles>
        help:
            connection: cross_site
            mappings:
                HelpBundle: ~

The intention is to have most of the entities / tables in the 'default' database, but exclude any table beginning with 'help_'. Conversely, the second database should contain only those tables beginning with 'help_'.

However, whenever I run a doctrine migration using either entity manager, it will simply include all tables regardless of their name.

php app/console doctrine:migrations:migrate # includes every table including help_*
php app/console doctrine:migrations:migrate --em="help" # includes every table

Am I mis-using schema_filter? Is there another way to achieve my goal?

Update: It's actually worse... after the first migration using the 'help' entity manager, I am no longer able to run additional migrations using --em="help"; I receive this error:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'migration_versions' already exists 

Migrations on the original EM continue to work fine. Help?

4

1 回答 1

6

所以,我学到了几件事帮助我解决了这个问题......在这里发帖以防其他人陷入类似的纠结。

  1. 学说:迁移:迁移或多或少会忽略您的 schema_filter。这可能是它应该做的......它的工作只是在你的迁移文件中运行 SQL。
  2. 教义:迁移:差异是您应该担心您的 schema_filter 的地方。它将通过 schema_filter 的镜头查看数据库的当前状态。请记住通过 --em=[name] 将正确的实体管理器传递给 diff 命令和 migrate 命令。
  3. 不要使用您的 schema_filter 排除 migration_versions 表。如果这样做,则学说迁移捆绑包将认为它不存在,并始终尝试重新创建它(这将失败)。

因此,我添加了两件事或多或少地实现了我的目标。在我的第二个数据库的配置中,我调整了 schema_filter 以包括 migration_versions 表:

            cross_site: 
                schema_filter: ~^(help_|migration_versions)~

我还将以下代码添加到处理第二个数据库/实体管理器的任何迁移版本文件(在 app/DoctrineMigrations 中):

$this->skipIf( $this->connection->getDatabase() != '[second DB name]', 'Skipping help database.' );

这确保了应该只在第二个数据库中的表不会污染原始数据库。请注意,这应该同时出现在 up() 和 down() 方法中。

于 2013-10-02T10:07:46.567 回答