migrations/Version20250719170538.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 Version20250719170538 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Correct des etablissements sans utilisateur';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // UNIQUE temporary password for this migration
  18.         $tempPassword 'TEMP_MIGRATION_' date('YmdHis') . '_' uniqid();
  19.         $finalPassword '$2y$13$Kvhb8l5ViHYWcS74ovLLGO4VohK3C.C20nM5KOTC4CmvBfBqGpyZe';
  20.         // Table to save new users
  21.         $this->addSql('CREATE TEMPORARY TABLE temp_new_users (id INT PRIMARY KEY, phone_standard VARCHAR(255))');
  22.         // 1. Création des utilisateurs avec mot de passe temporaire UNIQUE
  23.         $this->addSql("
  24.             INSERT INTO user (
  25.                 country_id,
  26.                 region_id,
  27.                 residence_country_id,
  28.                 residence_region_id,
  29.                 username,
  30.                 roles,
  31.                 password,
  32.                 phone,
  33.                 enabled,
  34.                 email,
  35.                 email_canonical,
  36.                 username_canonical,
  37.                 diaspora
  38.             )
  39.             SELECT 
  40.                 s.id_country AS country_id,
  41.                 s.id_region AS region_id,
  42.                 s.id_country AS residence_country_id,
  43.                 s.id_region AS residence_region_id,
  44.                 CAST(s.phone_standard AS CHAR) AS username,
  45.                 'a:0:{}' AS roles,
  46.                 ? AS password,
  47.                 CAST(s.phone_standard AS CHAR) AS phone,
  48.                 1 AS enabled,
  49.                 CAST(s.email AS CHAR) AS email,
  50.                 CAST(s.email AS CHAR) AS email_canonical,
  51.                 CAST(s.phone_standard AS CHAR) AS username_canonical,
  52.                 0 AS diaspora
  53.             FROM school s
  54.             WHERE BINARY s.phone_standard NOT IN (
  55.                 SELECT DISTINCT BINARY u.username FROM user u WHERE u.username IS NOT NULL
  56.             )
  57.             AND BINARY s.email NOT IN (
  58.                 SELECT DISTINCT BINARY u.email FROM user u WHERE u.email IS NOT NULL
  59.             )
  60.             AND s.phone_standard IS NOT NULL
  61.             AND s.phone_standard != ''
  62.             AND s.email IS NOT NULL
  63.             AND s.email != ''
  64.         ", [$tempPassword]);
  65.         // 2. Retrieving IDs of newly created users (with temporary password)
  66.         $this->addSql("
  67.             INSERT INTO temp_new_users (id, phone_standard)
  68.             SELECT u.id, CAST(u.phone AS CHAR)
  69.             FROM user u
  70.             WHERE BINARY u.password = ?
  71.         ", [$tempPassword]);
  72.         // 3. Update with real password
  73.         $this->addSql("
  74.             UPDATE user u
  75.             JOIN temp_new_users t ON BINARY t.phone_standard = BINARY u.phone
  76.             SET u.password = ?
  77.         ", [$finalPassword]);
  78.         // 4. Assigning the ROLE_ESTABLISSEMENT role to new users
  79.         $this->addSql("
  80.             INSERT INTO user_role (user_id, role_id)
  81.             SELECT 
  82.                 t.id AS user_id,
  83.                 (SELECT id FROM role WHERE role = 'ROLE_ETABLISSEMENT' LIMIT 1) AS role_id
  84.             FROM temp_new_users t
  85.             WHERE NOT EXISTS (
  86.                 SELECT 1 FROM user_role ur 
  87.                 WHERE ur.user_id = t.id 
  88.                 AND ur.role_id = (SELECT id FROM role WHERE role = 'ROLE_ETABLISSEMENT' LIMIT 1)
  89.             )
  90.         ");
  91.         // 5. Updating schools with corresponding user IDs
  92.         $this->addSql("
  93.             UPDATE school s
  94.             JOIN temp_new_users t ON BINARY t.phone_standard = BINARY s.phone_standard
  95.             SET s.user_id = t.id
  96.         ");
  97.         // Cleaning the temporary table
  98.         $this->addSql('DROP TEMPORARY TABLE temp_new_users');
  99.     }
  100.     public function down(Schema $schema): void
  101.     {
  102.         $finalPassword '$2y$13$Kvhb8l5ViHYWcS74ovLLGO4VohK3C.C20nM5KOTC4CmvBfBqGpyZe';
  103.         // Création de la table temporaire pour le rollback
  104.         $this->addSql('CREATE TEMPORARY TABLE temp_users_to_delete (id INT PRIMARY KEY)');
  105.         $this->addSql("
  106.             INSERT INTO temp_users_to_delete (id)
  107.             SELECT DISTINCT u.id 
  108.             FROM user u
  109.             JOIN school s ON BINARY u.phone = BINARY s.phone_standard
  110.             WHERE BINARY u.password = ?
  111.             AND EXISTS (
  112.                 SELECT 1 FROM user_role ur 
  113.                 JOIN role r ON ur.role_id = r.id 
  114.                 WHERE ur.user_id = u.id 
  115.                 AND r.role = 'ROLE_ETABLISSEMENT'
  116.             )
  117.             AND BINARY u.username = BINARY u.phone
  118.             AND BINARY u.email_canonical = BINARY u.email
  119.         ", [$finalPassword]);
  120.         $this->addSql("
  121.             UPDATE school s
  122.             SET s.user_id = NULL 
  123.             WHERE s.user_id IN (SELECT id FROM temp_users_to_delete)
  124.         ");
  125.         $this->addSql("
  126.             DELETE ur FROM user_role ur
  127.             JOIN temp_users_to_delete t ON ur.user_id = t.id
  128.         ");
  129.         $this->addSql("
  130.             DELETE u FROM user u
  131.             JOIN temp_users_to_delete t ON u.id = t.id
  132.         ");
  133.         $this->addSql('DROP TEMPORARY TABLE temp_users_to_delete');
  134.     }
  135. }