From aff0c90976cc9b1af2fa73c3bf1c4c43c8c4e48b Mon Sep 17 00:00:00 2001 From: saint Date: Sat, 29 Jun 2024 02:17:30 +1000 Subject: [PATCH] v0.21.3 Fix coding oversight for SQLite methods --- gossamer_forums.rb | 87 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 19 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 0c198bd..0a3b378 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.21.2 +# v0.21.3 require 'mysql2' require 'open-uri' @@ -173,7 +173,7 @@ class GossamerForumsImporter < ImportScripts::Base end # Helper methods to interact with the SQLite database for persisting topic-post related values - def fetch_from_db(table, key) + def fetch_db(table, key) @db.get_first_value "SELECT value FROM #{table} WHERE key = ?", key end @@ -181,6 +181,55 @@ class GossamerForumsImporter < ImportScripts::Base @db.execute "INSERT OR REPLACE INTO #{table} (key, value) VALUES (?, ?)", key, value end + def fetch_db_topic_last_post_time(topic_id) + @db.get_first_value "SELECT last_post_time FROM topic_last_post_time WHERE topic_id = ?", topic_id + end + + def fetch_db_topic_last_post_user(topic_id) + @db.get_first_value "SELECT user_id FROM topic_last_post_user WHERE topic_id = ?", topic_id + end + + def fetch_db_topic_post_count(topic_id) + @db.get_first_value "SELECT post_count FROM topic_post_count WHERE topic_id = ?", topic_id + end + + def fetch_db_user_topic_count(user_id) + @db.get_first_value "SELECT topic_count FROM user_topic_count WHERE user_id = ?", user_id + end + + def fetch_db_user_post_count(user_id) + @db.get_first_value "SELECT post_count FROM user_post_count WHERE user_id = ?", user_id + end + + def fetch_db_topic_post_numbers(topic_id) + @db.get_first_value "SELECT post_number FROM topic_post_numbers WHERE topic_id = ?", topic_id + end + + def update_db_topic_last_post_time(topic_id, last_post_time) + @db.execute "INSERT OR REPLACE INTO topic_last_post_time (topic_id, last_post_time) VALUES (?, ?)", topic_id, last_post_time + end + + def update_db_topic_last_post_user(topic_id, user_id) + @db.execute "INSERT OR REPLACE INTO topic_last_post_user (topic_id, user_id) VALUES (?, ?)", topic_id, user_id + end + + def update_db_topic_post_count(topic_id, post_count) + @db.execute "INSERT OR REPLACE INTO topic_post_count (topic_id, post_count) VALUES (?, ?)", topic_id, post_count + end + + def update_db_user_topic_count(user_id, topic_count) + @db.execute "INSERT OR REPLACE INTO user_topic_count (user_id, topic_count) VALUES (?, ?)", user_id, topic_count + end + + def update_db_user_post_count(user_id, post_count) + @db.execute "INSERT OR REPLACE INTO user_post_count (user_id, post_count) VALUES (?, ?)", user_id, post_count + end + + def update_db_topic_post_numbers(topic_id, post_number) + @db.execute "INSERT OR REPLACE INTO topic_post_numbers (topic_id, post_number) VALUES (?, ?)", topic_id, post_number + end + + # Execute an SQL query on the Gossamer Forums database def execute_query(query) @mysql_client.query(query, as: :hash) @@ -754,12 +803,12 @@ class GossamerForumsImporter < ImportScripts::Base # Track last post time and user for the topic # topic_last_post_time[topic.id] = Time.at(row['post_time']) # topic_last_post_user[topic.id] = discourse_user_id - update_db('topic_last_post_time', topic.id, Time.at(row['post_time']).to_i) - update_db('topic_last_post_user', topic.id, discourse_user_id) + update_db_topic_last_post_time(topic.id, Time.at(row['post_time']).to_i) + update_db_topic_last_post_user(topic.id, discourse_user_id) # Increment the count of the number of topics created by each user # user_topic_count[discourse_user_id] += 1 - update_db('user_topic_count', discourse_user_id, fetch_from_db(db, 'user_topic_count', discourse_user_id).to_i + 1) + update_db_user_topic_count(discourse_user_id, fetch_db_user_topic_count(discourse_user_id).to_i + 1) # # Ensure the raw post stirng contents itself is acceptable to Discourse # sanitized_post_message = row['post_message']&.tr("\0", '') || "" @@ -778,8 +827,8 @@ class GossamerForumsImporter < ImportScripts::Base # Increment the number of posts in the given topic. # topic_post_numbers[topic.id] += 1 - post_number = fetch_from_db(db, 'topic_post_numbers', topic.id).to_i + 1 - update_db(db, 'topic_post_numbers', topic.id, post_number) + post_number = fetch_db_topic_post_numbers(topic.id).to_i + 1 + update_db_topic_post_numbers(topic.id, post_number) # Create the initial post in the topic post = Post.create!( @@ -801,8 +850,8 @@ class GossamerForumsImporter < ImportScripts::Base # Track the number of posts in the topic and by the user # topic_post_count[topic.id] += 1 # user_post_count[discourse_user_id] += 1 - update_db('topic_post_count', topic.id, fetch_from_db(db, 'topic_post_count', topic.id).to_i + 1) - update_db('user_post_count', discourse_user_id, fetch_from_db(db, 'user_post_count', discourse_user_id).to_i + 1) + update_db_topic_post_count(topic.id, fetch_db_topic_post_count(topic.id).to_i + 1) + update_db_user_post_count(discourse_user_id, fetch_db_user_post_count(discourse_user_id).to_i + 1) # Handle attachments for the post handle_post_attachments(row['post_id'], post, discourse_user_id) @@ -840,8 +889,8 @@ class GossamerForumsImporter < ImportScripts::Base sanitized_post_message = sanitize_post_message(row['post_message']) # topic_post_numbers[topic_id] += 1 - post_number = fetch_from_db(db, 'topic_post_numbers', topic_id).to_i + 1 - update_db('topic_post_numbers', topic_id, post_number) + post_number = fetch_db_topic_post_numbers(topic_id).to_i + 1 + update_db_topic_post_numbers(topic_id, post_number) # Create the post in the existing topic post = Post.create!( @@ -854,7 +903,7 @@ class GossamerForumsImporter < ImportScripts::Base updated_at: Time.at(row['post_time']), reply_to_post_number: reply_to_post_number, like_count: row['post_replies'] || 0, - reads: post_views || fetch_from_db('topic_post_count', topic_id).to_i, + reads: post_views || fetch_db_topic_post_count(topic_id).to_i, post_number: post_number ) post.custom_fields['original_gossamer_id'] = row['post_id'] @@ -863,17 +912,17 @@ class GossamerForumsImporter < ImportScripts::Base # Track the number of posts in the topic and by the user # topic_post_count[topic_id] += 1 # user_post_count[discourse_user_id] += 1 - update_db('topic_post_count', topic_id, fetch_from_db('topic_post_count', topic_id).to_i + 1) - update_db('user_post_count', discourse_user_id, fetch_from_db('user_post_count', discourse_user_id).to_i + 1) + update_db_topic_post_count(topic_id, fetch_db_topic_post_count(topic_id).to_i + 1) + update_db_user_post_count(discourse_user_id, fetch_db_user_post_count(discourse_user_id).to_i + 1) # Update last post time and user for the topic # if topic_last_post_time[topic_id].nil? || Time.at(row['post_time']) > topic_last_post_time[topic_id] # topic_last_post_time[topic_id] = Time.at(row['post_time']) # topic_last_post_user[topic_id] = discourse_user_id # end - if fetch_from_db('topic_last_post_time', topic_id).nil? || Time.at(row['post_time']).to_i > fetch_from_db('topic_last_post_time', topic_id).to_i - update_db('topic_last_post_time', topic_id, Time.at(row['post_time']).to_i) - update_db('topic_last_post_user', topic_id, discourse_user_id) + if fetch_db_topic_last_post_time(topic_id).nil? || Time.at(row['post_time']).to_i > fetch_db_topic_last_post_time(topic_id).to_i + update_db_topic_last_post_time(topic_id, Time.at(row['post_time']).to_i) + update_db_topic_last_post_user(topic_id, discourse_user_id) end # Handle attachments for the post @@ -902,10 +951,10 @@ class GossamerForumsImporter < ImportScripts::Base topic_id, last_post_time = row Topic.find(topic_id).update!( updated_at: Time.at(last_post_time), - posts_count: fetch_from_db(db, 'topic_post_count', topic_id).to_i, + 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_from_db(db, 'topic_last_post_user', topic_id).to_i + last_post_user_id: fetch_db_topic_last_post_user(topic_id).to_i ) end