migrations/Version20241212161337.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. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20241212161337 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         $this->addSql('ALTER TABLE w_stream ADD recent_comment_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', DROP last_contacted, CHANGE interactions total_comments INT DEFAULT NULL');
  19.         $this->addSql('ALTER TABLE w_stream ADD CONSTRAINT FK_5701C10142BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
  20.         $this->addSql('CREATE INDEX IDX_5701C10142BE61A4 ON w_stream (recent_comment_id)');
  21.         $this->addSql('ALTER TABLE ws_document ADD recent_comment_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', DROP last_contacted, CHANGE interactions total_comments INT DEFAULT NULL');
  22.         $this->addSql('ALTER TABLE ws_document ADD CONSTRAINT FK_B14013BF42BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
  23.         $this->addSql('CREATE INDEX IDX_B14013BF42BE61A4 ON ws_document (recent_comment_id)');
  24.         $this->addSql('ALTER TABLE ws_event ADD recent_comment_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', DROP last_contacted, CHANGE interactions total_comments INT DEFAULT NULL');
  25.         $this->addSql('ALTER TABLE ws_event ADD CONSTRAINT FK_1E63CACE42BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
  26.         $this->addSql('CREATE INDEX IDX_1E63CACE42BE61A4 ON ws_event (recent_comment_id)');
  27.         $this->addSql('ALTER TABLE ws_task ADD recent_comment_id CHAR(36) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', DROP last_contacted, CHANGE interactions total_comments INT DEFAULT NULL');
  28.         $this->addSql('ALTER TABLE ws_task ADD CONSTRAINT FK_F06C129742BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
  29.         $this->addSql('CREATE INDEX IDX_F06C129742BE61A4 ON ws_task (recent_comment_id)');
  30.         foreach ($this->getEntities() as $entity) {
  31.             $table $entity['table'];
  32.             $type $entity['type'];
  33.             if ('stream' === $type) {
  34.                 // Special case for stream: include all related comments
  35.                 $this->addSql("
  36.                     UPDATE $table t
  37.                     LEFT JOIN (
  38.                         SELECT
  39.                             stream_id AS related_id,
  40.                             COUNT(id) AS total_comments,
  41.                             MAX(created_at) AS latest_comment_time,
  42.                             (SELECT id
  43.                              FROM ws_comment
  44.                              WHERE stream_id = c.stream_id
  45.                                AND created_at = MAX(c.created_at)
  46.                                AND system_update = 0
  47.                                LIMIT 1) AS recent_comment_id
  48.                         FROM ws_comment c
  49.                         WHERE system_update = 0
  50.                         GROUP BY stream_id
  51.                     ) AS comments_data
  52.                     ON t.id = comments_data.related_id
  53.                     SET
  54.                         t.total_comments = comments_data.total_comments,
  55.                         t.recent_comment_id = comments_data.recent_comment_id;
  56.                 ");
  57.             } else {
  58.                 // Default case: filter comments by type
  59.                 $this->addSql("
  60.                     UPDATE $table t
  61.                     LEFT JOIN (
  62.                         SELECT
  63.                             {$type}_id AS related_id,
  64.                             COUNT(id) AS total_comments,
  65.                             MAX(created_at) AS latest_comment_time,
  66.                             (SELECT id
  67.                              FROM ws_comment
  68.                              WHERE {$type}_id = c.{$type}_id
  69.                                AND created_at = MAX(c.created_at)
  70.                                AND system_update = 0
  71.                                AND type = '$type'
  72.                                LIMIT 1) AS recent_comment_id
  73.                         FROM ws_comment c
  74.                         WHERE system_update = 0 AND type = '$type'
  75.                         GROUP BY {$type}_id
  76.                     ) AS comments_data
  77.                     ON t.id = comments_data.related_id
  78.                     SET
  79.                         t.total_comments = comments_data.total_comments,
  80.                         t.recent_comment_id = comments_data.recent_comment_id;
  81.                 ");
  82.             }
  83.         }
  84.     }
  85.     public function down(Schema $schema): void
  86.     {
  87.         // this down() migration is auto-generated, please modify it to your needs
  88.         $this->addSql('ALTER TABLE w_stream DROP FOREIGN KEY FK_5701C10142BE61A4');
  89.         $this->addSql('DROP INDEX IDX_5701C10142BE61A4 ON w_stream');
  90.         $this->addSql('ALTER TABLE w_stream ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
  91.         $this->addSql('ALTER TABLE ws_document DROP FOREIGN KEY FK_B14013BF42BE61A4');
  92.         $this->addSql('DROP INDEX IDX_B14013BF42BE61A4 ON ws_document');
  93.         $this->addSql('ALTER TABLE ws_document ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
  94.         $this->addSql('ALTER TABLE ws_event DROP FOREIGN KEY FK_1E63CACE42BE61A4');
  95.         $this->addSql('DROP INDEX IDX_1E63CACE42BE61A4 ON ws_event');
  96.         $this->addSql('ALTER TABLE ws_event ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
  97.         $this->addSql('ALTER TABLE ws_task DROP FOREIGN KEY FK_F06C129742BE61A4');
  98.         $this->addSql('DROP INDEX IDX_F06C129742BE61A4 ON ws_task');
  99.         $this->addSql('ALTER TABLE ws_task ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
  100.         foreach ($this->getEntities() as $entity) {
  101.             $table $entity['table'];
  102.             $type $entity['type'];
  103.             $this->addSql("
  104.                 UPDATE $table t
  105.                 LEFT JOIN (
  106.                     SELECT
  107.                         {$type}_id AS related_id,
  108.                         COUNT(id) AS interactions,
  109.                         MAX(created_at) AS last_contacted
  110.                     FROM ws_comment c
  111.                     WHERE system_update = 0 AND type = '$type'
  112.                     GROUP BY {$type}_id
  113.                 ) AS comments_data
  114.                 ON t.id = comments_data.related_id
  115.                 SET
  116.                     t.interactions = comments_data.interactions,
  117.                     t.last_contacted = comments_data.last_contacted;
  118.             ");
  119.         }
  120.     }
  121.     /**
  122.      * @return array
  123.      */
  124.     private function getEntities(): array
  125.     {
  126.         return [
  127.             ['table' => 'w_stream''type' => 'stream'],
  128.             ['table' => 'ws_document''type' => 'document'],
  129.             ['table' => 'ws_event''type' => 'event'],
  130.             ['table' => 'ws_task''type' => 'task'],
  131.         ];
  132.     }
  133. }