BackfillQuestionMetaCommand.php 978 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Question;
  4. use App\Models\QuestionMeta;
  5. use Illuminate\Console\Command;
  6. class BackfillQuestionMetaCommand extends Command
  7. {
  8. protected $signature = 'question:backfill-meta';
  9. protected $description = 'Ensure question_meta rows exist for every question';
  10. public function handle(): int
  11. {
  12. $questions = Question::query()->get(['id']);
  13. $created = 0;
  14. foreach ($questions as $question) {
  15. $meta = QuestionMeta::firstOrCreate(
  16. ['question_id' => $question->id],
  17. [
  18. 'abilities' => [],
  19. 'generation_info' => [],
  20. 'review_status' => 'pending',
  21. ]
  22. );
  23. if ($meta->wasRecentlyCreated) {
  24. $created++;
  25. }
  26. }
  27. $this->info(sprintf('Question meta backfilled: %d created', $created));
  28. return self::SUCCESS;
  29. }
  30. }