<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241227210638 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
// Fetch all users with non-empty params
$users = $this->connection->fetchAllAssociative('SELECT id, params FROM user WHERE params IS NOT NULL');
foreach ($users as $user) {
$userId = $user['id'];
$params = unserialize($user['params']);
// Check if params is a valid array
if (!\is_array($params)) {
continue;
}
// Extract all stream IDs referenced in this user's params
$streamIds = $this->getStreamIdsFromParams($params);
// Clone params to track updates
$updatedParams = $params;
// Add "streams" property to each workspace if not already present
if (isset($params['workspaces']) && \is_array($updatedParams['workspaces'])) {
foreach ($updatedParams['workspaces'] as &$workspace) {
// Ensure streams is set for the workspace
$workspace['streams'] = $workspace['streams'] ?? $streamIds;
}
}
// Update the user params in the database if changes were made
if ($updatedParams !== $params) {
$this->addSql('UPDATE user SET params = ? WHERE id = ?', [serialize($updatedParams), $userId]);
}
}
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
// Fetch all users with non-empty params
$users = $this->connection->fetchAllAssociative('SELECT id, params FROM user WHERE params IS NOT NULL');
foreach ($users as $user) {
$userId = $user['id'];
$params = unserialize($user['params']);
// Check if params is a valid array
if (!\is_array($params)) {
continue;
}
// Clone params to track updates
$updatedParams = $params;
// Remove "streams" property from each workspace if present
if (isset($params['workspaces']) && \is_array($updatedParams['workspaces'])) {
foreach ($updatedParams['workspaces'] as &$workspace) {
// Remove the "streams" property if it exists
if (isset($workspace['streams'])) {
unset($workspace['streams']);
}
}
}
// Update the user params in the database if changes were made
if ($updatedParams !== $params) {
$this->addSql('UPDATE user SET params = ? WHERE id = ?', [serialize($updatedParams), $userId]);
}
}
}
/**
* Extract stream IDs from user's params.
*/
private function getStreamIdsFromParams(array $params): array
{
$streamIds = [];
if (isset($params['workspaces']) && \is_array($params['workspaces'])) {
foreach ($params['workspaces'] as $workspace) {
// Collect stream IDs from favorites
if (isset($workspace['favorites'])) {
$streamIds = array_merge($streamIds, $workspace['favorites']);
}
// Collect stream IDs from folders
if (isset($workspace['folders']) && \is_array($workspace['folders'])) {
foreach ($workspace['folders'] as $folder) {
$streamIds = array_merge($streamIds, $folder);
}
}
// Collect stream IDs from unassigned
if (isset($workspace['unassigned'])) {
$streamIds = array_merge($streamIds, $workspace['unassigned']);
}
}
}
// Remove duplicates
$streamIds = array_unique($streamIds);
// Fetch and sort streams by modified_at
$streams = $this->connection->fetchAllAssociative(
'SELECT id, modified_at FROM w_stream WHERE id IN (:ids) ORDER BY modified_at DESC',
['ids' => $streamIds],
['ids' => Connection::PARAM_STR_ARRAY]
);
// Return sorted stream IDs
return array_column($streams, 'id');
}
}