update
This commit is contained in:
@@ -17,6 +17,7 @@ return new class extends Migration
|
||||
$table->string('subpoli', 250)->nullable();
|
||||
$table->string('subsubpoli', 250)->nullable();
|
||||
$table->string('modaliti', 150)->nullable();
|
||||
$table->string('kelompok', 150)->nullable();
|
||||
$table->string('modaliti2', 150)->nullable();
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
$table->timestamp('updated_at')->useCurrent();
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('poli', function (Blueprint $table) {
|
||||
if (!Schema::hasColumn('poli', 'kelompok')) {
|
||||
$table->string('kelompok', 150)->nullable();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('poli', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('poli', 'kelompok')) {
|
||||
$table->dropColumn('kelompok');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Poli;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class PoliSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
@@ -13,15 +14,107 @@ class PoliSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Poli::truncate();
|
||||
$json1 = file_get_contents(database_path('seeders/data/poli.json'));
|
||||
$data1 = json_decode($json1, true);
|
||||
$this->ensureTableAndColumns();
|
||||
|
||||
// Bagi data ke dalam batch 500
|
||||
$chunks = array_chunk($data1, 500);
|
||||
DB::table('poli')->truncate();
|
||||
|
||||
foreach ($chunks as $chunk) {
|
||||
foreach (array_chunk($this->jsonData(), 500) as $chunk) {
|
||||
DB::table('poli')->insert($chunk);
|
||||
}
|
||||
|
||||
$this->resetAutoIncrement();
|
||||
}
|
||||
|
||||
private function ensureTableAndColumns(): void
|
||||
{
|
||||
if (!Schema::hasTable('poli')) {
|
||||
Schema::create('poli', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('poli', 250)->nullable();
|
||||
$table->string('subpoli', 250)->nullable();
|
||||
$table->string('subsubpoli', 250)->nullable();
|
||||
$table->string('modaliti', 150)->nullable();
|
||||
$table->string('kelompok', 150)->nullable();
|
||||
$table->string('modaliti2', 150)->nullable();
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
$table->timestamp('updated_at')->useCurrent();
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('poli', function (Blueprint $table) {
|
||||
if (!Schema::hasColumn('poli', 'poli')) {
|
||||
$table->string('poli', 250)->nullable();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'subpoli')) {
|
||||
$table->string('subpoli', 250)->nullable();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'subsubpoli')) {
|
||||
$table->string('subsubpoli', 250)->nullable();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'modaliti')) {
|
||||
$table->string('modaliti', 150)->nullable();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'kelompok')) {
|
||||
$table->string('kelompok', 150)->nullable();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'modaliti2')) {
|
||||
$table->string('modaliti2', 150)->nullable();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'created_at')) {
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
}
|
||||
|
||||
if (!Schema::hasColumn('poli', 'updated_at')) {
|
||||
$table->timestamp('updated_at')->useCurrent();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function jsonData(): array
|
||||
{
|
||||
$path = database_path('seeders/data/poli.json');
|
||||
$json = file_get_contents($path);
|
||||
|
||||
if ($json === false) {
|
||||
throw new \RuntimeException("Gagal membuka file seeder poli: {$path}");
|
||||
}
|
||||
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new \RuntimeException("Format JSON seeder poli tidak valid: {$path}");
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function resetAutoIncrement(): void
|
||||
{
|
||||
$driver = DB::getDriverName();
|
||||
|
||||
if ($driver === 'pgsql') {
|
||||
DB::statement("
|
||||
SELECT setval(
|
||||
pg_get_serial_sequence('poli', 'id'),
|
||||
(SELECT COALESCE(MAX(id), 1) FROM poli),
|
||||
(SELECT COUNT(*) > 0 FROM poli)
|
||||
)
|
||||
");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($driver === 'mysql') {
|
||||
$nextId = ((int) DB::table('poli')->max('id')) + 1;
|
||||
DB::statement("ALTER TABLE poli AUTO_INCREMENT = {$nextId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
+2256
-205
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user