<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Fügt 'active' Flag zur Person-Tabelle hinzu für Soft-Delete Funktionalität
*/
final class Version20251022120000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Adds active flag to person table for soft-delete functionality';
}
public function up(Schema $schema): void
{
// Prüfe, ob Spalte bereits existiert
$this->addSql("
SET @col_exists = (
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'person'
AND COLUMN_NAME = 'active'
);
");
// Füge Spalte nur hinzu, wenn sie noch nicht existiert
$this->addSql("
SET @sql = IF(@col_exists = 0,
'ALTER TABLE person ADD active TINYINT(1) DEFAULT 1 NULL AFTER is_customer',
'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
");
// Setze alle bestehenden Personen auf active = 1
$this->addSql("UPDATE person SET active = 1 WHERE active IS NULL");
}
public function down(Schema $schema): void
{
// Entferne Spalte nur, wenn sie existiert
$this->addSql("
SET @col_exists = (
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'person'
AND COLUMN_NAME = 'active'
);
");
$this->addSql("
SET @sql = IF(@col_exists > 0,
'ALTER TABLE person DROP COLUMN active',
'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
");
}
}