我在我的Symfony项目中使用doctrine,通过连接到已经存在的postgres数据库。
数据库有几个模式,但symfony应用程序只会使用自己的模式。我创建的第一个Entity
类是以下类:
namespace BelkaTestBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="app_auth.User", schema="app_auth")
*/
class User {
/**
* @ORMColumn(type="string")
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $username;
/**
* @ORMColumn(type="string")
*/
private $email;
/**
* @ORMColumn(type="string")
*/
private $password;
}
如您所见,Entity
正在指定自己的架构app_auth
。接下来,我尝试使用迁移包。因此,我安装并配置了它,以便除了我的架构之外不考虑任何东西:
config.yml
摘录 :
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
schema_filter: ~^app_auth..*~
config_dev.yml
摘录 :
doctrine_migrations:
dir_name: "%kernel.root_dir%/../.container/update/DoctrineMigrations"
namespace: ApplicationMigrations
table_name: "app_auth.migration_versions"
name: Application Migrations
我运行差异:
php app/console doctrine:migrations:diff
遗憾的是,生成的迁移类如下:
namespace ApplicationMigrations;
use DoctrineDBALMigrationsAbstractMigration;
use DoctrineDBALSchemaSchema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160422171409 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
}
/**
* @param Schema $schema
*/
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'.');
$this->addSql('CREATE SCHEMA app_auth');
}
}
那么我的配置有什么问题呢?
肮脏但有效的解决方案:
使用原则/迁移进行测试:3.0.1
我们可以修补 DiffGenerator 的模式删除功能。我个人用过这个 https://github.com/cweagans/composer-patches
安装软件包,然后在 composer.json "extra" 部分添加:
"patches": {
"doctrine/migrations": {
"Change doctrine behavior to correctly pass PostgreSQL schema to schema_filter while diffing": "patches/doctrine-diff-generator.patch"
}
}
patches/doctrine-diff-generator.patch中的补丁文件:
--- lib/Doctrine/Migrations/Generator/DiffGenerator.php 2020-06-21 10:55:42.000000000 +0200
+++ lib/Doctrine/Migrations/Generator/DiffGenerator.patched.php 2020-12-23 12:33:02.689405221 +0100
@@ -142,8 +142,6 @@
*/
private function resolveTableName(string $name) : string
{
- $pos = strpos($name, '.');
-
- return $pos === false ? $name : substr($name, $pos + 1);
+ return $name;
}
}
当然,你必须记住,对教义的更新可能会打破补丁。而且您正在为您的学说全局更改此功能,因此请注意重大更改。
就个人而言,对于我的用例,即将教义实体添加到旧数据库中,而不会破坏它,这很好用,并且使我免于将教义管理的每个新表添加到schema_filter中。
解释:当我们研究 DiffGenerator 实现时,它实际上使用模式正确解码表名,但仅在收集当前数据库模式时。这发生在PostgreSqlSchemaManager.php中:
/**
* {@inheritdoc}
*/
protected function _getPortableTableDefinition($table)
{
$schemas = $this->getExistingSchemaSearchPaths();
$firstSchema = array_shift($schemas);
if ($table['schema_name'] === $firstSchema) {
return $table['table_name'];
}
return $table['schema_name'] . '.' . $table['table_name'];
}
但是,在计算目标架构时,此信息在 DiffGenerator 中丢失.php:
/**
* Resolve a table name from its fully qualified name. The `$name` argument
* comes from DoctrineDBALSchemaTable#getName which can sometimes return
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
* the table name from that.
*/
private function resolveTableName(string $name) : string
{
$pos = strpos($name, '.');
return $pos === false ? $name : substr($name, $pos + 1);
}
只有之后,这个函数的返回值才会被传递到schema_filter。不幸,但我想这是有原因的:)