12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- declare(strict_types=1);
- namespace DoctrineMigrations;
- use Doctrine\DBAL\Schema\Schema;
- use Doctrine\Migrations\AbstractMigration;
- /**
- * Auto-generated Migration: Please modify to your needs!
- */
- final class Version20240803144323 extends AbstractMigration
- {
- public function getDescription(): string
- {
- return '';
- }
- public function up(Schema $schema): void
- {
- $this->addSql('
- ALTER TABLE user MODIFY roles LONGTEXT
- ');
- // Fetch all users to update their roles
- $users = $this->connection->fetchAllAssociative('SELECT id, roles FROM user');
-
- foreach ($users as $user) {
- $roles = unserialize($user['roles']);
- $jsonRoles = json_encode($roles);
- $this->addSql('UPDATE user SET roles = ? WHERE id = ?', [$jsonRoles, $user['id']]);
- }
- $this->addSql('
- ALTER TABLE user MODIFY roles JSON
- ');
- // this up() migration is auto-generated, please modify it to your needs
- $this->addSql('ALTER TABLE user CHANGE roles roles JSON NOT NULL');
- // Convert the roles column data from longtext to JSON
- }
- public function down(Schema $schema): void
- {
- // this down() migration is auto-generated, please modify it to your needs
- // Reverse the conversion if necessary
- $users = $this->connection->fetchAllAssociative('SELECT id, roles FROM user');
-
- foreach ($users as $user) {
- $roles = json_decode($user['roles'], true);
- $serializedRoles = serialize($roles);
- $this->addSql('UPDATE user SET roles = ? WHERE id = ?', [$serializedRoles, $user['id']]);
- }
- $this->addSql('
- ALTER TABLE user MODIFY roles LONGTEXT
- ');
- $this->addSql('ALTER TABLE user CHANGE roles roles LONGTEXT NOT NULL COMMENT \'(DC2Type:array)\'');
- }
- }
|