From 1e62f5ee09b8ea04e439b291ac323df1415f7d36 Mon Sep 17 00:00:00 2001 From: saint Date: Thu, 20 Jun 2024 13:28:14 +1000 Subject: [PATCH] v0.11 Add support for SQLite DB and CSV file generation for mapping of old to new OP / new topic URLs --- gossamer_forums.rb | 56 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 4bca310..2ef41c6 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.10 +# v0.11 require 'mysql2' require 'open-uri' @@ -58,6 +58,13 @@ class GossamerForumsImporter < ImportScripts::Base real_name TEXT ); SQL + @db.execute <<-SQL + CREATE TABLE IF NOT EXISTS url_map ( + old_url TEXT, + new_url TEXT, + title TEXT + ); + SQL end def insert_user_id_mapping(old_user_id, new_user_id) @@ -91,6 +98,24 @@ class GossamerForumsImporter < ImportScripts::Base csv << row end end + puts "Exported changed username mappings to #{filename}" + end + + # Insert a URL mapping into the SQLite database + def insert_url_mapping(old_url, new_url, title) + @db.execute "INSERT INTO url_map (old_url, new_url, title) VALUES (?, ?, ?, ?, ?)", [old_url, new_url, title] + end + + # Export the URL mappings to a CSV file + def export_url_mapping_to_csv(filename) + CSV.open(filename, "wb") do |csv| + # Add headers + csv << ["Old URL", "New URL", "Title"] + @db.execute("SELECT * FROM url_map") do |row| + csv << row + end + end + puts "Exported URL mappings to #{filename}" end @@ -469,6 +494,12 @@ def import_topics_and_posts ) post.custom_fields['original_gossamer_id'] = row['post_id'] post.save! + + # 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(old_url, new_url, title) + rescue ActiveRecord::RecordInvalid => e puts "Error importing topic with post_id #{row['post_id']}: #{e.message}" end @@ -589,15 +620,26 @@ end # Main method to perform the import def perform_import + # Secret trick to disable RateLimiting support in Discourse RateLimiter.disable - puts "Starting Gossamer Forums import..." - # import_users - # export_username_mapping_to_csv - # import_categories - # import_topics_and_posts + + # Set our unique timestamp for this migration run + timestamp = Time.now.strftime("-%y%m%d%H%M%S") + + puts "Starting Gossamer Forums import... #{timestamp}" + + import_users + export_username_mapping_to_csv("gossamer-migration-username-mapping#{timestamp}") + + import_categories + import_topics_and_posts + export_url_mapping_to_csv("gossamer-migration-url-mapping#{timestamp}") + import_personal_messages + # import_attachments - puts "Gossamer Forums import complete!" + + puts "Gossamer Forums import complete! #{timestamp}" end end