Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

492
Visualizações
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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda