A platform for community discussion. Free, open, simple.
DEV: speed up posts base imports
Régis Hanol788719d27143
diff --git a/lib/post_creator.rb b/lib/post_creator.rbindex c5c09d04d1b..173a30cf20b 100644--- a/lib/post_creator.rb+++ b/lib/post_creator.rb@@ -174,15 +174,14 @@ class PostCreatorupdate_user_countscreate_embedded_topiclink_post_uploads-ensure_in_allowed_users if guardian.is_staff?unarchive_message- @post.advance_draft_sequence+ @post.advance_draft_sequence unless @opts[:import_mode]@post.save_reply_relationshipsendend- if @post && errors.blank?+ if @post && errors.blank? && !@opts[:import_mode]# update counters etc.@post.topic.reload@@ -194,12 +193,10 @@ class PostCreatortrigger_after_events unless opts[:skip_events]- auto_close unless @opts[:import_mode]+ auto_closeend- if @post || @spam- handle_spam unless @opts[:import_mode]- end+ handle_spam if !opts[:import_mode] && (@post || @spam)@postend@@ -428,6 +425,8 @@ class PostCreatorenddef update_topic_auto_close+ return if @opts[:import_mode]+if @topic.closed?@topic.delete_topic_timer(TopicTimer.types[:close])else@@ -510,9 +509,7 @@ class PostCreatorenddef publish- return if @opts[:import_mode]- return unless @post.post_number > 1-+ return if @opts[:import_mode] || @post.post_number == 1@post.publish_change_to_clients! :createdend@@ -522,7 +519,7 @@ class PostCreatorenddef track_topic- return if @opts[:auto_track] == false+ return if @opts[:import_mode] || @opts[:auto_track] == falseunless @user.user_option.disable_jump_reply?TopicUser.change(@post.user_id,@@ -540,8 +537,7 @@ class PostCreatorif @user.stagedTopicUser.auto_notification_for_staging(@user.id, @topic.id, TopicUser.notification_reasons[:auto_watch])- else- return if @topic.private_message?+ elsif [email protected]_message?notification_level = @user.user_option.notification_level_when_replying || NotificationLevels.topic_levels[:tracking]TopicUser.auto_notification(@user.id, @topic.id, TopicUser.notification_reasons[:created_post], notification_level)enddiff --git a/lib/upload_creator.rb b/lib/upload_creator.rbindex 2d7cc9166ca..a1386feb2e3 100644--- a/lib/upload_creator.rb+++ b/lib/upload_creator.rb@@ -80,8 +80,8 @@ class UploadCreatorendfixed_original_filename = nil- if is_image+ if is_imagecurrent_extension = File.extname(@filename).downcase.sub("jpeg", "jpg")expected_extension = ".#{image_type}".downcase.sub("jpeg", "jpg")@@ -89,11 +89,7 @@ class UploadCreator# otherwise validation will fail and we can not save# TODO decide if we only run the validation on the extensionif current_extension != expected_extension- basename = File.basename(@filename, current_extension)-- if basename.length == 0- basename = "image"- end+ basename = File.basename(@filename, current_extension).presence || "image"fixed_original_filename = "#{basename}#{expected_extension}"endend@@ -173,10 +169,7 @@ class UploadCreatorMIN_CONVERT_TO_JPEG_SAVING_RATIO = 0.70def convert_to_jpeg!-- if filesize < MIN_CONVERT_TO_JPEG_BYTES_SAVED- return- end+ return if filesize < MIN_CONVERT_TO_JPEG_BYTES_SAVEDjpeg_tempfile = Tempfile.new(["image", ".jpg"])@@ -290,7 +283,6 @@ class UploadCreatordef crop!max_pixel_ratio = Discourse::PIXEL_RATIOS.max-filename_with_correct_ext = "image.#{@image_info.type}"case @opts[:type]diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rbindex 3ce3ef04526..30dfb7cb080 100644--- a/script/import_scripts/base.rb+++ b/script/import_scripts/base.rb@@ -54,6 +54,8 @@ class ImportScripts::Baseupdate_last_posted_atupdate_last_seen_atupdate_user_stats+ update_topic_users+ update_post_timingsupdate_feature_topic_usersupdate_category_featured_topicsupdate_topic_count_replies@@ -313,7 +315,7 @@ class ImportScripts::Baseunless opts[:email][EmailValidator.email_regex]opts[:email] = fake_email- puts "Invalid email #{original_email} for #{opts[:username]}. Using: #{opts[:email]}"+ puts "Invalid email '#{original_email}' for '#{opts[:username]}'. Using '#{opts[:email]}'"endopts[:name] = original_username if original_name.blank? && opts[:username] != original_username@@ -326,7 +328,7 @@ class ImportScripts::Baseu = User.new(opts)(opts[:custom_fields] || {}).each { |k, v| u.custom_fields[k] = v }u.custom_fields["import_id"] = import_id- u.custom_fields["import_username"] = opts[:username] if original_username.present?+ u.custom_fields["import_username"] = original_username if original_username.present? && original_username != opts[:username]u.custom_fields["import_avatar_url"] = avatar_url if avatar_url.present?u.custom_fields["import_pass"] = opts[:password] if opts[:password].present?u.custom_fields["import_email"] = original_email if original_email != opts[:email]@@ -506,21 +508,19 @@ class ImportScripts::Baseimport_id = params.delete(:id).to_sif post_id_from_imported_post_id(import_id)- skipped += 1 # already imported this post+ skipped += 1elsebeginnew_post = create_post(params, import_id)if new_post.is_a?(Post)add_post(import_id, new_post)add_topic(new_post)-created_post(new_post)-created += 1elseskipped += 1puts "Error creating post #{import_id}. Skipping."- puts new_post.inspect+ p new_postendrescue Discourse::InvalidAccess => eskipped += 1@@ -632,7 +632,7 @@ class ImportScripts::Basedef update_topic_statusputs "", "Updating topic status"- DB.exec(<<~SQL)+ DB.exec <<~SQLUPDATE topics AS tSET closed = TRUEWHERE EXISTS(@@ -642,7 +642,7 @@ class ImportScripts::Base)SQL- DB.exec(<<~SQL)+ DB.exec <<~SQLUPDATE topics AS tSET archived = TRUEWHERE EXISTS(@@ -652,7 +652,7 @@ class ImportScripts::Base)SQL- DB.exec(<<~SQL)+ DB.exec <<~SQLDELETE FROM topic_custom_fieldsWHERE name IN ('import_closed', 'import_archived')SQL@@ -660,13 +660,16 @@ class ImportScripts::Basedef update_bumped_atputs "", "Updating bumped_at on topics"- DB.exec("update topics t set bumped_at = COALESCE((select max(created_at) from posts where topic_id = t.id and post_type = #{Post.types[:regular]}), bumped_at)")+ DB.exec <<~SQL+ UPDATE topics t+ SET bumped_at = COALESCE((SELECT MAX(created_at) FROM posts WHERE topic_id = t.id AND post_type = #{Post.types[:regular]}), bumped_at)+ SQLenddef update_last_posted_atputs "", "Updating last posted at on users"- sql = <<-SQL+ DB.exec <<~SQLWITH lpa AS (SELECT user_id, MAX(posts.created_at) AS last_posted_atFROM posts@@ -679,8 +682,6 @@ class ImportScripts::BaseWHERE u1.id = users.idAND users.last_posted_at <> lpa.last_posted_atSQL-- DB.exec(sql)enddef update_user_stats@@ -699,7 +700,7 @@ class ImportScripts::Baseputs "", "Updating first_post_created_at..."- sql = <<-SQL+ DB.exec <<~SQLWITH sub AS (SELECT user_id, MIN(posts.created_at) AS first_post_created_atFROM posts@@ -713,11 +714,9 @@ class ImportScripts::BaseAND user_stats.first_post_created_at <> sub.first_post_created_atSQL- DB.exec(sql)-puts "", "Updating user post_count..."- sql = <<-SQL+ DB.exec <<~SQLWITH sub AS (SELECT user_id, COUNT(*) AS post_countFROM posts@@ -731,11 +730,9 @@ class ImportScripts::BaseAND user_stats.post_count <> sub.post_countSQL- DB.exec(sql)-puts "", "Updating user topic_count..."- sql = <<-SQL+ DB.exec <<~SQLWITH sub AS (SELECT user_id, COUNT(*) AS topic_countFROM topics@@ -748,8 +745,6 @@ class ImportScripts::BaseWHERE u1.user_id = user_stats.user_idAND user_stats.topic_count <> sub.topic_countSQL-- DB.exec(sql)end# scripts that are able to import last_seen_at from the source data should override this method@@ -760,6 +755,31 @@ class ImportScripts::BaseDB.exec("UPDATE users SET last_seen_at = last_posted_at WHERE last_posted_at IS NOT NULL")end+ def update_topic_users+ puts "", "Updating topic users"++ DB.exec <<~SQL+ INSERT INTO topic_users (user_id, topic_id, posted, last_read_post_number, highest_seen_post_number, first_visited_at, last_visited_at, total_msecs_viewed)+ SELECT user_id, topic_id, 't' , MAX(post_number), MAX(post_number), MIN(created_at), MAX(created_at), COUNT(id) * 5000+ FROM posts+ WHERE user_id > 0+ GROUP BY user_id, topic_id+ ON CONFLICT DO NOTHING+ SQL+ end++ def update_post_timings+ puts "", "Updating post timings"++ DB.exec <<~SQL+ INSERT INTO post_timings (topic_id, post_number, user_id, msecs)+ SELECT topic_id, post_number, user_id, 5000+ FROM posts+ WHERE user_id > 0+ ON CONFLICT DO NOTHING+ SQL+ end+def update_feature_topic_usersputs "", "Updating featured topic users"