Compare commits
4 Commits
e24eaa963d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fd8afd8df | |||
|
|
429a3a99fd | ||
| 2234ab2147 | |||
|
|
8d7a8d0fc7 |
@@ -1,15 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class extends Migration {
|
return new class extends Migration {
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
public function up()
|
public function up(): void
|
||||||
: void
|
|
||||||
{
|
{
|
||||||
Schema::create('branches', function (Blueprint $table) {
|
Schema::create('branches', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
@@ -31,9 +30,10 @@
|
|||||||
/**
|
/**
|
||||||
* Reverse the migrations.
|
* Reverse the migrations.
|
||||||
*/
|
*/
|
||||||
public function down()
|
public function down(): void
|
||||||
: void
|
|
||||||
{
|
{
|
||||||
|
Schema::disableForeignKeyConstraints();
|
||||||
Schema::dropIfExists('branches');
|
Schema::dropIfExists('branches');
|
||||||
|
Schema::enableForeignKeyConstraints();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,18 +3,30 @@
|
|||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
return new class extends Migration
|
return new class extends Migration {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Run the migrations.
|
* Run the migrations.
|
||||||
*/
|
*/
|
||||||
public function up(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
Schema::table('branches', function (Blueprint $table) {
|
Schema::table('branches', function (Blueprint $table) {
|
||||||
|
// Tambah kolom parent_id jika belum ada
|
||||||
|
if (!Schema::hasColumn('branches', 'parent_id')) {
|
||||||
$table->unsignedBigInteger('parent_id')->nullable()->after('name');
|
$table->unsignedBigInteger('parent_id')->nullable()->after('name');
|
||||||
$table->foreign('parent_id')->references('id')->on('branches')->onDelete('set null');
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Tambah foreign key jika belum ada
|
||||||
|
if (!$this->foreignKeyExists('branches', 'branches_parent_id_foreign')) {
|
||||||
|
Schema::table('branches', function (Blueprint $table) {
|
||||||
|
$table->foreign('parent_id', 'branches_parent_id_foreign')
|
||||||
|
->references('id')
|
||||||
|
->on('branches')
|
||||||
|
->onDelete('set null');
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,9 +34,39 @@ return new class extends Migration
|
|||||||
*/
|
*/
|
||||||
public function down(): void
|
public function down(): void
|
||||||
{
|
{
|
||||||
|
// Drop foreign key jika ada
|
||||||
|
if ($this->foreignKeyExists('branches', 'branches_parent_id_foreign')) {
|
||||||
Schema::table('branches', function (Blueprint $table) {
|
Schema::table('branches', function (Blueprint $table) {
|
||||||
$table->dropForeign(['parent_id']);
|
$table->dropForeign('branches_parent_id_foreign');
|
||||||
$table->dropColumn('parent_id');
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Drop kolom jika ada
|
||||||
|
Schema::table('branches', function (Blueprint $table) {
|
||||||
|
if (Schema::hasColumn('branches', 'parent_id')) {
|
||||||
|
$table->dropColumn('parent_id');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cek apakah foreign key exists
|
||||||
|
*/
|
||||||
|
private function foreignKeyExists(string $table, string $name): bool
|
||||||
|
{
|
||||||
|
$conn = Schema::getConnection();
|
||||||
|
$dbName = $conn->getDatabaseName();
|
||||||
|
|
||||||
|
$result = DB::select(
|
||||||
|
"SELECT CONSTRAINT_NAME
|
||||||
|
FROM information_schema.TABLE_CONSTRAINTS
|
||||||
|
WHERE TABLE_SCHEMA = ?
|
||||||
|
AND TABLE_NAME = ?
|
||||||
|
AND CONSTRAINT_NAME = ?
|
||||||
|
AND CONSTRAINT_TYPE = 'FOREIGN KEY'",
|
||||||
|
[$dbName, $table, $name]
|
||||||
|
);
|
||||||
|
|
||||||
|
return count($result) > 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ class BranchesSeeder extends Seeder
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'code' => 'ID0010031',
|
'code' => 'ID0010031',
|
||||||
'name' => 'BKR Bandung - KCP',
|
'name' => 'BKR BANDUNG - KCP',
|
||||||
'status' => true,
|
'status' => true,
|
||||||
'created_at' => $now,
|
'created_at' => $now,
|
||||||
'updated_at' => $now
|
'updated_at' => $now
|
||||||
@@ -319,7 +319,7 @@ class BranchesSeeder extends Seeder
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'code' => 'ID0010063',
|
'code' => 'ID0010063',
|
||||||
'name' => 'Diponegoro Denpasar KK',
|
'name' => 'DIPONEGORO DENPASAR KK',
|
||||||
'status' => true,
|
'status' => true,
|
||||||
'created_at' => $now,
|
'created_at' => $now,
|
||||||
'updated_at' => $now
|
'updated_at' => $now
|
||||||
@@ -704,7 +704,7 @@ class BranchesSeeder extends Seeder
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'code' => 'ID0010272',
|
'code' => 'ID0010272',
|
||||||
'name' => 'Setiabudi Kuningan KK',
|
'name' => 'SETIABUDI KUNINGAN KK',
|
||||||
'status' => true,
|
'status' => true,
|
||||||
'created_at' => $now,
|
'created_at' => $now,
|
||||||
'updated_at' => $now
|
'updated_at' => $now
|
||||||
|
|||||||
Reference in New Issue
Block a user