Files
zibo-dashboard/db/migrations/20260605095127_alter_scad_functions_add_contact_fields.php
T
2026-06-05 14:35:19 +02:00

139 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AlterScadFunctionsAddContactFields extends AbstractMigration
{
public function up(): void
{
if (!$this->hasTable('scad_functions')) {
throw new RuntimeException('Table scad_functions does not exist.');
}
$table = $this->table('scad_functions');
if (!$table->hasColumn('person_full_name')) {
$table->addColumn('person_full_name', 'string', [
'limit' => 200,
'null' => true,
'after' => 'description',
'comment' => 'Full name and surname of the person assigned to the function',
]);
}
if (!$table->hasColumn('phone')) {
$table->addColumn('phone', 'string', [
'limit' => 80,
'null' => true,
'after' => 'person_full_name',
]);
}
if (!$table->hasColumn('email')) {
$table->addColumn('email', 'string', [
'limit' => 190,
'null' => true,
'after' => 'phone',
]);
}
if (!$table->hasColumn('notes')) {
$table->addColumn('notes', 'text', [
'null' => true,
'after' => 'email',
]);
}
if (!$table->hasColumn('sort_order')) {
$table->addColumn('sort_order', 'integer', [
'signed' => false,
'null' => false,
'default' => 0,
'after' => 'status',
]);
}
if (!$table->hasIndexByName('idx_scad_functions_name')) {
$table->addIndex(['name'], [
'name' => 'idx_scad_functions_name',
]);
}
if (!$table->hasIndexByName('idx_scad_functions_person_full_name')) {
$table->addIndex(['person_full_name'], [
'name' => 'idx_scad_functions_person_full_name',
]);
}
if (!$table->hasIndexByName('idx_scad_functions_email')) {
$table->addIndex(['email'], [
'name' => 'idx_scad_functions_email',
]);
}
if (!$table->hasIndexByName('idx_scad_functions_status_sort')) {
$table->addIndex(['status', 'sort_order'], [
'name' => 'idx_scad_functions_status_sort',
]);
}
$table->update();
// Set a default order for existing rows without changing their names.
$this->execute("
UPDATE scad_functions
SET sort_order = id * 10
WHERE sort_order = 0
");
}
public function down(): void
{
if (!$this->hasTable('scad_functions')) {
return;
}
$table = $this->table('scad_functions');
if ($table->hasIndexByName('idx_scad_functions_status_sort')) {
$table->removeIndexByName('idx_scad_functions_status_sort');
}
if ($table->hasIndexByName('idx_scad_functions_email')) {
$table->removeIndexByName('idx_scad_functions_email');
}
if ($table->hasIndexByName('idx_scad_functions_person_full_name')) {
$table->removeIndexByName('idx_scad_functions_person_full_name');
}
if ($table->hasIndexByName('idx_scad_functions_name')) {
$table->removeIndexByName('idx_scad_functions_name');
}
if ($table->hasColumn('sort_order')) {
$table->removeColumn('sort_order');
}
if ($table->hasColumn('notes')) {
$table->removeColumn('notes');
}
if ($table->hasColumn('email')) {
$table->removeColumn('email');
}
if ($table->hasColumn('phone')) {
$table->removeColumn('phone');
}
if ($table->hasColumn('person_full_name')) {
$table->removeColumn('person_full_name');
}
$table->update();
}
}