瀏覽代碼

Add postgres migration to fix duplicate hashtags

Daniel Supernault 2 年之前
父節點
當前提交
64059cb47e
共有 1 個文件被更改,包括 46 次插入0 次删除
  1. 46 0
      database/migrations/2023_05_03_042219_fix_postgres_hashtags.php

+ 46 - 0
database/migrations/2023_05_03_042219_fix_postgres_hashtags.php

@@ -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
+    {
+        //
+    }
+};