StatusDedupe.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Status;
  5. use DB;
  6. use App\Jobs\StatusPipeline\StatusDelete;
  7. class StatusDedupe extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'status:dedup';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Removes duplicate statuses from before unique uri migration';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. if(config('database.default') == 'pgsql') {
  38. $this->info('This command is not compatible with Postgres, we are working on a fix.');
  39. return;
  40. }
  41. DB::table('statuses')
  42. ->selectRaw('id, uri, count(uri) as occurences')
  43. ->whereNull('deleted_at')
  44. ->whereNotNull('uri')
  45. ->groupBy('uri')
  46. ->orderBy('created_at')
  47. ->having('occurences', '>', 1)
  48. ->chunk(50, function($statuses) {
  49. foreach($statuses as $status) {
  50. $this->info("Found duplicate: $status->uri");
  51. Status::whereUri($status->uri)
  52. ->where('id', '!=', $status->id)
  53. ->get()
  54. ->map(function($status) {
  55. $this->info("Deleting Duplicate ID: $status->id");
  56. StatusDelete::dispatch($status);
  57. });
  58. }
  59. });
  60. }
  61. }