fixed add sub roles
This commit is contained in:
@@ -4,101 +4,121 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
use Phinx\Migration\AbstractMigration;
|
use Phinx\Migration\AbstractMigration;
|
||||||
|
|
||||||
final class AddJobSubRoleIdToEmployeesTable extends AbstractMigration
|
final class CreateJobSubRolesTable extends AbstractMigration
|
||||||
{
|
{
|
||||||
public function up(): void
|
public function change(): void
|
||||||
{
|
{
|
||||||
if (!$this->hasTable('employees')) {
|
if (!$this->hasTable('job_roles')) {
|
||||||
throw new RuntimeException('Table employees does not exist.');
|
$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();
|
||||||
}
|
}
|
||||||
|
|
||||||
$table = $this->table('employees');
|
if (!$this->hasTable('job_sub_roles')) {
|
||||||
|
$table = $this->table('job_sub_roles', [
|
||||||
|
'id' => false,
|
||||||
|
'primary_key' => ['id'],
|
||||||
|
'collation' => 'utf8mb4_unicode_ci',
|
||||||
|
'encoding' => 'utf8mb4',
|
||||||
|
]);
|
||||||
|
|
||||||
if (!$table->hasColumn('job_role_id')) {
|
|
||||||
$table
|
$table
|
||||||
|
->addColumn('id', 'integer', [
|
||||||
|
'identity' => true,
|
||||||
|
'signed' => false,
|
||||||
|
])
|
||||||
->addColumn('job_role_id', 'integer', [
|
->addColumn('job_role_id', 'integer', [
|
||||||
'signed' => false,
|
'signed' => false,
|
||||||
|
'null' => false,
|
||||||
|
])
|
||||||
|
->addColumn('name', 'string', [
|
||||||
|
'limit' => 255,
|
||||||
|
'null' => false,
|
||||||
|
])
|
||||||
|
->addColumn('description', 'text', [
|
||||||
'null' => true,
|
'null' => true,
|
||||||
'after' => 'department_id',
|
'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(['job_role_id'], [
|
->addIndex(['job_role_id'], [
|
||||||
'name' => 'idx_employees_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',
|
||||||
])
|
])
|
||||||
->addForeignKey(
|
->addForeignKey(
|
||||||
'job_role_id',
|
'job_role_id',
|
||||||
'job_roles',
|
'job_roles',
|
||||||
'id',
|
'id',
|
||||||
[
|
[
|
||||||
'delete' => 'SET_NULL',
|
'delete' => 'CASCADE',
|
||||||
'update' => 'CASCADE',
|
'update' => 'CASCADE',
|
||||||
'constraint' => 'fk_employees_job_role',
|
'constraint' => 'fk_job_sub_roles_job_role',
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
->update();
|
->create();
|
||||||
}
|
|
||||||
|
|
||||||
$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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,30 +6,99 @@ use Phinx\Migration\AbstractMigration;
|
|||||||
|
|
||||||
final class AddJobSubRoleIdToEmployeesTable extends AbstractMigration
|
final class AddJobSubRoleIdToEmployeesTable extends AbstractMigration
|
||||||
{
|
{
|
||||||
public function change(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
|
if (!$this->hasTable('employees')) {
|
||||||
|
throw new RuntimeException('Table employees does not exist.');
|
||||||
|
}
|
||||||
|
|
||||||
$table = $this->table('employees');
|
$table = $this->table('employees');
|
||||||
|
|
||||||
$table
|
if (!$table->hasColumn('job_role_id')) {
|
||||||
->addColumn('job_sub_role_id', 'integer', [
|
$table
|
||||||
'signed' => false,
|
->addColumn('job_role_id', 'integer', [
|
||||||
'null' => true,
|
'signed' => false,
|
||||||
'default' => null,
|
'null' => true,
|
||||||
'after' => 'job_role_id',
|
'after' => 'department_id',
|
||||||
])
|
])
|
||||||
->addIndex(['job_sub_role_id'], [
|
->addIndex(['job_role_id'], [
|
||||||
'name' => 'idx_employees_job_sub_role_id',
|
'name' => 'idx_employees_job_role_id',
|
||||||
])
|
])
|
||||||
->addForeignKey(
|
->addForeignKey(
|
||||||
'job_sub_role_id',
|
'job_role_id',
|
||||||
'job_sub_roles',
|
'job_roles',
|
||||||
'id',
|
'id',
|
||||||
[
|
[
|
||||||
'delete' => 'SET_NULL',
|
'delete' => 'SET_NULL',
|
||||||
'update' => 'CASCADE',
|
'update' => 'CASCADE',
|
||||||
'constraint' => 'fk_employees_job_sub_role',
|
'constraint' => 'fk_employees_job_role',
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
->update();
|
->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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user