Add postgres migration to fix duplicate hashtags

This commit is contained in:
Daniel Supernault 2023-05-02 23:34:06 -06:00
parent 6e20d0a670
commit 64059cb47e
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Hashtag;
use App\StatusHashtag;
use Illuminate\Support\Facades\Cache;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$type = config('database.default');
if($type !== 'pgsql') {
return;
}
foreach(Hashtag::lazyById(50, 'id') as $tag) {
$dups = Hashtag::where('name', 'ilike', $tag->name)->orderBy('id')->get();
if($dups->count() === 1) {
continue;
}
$first = $dups->shift();
$dups->each(function($dup) use($first) {
StatusHashtag::whereHashtagId($dup->id)->update(['hashtag_id' => $first->id]);
$dup->delete();
});
}
Cache::clear();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};