v0.41 Further improve FULL concurrency support, for both MySQL/MariaDB _and_ importantly, the PostGreSQL Discourse DB additions and changes with ActiveRecord connection pooling and Mutex

This commit is contained in:
David Sainty 2024-08-17 16:34:46 +10:00
parent 5e2d2e78e5
commit 8dd8e2f72e

View File

@ -1157,13 +1157,15 @@ class GossamerForumsImporter < ImportScripts::Base
#check if exists, create if not #check if exists, create if not
#get children, create -- SELECT post_id, user_id_fk, forum_id_fk, post_root_id, post_subject, post_time, post_message, post_father_id, post_replies FROM gforum_Post WHERE post_root_id = post_id #get children, create -- SELECT post_id, user_id_fk, forum_id_fk, post_root_id, post_subject, post_time, post_message, post_father_id, post_replies FROM gforum_Post WHERE post_root_id = post_id
#this parts needs to be synchronously to avoid race conditions #this parts needs to be synchronously to avoid race conditions
puts "TIJ AA post_id #{post_id}"
# Fetch the post data for the given post_id (this is the first post in the topic) # Fetch the post data for the given post_id (this is the first post in the topic)
row = execute_query("SELECT post_id, user_id_fk, forum_id_fk, post_root_id, post_subject, post_time, post_message, post_father_id, post_replies FROM gforum_Post WHERE post_id = #{post_id}", mysql_client).first row = execute_query("SELECT post_id, user_id_fk, forum_id_fk, post_root_id, post_subject, post_time, post_message, post_father_id, post_replies FROM gforum_Post WHERE post_id = #{post_id}", mysql_client).first
puts "TIJ BB post_id #{post_id}"
# Early return if the post data is not found # Early return if the post data is not found
return unless row return unless row
puts "TIJ CC post_id #{post_id}"
# Extract key values from the fetched row # Extract key values from the fetched row
post_id = row['post_id'].to_i post_id = row['post_id'].to_i
puts "Processing post_id #{row['post_id']} post_root_id #{row['post_root_id']} post_subject/title #{row['post_subject']} forum_id_fk/category_id #{row['forum_id_fk']}" puts "Processing post_id #{row['post_id']} post_root_id #{row['post_root_id']} post_subject/title #{row['post_subject']} forum_id_fk/category_id #{row['forum_id_fk']}"
@ -1174,6 +1176,7 @@ class GossamerForumsImporter < ImportScripts::Base
puts "discourse_user_id #{discourse_user_id} discourse_category_id #{discourse_category_id}" puts "discourse_user_id #{discourse_user_id} discourse_category_id #{discourse_category_id}"
return unless discourse_user_id && discourse_category_id return unless discourse_user_id && discourse_category_id
puts "TIJ DD post_id #{post_id}"
# Ensure the topic title is valid and generate a unique title if needed # Ensure the topic title is valid and generate a unique title if needed
title = ensure_valid_title(row['post_subject']) title = ensure_valid_title(row['post_subject'])
unique_title = title unique_title = title
@ -1183,6 +1186,7 @@ class GossamerForumsImporter < ImportScripts::Base
# Check if the topic has already been imported using the custom field 'original_gossamer_id' # Check if the topic has already been imported using the custom field 'original_gossamer_id'
unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id']) unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id'])
puts "TIJ EE post_id #{post_id}"
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# Create the new topic in Discourse # Create the new topic in Discourse
begin begin
@ -1191,6 +1195,7 @@ class GossamerForumsImporter < ImportScripts::Base
while !topic_created while !topic_created
begin begin
puts "TIJ FF post_id #{post_id}"
puts "CREATE TOPIC unique_title #{unique_title} title #{title} discourse_user_id #{discourse_user_id} category_id #{discourse_category_id}" puts "CREATE TOPIC unique_title #{unique_title} title #{title} discourse_user_id #{discourse_user_id} category_id #{discourse_category_id}"
topic = Topic.create!( topic = Topic.create!(
title: unique_title, title: unique_title,
@ -1230,7 +1235,8 @@ class GossamerForumsImporter < ImportScripts::Base
# Increment the post count for the topic # Increment the post count for the topic
post_number = fetch_db_topic_post_numbers(topic.id).to_i + 1 post_number = fetch_db_topic_post_numbers(topic.id).to_i + 1
update_db_topic_post_numbers(topic.id, post_number) update_db_topic_post_numbers(topic.id, post_number)
puts "TIJ GG post_id #{post_id}"
# Create the initial post in the new topic # Create the initial post in the new topic
post = Post.create!( post = Post.create!(
topic_id: topic.id, topic_id: topic.id,
@ -1247,7 +1253,8 @@ class GossamerForumsImporter < ImportScripts::Base
# Increment the post count for the topic and user # Increment the post count for the topic and user
update_db_topic_post_count(topic.id, fetch_db_topic_post_count(topic.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_db_user_post_count(discourse_user_id, fetch_db_user_post_count(discourse_user_id).to_i + 1)
puts "TIJ HH post_id #{post_id}"
# Handle any attachments associated with the post # Handle any attachments associated with the post
handle_post_attachments(row['post_id'], post, discourse_user_id) handle_post_attachments(row['post_id'], post, discourse_user_id)