fix: use firstOrCreate in seeders to prevent duplicate entry
This commit is contained in:
parent
8410e6c4d0
commit
6ba992dab1
|
|
@ -2,16 +2,12 @@
|
||||||
|
|
||||||
namespace Database\Seeders;
|
namespace Database\Seeders;
|
||||||
|
|
||||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Illuminate\Support\Facades\DB;
|
use App\Models\Device;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
|
||||||
class DeviceSeeder extends Seeder
|
class DeviceSeeder extends Seeder
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Run the database seeds.
|
|
||||||
*/
|
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
$devices = [
|
$devices = [
|
||||||
|
|
@ -33,8 +29,6 @@ public function run(): void
|
||||||
]),
|
]),
|
||||||
'description' => 'Node sensor utama untuk monitoring rak dengan sensor PIR, getaran, dan reed switch',
|
'description' => 'Node sensor utama untuk monitoring rak dengan sensor PIR, getaran, dan reed switch',
|
||||||
'is_active' => true,
|
'is_active' => true,
|
||||||
'created_at' => Carbon::now(),
|
|
||||||
'updated_at' => Carbon::now(),
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'LoRa Gateway',
|
'name' => 'LoRa Gateway',
|
||||||
|
|
@ -54,8 +48,6 @@ public function run(): void
|
||||||
]),
|
]),
|
||||||
'description' => 'Gateway LoRa untuk menerima data dari semua sensor node',
|
'description' => 'Gateway LoRa untuk menerima data dari semua sensor node',
|
||||||
'is_active' => true,
|
'is_active' => true,
|
||||||
'created_at' => Carbon::now(),
|
|
||||||
'updated_at' => Carbon::now(),
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'Server Monitoring',
|
'name' => 'Server Monitoring',
|
||||||
|
|
@ -75,11 +67,14 @@ public function run(): void
|
||||||
]),
|
]),
|
||||||
'description' => 'Server utama untuk processing dan storage data monitoring',
|
'description' => 'Server utama untuk processing dan storage data monitoring',
|
||||||
'is_active' => true,
|
'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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,22 @@
|
||||||
|
|
||||||
namespace Database\Seeders;
|
namespace Database\Seeders;
|
||||||
|
|
||||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Illuminate\Support\Facades\DB;
|
use App\Models\Sensor;
|
||||||
|
use App\Models\Device;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
|
||||||
class SensorSeeder extends Seeder
|
class SensorSeeder extends Seeder
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Run the database seeds.
|
|
||||||
*/
|
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
|
$device = Device::where('device_id', 'RACK_A_001')->first();
|
||||||
|
|
||||||
|
if (!$device) return;
|
||||||
|
|
||||||
$sensors = [
|
$sensors = [
|
||||||
// Sensors untuk Device Rak A (device_id = 1)
|
|
||||||
[
|
[
|
||||||
'device_id' => 1,
|
'device_id' => $device->id,
|
||||||
'name' => 'PIR Motion Sensor',
|
'name' => 'PIR Motion Sensor',
|
||||||
'type' => 'pir',
|
'type' => 'pir',
|
||||||
'pin_number' => 'GPIO2',
|
'pin_number' => 'GPIO2',
|
||||||
|
|
@ -33,11 +33,9 @@ public function run(): void
|
||||||
]),
|
]),
|
||||||
'description' => 'Sensor PIR untuk mendeteksi gerakan manusia di area rak',
|
'description' => 'Sensor PIR untuk mendeteksi gerakan manusia di area rak',
|
||||||
'is_active' => true,
|
'is_active' => true,
|
||||||
'created_at' => Carbon::now(),
|
|
||||||
'updated_at' => Carbon::now(),
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'device_id' => 1,
|
'device_id' => $device->id,
|
||||||
'name' => 'Vibration Sensor SW-420',
|
'name' => 'Vibration Sensor SW-420',
|
||||||
'type' => 'vibration',
|
'type' => 'vibration',
|
||||||
'pin_number' => 'GPIO3',
|
'pin_number' => 'GPIO3',
|
||||||
|
|
@ -53,11 +51,9 @@ public function run(): void
|
||||||
]),
|
]),
|
||||||
'description' => 'Sensor getaran SW-420 untuk mendeteksi guncangan pada rak',
|
'description' => 'Sensor getaran SW-420 untuk mendeteksi guncangan pada rak',
|
||||||
'is_active' => true,
|
'is_active' => true,
|
||||||
'created_at' => Carbon::now(),
|
|
||||||
'updated_at' => Carbon::now(),
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'device_id' => 1,
|
'device_id' => $device->id,
|
||||||
'name' => 'Reed Switch',
|
'name' => 'Reed Switch',
|
||||||
'type' => 'reed_switch',
|
'type' => 'reed_switch',
|
||||||
'pin_number' => 'GPIO4',
|
'pin_number' => 'GPIO4',
|
||||||
|
|
@ -72,11 +68,17 @@ public function run(): void
|
||||||
]),
|
]),
|
||||||
'description' => 'Reed switch untuk mendeteksi status buka/tutup rak',
|
'description' => 'Reed switch untuk mendeteksi status buka/tutup rak',
|
||||||
'is_active' => true,
|
'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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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}
|
||||||
Loading…
Reference in New Issue