v0.23 Corner case for duplicate topic name/title, not allowed in Discourse

This commit is contained in:
David Sainty 2024-07-01 18:49:59 +10:00
parent efb56dba7e
commit 101d7cc0cb

View File

@ -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}"