原理迁移:为mysql和postgresql创建代码

时间:2018-06-21 13:33:14

标签: doctrine doctrine-migrations

我在Symfony 3.4.4项目中使用Doctrine ORM 2.6.1。 我的一些实例可以在MySQL数据库上运行,某些实例可以在Postgresql上运行,还有一些安装甚至可以访问MicosoftSQL服务器。无需对我的项目或实体进行任何特殊更改即可正常工作,我只需要配置相应的连接参数。

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

我使用postgres-concection开发,所以我只生成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 ?
            ...
        }
    }
}

编辑:

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

1 个答案:

答案 0 :(得分:0)

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

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

private void requestSync(){
    ThreadPool.getInstance().execute(createRunnable());
}

然后,我可以发出diff命令3次,而不是一次

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: ~

这将创建三个迁移,每个迁移以栅栏线开始:

$ 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\'.'); 交换abortIf,这样,如果当前迁移是针对其他数据库类型,则迁移过程不会中止,只是跳过了:

skipIf

我希望这对某人有帮助。