diff --git a/database/migrations/2021_10_29_160416_repair_stats_jobs.php b/database/migrations/2021_10_29_160416_repair_stats_jobs.php new file mode 100644 index 0000000..ac46b63 --- /dev/null +++ b/database/migrations/2021_10_29_160416_repair_stats_jobs.php @@ -0,0 +1,62 @@ +count(); + $totalPages = ceil($total / $perPage); + do { + $jobs = DB::table('jobs')->paginate($perPage, ['*'], 'page', $page++); + foreach ($jobs as $job) { + $payload = json_decode($job->payload); + if ($payload->displayName === SetServiceStats::class) { + $ref = new ReflectionProperty(SetServiceStats::class, 'service'); + $ref->setAccessible(true); + $service = $ref->getValue(unserialize($payload->data->command)); + + $date = Carbon::parse($job->created_at)->format('Y-m-d'); + + $serviceStat = ServiceStat::whereDate('date', $date) + ->where('service_id', $service->id)->where('brand_id', $service->brand->id) + ->firstOrNew(); + + $serviceStat->date = $date; + if (!$serviceStat->exists) { + $serviceStat->service()->associate($service); + $serviceStat->brand()->associate($service->brand); + } + $serviceStat->nb_calls += 1; + $serviceStat->saveOrFail(); + } + } + DB::table('jobs')->whereIn('id', $jobIDS) + ->delete(); + $jobIDS = []; + } while ($page <= $totalPages); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}