Oll*_*etz 7 doctrine doctrine-migrations
我在 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\'.');
//...
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:如何告诉迁移包为每个平台创建语句,例如:
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 ?
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
因此,我认为解决我的问题的唯一方法是定义多个数据库连接和实体管理器,并始终为每种连接类型创建不同的迁移。根据这篇文章,我可以定义几个连接:
我找到了一个可行的解决方案:
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: ~
Run Code Online (Sandbox Code Playgroud)
然后,我可以发出 diff 命令 3 次,而不是只发出一次
$ bin/console doctrine:migrations:diff --em=default
$ bin/console doctrine:migrations:diff --em=my
$ bin/console doctrine:migrations:diff --em=ms
Run Code Online (Sandbox Code Playgroud)
这将创建三个迁移,每个迁移都以栅栏线开始:
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mssql', 'Migration can only be executed safely on \'mssql\'.');
Run Code Online (Sandbox Code Playgroud)
其中我abortIf
通过进行交换skipIf
,这样如果当前迁移针对不同的数据库类型,则迁移过程不会中止,而只是跳过:
$this->skipIf($this->connection->getDatabasePlatform()->getName() !== 'mssql', 'Migration can only be executed safely on \'mssql\'.');
Run Code Online (Sandbox Code Playgroud)
我希望这对某人有帮助。
归档时间: |
|
查看次数: |
3744 次 |
最近记录: |