From bac21d317b2beace443b1650981833f12165325a Mon Sep 17 00:00:00 2001 From: Quangbuu Le Date: Tue, 1 Aug 2017 15:01:45 +0700 Subject: [PATCH] Bulk import likes from vBulletin thanks (#5014) --- script/bulk_import/base.rb | 20 ++++++++++++++++++++ script/bulk_import/vbulletin.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/script/bulk_import/base.rb b/script/bulk_import/base.rb index efacea373bc..189d154740a 100644 --- a/script/bulk_import/base.rb +++ b/script/bulk_import/base.rb @@ -110,6 +110,9 @@ class BulkImport::Base @last_post_id = Post.unscoped.maximum(:id) @post_number_by_post_id = Post.unscoped.pluck(:id, :post_number).to_h @topic_id_by_post_id = Post.unscoped.pluck(:id, :topic_id).to_h + + puts "Loading post actions indexes..." + @last_post_action_id = PostAction.unscoped.maximum(:id) || 0 end def execute @@ -124,6 +127,7 @@ class BulkImport::Base @raw_connection.exec("SELECT setval('#{Category.sequence_name}', #{@last_category_id})") @raw_connection.exec("SELECT setval('#{Topic.sequence_name}', #{@last_topic_id})") @raw_connection.exec("SELECT setval('#{Post.sequence_name}', #{@last_post_id})") + @raw_connection.exec("SELECT setval('#{PostAction.sequence_name}', #{@last_post_action_id})") end def group_id_from_imported_id(id); @groups[id.to_s]; end @@ -179,6 +183,12 @@ class BulkImport::Base like_count raw cooked hidden word_count created_at last_version_at updated_at } + POST_ACTION_COLUMNS ||= %i{ + id post_id user_id post_action_type_id deleted_at created_at updated_at + deleted_by_id related_post_id staff_took_action deferred_by_id targets_topic + agreed_at agreed_by_id deferred_at disagreed_at disagreed_by_id + } + TOPIC_ALLOWED_USER_COLUMNS ||= %i{ topic_id user_id created_at updated_at } @@ -205,6 +215,7 @@ class BulkImport::Base def create_categories(rows, &block); create_records(rows, "category", CATEGORY_COLUMNS, &block); end def create_topics(rows, &block); create_records(rows, "topic", TOPIC_COLUMNS, &block); end def create_posts(rows, &block); create_records(rows, "post", POST_COLUMNS, &block); end + def create_post_actions(rows, &block); create_records(rows, "post_action", POST_ACTION_COLUMNS, &block); end def create_topic_allowed_users(rows, &block); create_records(rows, "topic_allowed_user", TOPIC_ALLOWED_USER_COLUMNS, &block); end def process_group(group) @@ -358,6 +369,15 @@ class BulkImport::Base post end + def process_post_action(post_action) + post_action[:id] ||= @last_post_action_id += 1 + post_action[:staff_took_action] ||= false + post_action[:targets_topic] ||= false + post_action[:created_at] ||= NOW + post_action[:updated_at] ||= post_action[:created_at] + post_action + end + def process_topic_allowed_user(topic_allowed_user) topic_allowed_user[:created_at] = NOW topic_allowed_user[:updated_at] = NOW diff --git a/script/bulk_import/vbulletin.rb b/script/bulk_import/vbulletin.rb index 20eec2398fd..d9d394a7168 100644 --- a/script/bulk_import/vbulletin.rb +++ b/script/bulk_import/vbulletin.rb @@ -93,6 +93,8 @@ class BulkImport::VBulletin < BulkImport::Base import_topics import_posts + import_likes + import_private_topics import_topic_allowed_users import_private_posts @@ -383,6 +385,36 @@ class BulkImport::VBulletin < BulkImport::Base end end + def import_likes + return unless @has_post_thanks + puts "Importing likes..." + + @imported_likes = Set.new + @last_imported_post_id = 0 + + post_thanks = mysql_stream <<-SQL + SELECT postid, userid, date + FROM post_thanks + WHERE postid > #{@last_imported_post_id} + ORDER BY postid + SQL + + create_post_actions(post_thanks) do |row| + post_id = post_id_from_imported_id(row[0]) + user_id = user_id_from_imported_id(row[1]) + + next if post_id.nil? || user_id.nil? + next if @imported_likes.add?([post_id, user_id]).nil? + + { + post_id: post_id_from_imported_id(row[0]), + user_id: user_id_from_imported_id(row[1]), + post_action_type_id: 2, + created_at: Time.zone.at(row[2]) + } + end + end + def import_private_topics puts "Importing private topics..."