Files
project-replica/routes/console.php
2026-03-23 00:51:38 +09:00

45 lines
1.4 KiB
PHP

<?php
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;
use App\Models\MigrationSchedule;
use App\Models\MigrationRun;
use App\Jobs\RunMigrationJob;
// Check for due migration schedules every minute
Schedule::call(function () {
$schedules = MigrationSchedule::where('is_active', true)
->where(function ($query) {
$query->whereNull('next_run_at')
->orWhere('next_run_at', '<=', now());
})
->get();
foreach ($schedules as $schedule) {
// Skip if there's already a running or pending migration
$existingRun = MigrationRun::where('schedule_id', $schedule->id)
->whereIn('status', ['pending', 'running'])
->latest()
->first();
if ($existingRun) {
continue; // Skip this schedule, migration already in progress
}
// Create migration run and dispatch job
$migrationRun = MigrationRun::create([
'schedule_id' => $schedule->id,
'status' => 'pending',
'total_tables' => count($schedule->tables),
]);
RunMigrationJob::dispatch($migrationRun)->onQueue('default');
}
})->name('check-migration-schedules')->everyMinute()->withoutOverlapping();
// Check for schema changes every hour
Schedule::command('schema:check-changes')
->hourly()
->withoutOverlapping()
->onOneServer();