简体   繁体   中英

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:

$ symfony console make:migration

Now, I want to migrate to a specific schema into my PostgresSQL database.

In the documentation of Symfony says:

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. By default, such tables will be removed by the doctrine:migrations:diff command. If you follow a specific scheme you can configure doctrine/dbal to ignore those tables. Let's say all custom tables will be prefixed by t_. In this case, you just have to add the following configuration option to your doctrine configuration:

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

I understand that schema_filter is a filter applied to table names (only tables?) through a regular expression. Well, but schema_filter no reference the schemas created into a PostgreSQL database.

Then how can I migrate to a specific schema into a database?

When I execute the command:

$ symfony console make:migration

it creates a migration file that contains a class with the functions 'up' and 'down', both with a parameter $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.

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';');

     $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 = '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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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