<?php
declare(strict_types=1);
namespace ProjectsBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20240422074343 extends AbstractMigration
{
private array $links = [];
public function preUp(Schema $schema): void
{
$this->links = $this->connection->executeQuery(
'SELECT id, drive_link FROM projects_bundle__project'
)->fetchAllAssociative();
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE projects_bundle__project ADD links JSON DEFAULT NULL, DROP drive_link');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE projects_bundle__project ADD drive_link VARCHAR(255) DEFAULT NULL, DROP links');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$conn = $this->connection;
foreach ($this->links as $link) {
$conn->executeStatement(
'UPDATE projects_bundle__project SET links = :links WHERE id = :id',
[
'links' => json_encode([['type' => 'drive', 'link' => $link['drive_link']]]),
'id' => $link['id']
]
);
}
}
}