59 lines
1.7 KiB
PHP
59 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class AddFirstNameLastNameToUsers extends Migration
|
|
{
|
|
public function up(): void
|
|
{
|
|
if (! $this->db->fieldExists('first_name', 'users')) {
|
|
$this->forge->addColumn('users', [
|
|
'first_name' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 50,
|
|
'null' => true,
|
|
],
|
|
]);
|
|
}
|
|
|
|
if (! $this->db->fieldExists('last_name', 'users')) {
|
|
$this->forge->addColumn('users', [
|
|
'last_name' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 50,
|
|
'null' => true,
|
|
],
|
|
]);
|
|
}
|
|
|
|
if ($this->db->fieldExists('name', 'users')) {
|
|
$this->db->query("
|
|
UPDATE users
|
|
SET
|
|
first_name = CASE
|
|
WHEN LOCATE(' ', name) > 0 THEN SUBSTRING_INDEX(name, ' ', 1)
|
|
ELSE name
|
|
END,
|
|
last_name = CASE
|
|
WHEN LOCATE(' ', name) > 0 THEN SUBSTRING_INDEX(name, ' ', -1)
|
|
ELSE NULL
|
|
END
|
|
WHERE first_name IS NULL AND name IS NOT NULL
|
|
");
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
if ($this->db->fieldExists('first_name', 'users')) {
|
|
$this->forge->dropColumn('users', 'first_name');
|
|
}
|
|
|
|
if ($this->db->fieldExists('last_name', 'users')) {
|
|
$this->forge->dropColumn('users', 'last_name');
|
|
}
|
|
}
|
|
}
|