diff --git a/gossamer_forums.rb b/gossamer_forums.rb index d54e825..f88328b 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,7 +1,7 @@ # Federated Computer, Inc. # David Sainty 2024 A.D. # Gossamer Threads to Discourse -- Migration-Import Script -# v0.54 Tweak trust levels +# v0.55 Exception handling for topic or user entries in SQLite that are no (longer) in the Discourse DB require 'mysql2' require 'open-uri' @@ -229,7 +229,7 @@ class GossamerForumsImporter < ImportScripts::Base end # Method to create Nginx rewrite rules file - def create_nginx_rewrite_rules(filename) + def export_nginx_rewrite_rules(filename) File.open(filename, "w") do |file| @db.execute("SELECT old_post_id, new_url FROM url_map") do |row| old_post_id, new_url = row @@ -1772,17 +1772,22 @@ class GossamerForumsImporter < ImportScripts::Base # end @db.execute("SELECT * FROM topic_last_post_time").each do |row| topic_id, last_post_time = row -# Validation error: Topic.find(topic_id).update!( - topic = Topic.find(topic_id) - # Ensure we are only updating necessary fields - topic.update_columns( - updated_at: Time.at(last_post_time), - posts_count: fetch_db_topic_post_count(topic_id).to_i, - last_posted_at: Time.at(last_post_time), - bumped_at: Time.at(last_post_time), - last_post_user_id: fetch_db_topic_last_post_user(topic_id).to_i - ) + begin + # Validation error: Topic.find(topic_id).update!( + topic = Topic.find(topic_id) + + # Ensure we are only updating necessary fields + topic.update_columns( + updated_at: Time.at(last_post_time), + posts_count: fetch_db_topic_post_count(topic_id).to_i, + last_posted_at: Time.at(last_post_time), + bumped_at: Time.at(last_post_time), + last_post_user_id: fetch_db_topic_last_post_user(topic_id).to_i + ) + rescue ActiveRecord::RecordNotFound + puts "WARNING: Could not find Topic with id=#{topic_id}. Skipping..." + end end end @@ -1802,8 +1807,12 @@ class GossamerForumsImporter < ImportScripts::Base # user = User.find(user_id) # user.update!(topic_count: count) puts "update_user_stats user_id #{user_id} topic_count #{count}" - user_stat = UserStat.find_or_initialize_by(user_id: user_id) - user_stat.update_columns(topic_count: count) + begin + user_stat = UserStat.find_or_initialize_by(user_id: user_id) + user_stat.update_columns(topic_count: count) + rescue + puts "WARNING: Could not find User with id=#{user_id}. Skipping..." + end end @db.execute("SELECT * FROM user_post_count").each do |row| @@ -1811,28 +1820,29 @@ class GossamerForumsImporter < ImportScripts::Base # user = User.find(user_id) # user.update!(post_count: count) puts "update_user_stats user_id #{user_id} post_count #{count}" - user_stat = UserStat.find_or_initialize_by(user_id: user_id) - user_stat.update_columns(post_count: count) + begin + user_stat = UserStat.find_or_initialize_by(user_id: user_id) + user_stat.update_columns(post_count: count) - # Determine the new Trust Level based on post_count - user = User.find(user_id) - new_trust_level = case count - when 0..29 then 1 # basic user - else 2 # member, regular reserved for now. -# when 3..50 then 2 # member -# else 3 # regular or above when 51..100 - end + # Fetch the current user and check if Trust Level needs updating + new_trust_level = case count + when 0..29 then 1 # basic user + else 2 # member, regular reserved for now. +# when 3..50 then 2 # member +# else 3 # regular or above when 51..100 + end + user = User.find(user_id) + current_trust_level = user.trust_level || 1 # default to 1 if not set - # Fetch the current user and check if Trust Level needs updating - user = User.find(user_id) - current_trust_level = user.trust_level || 1 # default to 1 if not set - - # Only update trust level if the new level is higher than the current one - if new_trust_level > current_trust_level - user.update!(trust_level: new_trust_level) - puts "update_user_stats user_id #{user_id} trust_level updated to #{new_trust_level}" - else - puts "update_user_stats user_id #{user_id} trust_level remains at #{current_trust_level}" + # Only update trust level if the new level is higher than the current one + if new_trust_level != current_trust_level + user.update!(trust_level: new_trust_level) + puts "update_user_stats user_id #{user_id} trust_level was #{current_trust_level}, now updated to #{new_trust_level}" + else + puts "update_user_stats user_id #{user_id} trust_level remains at #{current_trust_level}" + end + rescue + puts "WARNING: Could not find or modify User with id=#{user_id}. Skipping..." end end end @@ -1988,9 +1998,9 @@ class GossamerForumsImporter < ImportScripts::Base # import_users # generate_user_id_mapping -# export_username_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-username-mapping#{timestamp}") + export_username_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-username-mapping#{timestamp}") - set_user_bio_images +# set_user_bio_images # import_categories @@ -2000,7 +2010,7 @@ class GossamerForumsImporter < ImportScripts::Base update_topic_stats update_user_stats export_url_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-url-mapping#{timestamp}") - create_nginx_rewrite_rules("/bitnami/discourse/sqlite/gossamer-redirects#{timestamp}.conf") + export_nginx_rewrite_rules("/bitnami/discourse/sqlite/gossamer-redirects#{timestamp}.conf") import_personal_messages