v0.11 Add support for SQLite DB and CSV file generation for mapping of old to new OP / new topic URLs

This commit is contained in:
David Sainty 2024-06-20 13:28:14 +10:00
parent a6d48e825d
commit 1e62f5ee09

View File

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