<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use App\Service\FileUploader;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Uid\Uuid;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250216154539 extends AbstractMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE ws_whiteboard ADD photo VARCHAR(255) DEFAULT NULL');
$whiteboards = $this->connection->fetchAllAssociative('
SELECT ws_whiteboard.id, ws_whiteboard.svg, workspace.owner_id
FROM ws_whiteboard
LEFT JOIN w_stream ON (ws_whiteboard.stream_id = w_stream.id)
LEFT JOIN workspace ON (w_stream.workspace_id = workspace.id)
WHERE LENGTH(ws_whiteboard.svg) > 0
');
foreach ($whiteboards as $whiteboard) {
if (!str_contains($whiteboard['svg'], '<svg')) {
continue;
}
try {
$fileName = sprintf('%s.svg', Uuid::v4());
$fileName = $this->getFileUploader()->create($fileName, $whiteboard['owner_id'], $whiteboard['svg']);
} catch (\Exception $e) {
continue;
}
$this->addSql('UPDATE ws_whiteboard SET photo = ? WHERE id = ?', [$fileName, $whiteboard['id']]);
}
$this->addSql('ALTER TABLE ws_whiteboard DROP svg');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE ws_whiteboard ADD svg LONGTEXT DEFAULT NULL');
$whiteboards = $this->connection->fetchAllAssociative('
SELECT id, photo FROM ws_whiteboard WHERE LENGTH(photo) > 0
');
foreach ($whiteboards as $whiteboard) {
try {
$svgContent = $this->getFileUploader()->getContent($whiteboard['photo']);
$this->getFileUploader()->delete($whiteboard['photo']);
} catch (\Exception $e) {
continue;
}
$this->addSql('UPDATE ws_whiteboard SET svg = ? WHERE id = ?', [$svgContent, $whiteboard['id']]);
}
$this->addSql('ALTER TABLE ws_whiteboard DROP photo');
}
/**
* @return FileUploader
*/
private function getFileUploader(): FileUploader
{
return $this->container->get(FileUploader::class);
}
}