浏览代码

Add new command

Daniel Supernault 5 年之前
父节点
当前提交
38a0fd30fe
共有 1 个文件被更改,包括 62 次插入0 次删除
  1. 62 0
      app/Console/Commands/StatusDedupe.php

+ 62 - 0
app/Console/Commands/StatusDedupe.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Status;
+use DB;
+use App\Jobs\StatusPipeline\StatusDelete;
+
+class StatusDedupe extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'status:dedup';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Removes duplicate statuses from before unique uri migration';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        DB::table('statuses')
+            ->selectRaw('id, uri, count(uri) as occurences')
+            ->whereNotNull('uri')
+            ->groupBy('uri')
+            ->orderBy('created_at')
+            ->having('occurences', '>', 1)
+            ->chunk(50, function($statuses) {
+                foreach($statuses as $status) {
+                    $this->info("Found duplicate: $status->uri");
+                    Status::whereUri($status->uri)
+                        ->where('id', '!=', $status->id)
+                        ->get()
+                        ->map(function($status) {
+                            $this->info("Deleting Duplicate ID: $status->id");
+                            StatusDelete::dispatch($status);
+                        });
+                }
+            });
+    }
+}