<?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 Version20230911114136 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
$this->addSql('SET foreign_key_checks = 0');
$offset = 0;
while(true) {
// get all tasks where parent_task_id IS NULL
$query = "SELECT * FROM task WHERE `parent_task_id` IS NULL LIMIT 500 OFFSET $offset";
$stmt = $this->connection->prepare($query);
$queryResult = $stmt->executeQuery();
$parentTasks = $queryResult->fetchAllAssociative();
if (empty($parentTasks)) {
break;
}
foreach ($parentTasks as $parentTask) {
// for every parent_task get his child
$parentTaskId = $parentTask['id'];
$query = "SELECT * FROM task WHERE parent_task_id = $parentTaskId ORDER BY id DESC";
$stmt = $this->connection->prepare($query);
$queryResult = $stmt->executeQuery();
$childTasks = $queryResult->fetchAllAssociative();
$mainTaskId = null;
// insert and delete all child except the last one (because that one is the newest one) into the task_history
foreach ($childTasks as $index => $childTask) {
if ($index === 0) {
// this is the last one
// update the last one to have parent_task_id === NULL
$this->addSql("UPDATE task SET parent_task_id = -1 WHERE id = :id",
[
'id' => $childTask['id']
]
);
$mainTaskId = $childTask['id'];
} else {
$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)
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);",
[
'building_id' => $childTask['building_id'],
'description' => $childTask['description'],
'status' => $childTask['status'],
'created_at' => $childTask['created_at'],
'updated_at' => $childTask['updated_at'],
'task_type_id' => $childTask['task_type_id'],
'task_subtype_id' => $childTask['task_subtype_id'],
'device_id' => $childTask['device_id'],
'display_flat_id' => $childTask['display_flat_id'],
'email' => $childTask['email'],
'phoneNumber' => $childTask['phoneNumber'],
'createdBy' => $childTask['createdBy'],
'priority' => $childTask['priority'],
'task_id' => $mainTaskId,
'comment' => $childTask['comment'],
'change_date' => $childTask['change_date'],
]
);
$this->addSql("DELETE FROM task WHERE id = :id", [
'id' => $childTask['id']
]);
}
}
// insert parent task into the task_history and delete it
if ($mainTaskId) {
$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)
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);",
[
'building_id' => $parentTask['building_id'],
'description' => $parentTask['description'],
'status' => $parentTask['status'],
'created_at' => $parentTask['created_at'],
'updated_at' => $parentTask['updated_at'],
'task_type_id' => $parentTask['task_type_id'],
'task_subtype_id' => $parentTask['task_subtype_id'],
'device_id' => $parentTask['device_id'],
'display_flat_id' => $parentTask['display_flat_id'],
'email' => $parentTask['email'],
'phoneNumber' => $parentTask['phoneNumber'],
'createdBy' => $parentTask['createdBy'],
'priority' => $parentTask['priority'],
'task_id' => $mainTaskId,
'comment' => $parentTask['comment'],
'change_date' => $parentTask['change_date'],
]
);
$this->addSql("DELETE FROM task WHERE id = :id", [
'id' => $parentTask['id']
]);
}
}
$offset += 500;
}
// $this->addSql('ALTER TABLE task DROP FOREIGN KEY FK_527EDB25FFFE75C0');
// $this->addSql('DROP INDEX IDX_527EDB25FFFE75C0 ON task');
// $this->addSql('ALTER TABLE task DROP parent_task_id');
$this->addSql('SET foreign_key_checks = 1');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
}
}