简体   繁体   English

如何迁移到与 Symfony 和 Doctrine 一起使用的数据库 PostgreSQL 的特定模式?

[英]How migrate to specific schema of database PostgreSQL working with Symfony and Doctrine?

I did a migration after of created my database structure using make:entity command:在使用make:entity命令创建数据库结构后,我进行了迁移:

$ symfony console make:migration $ symfony 控制台制造:迁移

Now, I want to migrate to a specific schema into my PostgresSQL database.现在,我想将特定模式迁移到我的PostgresSQL数据库中。

In the documentation of Symfony says: 在 Symfony 的文档中说:

MANUAL TABLES.手册表。 It is a common use case, that in addition to your generated database structure based on your doctrine entities you might need custom tables.这是一个常见的用例,除了基于 doctrine 实体生成的数据库结构之外,您可能还需要自定义表。 By default, such tables will be removed by the doctrine:migrations:diff command.默认情况下, doctrine:migrations:diff命令将删除此类表。 If you follow a specific scheme you can configure doctrine/dbal to ignore those tables.如果您遵循特定的方案,您可以将教义/dbal 配置为忽略这些表。 Let's say all custom tables will be prefixed by t_.假设所有自定义表都以 t_ 为前缀。 In this case, you just have to add the following configuration option to your doctrine configuration:在这种情况下,您只需将以下配置选项添加到您的 doctrine 配置中:

doctrine: dbal: schema_filter: ~^(?!t_)~

I understand that schema_filter is a filter applied to table names (only tables?) through a regular expression.我知道schema_filter是通过正则表达式应用于表名(仅表?)的过滤器。 Well, but schema_filter no reference the schemas created into a PostgreSQL database.好吧,但是schema_filter没有引用创建到PostgreSQL数据库中的模式。

Then how can I migrate to a specific schema into a database?那么如何将特定模式迁移到数据库中呢?

When I execute the command:当我执行命令时:

$ symfony console make:migration $ symfony 控制台制造:迁移

it creates a migration file that contains a class with the functions 'up' and 'down', both with a parameter $schema .它会创建一个迁移文件,其中包含一个 class 和函数 'up' 和 'down',两者都带有参数$schema Here a fragment:这里有一个片段:

 public function up(Schema $schema) : void
 {
     // 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('CREATE SEQUENCE division_id_seq INCREMENT BY 1 MINVALUE 1 START 1');

This parameter can't assign a value when it is declared.此参数在声明时不能赋值。 Must be null.必须是 null。

Is it possible to use it to migrate to a specific schema?是否可以使用它迁移到特定模式?

Well, while I find a way, I did it like this:好吧,当我找到一种方法时,我是这样做的:

public function up(Schema $schema): void { // 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';'); public function up(Schema $schema): void { // 此 up() 迁移是自动生成的,请根据需要修改 $this->abortIf($this->connection->getDatabasePlatform()->getName() ,== 'postgresql'. '迁移只能在'postgresql'上安全执行;');

     $this->addSql('CREATE SEQUENCE my_schema_name.division_id_seq INCREMENT BY 1 MINVALUE 1 START 1');

I mean that I write the schema name in all migration code generate by the command.我的意思是我在命令生成的所有迁移代码中都写了模式名称。

But I still believe that is possible to do it in another way.但我仍然相信有可能以另一种方式做到这一点。 If yes then how?如果是,那怎么办?

I forgot it.我忘记了。 Migration fails when I try with this line before of first sentence:当我在第一句话之前尝试使用此行时,迁移失败:

 $this->addSql('SET search_path TO my_schema_name');

Also I try assigning the schema name to the parameter $schema:我也尝试将模式名称分配给参数 $schema:

 $schema = 'my_schema_name'

It didn't work either.它也没有用。

Well, thank you.嗯,谢谢。 I read this:我读到这个:

the schema should be set via the db connection dsn, all tables by default end up in that schema.架构应通过 db 连接 dsn 设置,默认情况下所有表都以该架构结束。

Then I try set the connection parameters in doctrine.yaml : url , dbname , and connectstring , using this notation: dbname.schema , even: dbname/schema , but it does not work.然后我尝试在doctrine.yaml中设置连接参数: urldbname dbname/schema connectstring ,使用这个表示dbname.schema

Then, a spite of this:然后,尽管如此:

you shouldn't hard-code schema names into migrations.您不应该将模式名称硬编码到迁移中。

In the migration file was need set the schema for the connection:在迁移文件中需要设置连接的架构:

public function up(Schema $schema) : void
{
    // 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->connection->exec('SET search_path TO my_schema_name;');

    $this->addSql('CREATE SEQUENCE division_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
    ...

However the table migration_versions did not exists in the schema, so I had to create it.但是表migration_versions不存在于模式中,所以我必须创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM