Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

493
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!