54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DoctrineMigrations;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
use Doctrine\Migrations\AbstractMigration;
|
|
|
|
final class Version20260515142000 extends AbstractMigration
|
|
{
|
|
private const TABLES = [
|
|
'news',
|
|
'promo',
|
|
'disease',
|
|
'medical_center',
|
|
'site_services',
|
|
];
|
|
|
|
public function getDescription(): string
|
|
{
|
|
return 'Add generated id defaults for content CRUD entities';
|
|
}
|
|
|
|
public function up(Schema $schema): void
|
|
{
|
|
foreach (self::TABLES as $table) {
|
|
$sequence = $table . '_id_seq';
|
|
|
|
$this->addSql(sprintf('CREATE SEQUENCE IF NOT EXISTS %s OWNED BY %s.id', $sequence, $table));
|
|
$this->addSql(sprintf(
|
|
'SELECT setval(\'%s\', COALESCE((SELECT MAX(id) FROM %s), 0) + 1, false)',
|
|
$sequence,
|
|
$table,
|
|
));
|
|
$this->addSql(sprintf(
|
|
'ALTER TABLE %s ALTER COLUMN id SET DEFAULT nextval(\'%s\')',
|
|
$table,
|
|
$sequence,
|
|
));
|
|
}
|
|
}
|
|
|
|
public function down(Schema $schema): void
|
|
{
|
|
foreach (array_reverse(self::TABLES) as $table) {
|
|
$sequence = $table . '_id_seq';
|
|
|
|
$this->addSql(sprintf('ALTER TABLE %s ALTER COLUMN id DROP DEFAULT', $table));
|
|
$this->addSql(sprintf('DROP SEQUENCE IF EXISTS %s', $sequence));
|
|
}
|
|
}
|
|
}
|