From 77d3ebd452b572730931e1502d94a6c4d5c0c5ca Mon Sep 17 00:00:00 2001 From: shibafu Date: Sat, 22 Feb 2020 16:32:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E9=87=8D=E8=A4=87=E3=82=BF=E3=82=B0?= =?UTF-8?q?=E3=81=AE=E5=89=8A=E9=99=A4=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/DedupTags.php | 113 +++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/Console/Commands/DedupTags.php diff --git a/app/Console/Commands/DedupTags.php b/app/Console/Commands/DedupTags.php new file mode 100644 index 0000000..3300e59 --- /dev/null +++ b/app/Console/Commands/DedupTags.php @@ -0,0 +1,113 @@ +option('dry-run')) { + $this->warn('dry-runモードで実行します。'); + } else { + if (!$this->confirm('dry-runオプションが付いてないけど、本当に実行しますか?')) { + return; + } + } + + DB::transaction(function () { + $duplicatedTags = DB::table('tags') + ->select('name', DB::raw('count(*)')) + ->groupBy('name') + ->having(DB::raw('count(*)'), '>=', 2) + ->get(); + + $this->info($duplicatedTags->count() . ' duplicated tags found.'); + + foreach ($duplicatedTags as $tag) { + $this->line('Tag name: ' . $tag->name); + + $tagIds = Tag::where('name', $tag->name)->orderBy('id')->pluck('id'); + $newId = $tagIds->first(); + $dropIds = $tagIds->slice(1); + + $this->line(' New ID: ' . $newId); + $this->line(' Drop IDs: ' . $dropIds->implode(', ')); + + if ($this->option('dry-run')) { + continue; + } + + // 同じタグ名でIDが違うものについて、全て統一する + foreach (['ejaculation_tag', 'metadata_tag'] as $table) { + DB::table($table) + ->whereIn('tag_id', $dropIds) + ->update(['tag_id' => $newId]); + } + DB::table('tags')->whereIn('id', $dropIds)->delete(); + + // 統一した上で、重複しているレコードを削除する + DB::delete( + << 1 +) +SQL + ); + DB::delete( + << 1 +) +SQL + ); + } + }); + + $this->info('Done!'); + } +} From 631ae820f30999686d1bada2c03be774ae642f53 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sat, 22 Feb 2020 16:46:15 +0900 Subject: [PATCH 2/2] add unique constraints --- ...add_unique_constraint_to_tag_relations.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 database/migrations/2020_02_22_164304_add_unique_constraint_to_tag_relations.php diff --git a/database/migrations/2020_02_22_164304_add_unique_constraint_to_tag_relations.php b/database/migrations/2020_02_22_164304_add_unique_constraint_to_tag_relations.php new file mode 100644 index 0000000..b8d6f50 --- /dev/null +++ b/database/migrations/2020_02_22_164304_add_unique_constraint_to_tag_relations.php @@ -0,0 +1,38 @@ +unique(['ejaculation_id', 'tag_id']); + }); + Schema::table('metadata_tag', function (Blueprint $table) { + $table->unique(['metadata_url', 'tag_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('ejaculation_tag', function (Blueprint $table) { + $table->dropUnique(['ejaculation_id', 'tag_id']); + }); + Schema::table('metadata_tag', function (Blueprint $table) { + $table->dropUnique(['metadata_url', 'tag_id']); + }); + } +}