|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* For licensing terms, see /license.txt */ |
| 6 | + |
| 7 | +namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
| 8 | + |
| 9 | +use Chamilo\CoreBundle\Entity\MessageRelUser; |
| 10 | +use Doctrine\DBAL\Schema\Schema; |
| 11 | +use Doctrine\Migrations\AbstractMigration; |
| 12 | + |
| 13 | +final class Version20240731120000 extends AbstractMigration |
| 14 | +{ |
| 15 | + public function getDescription(): string |
| 16 | + { |
| 17 | + return 'Add entries in message_rel_user for the sender during migration and update existing messages.'; |
| 18 | + } |
| 19 | + |
| 20 | + public function up(Schema $schema): void |
| 21 | + { |
| 22 | + $senderType = MessageRelUser::TYPE_SENDER; |
| 23 | + |
| 24 | + // Add entries for the sender in message_rel_user |
| 25 | + $this->addSql(" |
| 26 | + INSERT INTO message_rel_user (message_id, user_id, receiver_type, msg_read, starred) |
| 27 | + SELECT m.id, m.user_sender_id, $senderType, false, false |
| 28 | + FROM message m |
| 29 | + LEFT JOIN message_rel_user mru |
| 30 | + ON m.id = mru.message_id |
| 31 | + AND m.user_sender_id = mru.user_id |
| 32 | + WHERE mru.id IS NULL |
| 33 | + "); |
| 34 | + |
| 35 | + // Update message status based on message_rel_user entries |
| 36 | + $this->addSql(" |
| 37 | + UPDATE message m |
| 38 | + LEFT JOIN ( |
| 39 | + SELECT message_id, COUNT(*) AS rel_count |
| 40 | + FROM message_rel_user |
| 41 | + WHERE receiver_type = 1 |
| 42 | + GROUP BY message_id |
| 43 | + ) AS mru ON m.id = mru.message_id |
| 44 | + SET m.status = CASE |
| 45 | + WHEN mru.rel_count IS NULL THEN 3 -- Message::MESSAGE_STATUS_DELETED |
| 46 | + ELSE 0 -- Set to 0 or whatever the default status should be |
| 47 | + END |
| 48 | + "); |
| 49 | + } |
| 50 | + |
| 51 | + public function down(Schema $schema): void |
| 52 | + { |
| 53 | + $senderType = MessageRelUser::TYPE_SENDER; |
| 54 | + |
| 55 | + // Remove the entries added during the migration |
| 56 | + $this->addSql(" |
| 57 | + DELETE FROM message_rel_user |
| 58 | + WHERE receiver_type = $senderType |
| 59 | + "); |
| 60 | + } |
| 61 | +} |
0 commit comments