migrations/Version20181231180122.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 Version20181231180122 extends AbstractMigration
  10. {
  11.     public function up(Schema $schema): void
  12.     {
  13.         // this up() migration is auto-generated, please modify it to your needs
  14.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  15.         $this->addSql('
  16.             CREATE TABLE chat_participant (
  17.                 id INT AUTO_INCREMENT NOT NULL,
  18.                 chat_id INT DEFAULT NULL,
  19.                 user_id INT DEFAULT NULL,
  20.                 open TINYINT(1) NOT NULL,
  21.                 unread INT DEFAULT NULL,
  22.                 INDEX IDX_E8ED9C891A9A7125 (chat_id),
  23.                 INDEX IDX_E8ED9C89A76ED395 (user_id),
  24.                 PRIMARY KEY(id)
  25.             ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB
  26.         ');
  27.         $stmt $this->connection->prepare('SELECT id, participants FROM chats');
  28.         $stmt->execute();
  29.         $groups $this->getUsersByGroup();
  30.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  31.             $participants $row->participants unserialize($row->participants) : [];
  32.             if ($participants && \count($participants)) {
  33.                 $users = [];
  34.                 foreach ($participants as $key => $value) {
  35.                     if (\is_string($key) && 'g' === substr($key01)) {
  36.                         if (isset($groups[substr($key1)])) {
  37.                             foreach ($groups[substr($key1)] as $userId) {
  38.                                 if (!\in_array($userId$users)) {
  39.                                     $users[$userId] = [
  40.                                         'open' => true,
  41.                                         'unread' => 1,
  42.                                     ];
  43.                                 }
  44.                             }
  45.                         }
  46.                     } else {
  47.                         $users[$key] = $value;
  48.                     }
  49.                 }
  50.                 foreach ($users as $key => $value) {
  51.                     $this->addSql('INSERT INTO chat_participant (chat_id, user_id, open, unread) VALUES (:chat_id, :user_id, :open, :unread)', [
  52.                         'chat_id' => $row->id,
  53.                         'user_id' => $key,
  54.                         'open' => (int) ($value['open'] ?? true),
  55.                         'unread' => (int) ($value['unread'] ?? 0),
  56.                     ]);
  57.                 }
  58.             }
  59.         }
  60.         $this->addSql('RENAME TABLE chats TO chat');
  61.         $this->addSql('ALTER TABLE chat ENGINE = InnoDB');
  62.         $this->addSql('ALTER TABLE chat CHARACTER SET = utf8mb4');
  63.         $this->addSql('
  64.             ALTER TABLE chat
  65.                 DROP participants,
  66.                 ADD user_id INT DEFAULT NULL,
  67.                 CHANGE date_added created_at DATETIME NOT NULL,
  68.                 CHANGE last_modified modified_at DATETIME NOT NULL
  69.         ');
  70.         $this->addSql('CREATE INDEX IDX_659DF2AAA76ED395 ON chat (user_id)');
  71.         // get chat creator (user of the first message sent)
  72.         $stmt $this->connection->prepare('SELECT chats_id, administrators_id FROM chats_messages GROUP BY chats_id ORDER BY date_added');
  73.         $stmt->execute();
  74.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  75.             $this->addSql('UPDATE chat SET user_id = :user_id WHERE id = :id', [
  76.                 'id' => $row->chats_id,
  77.                 'user_id' => $row->administrators_id,
  78.             ]);
  79.         }
  80.         $this->addSql('RENAME TABLE chats_messages TO chat_message');
  81.         $this->addSql('ALTER TABLE chat_message ENGINE = InnoDB');
  82.         $this->addSql('ALTER TABLE chat_message CHARACTER SET = utf8mb4');
  83.         $this->addSql('DELETE FROM chat_message WHERE message IS NULL OR message = \'\'');
  84.         $this->addSql('
  85.             ALTER TABLE chat_message
  86.                 CHANGE chats_id chat_id INT DEFAULT NULL,
  87.                 CHANGE administrators_id user_id INT DEFAULT NULL,
  88.                 CHANGE message body LONGTEXT NOT NULL,
  89.                 CHANGE file file VARCHAR(255) DEFAULT NULL,
  90.                 CHANGE date_added created_at DATETIME NOT NULL
  91.         ');
  92.         $this->addSql('DROP INDEX idx_chats_id ON chat_message');
  93.         $this->addSql('DROP INDEX idx_administrators_id ON chat_message');
  94.         $this->addSql('CREATE INDEX IDX_FAB3FC161A9A7125 ON chat_message (chat_id)');
  95.         $this->addSql('CREATE INDEX IDX_FAB3FC16A76ED395 ON chat_message (user_id)');
  96.         $this->addSql('DELETE FROM chat WHERE user_id IS NULL');
  97.         $this->addSql('UPDATE chat SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
  98.         $this->addSql('DELETE FROM chat_message WHERE chat_id NOT IN (SELECT id FROM chat)');
  99.         $this->addSql('UPDATE chat_message SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
  100.         $this->addSql('DELETE FROM chat_participant WHERE chat_id NOT IN (SELECT id FROM chat)');
  101.         $this->addSql('DELETE FROM chat_participant WHERE user_id NOT IN (SELECT id FROM user)');
  102.         $this->addSql('ALTER TABLE chat ADD CONSTRAINT FK_659DF2AAA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
  103.         $this->addSql('ALTER TABLE chat_message ADD CONSTRAINT FK_FAB3FC161A9A7125 FOREIGN KEY (chat_id) REFERENCES chat (id) ON DELETE CASCADE');
  104.         $this->addSql('ALTER TABLE chat_message ADD CONSTRAINT FK_FAB3FC16A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
  105.         $this->addSql('ALTER TABLE chat_participant ADD CONSTRAINT FK_E8ED9C891A9A7125 FOREIGN KEY (chat_id) REFERENCES chat (id) ON DELETE CASCADE');
  106.         $this->addSql('ALTER TABLE chat_participant ADD CONSTRAINT FK_E8ED9C89A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE');
  107.     }
  108.     public function down(Schema $schema): void
  109.     {
  110.         // this down() migration is auto-generated, please modify it to your needs
  111.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  112.         $this->addSql('ALTER TABLE chat_participant DROP FOREIGN KEY FK_E8ED9C89A76ED395');
  113.         $this->addSql('ALTER TABLE chat_participant DROP FOREIGN KEY FK_E8ED9C891A9A7125');
  114.         $this->addSql('ALTER TABLE chat_message DROP FOREIGN KEY FK_FAB3FC16A76ED395');
  115.         $this->addSql('ALTER TABLE chat_message DROP FOREIGN KEY FK_FAB3FC161A9A7125');
  116.         $this->addSql('ALTER TABLE chat DROP FOREIGN KEY FK_659DF2AAA76ED395');
  117.         $this->addSql('RENAME TABLE chat TO chats');
  118.         $this->addSql('ALTER TABLE chats ENGINE = MyISAM');
  119.         $this->addSql('ALTER TABLE chats CHARACTER SET = latin1');
  120.         $this->addSql('
  121.             ALTER TABLE chats
  122.                 DROP user_id,
  123.                 ADD participants LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:array)\' AFTER id,
  124.                 CHANGE created_at date_added DATETIME DEFAULT NULL,
  125.                 CHANGE modified_at last_modified DATETIME DEFAULT NULL
  126.         ');
  127.         $stmt $this->connection->prepare('SELECT * FROM chat_participant');
  128.         $stmt->execute();
  129.         $chats = [];
  130.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  131.             $chats[$row->chat_id][] = [
  132.                 $row->user_id = [
  133.                     'open' => $row->open,
  134.                     'unread' => $row->unread,
  135.                 ],
  136.             ];
  137.         }
  138.         foreach ($chats as $key => $value) {
  139.             $participants serialize($value);
  140.             $this->addSql('UPDATE chats SET participants = :participants WHERE id = :id', [
  141.                 'participants' => $participants,
  142.                 'id' => $key,
  143.             ]);
  144.         }
  145.         $this->addSql('DROP TABLE chat_participant');
  146.         $this->addSql('RENAME TABLE chat_message TO chats_messages');
  147.         $this->addSql('ALTER TABLE chats_messages ENGINE = InnoDB');
  148.         $this->addSql('ALTER TABLE chats_messages CHARACTER SET = utf8mb4');
  149.         $this->addSql('
  150.             ALTER TABLE chats_messages
  151.                 CHANGE chat_id chats_id INT NOT NULL,
  152.                 CHANGE user_id administrators_id INT NOT NULL,
  153.                 CHANGE body message LONGTEXT DEFAULT NULL,
  154.                 CHANGE file file VARCHAR(255) DEFAULT NULL,
  155.                 CHANGE created_at date_added DATETIME DEFAULT NULL
  156.         ');
  157.         $this->addSql('DROP INDEX IDX_FAB3FC161A9A7125 ON chats_messages');
  158.         $this->addSql('DROP INDEX IDX_FAB3FC16A76ED395 ON chats_messages');
  159.         $this->addSql('CREATE INDEX idx_chats_id ON chats_messages (chats_id)');
  160.         $this->addSql('CREATE INDEX idx_administrators_id ON chats_messages (administrators_id)');
  161.     }
  162.     /**
  163.      * @return array
  164.      */
  165.     private function getUsersByGroup(): array
  166.     {
  167.         $groups = [];
  168.         $stmt $this->connection->prepare('SELECT u.id, ujg.group_id FROM user u LEFT JOIN user_join_group ujg ON (u.id = ujg.user_id) WHERE u.enabled = 1');
  169.         $stmt->execute();
  170.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  171.             $groups[$row->group_id][] = $row->id;
  172.         }
  173.         return $groups;
  174.     }
  175. }