From 088e518db10b0981aba3b38454f7d316fdf8152c Mon Sep 17 00:00:00 2001 From: solocla Date: Fri, 5 Jun 2026 14:50:51 +0200 Subject: [PATCH] fixed migration --- ...60603183520_create_job_sub_roles_table.php | 170 ++++++++---------- 1 file changed, 75 insertions(+), 95 deletions(-) diff --git a/db/migrations/20260603183520_create_job_sub_roles_table.php b/db/migrations/20260603183520_create_job_sub_roles_table.php index b1d85ed..357f833 100644 --- a/db/migrations/20260603183520_create_job_sub_roles_table.php +++ b/db/migrations/20260603183520_create_job_sub_roles_table.php @@ -4,121 +4,101 @@ declare(strict_types=1); use Phinx\Migration\AbstractMigration; -final class CreateJobSubRolesTable extends AbstractMigration +final class AddJobSubRoleIdToEmployeesTable extends AbstractMigration { - public function change(): void + public function up(): void { - if (!$this->hasTable('job_roles')) { - $rolesTable = $this->table('job_roles', [ - 'id' => false, - 'primary_key' => ['id'], - 'collation' => 'utf8mb4_unicode_ci', - 'encoding' => 'utf8mb4', - ]); - - $rolesTable - ->addColumn('id', 'integer', [ - 'identity' => true, - 'signed' => false, - ]) - ->addColumn('name', 'string', [ - 'limit' => 255, - 'null' => false, - ]) - ->addColumn('description', 'text', [ - 'null' => true, - 'default' => null, - ]) - ->addColumn('sort_order', 'integer', [ - 'signed' => false, - 'null' => false, - 'default' => 999, - ]) - ->addColumn('is_active', 'boolean', [ - 'null' => false, - 'default' => 1, - ]) - ->addColumn('created_at', 'timestamp', [ - 'null' => true, - 'default' => 'CURRENT_TIMESTAMP', - ]) - ->addColumn('updated_at', 'timestamp', [ - 'null' => true, - 'default' => 'CURRENT_TIMESTAMP', - 'update' => 'CURRENT_TIMESTAMP', - ]) - ->addIndex(['is_active'], [ - 'name' => 'idx_job_roles_is_active', - ]) - ->addIndex(['sort_order'], [ - 'name' => 'idx_job_roles_sort_order', - ]) - ->create(); + if (!$this->hasTable('employees')) { + throw new RuntimeException('Table employees does not exist.'); } - if (!$this->hasTable('job_sub_roles')) { - $table = $this->table('job_sub_roles', [ - 'id' => false, - 'primary_key' => ['id'], - 'collation' => 'utf8mb4_unicode_ci', - 'encoding' => 'utf8mb4', - ]); + $table = $this->table('employees'); + if (!$table->hasColumn('job_role_id')) { $table - ->addColumn('id', 'integer', [ - 'identity' => true, - 'signed' => false, - ]) ->addColumn('job_role_id', 'integer', [ 'signed' => false, - 'null' => false, - ]) - ->addColumn('name', 'string', [ - 'limit' => 255, - 'null' => false, - ]) - ->addColumn('description', 'text', [ 'null' => true, - 'default' => null, - ]) - ->addColumn('sort_order', 'integer', [ - 'signed' => false, - 'null' => false, - 'default' => 999, - ]) - ->addColumn('is_active', 'boolean', [ - 'null' => false, - 'default' => 1, - ]) - ->addColumn('created_at', 'timestamp', [ - 'null' => true, - 'default' => 'CURRENT_TIMESTAMP', - ]) - ->addColumn('updated_at', 'timestamp', [ - 'null' => true, - 'default' => 'CURRENT_TIMESTAMP', - 'update' => 'CURRENT_TIMESTAMP', + 'after' => 'department_id', ]) ->addIndex(['job_role_id'], [ - 'name' => 'idx_job_sub_roles_job_role_id', - ]) - ->addIndex(['is_active'], [ - 'name' => 'idx_job_sub_roles_is_active', - ]) - ->addIndex(['sort_order'], [ - 'name' => 'idx_job_sub_roles_sort_order', + 'name' => 'idx_employees_job_role_id', ]) ->addForeignKey( 'job_role_id', 'job_roles', 'id', [ - 'delete' => 'CASCADE', + 'delete' => 'SET_NULL', 'update' => 'CASCADE', - 'constraint' => 'fk_job_sub_roles_job_role', + 'constraint' => 'fk_employees_job_role', ] ) - ->create(); + ->update(); + } + + $table = $this->table('employees'); + + if (!$table->hasColumn('job_sub_role_id')) { + $afterColumn = $table->hasColumn('job_role_id') ? 'job_role_id' : 'department_id'; + + $table + ->addColumn('job_sub_role_id', 'integer', [ + 'signed' => false, + 'null' => true, + 'after' => $afterColumn, + ]) + ->addIndex(['job_sub_role_id'], [ + 'name' => 'idx_employees_job_sub_role_id', + ]) + ->addForeignKey( + 'job_sub_role_id', + 'job_sub_roles', + 'id', + [ + 'delete' => 'SET_NULL', + 'update' => 'CASCADE', + 'constraint' => 'fk_employees_job_sub_role', + ] + ) + ->update(); + } + } + + public function down(): void + { + if (!$this->hasTable('employees')) { + return; + } + + $table = $this->table('employees'); + + if ($table->hasForeignKey('job_sub_role_id')) { + $table->dropForeignKey('job_sub_role_id')->update(); + } + + if ($table->hasForeignKey('job_role_id')) { + $table->dropForeignKey('job_role_id')->update(); + } + + $table = $this->table('employees'); + + if ($table->hasIndexByName('idx_employees_job_sub_role_id')) { + $table->removeIndexByName('idx_employees_job_sub_role_id')->update(); + } + + if ($table->hasIndexByName('idx_employees_job_role_id')) { + $table->removeIndexByName('idx_employees_job_role_id')->update(); + } + + $table = $this->table('employees'); + + if ($table->hasColumn('job_sub_role_id')) { + $table->removeColumn('job_sub_role_id')->update(); + } + + if ($table->hasColumn('job_role_id')) { + $table->removeColumn('job_role_id')->update(); } } }