Files
haushalt/backend/migrations/Version20260331150000.php
Marek Lenczewski b998940caa update
2026-03-31 18:09:15 +02:00

112 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260331150000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Major refactoring: English enums, unified days field, nullable task.schema, detach single tasks';
}
public function up(Schema $schema): void
{
// 1. Migrate enum values to English
$this->addSql("UPDATE task SET status = 'active' WHERE status = 'aktiv'");
$this->addSql("UPDATE task SET status = 'done' WHERE status = 'erledigt'");
$this->addSql("UPDATE task_schema SET status = 'active' WHERE status = 'aktiv'");
$this->addSql("UPDATE task_schema SET status = 'disabled' WHERE status IN ('erledigt', 'inaktiv')");
$this->addSql("UPDATE task_schema SET task_type = 'single' WHERE task_type = 'einzel'");
$this->addSql("UPDATE task_schema SET task_type = 'daily' WHERE task_type = 'taeglich'");
$this->addSql("UPDATE task_schema SET task_type = 'custom' WHERE task_type IN ('multi', 'woechentlich', 'monatlich', 'jaehrlich')");
// 2. Add unified days column
$this->addSql('ALTER TABLE task_schema ADD days JSON DEFAULT NULL');
// 3. Migrate weekdays/monthDays/yearDays into days JSON via PHP
$this->migrateDaysData();
// 4. Drop old columns from task_schema
$this->addSql('ALTER TABLE task_schema DROP deadline, DROP weekdays, DROP month_days, DROP year_days');
// 5. Backfill task names/categories from schema (for all tasks that relied on getEffectiveName fallback)
$this->addSql("UPDATE task t INNER JOIN task_schema ts ON t.task_id = ts.id SET t.name = ts.name WHERE t.name IS NULL");
$this->addSql("UPDATE task t INNER JOIN task_schema ts ON t.task_id = ts.id SET t.category_id = ts.category_id WHERE t.category_id IS NULL AND ts.category_id IS NOT NULL");
// 6. Detach single tasks
$this->addSql("UPDATE task t INNER JOIN task_schema ts ON t.task_id = ts.id SET t.name = ts.name WHERE ts.task_type = 'single' AND t.name IS NULL");
$this->addSql("UPDATE task t INNER JOIN task_schema ts ON t.task_id = ts.id SET t.category_id = ts.category_id WHERE ts.task_type = 'single' AND t.category_id IS NULL");
// Drop FK + unique constraint before modifying task_id
$this->addSql('ALTER TABLE task DROP FOREIGN KEY FK_527EDB258DB60186');
$this->addSql('DROP INDEX UNIQ_527EDB258DB60186AA9E377A ON task');
// Detach single tasks
$this->addSql("UPDATE task t INNER JOIN task_schema ts ON t.task_id = ts.id SET t.task_id = NULL WHERE ts.task_type = 'single'");
// Delete single schemas
$this->addSql("DELETE FROM task_schema WHERE task_type = 'single'");
// Make task_id nullable with SET NULL on delete
$this->addSql('ALTER TABLE task MODIFY task_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB258DB60186 FOREIGN KEY (task_id) REFERENCES task_schema (id) ON DELETE SET NULL');
}
private function migrateDaysData(): void
{
$rows = $this->connection->fetchAllAssociative(
'SELECT id, weekdays, month_days, year_days FROM task_schema'
);
foreach ($rows as $row) {
$days = [];
$weekdays = $row['weekdays'] ? json_decode($row['weekdays'], true) : null;
$monthDays = $row['month_days'] ? json_decode($row['month_days'], true) : null;
$yearDays = $row['year_days'] ? json_decode($row['year_days'], true) : null;
if (!empty($weekdays)) {
$days['week'] = $weekdays;
}
if (!empty($monthDays)) {
$days['month'] = $monthDays;
}
if (!empty($yearDays)) {
$days['year'] = $yearDays;
}
if (!empty($days)) {
$this->connection->executeStatement(
'UPDATE task_schema SET days = ? WHERE id = ?',
[json_encode($days), $row['id']]
);
}
}
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE task_schema ADD deadline DATE DEFAULT NULL, ADD weekdays JSON DEFAULT NULL, ADD month_days JSON DEFAULT NULL, ADD year_days JSON DEFAULT NULL');
$this->addSql('ALTER TABLE task_schema DROP days');
$this->addSql('ALTER TABLE task DROP FOREIGN KEY FK_527EDB258DB60186');
$this->addSql('ALTER TABLE task MODIFY task_id INT NOT NULL');
$this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB258DB60186 FOREIGN KEY (task_id) REFERENCES task_schema (id) ON DELETE CASCADE');
$this->addSql('CREATE UNIQUE INDEX UNIQ_527EDB258DB60186AA9E377A ON task (task_id, date)');
$this->addSql("UPDATE task SET status = 'aktiv' WHERE status = 'active'");
$this->addSql("UPDATE task SET status = 'erledigt' WHERE status = 'done'");
$this->addSql("UPDATE task_schema SET status = 'aktiv' WHERE status = 'active'");
$this->addSql("UPDATE task_schema SET status = 'inaktiv' WHERE status = 'disabled'");
$this->addSql("UPDATE task_schema SET task_type = 'einzel' WHERE task_type = 'single'");
$this->addSql("UPDATE task_schema SET task_type = 'taeglich' WHERE task_type = 'daily'");
}
}