migrations/Version20250109222633.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20250109222633 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // this up() migration is auto-generated, please modify it to your needs
  18.         $this->addSql('CREATE TABLE user_join_level (user_id INT NOT NULL, level_id INT NOT NULL, INDEX IDX_B4CF7952A76ED395 (user_id), INDEX IDX_B4CF79525FB14BA7 (level_id), PRIMARY KEY(user_id, level_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  19.         $this->addSql('ALTER TABLE user_join_level ADD CONSTRAINT FK_B4CF7952A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE');
  20.         $this->addSql('ALTER TABLE user_join_level ADD CONSTRAINT FK_B4CF79525FB14BA7 FOREIGN KEY (level_id) REFERENCES config_level (id) ON DELETE CASCADE');
  21.         $users $this->connection->fetchAllAssociative('SELECT id, levels FROM user WHERE levels IS NOT NULL');
  22.         foreach ($users as $user) {
  23.             $userId $user['id'];
  24.             $levelsArray unserialize($user['levels']);
  25.             if (!\is_array($levelsArray)) {
  26.                 continue;
  27.             }
  28.             foreach ($levelsArray as $levelName) {
  29.                 switch ($levelName) {
  30.                     case 'red'$levelName 'ruby'; break;
  31.                     case 'copper'$levelName 'sapphire'; break;
  32.                     case 'bronze'$levelName 'emerald'; break;
  33.                 }
  34.                 $levelId $this->connection->fetchOne('SELECT id FROM config_level WHERE name = ?', [$levelName]);
  35.                 if ($levelId) {
  36.                     $this->addSql('INSERT INTO user_join_level (user_id, level_id) VALUES (?, ?)', [$userId$levelId]);
  37.                 }
  38.             }
  39.         }
  40.         $this->addSql('ALTER TABLE user DROP levels');
  41.     }
  42.     public function down(Schema $schema): void
  43.     {
  44.         // this down() migration is auto-generated, please modify it to your needs
  45.         $this->addSql('ALTER TABLE user ADD levels LONGTEXT DEFAULT NULL COLLATE `utf8mb4_general_ci` COMMENT \'(DC2Type:array)\'');
  46.         $userLevels $this->connection->fetchAllAssociative('SELECT user_id, level_id FROM user_join_level');
  47.         $reverseMapping = [
  48.             'ruby' => 'red',
  49.             'sapphire' => 'copper',
  50.             'emerald' => 'bronze',
  51.         ];
  52.         $userLevelsMap = [];
  53.         foreach ($userLevels as $userLevel) {
  54.             $userId $userLevel['user_id'];
  55.             $levelName $this->connection->fetchOne('SELECT name FROM config_level WHERE id = ?', [$userLevel['level_id']]);
  56.             if ($levelName) {
  57.                 $levelName strtolower($levelName);
  58.                 $levelName $reverseMapping[$levelName] ?? $levelName;
  59.                 if (!isset($userLevelsMap[$userId])) {
  60.                     $userLevelsMap[$userId] = [];
  61.                 }
  62.                 $userLevelsMap[$userId][] = $levelName;
  63.             }
  64.         }
  65.         foreach ($userLevelsMap as $userId => $levels) {
  66.             $serializedLevels serialize($levels);
  67.             $this->addSql('UPDATE user SET levels = ? WHERE id = ?', [$serializedLevels$userId]);
  68.         }
  69.         $this->addSql('DROP TABLE user_join_level');
  70.     }
  71. }