From 33c7d520436d4f1d020559ba5a24d7c0447eb52b Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Thu, 19 Dec 2024 22:02:24 +0100 Subject: [PATCH] Don't use original ID as reaction ID during import --- script/bulk_import/base.rb | 21 ++++++++++++++++++++- script/bulk_import/generic_bulk.rb | 13 +++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/script/bulk_import/base.rb b/script/bulk_import/base.rb index 9b8b9e04b41..32faf7c8f59 100644 --- a/script/bulk_import/base.rb +++ b/script/bulk_import/base.rb @@ -120,6 +120,7 @@ class BulkImport::Base chat_channel: 6, chat_thread: 7, chat_message: 8, + discourse_reactions_reaction: 9, ) def create_migration_mappings_table @@ -309,6 +310,10 @@ class BulkImport::Base @chat_message_mapping = load_index(MAPPING_TYPES[:chat_message]) @last_chat_message_id = last_id(Chat::Message) + + puts "Loading reaction indexes..." + @discourse_reaction_mapping = load_index(MAPPING_TYPES[:discourse_reactions_reaction]) + @last_discourse_reaction_id = last_id(DiscourseReactions::Reaction) end def use_bbcode_to_md? @@ -390,6 +395,11 @@ class BulkImport::Base "SELECT setval('#{Chat::Message.sequence_name}', #{@last_chat_message_id})", ) end + if @last_discourse_reaction_id > 0 + @raw_connection.exec( + "SELECT setval('#{DiscourseReactions::Reaction.sequence_name}', #{@last_discourse_reaction_id})", + ) + end end def group_id_from_imported_id(id) @@ -475,6 +485,10 @@ class BulkImport::Base @chat_message_mapping[id.to_s]&.to_i end + def discourse_reaction_id_from_original_id(id) + @discourse_reaction_mapping[id.to_s]&.to_i + end + GROUP_COLUMNS = %i[ id name @@ -1174,7 +1188,7 @@ class BulkImport::Base end def create_reactions(rows, &block) - create_records(rows, "discourse_reactions_reaction", REACTION_COLUMNS, &block) + create_records_with_mapping(rows, "discourse_reactions_reaction", REACTION_COLUMNS, &block) end def process_group(group) @@ -1989,9 +2003,14 @@ class BulkImport::Base end def process_discourse_reactions_reaction(reaction) + reaction[:id] = @last_discourse_reaction_id += 1 reaction[:created_at] ||= NOW reaction[:updated_at] ||= NOW reaction[:reaction_users_count] ||= 0 + + @imported_records[reaction[:original_id].to_s] = reaction[:id] + @discourse_reaction_mapping[reaction[:original_id].to_s] = reaction[:id] + reaction end diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rb index e8795358853..1d636fec980 100644 --- a/script/bulk_import/generic_bulk.rb +++ b/script/bulk_import/generic_bulk.rb @@ -2972,16 +2972,17 @@ class BulkImport::Generic < BulkImport::Base user_id = user_id_from_imported_id(row["user_id"]) post_id = post_id_from_imported_id(row["post_id"]) + reaction_id = discourse_reaction_id_from_original_id(row["reaction_id"]) - next if post_id.blank? || user_id.blank? - next unless existing_reaction_users.add?([row["reaction_id"], user_id, post_id]) + next if post_id.blank? || user_id.blank? || reaction_id.blank? + next unless existing_reaction_users.add?([reaction_id, user_id, post_id]) { - reaction_id: row["reaction_id"], - user_id: user_id, + reaction_id:, + user_id:, created_at: to_datetime(row["created_at"]), updated_at: to_datetime(row["updated_at"]), - post_id: post_id, + post_id:, } end @@ -3014,7 +3015,7 @@ class BulkImport::Generic < BulkImport::Base next unless existing_reactions.add?([post_id, row["reaction_value"]]) { - id: row["id"], + original_id: row["id"], post_id: post_id, reaction_type: reaction_type_id, reaction_value: row["reaction_value"],