From 08521f59ca58f21ee81bee3d05a14ddeabc486f9 Mon Sep 17 00:00:00 2001 From: cocococosti Date: Tue, 14 May 2024 17:26:06 -0400 Subject: [PATCH] Adding import of topic custom fields --- script/bulk_import/generic_bulk.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rb index b3d61214b58..5325db3469d 100644 --- a/script/bulk_import/generic_bulk.rb +++ b/script/bulk_import/generic_bulk.rb @@ -80,6 +80,7 @@ class BulkImport::Generic < BulkImport::Base import_topics import_posts + import_topic_custom_fields import_post_custom_fields import_polls @@ -1073,6 +1074,32 @@ class BulkImport::Generic < BulkImport::Base post_custom_fields.close end + def import_topic_custom_fields + puts "", "Importing topic custom fields..." + + topic_custom_fields = query(<<~SQL) + SELECT * + FROM topic_custom_fields + ORDER BY topic_id, name + SQL + + field_names = + query("SELECT DISTINCT name FROM topic_custom_fields") { _1.map { |row| row["name"] } } + existing_topic_custom_fields = + TopicCustomField.where(name: field_names).pluck(:topic_id, :name).to_set + + create_topic_custom_fields(topic_custom_fields) do |row| + topic_id = topic_id_from_imported_id(row["topic_id"]) + next if topic_id.nil? + + next if existing_topic_custom_fields.include?([topic_id, row["name"]]) + + { topic_id: topic_id, name: row["name"], value: row["value"] } + end + + topic_custom_fields.close + end + def import_polls unless defined?(::Poll) puts "", "Skipping polls, because the poll plugin is not installed."