fix: use firstOrCreate in seeders to prevent duplicate entry

This commit is contained in:
Adamalfantino 2026-05-22 22:20:25 +07:00
parent 8410e6c4d0
commit 6ba992dab1
3 changed files with 40 additions and 28 deletions

View File

@ -2,16 +2,12 @@
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Models\Device;
use Carbon\Carbon;
class DeviceSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$devices = [
@ -33,8 +29,6 @@ public function run(): void
]),
'description' => 'Node sensor utama untuk monitoring rak dengan sensor PIR, getaran, dan reed switch',
'is_active' => true,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'LoRa Gateway',
@ -54,8 +48,6 @@ public function run(): void
]),
'description' => 'Gateway LoRa untuk menerima data dari semua sensor node',
'is_active' => true,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'name' => 'Server Monitoring',
@ -75,11 +67,14 @@ public function run(): void
]),
'description' => 'Server utama untuk processing dan storage data monitoring',
'is_active' => true,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
];
DB::table('devices')->insert($devices);
foreach ($devices as $device) {
Device::firstOrCreate(
['device_id' => $device['device_id']],
$device
);
}
}
}

View File

@ -2,22 +2,22 @@
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Models\Sensor;
use App\Models\Device;
use Carbon\Carbon;
class SensorSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$device = Device::where('device_id', 'RACK_A_001')->first();
if (!$device) return;
$sensors = [
// Sensors untuk Device Rak A (device_id = 1)
[
'device_id' => 1,
'device_id' => $device->id,
'name' => 'PIR Motion Sensor',
'type' => 'pir',
'pin_number' => 'GPIO2',
@ -33,11 +33,9 @@ public function run(): void
]),
'description' => 'Sensor PIR untuk mendeteksi gerakan manusia di area rak',
'is_active' => true,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'device_id' => 1,
'device_id' => $device->id,
'name' => 'Vibration Sensor SW-420',
'type' => 'vibration',
'pin_number' => 'GPIO3',
@ -53,11 +51,9 @@ public function run(): void
]),
'description' => 'Sensor getaran SW-420 untuk mendeteksi guncangan pada rak',
'is_active' => true,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
[
'device_id' => 1,
'device_id' => $device->id,
'name' => 'Reed Switch',
'type' => 'reed_switch',
'pin_number' => 'GPIO4',
@ -72,11 +68,17 @@ public function run(): void
]),
'description' => 'Reed switch untuk mendeteksi status buka/tutup rak',
'is_active' => true,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
],
];
DB::table('sensors')->insert($sensors);
foreach ($sensors as $sensor) {
Sensor::firstOrCreate(
[
'device_id' => $sensor['device_id'],
'type' => $sensor['type']
],
$sensor
);
}
}
}

15
start.sh Normal file
View File

@ -0,0 +1,15 @@
#!/bin/bash
set -e
echo "Running migrations..."
php artisan migrate --force
echo "Running seeders..."
php artisan db:seed --force || echo "Seeder skipped (data may already exist)"
echo "Clearing cache..."
php artisan config:clear
php artisan cache:clear
echo "Starting server..."
php artisan serve --host=0.0.0.0 --port=${PORT:-8000}