【问题标题】:Doctrine migrations: create code for mysql and postgresql教义迁移:为 mysql 和 postgresql 创建代码
【发布时间】:2018-06-21 13:33:14
【问题描述】:

我在 Symfony 3.4.4 项目中使用 Doctrine 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 ?
            ...
        }
    }
}

编辑:

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

【问题讨论】:

  • 不行,对不起。 Doctrine 的迁移工具不能这样工作。
  • 感谢您的回答。我想我必须在我的开发环境中定义几个并发数据库连接,然后为每个连接创建迁移。有没有办法自动将实体写入多个数据库?

标签: doctrine doctrine-migrations


【解决方案1】:

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

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-command 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\'.');

我在其中将abortIfskipIf 交换,这样如果当前迁移对于不同的数据库类型,迁移过程不会中止,而只是跳过:

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

我希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2018-02-05
    相关资源
    最近更新 更多