migrations/Version20250216154539.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Service\FileUploader;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  8. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  9. use Symfony\Component\Uid\Uuid;
  10. /**
  11.  * Auto-generated Migration: Please modify to your needs!
  12.  */
  13. final class Version20250216154539 extends AbstractMigration implements ContainerAwareInterface
  14. {
  15.     use ContainerAwareTrait;
  16.     public function getDescription(): string
  17.     {
  18.         return '';
  19.     }
  20.     public function up(Schema $schema): void
  21.     {
  22.         // this up() migration is auto-generated, please modify it to your needs
  23.         $this->addSql('ALTER TABLE ws_whiteboard ADD photo VARCHAR(255) DEFAULT NULL');
  24.         $whiteboards $this->connection->fetchAllAssociative('
  25.             SELECT ws_whiteboard.id, ws_whiteboard.svg, workspace.owner_id
  26.             FROM ws_whiteboard
  27.                 LEFT JOIN w_stream ON (ws_whiteboard.stream_id = w_stream.id)
  28.                 LEFT JOIN workspace ON (w_stream.workspace_id = workspace.id)
  29.             WHERE LENGTH(ws_whiteboard.svg) > 0
  30.         ');
  31.         foreach ($whiteboards as $whiteboard) {
  32.             if (!str_contains($whiteboard['svg'], '<svg')) {
  33.                 continue;
  34.             }
  35.             try {
  36.                 $fileName sprintf('%s.svg'Uuid::v4());
  37.                 $fileName $this->getFileUploader()->create($fileName$whiteboard['owner_id'], $whiteboard['svg']);
  38.             } catch (\Exception $e) {
  39.                 continue;
  40.             }
  41.             $this->addSql('UPDATE ws_whiteboard SET photo = ? WHERE id = ?', [$fileName$whiteboard['id']]);
  42.         }
  43.         $this->addSql('ALTER TABLE ws_whiteboard DROP svg');
  44.     }
  45.     public function down(Schema $schema): void
  46.     {
  47.         // this down() migration is auto-generated, please modify it to your needs
  48.         $this->addSql('ALTER TABLE ws_whiteboard ADD svg LONGTEXT DEFAULT NULL');
  49.         $whiteboards $this->connection->fetchAllAssociative('
  50.             SELECT id, photo FROM ws_whiteboard WHERE LENGTH(photo) > 0
  51.         ');
  52.         foreach ($whiteboards as $whiteboard) {
  53.             try {
  54.                 $svgContent $this->getFileUploader()->getContent($whiteboard['photo']);
  55.                 $this->getFileUploader()->delete($whiteboard['photo']);
  56.             } catch (\Exception $e) {
  57.                 continue;
  58.             }
  59.             $this->addSql('UPDATE ws_whiteboard SET svg = ? WHERE id = ?', [$svgContent$whiteboard['id']]);
  60.         }
  61.         $this->addSql('ALTER TABLE ws_whiteboard DROP photo');
  62.     }
  63.     /**
  64.      * @return FileUploader
  65.      */
  66.     private function getFileUploader(): FileUploader
  67.     {
  68.         return $this->container->get(FileUploader::class);
  69.     }
  70. }