migrations/Version20230911114136.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 Version20230911114136 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $this->addSql('SET foreign_key_checks = 0');
  18.         $offset 0;
  19.         while(true) {
  20.             // get all tasks where parent_task_id IS NULL
  21.             $query "SELECT * FROM task WHERE `parent_task_id` IS NULL LIMIT 500 OFFSET $offset";
  22.             $stmt $this->connection->prepare($query);
  23.             $queryResult $stmt->executeQuery();
  24.             $parentTasks $queryResult->fetchAllAssociative();
  25.             if (empty($parentTasks)) {
  26.                 break;
  27.             }
  28.             foreach ($parentTasks as $parentTask) {
  29.                 // for every parent_task get his child
  30.                 $parentTaskId $parentTask['id'];
  31.                 $query "SELECT * FROM task WHERE parent_task_id = $parentTaskId ORDER BY id DESC";
  32.                 $stmt $this->connection->prepare($query);
  33.                 $queryResult $stmt->executeQuery();
  34.                 $childTasks $queryResult->fetchAllAssociative();
  35.                 $mainTaskId null;
  36.                 // insert and delete all child except the last one (because that one is the newest one) into the task_history
  37.                 foreach ($childTasks as $index => $childTask) {
  38.                     if ($index === 0) {
  39.                         // this is the last one
  40.                         // update the last one to have parent_task_id === NULL
  41.                         $this->addSql("UPDATE task SET parent_task_id = -1 WHERE id = :id",
  42.                             [
  43.                                 'id' => $childTask['id']
  44.                             ]
  45.                         );
  46.                         $mainTaskId $childTask['id'];
  47.                     } else {
  48.                         $this->addSql("INSERT INTO task_history (building_id, description, status, created_at, updated_at, task_type_id, task_subtype_id, device_id, display_flat_id, email, phoneNumber, createdBy, priority, task_id, comment, change_date) 
  49.                             VALUES (:building_id, :description, :status, :created_at, :updated_at, :task_type_id, :task_subtype_id, :device_id, :display_flat_id, :email, :phoneNumber, :createdBy, :priority, :task_id, :comment, :change_date);",
  50.                             [
  51.                                 'building_id' => $childTask['building_id'],
  52.                                 'description' => $childTask['description'],
  53.                                 'status' => $childTask['status'],
  54.                                 'created_at' => $childTask['created_at'],
  55.                                 'updated_at' => $childTask['updated_at'],
  56.                                 'task_type_id' => $childTask['task_type_id'],
  57.                                 'task_subtype_id' => $childTask['task_subtype_id'],
  58.                                 'device_id' => $childTask['device_id'],
  59.                                 'display_flat_id' => $childTask['display_flat_id'],
  60.                                 'email' => $childTask['email'],
  61.                                 'phoneNumber' => $childTask['phoneNumber'],
  62.                                 'createdBy' => $childTask['createdBy'],
  63.                                 'priority' => $childTask['priority'],
  64.                                 'task_id' => $mainTaskId,
  65.                                 'comment' => $childTask['comment'],
  66.                                 'change_date' => $childTask['change_date'],
  67.                             ]
  68.                         );
  69.                         $this->addSql("DELETE FROM task WHERE id = :id", [
  70.                             'id' => $childTask['id']
  71.                         ]);
  72.                     }
  73.                 }
  74.                 // insert parent task into the task_history and delete it
  75.                 if ($mainTaskId) {
  76.                     $this->addSql("INSERT INTO task_history (building_id, description, status, created_at, updated_at, task_type_id, task_subtype_id, device_id, display_flat_id, email, phoneNumber, createdBy, priority, task_id, comment, change_date) 
  77.                             VALUES (:building_id, :description, :status, :created_at, :updated_at, :task_type_id, :task_subtype_id, :device_id, :display_flat_id, :email, :phoneNumber, :createdBy, :priority, :task_id, :comment, :change_date);",
  78.                         [
  79.                             'building_id' => $parentTask['building_id'],
  80.                             'description' => $parentTask['description'],
  81.                             'status' => $parentTask['status'],
  82.                             'created_at' => $parentTask['created_at'],
  83.                             'updated_at' => $parentTask['updated_at'],
  84.                             'task_type_id' => $parentTask['task_type_id'],
  85.                             'task_subtype_id' => $parentTask['task_subtype_id'],
  86.                             'device_id' => $parentTask['device_id'],
  87.                             'display_flat_id' => $parentTask['display_flat_id'],
  88.                             'email' => $parentTask['email'],
  89.                             'phoneNumber' => $parentTask['phoneNumber'],
  90.                             'createdBy' => $parentTask['createdBy'],
  91.                             'priority' => $parentTask['priority'],
  92.                             'task_id' => $mainTaskId,
  93.                             'comment' => $parentTask['comment'],
  94.                             'change_date' => $parentTask['change_date'],
  95.                         ]
  96.                     );
  97.                     $this->addSql("DELETE FROM task WHERE id = :id", [
  98.                         'id' => $parentTask['id']
  99.                     ]);
  100.                 }
  101.             }
  102.             $offset += 500;
  103.         }
  104. //        $this->addSql('ALTER TABLE task DROP FOREIGN KEY FK_527EDB25FFFE75C0');
  105. //        $this->addSql('DROP INDEX IDX_527EDB25FFFE75C0 ON task');
  106. //        $this->addSql('ALTER TABLE task DROP parent_task_id');
  107.         $this->addSql('SET foreign_key_checks = 1');
  108.     }
  109.     public function down(Schema $schema): void
  110.     {
  111.         // this down() migration is auto-generated, please modify it to your needs
  112.     }
  113. }