Migrations Fixes
- Every php migration contains in the function
public function down(...
, the command$this->addSql('CREATE SCHEMA public');
.
This is a doctrine bug (https://github.com/doctrine/dbal/issues/1110) reported in 2015 but doctrine never fixed it officially. We have to remove the command from every migration to support rollbacks of migrations.
To prevent new migrations from containing the command, use this:
<?php declare(strict_types=1);
namespace App\Doctrine\EventListener;
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
final class FixPostgreSqlDefaultSchemaListener
{
public function postGenerateSchema(GenerateSchemaEventArgs $args): void
{
$schemaManager = $args
->getEntityManager()
->getConnection()
->getSchemaManager()
;
if (!$schemaManager instanceof PostgreSqlSchemaManager) {
return;
}
foreach ($schemaManager->getExistingSchemaSearchPaths() as $namespace) {
if (!$args->getSchema()->hasNamespace($namespace)) {
$args->getSchema()->createNamespace($namespace);
}
}
}
}
services_dev.yaml
...
App\Doctrine\EventListener\FixPostgreSqlDefaultSchemaListener:
tags:
- { name: doctrine.event_listener, event: postGenerateSchema }
- DB Views are manually created in
create_views.sql
. They should get their own migration insql/schema
. We need initital migration for each view, with drop if exists / create. Otherwise we won’t have them when we run migrations from scratch.
AC
-
$this->addSql('CREATE SCHEMA public');
is removed from all migrationsdown()
method -
Code Snippet from above implemented -
create views SQL commands are moved into migrations -
new shell script is provided to run migrations without drop db and inserting examples
Review
-
$this->addSql('CREATE SCHEMA public');
is removed from all migrationsdown()
method -
Code Snippet from above implemented -
create views SQL commands are moved into migrations -
new shell script is provided to run migrations without drop db and inserting examples
Edited by Michael Voigt