migrations/Version20260712120000.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. final class Version20260712120000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Build performance indexes concurrently without rewriting application data.';
  11.     }
  12.     public function isTransactional(): bool
  13.     {
  14.         // PostgreSQL does not allow CREATE/DROP INDEX CONCURRENTLY inside a
  15.         // transaction. Running each statement separately avoids blocking
  16.         // normal writes for the duration of an index build.
  17.         return false;
  18.     }
  19.     public function up(Schema $schema): void
  20.     {
  21.         $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on postgresql.');
  22.         // Drop first so a retry also repairs an invalid index left behind by
  23.         // an interrupted CREATE INDEX CONCURRENTLY operation.
  24.         $this->rebuildIndex('IDX_M_DEVICE_FINISHED''measurements (device_id, finished_at DESC, id DESC)');
  25.         $this->rebuildIndex('IDX_MS_MEASUREMENT_DELETED''measurement_settings (measurement_id, is_deleted)');
  26.         $this->rebuildIndex('IDX_DS_DEVICE_LATEST''data_samples (device_id, id DESC)');
  27.         $this->rebuildIndex('IDX_DC_DEVICE_COMMAND''device_command (device_id, command)');
  28.         $this->rebuildIndex('IDX_PROBE_CHANNEL_TIME''probes (channel_id, time DESC)');
  29.     }
  30.     public function down(Schema $schema): void
  31.     {
  32.         $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on postgresql.');
  33.         $this->addSql('DROP INDEX CONCURRENTLY IF EXISTS IDX_PROBE_CHANNEL_TIME');
  34.         $this->addSql('DROP INDEX CONCURRENTLY IF EXISTS IDX_DC_DEVICE_COMMAND');
  35.         $this->addSql('DROP INDEX CONCURRENTLY IF EXISTS IDX_DS_DEVICE_LATEST');
  36.         $this->addSql('DROP INDEX CONCURRENTLY IF EXISTS IDX_MS_MEASUREMENT_DELETED');
  37.         $this->addSql('DROP INDEX CONCURRENTLY IF EXISTS IDX_M_DEVICE_FINISHED');
  38.     }
  39.     private function rebuildIndex(string $namestring $definition): void
  40.     {
  41.         $this->addSql(sprintf('DROP INDEX CONCURRENTLY IF EXISTS %s'$name));
  42.         $this->addSql(sprintf('CREATE INDEX CONCURRENTLY %s ON %s'$name$definition));
  43.     }
  44. }