Doctrine 条令迁移:为mysql和postgresql创建代码

Doctrine 条令迁移:为mysql和postgresql创建代码,doctrine,doctrine-migrations,Doctrine,Doctrine Migrations,我在Symfony 3.4.4项目中使用ORM 2.6.1。 我的一些实例在MySQL数据库上工作,一些在Postgresql上工作,一些安装甚至可以访问MicosoftSQL服务器。这在不对项目或实体进行任何特殊更改的情况下运行良好,我只需配置相应的连接参数 但是:如果创建迁移,迁移文件中只创建与当前数据库连接兼容的语句 我使用postgres连接进行开发,因此我只生成postgresql语句,如: class Version20180430083616 extends AbstractMig

我在Symfony 3.4.4项目中使用ORM 2.6.1。 我的一些实例在MySQL数据库上工作,一些在Postgresql上工作,一些安装甚至可以访问MicosoftSQL服务器。这在不对项目或实体进行任何特殊更改的情况下运行良好,我只需配置相应的连接参数

但是:如果创建迁移,迁移文件中只创建与当前数据库连接兼容的语句

我使用postgres连接进行开发,因此我只生成postgresql语句,如:

class Version20180430083616 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

        $this->addSql('DELETE FROM document_category');
        $this->addSql('DROP SEQUENCE document_category_id_seq CASCADE');
        $this->addSql('DROP TABLE document_category');
    }

    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
        //...
    }
}
我的问题:我如何告诉迁移包为每个平台创建语句,例如:

class Version20180430083616 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        if($this->connection->getDatabasePlatform()->getName() == 'postgresql'){

            $this->addSql('DELETE FROM document');
            $this->addSql('DELETE FROM document_category');
            $this->addSql('DROP SEQUENCE document_category_id_seq CASCADE');
            $this->addSql('DROP TABLE document_category');
        } else if($this->connection->getDatabasePlatform()->getName() == 'mysql'){
            ...
        } else if ($this->connection->getDatabasePlatform()->getName() == 'mssql') { // MicrosoftSQL ?
            ...
        }
    }
}
编辑:


因此,我认为我的问题的唯一解决方案是定义多个数据库连接和实体管理器,并始终为每种连接类型创建不同的迁移。根据,我可以将几个连接定义为:

我找到了一个可行的解决方案:

inf config.yml我为每种数据库类型定义一个连接和一个EntityManager:

doctrine:
    dbal:
        default_connection: pgdb
        connections:
            pgdb:
                driver: pdo_pgsql
                host: db
                port: 5432
                name: pgdb
                user: postgres
                password: example
                charset: utf8
                mapping_types:
                    enum: string
            mysql:
                driver: pdo_mysql
                host: mysqlhost
                port: 3306
                name: mydb
                dbname: mydb
                user: root
                password: xxx
                charset: utf8mb4
                default_table_options:
                    collate: utf8mb4_unicode_ci
                mapping_types:
                    enum: string
            mssql:
                driver: pdo_sqlsrv
                host: mssqlhost
                port: 1433
                name: msdb
                dbname: testdb
                user: sa
                password: xxx
                charset: utf8
                mapping_types:
                    enum: string

    orm:
        auto_generate_proxy_classes:  false
        proxy_dir:            '%kernel.cache_dir%/doctrine/orm/Proxies'
        proxy_namespace:      Proxies
        entity_managers:
            default:
                connection: pgdb
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    AppBundle: ~ 
            my:
                connection: mydb
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    AppBundle: ~
            ms:
                connection: msdb
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    AppBundle: ~
然后,我可以发出diff命令3次,而不是只发出一次

$ bin/console doctrine:migrations:diff --em=default
$ bin/console doctrine:migrations:diff --em=my
$ bin/console doctrine:migrations:diff --em=ms
这将创建三个迁移,每个迁移从一条栅栏线开始:

$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mssql', 'Migration can only be executed safely on \'mssql\'.');
在其中,我通过
skipIf
交换
abortIf
,这样,如果当前迁移针对不同的数据库类型,迁移过程不会中止,而只是跳过:

$this->skipIf($this->connection->getDatabasePlatform()->getName() !== 'mssql', 'Migration can only be executed safely on \'mssql\'.');

我希望这能帮助别人。

不行,对不起。Doctrine的迁移工具不是这样工作的。谢谢你的回答。我想我必须在我的开发环境中定义几个并发数据库连接,然后为每个连接创建迁移。是否有一种方法可以自动执行对多个数据库的实体写入?