This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
// 1. Jika tabel belum ada sama sekali, buat baru dengan struktur lengkap
|
||||
if (!Schema::hasTable('transactions')) {
|
||||
Schema::create('transactions', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('cabang_id')->constrained('cabang')->onDelete('cascade');
|
||||
$table->foreignId('budget_control_id')->nullable()->constrained('budget_control')->onDelete('set null');
|
||||
$table->date('transaction_date');
|
||||
$table->string('reference_number')->unique();
|
||||
$table->string('description');
|
||||
$table->decimal('amount', 15, 2);
|
||||
$table->enum('type', ['DEBIT', 'CREDIT']);
|
||||
$table->string('category')->default('General');
|
||||
$table->string('periode_month');
|
||||
$table->year('periode_year');
|
||||
$table->foreignId('created_by')->constrained('admins');
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. JIKA TABEL SUDAH EXIST: Tambahkan kolom baru tanpa merusak / menimpa kolom cabang_id yang sudah ada
|
||||
Schema::table('transactions', function (Blueprint $table) {
|
||||
|
||||
// Pastikan cabang_id tidak dibuat ulang, kita hanya memastikan tipenya aman jika belum ada
|
||||
if (!Schema::hasColumn('transactions', 'cabang_id')) {
|
||||
$table->foreignId('cabang_id')->constrained('cabang')->onDelete('cascade');
|
||||
}
|
||||
|
||||
// Tambahkan kolom pendukung lainnya secara aman jika belum eksis di database
|
||||
if (!Schema::hasColumn('transactions', 'budget_control_id')) {
|
||||
$table->foreignId('budget_control_id')->nullable()->after('cabang_id')->constrained('budget_control')->onDelete('set null');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'transaction_date')) {
|
||||
$table->date('transaction_date')->nullable()->after('budget_control_id');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'reference_number')) {
|
||||
$table->string('reference_number')->nullable()->after('transaction_date');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'description')) {
|
||||
$table->string('description')->nullable()->after('reference_number');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'amount')) {
|
||||
$table->decimal('amount', 15, 2)->default(0)->after('description');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'type')) {
|
||||
$table->enum('type', ['DEBIT', 'CREDIT'])->default('CREDIT')->after('amount');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'category')) {
|
||||
$table->string('category')->default('General')->after('type');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'periode_month')) {
|
||||
$table->string('periode_month')->nullable()->after('category');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'periode_year')) {
|
||||
$table->year('periode_year')->nullable()->after('periode_month');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'created_by')) {
|
||||
// Buat nullable dulu agar baris data lama yang sudah exist tidak error saat migrasi berjalan
|
||||
$table->foreignId('created_by')->nullable()->after('periode_year')->constrained('admins');
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('transactions', 'deleted_at')) {
|
||||
$table->softDeletes();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Opsional: Hanya drop jika Anda benar-benar sedang di fase development murni
|
||||
// Schema::dropIfExists('transactions');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user