From 101d7cc0cb25117bce33cc89fc1c8b2c445c1a34 Mon Sep 17 00:00:00 2001 From: saint Date: Mon, 1 Jul 2024 18:49:59 +1000 Subject: [PATCH] v0.23 Corner case for duplicate topic name/title, not allowed in Discourse --- gossamer_forums.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index f58e661..694f70a 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.22.3 +# v0.23 require 'mysql2' require 'open-uri' @@ -306,6 +306,18 @@ class GossamerForumsImporter < ImportScripts::Base email end + def generate_unique_title(base_title, user_id, timestamp) + unique_title = base_title + suffix = 1 + + while Topic.exists?(title: unique_title) + unique_title = "#{base_title} (#{suffix})" + suffix += 1 + end + + unique_title + end + # Helper method to download an attachment / image from a URL def download_attachment(url) begin @@ -788,7 +800,6 @@ class GossamerForumsImporter < ImportScripts::Base # topic_post_numbers = Hash.new { |hash, key| hash[key] = 0 } # Fetch the highest old_post_id from the url_map table -# FIX THIS PROPERLY highest_old_post_id = fetch_highest_old_post_id.to_i puts "Highest (OP) old_post_id in url_map: #{highest_old_post_id}" highest_processed_post_id = fetch_highest_processed_post_id.to_i @@ -826,9 +837,10 @@ class GossamerForumsImporter < ImportScripts::Base # Create the topic begin puts "#2" - puts "CREATE TOPIC title #{title} discourse_user_id #{discourse_user_id} category_id #{discourse_category_id}" + unique_title = generate_unique_title(title, discourse_user_id, Time.at(row['post_time'])) + puts "CREATE TOPIC unique_title #{unique_title} title #{title} discourse_user_id #{discourse_user_id} category_id #{discourse_category_id}" topic = Topic.create!( - title: title, + title: unique_title, user_id: discourse_user_id, created_at: Time.at(row['post_time']), updated_at: Time.at(row['post_time']), @@ -898,7 +910,7 @@ class GossamerForumsImporter < ImportScripts::Base # Create URL mappings # old_url = "https://old/forum/#{row['forum_name']}/topics/#{row['post_id']}" new_url = "https://new/t/#{topic.slug}/#{topic.id}" - insert_url_mapping(row['post_id'], new_url, title) + insert_url_mapping(row['post_id'], new_url, unique_title) # Update the highest processed post_id puts "Updated highest processed post_id #{post_id}"