migrations/Version20241227210638.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. /**
  8.  * Auto-generated Migration: Please modify to your needs!
  9.  */
  10. final class Version20241227210638 extends AbstractMigration
  11. {
  12.     public function getDescription(): string
  13.     {
  14.         return '';
  15.     }
  16.     public function up(Schema $schema): void
  17.     {
  18.         // this up() migration is auto-generated, please modify it to your needs
  19.         // Fetch all users with non-empty params
  20.         $users $this->connection->fetchAllAssociative('SELECT id, params FROM user WHERE params IS NOT NULL');
  21.         foreach ($users as $user) {
  22.             $userId $user['id'];
  23.             $params unserialize($user['params']);
  24.             // Check if params is a valid array
  25.             if (!\is_array($params)) {
  26.                 continue;
  27.             }
  28.             // Extract all stream IDs referenced in this user's params
  29.             $streamIds $this->getStreamIdsFromParams($params);
  30.             // Clone params to track updates
  31.             $updatedParams $params;
  32.             // Add "streams" property to each workspace if not already present
  33.             if (isset($params['workspaces']) && \is_array($updatedParams['workspaces'])) {
  34.                 foreach ($updatedParams['workspaces'] as &$workspace) {
  35.                     // Ensure streams is set for the workspace
  36.                     $workspace['streams'] = $workspace['streams'] ?? $streamIds;
  37.                 }
  38.             }
  39.             // Update the user params in the database if changes were made
  40.             if ($updatedParams !== $params) {
  41.                 $this->addSql('UPDATE user SET params = ? WHERE id = ?', [serialize($updatedParams), $userId]);
  42.             }
  43.         }
  44.     }
  45.     public function down(Schema $schema): void
  46.     {
  47.         // this down() migration is auto-generated, please modify it to your needs
  48.         // Fetch all users with non-empty params
  49.         $users $this->connection->fetchAllAssociative('SELECT id, params FROM user WHERE params IS NOT NULL');
  50.         foreach ($users as $user) {
  51.             $userId $user['id'];
  52.             $params unserialize($user['params']);
  53.             // Check if params is a valid array
  54.             if (!\is_array($params)) {
  55.                 continue;
  56.             }
  57.             // Clone params to track updates
  58.             $updatedParams $params;
  59.             // Remove "streams" property from each workspace if present
  60.             if (isset($params['workspaces']) && \is_array($updatedParams['workspaces'])) {
  61.                 foreach ($updatedParams['workspaces'] as &$workspace) {
  62.                     // Remove the "streams" property if it exists
  63.                     if (isset($workspace['streams'])) {
  64.                         unset($workspace['streams']);
  65.                     }
  66.                 }
  67.             }
  68.             // Update the user params in the database if changes were made
  69.             if ($updatedParams !== $params) {
  70.                 $this->addSql('UPDATE user SET params = ? WHERE id = ?', [serialize($updatedParams), $userId]);
  71.             }
  72.         }
  73.     }
  74.     /**
  75.      * Extract stream IDs from user's params.
  76.      */
  77.     private function getStreamIdsFromParams(array $params): array
  78.     {
  79.         $streamIds = [];
  80.         if (isset($params['workspaces']) && \is_array($params['workspaces'])) {
  81.             foreach ($params['workspaces'] as $workspace) {
  82.                 // Collect stream IDs from favorites
  83.                 if (isset($workspace['favorites'])) {
  84.                     $streamIds array_merge($streamIds$workspace['favorites']);
  85.                 }
  86.                 // Collect stream IDs from folders
  87.                 if (isset($workspace['folders']) && \is_array($workspace['folders'])) {
  88.                     foreach ($workspace['folders'] as $folder) {
  89.                         $streamIds array_merge($streamIds$folder);
  90.                     }
  91.                 }
  92.                 // Collect stream IDs from unassigned
  93.                 if (isset($workspace['unassigned'])) {
  94.                     $streamIds array_merge($streamIds$workspace['unassigned']);
  95.                 }
  96.             }
  97.         }
  98.         // Remove duplicates
  99.         $streamIds array_unique($streamIds);
  100.         // Fetch and sort streams by modified_at
  101.         $streams $this->connection->fetchAllAssociative(
  102.             'SELECT id, modified_at FROM w_stream WHERE id IN (:ids) ORDER BY modified_at DESC',
  103.             ['ids' => $streamIds],
  104.             ['ids' => Connection::PARAM_STR_ARRAY]
  105.         );
  106.         // Return sorted stream IDs
  107.         return array_column($streams'id');
  108.     }
  109. }