From 4eeec12b67fb82914a229c94e97e9677b9785f42 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Wed, 14 Aug 2024 12:56:14 -0500 Subject: [PATCH] add back tag dedupe code --- Rakefile | 18 ++++++++++++++++++ models/site.rb | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 8f048beb..fc218d94 100644 --- a/Rakefile +++ b/Rakefile @@ -238,3 +238,21 @@ task :generate_sitemap => [:environment] do gz.write %{} end end + +desc 'dedupe tags' +task :dedupetags => [:environment] do + Tag.all.each do |tag| + begin + tag.reload + rescue Sequel::Error => e + next if e.message =~ /Record not found/ + end + + matching_tags = Tag.exclude(id: tag.id).where(name: tag.name).all + + matching_tags.each do |matching_tag| + DB[:sites_tags].where(tag_id: matching_tag.id).update(tag_id: tag.id) + matching_tag.delete + end + end +end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index 5a7ae69d..fc5f6aeb 100644 --- a/models/site.rb +++ b/models/site.rb @@ -904,7 +904,7 @@ class Site < Sequel::Model end def add_tag_name(name) - add_tag Tag[name: name] || Tag.create(name: name) + add_tag Tag.find_or_create(name: name) end def before_create