From 82e68670bf5b798b5682dd8bd34273c7b321ebe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sat, 24 Feb 2018 00:42:17 +0100 Subject: [PATCH] FIX: SimplePress importer wasn't handling increment imports properly --- script/import_scripts/simplepress.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/script/import_scripts/simplepress.rb b/script/import_scripts/simplepress.rb index d5846a359ad..0e4c5e22a09 100644 --- a/script/import_scripts/simplepress.rb +++ b/script/import_scripts/simplepress.rb @@ -99,8 +99,6 @@ class ImportScripts::SimplePress < ImportScripts::Base total_count = mysql_query("SELECT COUNT(*) count FROM #{TABLE_PREFIX}posts WHERE post_index = 1").first["count"] - @topic_first_post_id = {} - batches(BATCH_SIZE) do |offset| results = mysql_query(" SELECT p.post_id id, @@ -126,11 +124,10 @@ class ImportScripts::SimplePress < ImportScripts::Base next if all_records_exist? :posts, results.map { |m| m['id'].to_i } create_posts(results, total: total_count, offset: offset) do |m| - @topic_first_post_id[m['topic_id']] = m['id'] created_at = Time.zone.at(m['post_time']) { id: m['id'], - user_id: user_id_from_imported_user_id(m['user_id']) || -1, + user_id: user_id_from_imported_user_id(m['user_id']) || -1, raw: process_simplepress_post(m['raw'], m['id']), created_at: created_at, category: category_id_from_imported_category_id(m['category_id']), @@ -145,6 +142,15 @@ class ImportScripts::SimplePress < ImportScripts::Base def import_posts puts "", "creating posts" + topic_first_post_id = {} + + mysql_query(" + SELECT t.topic_id, p.post_id + FROM #{TABLE_PREFIX}topics t + JOIN #{TABLE_PREFIX}posts p ON p.topic_id = t.topic_id + WHERE p.post_index = 1 + ").each { |r| topic_first_post_id[r["topic_id"]] = r["post_id"] } + total_count = mysql_query("SELECT count(*) count FROM #{TABLE_PREFIX}posts WHERE post_index <> 1").first["count"] batches(BATCH_SIZE) do |offset| @@ -168,13 +174,13 @@ class ImportScripts::SimplePress < ImportScripts::Base next if all_records_exist? :posts, results.map { |m| m['id'].to_i } create_posts(results, total: total_count, offset: offset) do |m| - if parent = topic_lookup_from_imported_post_id(@topic_first_post_id[m['topic_id']]) + if parent = topic_lookup_from_imported_post_id(topic_first_post_id[m['topic_id']]) { id: m['id'], user_id: user_id_from_imported_user_id(m['user_id']) || -1, topic_id: parent[:topic_id], - raw: process_simplepress_post(m['raw'], m['id']), - created_at: Time.zone.at(m['post_time']), + raw: process_simplepress_post(m['raw'], m['id']), + created_at: Time.zone.at(m['post_time']), } else puts "Parent post #{m['topic_id']} doesn't exist. Skipping #{m["id"]}"