migrations/Version20241114200328.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 Version20241114200328 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.         // Fetch all users with non-empty params
  19.         $users $this->connection->fetchAllAssociative('SELECT id, params FROM user WHERE params IS NOT NULL');
  20.         foreach ($users as $user) {
  21.             $userId $user['id'];
  22.             $params unserialize($user['params']);
  23.             // Check if params is a valid array
  24.             if (!\is_array($params)) {
  25.                 continue;
  26.             }
  27.             // Extract all stream IDs referenced in this user's params
  28.             $streamIds $this->getStreamIdsFromParams($params);
  29.             // Fetch only the direct message streams relevant to this user
  30.             $directMessageStreamIds $this->getDirectMessageStreamIds($streamIds);
  31.             // Clean up the params for the current user
  32.             $updatedParams $this->removeDirectMessageStreams($params$directMessageStreamIds);
  33.             // Update the user params in the database if changes were made
  34.             if ($updatedParams !== $params) {
  35.                 $this->addSql('UPDATE user SET params = ? WHERE id = ?', [serialize($updatedParams), $userId]);
  36.             }
  37.         }
  38.     }
  39.     public function down(Schema $schema): void
  40.     {
  41.         // this down() migration is auto-generated, please modify it to your needs
  42.         // No rollback required
  43.     }
  44.     /**
  45.      * Extract stream IDs from user's params.
  46.      */
  47.     private function getStreamIdsFromParams(array $params): array
  48.     {
  49.         $streamIds = [];
  50.         if (isset($params['workspaces']) && \is_array($params['workspaces'])) {
  51.             foreach ($params['workspaces'] as $workspace) {
  52.                 // Collect stream IDs from favorites
  53.                 if (isset($workspace['favorites'])) {
  54.                     $streamIds array_merge($streamIds$workspace['favorites']);
  55.                 }
  56.                 // Collect stream IDs from folders
  57.                 if (isset($workspace['folders']) && \is_array($workspace['folders'])) {
  58.                     foreach ($workspace['folders'] as $folder) {
  59.                         $streamIds array_merge($streamIds$folder);
  60.                     }
  61.                 }
  62.                 // Collect stream IDs from unassigned
  63.                 if (isset($workspace['unassigned'])) {
  64.                     $streamIds array_merge($streamIds$workspace['unassigned']);
  65.                 }
  66.             }
  67.         }
  68.         return array_unique($streamIds);
  69.     }
  70.     /**
  71.      * Fetch direct message streams for a given set of stream IDs.
  72.      */
  73.     private function getDirectMessageStreamIds(array $streamIds): array
  74.     {
  75.         if (empty($streamIds)) {
  76.             return [];
  77.         }
  78.         // Fetch only the streams that are marked as direct messages
  79.         $placeholders implode(','array_fill(0, \count($streamIds), '?'));
  80.         $query "SELECT id FROM w_stream WHERE direct_message = 1 AND id IN ($placeholders)";
  81.         $streams $this->connection->fetchAllAssociative($query$streamIds);
  82.         return array_column($streams'id');
  83.     }
  84.     /**
  85.      * Remove direct message streams from user params.
  86.      */
  87.     private function removeDirectMessageStreams(array $params, array $directMessageStreamIds): array
  88.     {
  89.         if (isset($params['workspaces']) && \is_array($params['workspaces'])) {
  90.             foreach ($params['workspaces'] as &$workspace) {
  91.                 // Remove from favorites
  92.                 if (isset($workspace['favorites'])) {
  93.                     $workspace['favorites'] = array_values(
  94.                         array_diff($workspace['favorites'], $directMessageStreamIds)
  95.                     );
  96.                 }
  97.                 // Remove from folders
  98.                 if (isset($workspace['folders']) && \is_array($workspace['folders'])) {
  99.                     foreach ($workspace['folders'] as &$folder) {
  100.                         $folder array_values(
  101.                             array_diff($folder$directMessageStreamIds)
  102.                         );
  103.                     }
  104.                 }
  105.                 // Remove from unassigned streams
  106.                 if (isset($workspace['unassigned'])) {
  107.                     $workspace['unassigned'] = array_values(
  108.                         array_diff($workspace['unassigned'], $directMessageStreamIds)
  109.                     );
  110.                 }
  111.             }
  112.         }
  113.         return $params;
  114.     }
  115. }