parfum/app/Console/Commands/Migrations/MigrateAllFromOldData.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

168 lines
5.5 KiB
PHP

<?php
namespace App\Console\Commands\Migrations;
use Database\Seeders\FormulaSeeder;
use Database\Seeders\UserSeeder;
use Illuminate\Console\Command;
class MigrateAllFromOldData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:all {--force : Force migration without confirmation} {--skip-confirm : Skip individual command confirmations}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run all data migrations from old database to new structure';
/**
* Execute the console command.
*/
public function handle()
{
if (! $this->option('force') && ! $this->confirm('Are you sure you want to run ALL migrations? This will add data to your database.')) {
$this->info('Migration cancelled.');
return;
}
$this->info('🚀 Starting complete data migration from old database...');
$this->newLine();
$commands = [
['command' => 'migrate:outlets', 'name' => 'Outlets', 'options' => ['--recreate' => true]],
['command' => 'migrate:users', 'name' => 'Users'],
['command' => 'migrate:employees', 'name' => 'Employees'],
['command' => 'migrate:categories', 'name' => 'Categories'],
['command' => 'migrate:products', 'name' => 'Products', 'options' => ['--recreate' => true]],
['command' => 'migrate:perfumes', 'name' => 'Perfumes', 'options' => ['--recreate' => true]],
['command' => 'migrate:bottles', 'name' => 'Bottles', 'options' => ['--recreate' => true]],
];
$totalCommands = count($commands);
$completed = 0;
$failed = 0;
foreach ($commands as $commandConfig) {
$commandName = $commandConfig['command'];
$name = $commandConfig['name'];
$options = $commandConfig['options'] ?? [];
// Always add force option
$options['--force'] = $this->option('skip-confirm');
$this->info("📋 Migrating {$name}...");
$this->newLine();
try {
$exitCode = $this->call($commandName, $options);
if ($exitCode === 0) {
$this->info("{$name} migration completed successfully!");
$completed++;
} else {
$this->error("{$name} migration failed!");
$failed++;
if (! $this->confirm("Continue with remaining migrations despite {$name} failure?")) {
$this->warn('Migration stopped by user.');
break;
}
}
} catch (\Exception $e) {
$this->error("{$name} migration error: {$e->getMessage()}");
$failed++;
if (! $this->confirm("Continue with remaining migrations despite {$name} error?")) {
$this->warn('Migration stopped by user.');
break;
}
}
$this->newLine(2);
}
$this->newLine();
$this->info('🎉 Migration process completed!');
$this->info('📊 Summary:');
$this->info(" ✅ Completed: {$completed}/{$totalCommands} migrations");
if ($failed > 0) {
$this->warn(" ❌ Failed: {$failed} migrations");
}
if ($completed === $totalCommands) {
$this->info('🎊 All migrations completed successfully!');
// Run seeders
$this->runSeeders();
} else {
$this->warn('⚠️ Some migrations failed. Please check the output above for details.');
}
return $failed > 0 ? 1 : 0;
}
/**
* Run the seeders after successful migration
*/
private function runSeeders()
{
$this->newLine();
$this->info('🌱 Running seeders...');
$seeders = [
['class' => UserSeeder::class, 'name' => 'UserSeeder'],
['class' => FormulaSeeder::class, 'name' => 'FormulaSeeder'],
];
$totalSeeders = count($seeders);
$completedSeeders = 0;
$failedSeeders = 0;
foreach ($seeders as $seederConfig) {
$seederClass = $seederConfig['class'];
$name = $seederConfig['name'];
$this->info("📋 Seeding {$name}...");
try {
$this->call($seederClass);
$this->info("{$name} seeded successfully!");
$completedSeeders++;
} catch (\Exception $e) {
$this->error("{$name} seeding failed: {$e->getMessage()}");
$failedSeeders++;
if (! $this->confirm("Continue with remaining seeders despite {$name} failure?")) {
$this->warn('Seeding stopped by user.');
break;
}
}
$this->newLine();
}
$this->info('🌱 Seeder process completed!');
$this->info('📊 Seeder Summary:');
$this->info(" ✅ Completed: {$completedSeeders}/{$totalSeeders} seeders");
if ($failedSeeders > 0) {
$this->warn(" ❌ Failed: {$failedSeeders} seeders");
}
if ($completedSeeders === $totalSeeders) {
$this->info('🎊 All seeders completed successfully!');
} else {
$this->warn('⚠️ Some seeders failed. Please check the output above for details.');
}
}
}