pixelfed/app/Console/Commands/StatusDedupe.php

69 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2019-08-12 08:46:51 +00:00
<?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()
{
2019-10-19 01:29:03 +00:00
if(config('database.default') == 'pgsql') {
$this->info('This command is not compatible with Postgres, we are working on a fix.');
return;
}
2019-08-12 08:46:51 +00:00
DB::table('statuses')
->selectRaw('id, uri, count(uri) as occurences')
2019-08-12 09:12:03 +00:00
->whereNull('deleted_at')
2019-08-12 08:46:51 +00:00
->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);
});
}
});
}
}