Langkah 3 dari 11
Membuat Migration Tabel Products
Buat migration:
php spark make:migration CreateProducts
Isi file migration yang terbentuk di app/Database/Migrations dengan struktur berikut:
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateProducts extends Migration
{
public function up(): void
{
$this->forge->addField([
'id' => [
'type' => 'INT', 'constraint' => 11,
'unsigned' => true, 'auto_increment' => true,
],
'sku' => ['type' => 'VARCHAR', 'constraint' => 40],
'name' => ['type' => 'VARCHAR', 'constraint' => 150],
'price' => ['type' => 'DECIMAL', 'constraint' => '12,2', 'default' => 0],
'stock' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'default' => 0],
'description' => ['type' => 'TEXT', 'null' => true],
'created_at' => ['type' => 'DATETIME', 'null' => true],
'updated_at' => ['type' => 'DATETIME', 'null' => true],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey('sku');
$this->forge->createTable('products');
}
public function down(): void
{
$this->forge->dropTable('products');
}
}
Jalankan migration:
php spark migrate
Migration menyimpan perubahan struktur database sebagai kode dan menyediakan operasi rollback melalui method down().
