<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241212161337 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$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');
$this->addSql('ALTER TABLE w_stream ADD CONSTRAINT FK_5701C10142BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_5701C10142BE61A4 ON w_stream (recent_comment_id)');
$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');
$this->addSql('ALTER TABLE ws_document ADD CONSTRAINT FK_B14013BF42BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_B14013BF42BE61A4 ON ws_document (recent_comment_id)');
$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');
$this->addSql('ALTER TABLE ws_event ADD CONSTRAINT FK_1E63CACE42BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_1E63CACE42BE61A4 ON ws_event (recent_comment_id)');
$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');
$this->addSql('ALTER TABLE ws_task ADD CONSTRAINT FK_F06C129742BE61A4 FOREIGN KEY (recent_comment_id) REFERENCES ws_comment (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_F06C129742BE61A4 ON ws_task (recent_comment_id)');
foreach ($this->getEntities() as $entity) {
$table = $entity['table'];
$type = $entity['type'];
if ('stream' === $type) {
// Special case for stream: include all related comments
$this->addSql("
UPDATE $table t
LEFT JOIN (
SELECT
stream_id AS related_id,
COUNT(id) AS total_comments,
MAX(created_at) AS latest_comment_time,
(SELECT id
FROM ws_comment
WHERE stream_id = c.stream_id
AND created_at = MAX(c.created_at)
AND system_update = 0
LIMIT 1) AS recent_comment_id
FROM ws_comment c
WHERE system_update = 0
GROUP BY stream_id
) AS comments_data
ON t.id = comments_data.related_id
SET
t.total_comments = comments_data.total_comments,
t.recent_comment_id = comments_data.recent_comment_id;
");
} else {
// Default case: filter comments by type
$this->addSql("
UPDATE $table t
LEFT JOIN (
SELECT
{$type}_id AS related_id,
COUNT(id) AS total_comments,
MAX(created_at) AS latest_comment_time,
(SELECT id
FROM ws_comment
WHERE {$type}_id = c.{$type}_id
AND created_at = MAX(c.created_at)
AND system_update = 0
AND type = '$type'
LIMIT 1) AS recent_comment_id
FROM ws_comment c
WHERE system_update = 0 AND type = '$type'
GROUP BY {$type}_id
) AS comments_data
ON t.id = comments_data.related_id
SET
t.total_comments = comments_data.total_comments,
t.recent_comment_id = comments_data.recent_comment_id;
");
}
}
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE w_stream DROP FOREIGN KEY FK_5701C10142BE61A4');
$this->addSql('DROP INDEX IDX_5701C10142BE61A4 ON w_stream');
$this->addSql('ALTER TABLE w_stream ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
$this->addSql('ALTER TABLE ws_document DROP FOREIGN KEY FK_B14013BF42BE61A4');
$this->addSql('DROP INDEX IDX_B14013BF42BE61A4 ON ws_document');
$this->addSql('ALTER TABLE ws_document ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
$this->addSql('ALTER TABLE ws_event DROP FOREIGN KEY FK_1E63CACE42BE61A4');
$this->addSql('DROP INDEX IDX_1E63CACE42BE61A4 ON ws_event');
$this->addSql('ALTER TABLE ws_event ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
$this->addSql('ALTER TABLE ws_task DROP FOREIGN KEY FK_F06C129742BE61A4');
$this->addSql('DROP INDEX IDX_F06C129742BE61A4 ON ws_task');
$this->addSql('ALTER TABLE ws_task ADD last_contacted DATETIME DEFAULT NULL, DROP recent_comment_id, CHANGE total_comments interactions INT DEFAULT NULL');
foreach ($this->getEntities() as $entity) {
$table = $entity['table'];
$type = $entity['type'];
$this->addSql("
UPDATE $table t
LEFT JOIN (
SELECT
{$type}_id AS related_id,
COUNT(id) AS interactions,
MAX(created_at) AS last_contacted
FROM ws_comment c
WHERE system_update = 0 AND type = '$type'
GROUP BY {$type}_id
) AS comments_data
ON t.id = comments_data.related_id
SET
t.interactions = comments_data.interactions,
t.last_contacted = comments_data.last_contacted;
");
}
}
/**
* @return array
*/
private function getEntities(): array
{
return [
['table' => 'w_stream', 'type' => 'stream'],
['table' => 'ws_document', 'type' => 'document'],
['table' => 'ws_event', 'type' => 'event'],
['table' => 'ws_task', 'type' => 'task'],
];
}
}