parfum/app/Console/Commands/Migrations/MigrateOutletsFromOldData.php
Yoga Pangestu 3a05ee2617 feat: implement comprehensive migration commands for legacy data
- Added migration commands for users, employees, outlets, categories, products, bottles, and perfumes to transfer data from the old database to the new structure.
- Each command includes options for forced migration and recreation of existing records.
- Implemented progress feedback and error handling during migration processes.
- Updated database configuration to include connection settings for the old database.
- Enhanced DatabaseSeeder to automatically trigger the migration process after seeding essential data.
2026-01-10 19:04:31 +07:00

182 lines
6.0 KiB
PHP

<?php
namespace App\Console\Commands\Migrations;
use App\Enums\Day;
use App\Enums\OutletStatus;
use App\Models\Facility;
use App\Models\OpeningHour;
use App\Models\Outlet;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class MigrateOutletsFromOldData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:outlets {--force : Force migration without confirmation} {--recreate : Delete existing outlets and recreate}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrate outlets data from old database to new structure. Use --recreate to delete and recreate all outlets with opening hours and facilities';
/**
* Execute the console command.
*/
public function handle()
{
if ($this->option('recreate')) {
if (! $this->confirm('Are you sure you want to DELETE ALL EXISTING OUTLETS and recreate them? This action cannot be undone.')) {
$this->info('Migration cancelled.');
return;
}
$this->warn('Deleting all existing outlets...');
Outlet::query()->delete(); // This will cascade delete opening hours and facilities
$this->info('All outlets deleted.');
} elseif (! $this->option('force') && ! $this->confirm('Are you sure you want to migrate outlets data? This will add new outlets to your database.')) {
$this->info('Migration cancelled.');
return;
}
$this->info('Starting outlets migration...');
try {
$oldStores = DB::connection('old_database')->table('stores')->whereNull('deleted_at')->get();
$this->info("Found {$oldStores->count()} outlets to migrate.");
$bar = $this->output->createProgressBar($oldStores->count());
$migrated = 0;
$skipped = 0;
foreach ($oldStores as $oldStore) {
if (! $this->option('recreate')) {
$existingOutlet = Outlet::where('name', $oldStore->name)->first();
if ($existingOutlet) {
// Outlet exists, but ensure opening hours and facilities exist
$this->ensureOpeningHoursExist($existingOutlet->id);
$this->ensureFacilitiesExist($existingOutlet->id);
$this->warn("Outlet '{$oldStore->name}' already exists, ensured opening hours and facilities.");
$skipped++;
$bar->advance();
continue;
}
}
$outlet = Outlet::create([
'name' => $oldStore->name,
'slug' => Str::slug($oldStore->name),
'phone_number' => '0838 1693 1711',
'address' => $oldStore->address,
'landmark' => $oldStore->benchmark,
'maps_url' => null,
'status' => $oldStore->closed ? OutletStatus::TERMINATED : OutletStatus::OPERATIONAL,
'opened_date' => $oldStore->opened,
'closed_date' => $oldStore->closed,
'created_at' => $oldStore->created_at,
'updated_at' => $oldStore->updated_at,
]);
// Create opening hours for this outlet (9am-9pm)
$this->createOpeningHours($outlet->id);
// Create facilities for this outlet
$this->createFacilities($outlet->id);
$migrated++;
$bar->advance();
}
$bar->finish();
$this->newLine(2);
$this->info('✅ Outlets migration completed!');
$this->info("📊 Migrated: {$migrated} outlets");
$this->info("⏭️ Skipped: {$skipped} outlets (already exist)");
} catch (\Exception $e) {
$this->error("❌ Migration failed: {$e->getMessage()}");
return 1;
}
return 0;
}
private function ensureOpeningHoursExist(int $outletId): void
{
$existingHours = OpeningHour::where('outlet_id', $outletId)->count();
if ($existingHours < 7) { // Should have 7 days
// Delete existing and recreate
OpeningHour::where('outlet_id', $outletId)->delete();
$this->createOpeningHours($outletId);
$this->info("Recreated opening hours for outlet ID: {$outletId}");
}
}
private function ensureFacilitiesExist(int $outletId): void
{
$existingFacilities = Facility::where('outlet_id', $outletId)->count();
if ($existingFacilities < 4) { // Should have 4 facilities
// Delete existing and recreate
Facility::where('outlet_id', $outletId)->delete();
$this->createFacilities($outletId);
$this->info("Recreated facilities for outlet ID: {$outletId}");
}
}
private function createOpeningHours(int $outletId): void
{
// Create opening hours from 9am to 9pm for all days
$daysOfWeek = [
Day::MONDAY,
Day::TUESDAY,
Day::WEDNESDAY,
Day::THURSDAY,
Day::FRIDAY,
Day::SATURDAY,
Day::SUNDAY,
];
foreach ($daysOfWeek as $day) {
OpeningHour::create([
'outlet_id' => $outletId,
'day' => $day,
'open_time' => '09:00:00',
'close_time' => '21:00:00',
]);
}
}
private function createFacilities(int $outletId): void
{
// Facilities: parkir, sofa, air minum, permen
$facilities = [
'Parkir',
'Sofa',
'Air Minum',
'Permen',
];
foreach ($facilities as $facilityName) {
Facility::create([
'outlet_id' => $outletId,
'name' => $facilityName,
]);
}
}
}