Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add import support for Topic Voting plugin (#29442)

Ruben Oussoren4b8b7b2fb589
diff --git a/script/bulk_import/base.rb b/script/bulk_import/base.rb
index 93ddecf3a1c..d4b894053c2 100644
--- a/script/bulk_import/base.rb
+++ b/script/bulk_import/base.rb
@@ -761,6 +761,8 @@ class BulkImport::Base
POST_VOTING_VOTE_COLUMNS = %i[user_id votable_type votable_id direction created_at]
+ TOPIC_VOTING_COLUMNS = %i[topic_id user_id archive created_at updated_at]
+
BADGE_COLUMNS = %i[
id
name
@@ -1051,6 +1053,10 @@ class BulkImport::Base
create_records(rows, "post_voting_vote", POST_VOTING_VOTE_COLUMNS, &block)
end
+ def create_topic_voting_votes(rows, &block)
+ create_records(rows, "topic_voting_vote", TOPIC_VOTING_COLUMNS, &block)
+ end
+
def create_post_custom_fields(rows, &block)
create_records(rows, "post_custom_field", POST_CUSTOM_FIELD_COLUMNS, &block)
end
@@ -1558,6 +1564,12 @@ class BulkImport::Base
vote
end
+ def process_topic_voting_vote(topic_vote)
+ topic_vote[:created_at] ||= NOW
+ topic_vote[:updated_at] ||= NOW
+ topic_vote
+ end
+
def process_user_avatar(avatar)
avatar[:id] = @last_user_avatar_id += 1
avatar[:created_at] ||= NOW
diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rb
index 67bdcb71915..69dc3fdbd9c 100644
--- a/script/bulk_import/generic_bulk.rb
+++ b/script/bulk_import/generic_bulk.rb
@@ -91,7 +91,8 @@ class BulkImport::Generic < BulkImport::Base
import_topic_allowed_groups
import_likes
- import_votes
+ import_post_voting_votes
+ import_topic_voting_votes
import_answers
import_gamification_scores
import_post_events
@@ -1949,7 +1950,7 @@ class BulkImport::Generic < BulkImport::Base
topic_tags.close
end
- def import_votes
+ def import_post_voting_votes
puts "", "Importing votes for posts..."
unless defined?(::PostVoting)
@@ -2006,6 +2007,78 @@ class BulkImport::Generic < BulkImport::Base
puts " Update took #{(Time.now - start_time).to_i} seconds."
end
+ def import_topic_voting_votes
+ unless defined?(::DiscourseTopicVoting)
+ puts "", "Skipping topic voting votes, because the topic voting plugin is not installed."
+ return
+ end
+
+ puts "", "Importing votes for topics..."
+
+ topic_votes = query(<<~SQL)
+ SELECT *
+ FROM topic_voting_votes
+ SQL
+
+ existing_topic_votes = DiscourseTopicVoting::Vote.pluck(:topic_id, :user_id).to_set
+
+ create_topic_voting_votes(topic_votes) do |row|
+ topic_id = topic_id_from_imported_id(row["topic_id"])
+ user_id = user_id_from_imported_id(row["user_id"])
+
+ next unless topic_id && user_id
+ next unless existing_topic_votes.add?([topic_id, user_id])
+
+ {
+ topic_id: topic_id,
+ user_id: user_id,
+ archive: to_boolean(row["archive"]),
+ created_at: to_datetime(row["created_at"]),
+ updated_at: to_datetime(row["updated_at"]),
+ }
+ end
+
+ topic_votes.close
+
+ puts "", "Updating vote counts of topics..."
+
+ start_time = Time.now
+
+ DB.exec(<<~SQL)
+ WITH
+ votes AS (
+ SELECT topic_id, COUNT(*) AS votes_count
+ FROM topic_voting_votes
+ GROUP BY topic_id
+ )
+ UPDATE topic_voting_topic_vote_count
+ SET votes_count = votes.votes_count
+ FROM votes
+ WHERE votes.topic_id = topic_voting_topic_vote_count.topic_id
+ AND votes.votes_count <> topic_voting_topic_vote_count.votes_count
+ SQL
+
+ DB.exec(<<~SQL)
+ WITH
+ missing_votes AS (
+ SELECT v.topic_id, COUNT(*) AS votes_count
+ FROM topic_voting_votes v
+ WHERE NOT EXISTS (
+ SELECT 1
+ FROM topic_voting_topic_vote_count c
+ WHERE v.topic_id = c.topic_id
+ )
+ GROUP BY topic_id
+ )
+ INSERT
+ INTO topic_voting_topic_vote_count (votes_count, topic_id, created_at, updated_at)
+ SELECT votes_count, topic_id, NOW(), NOW()
+ FROM missing_votes
+ SQL
+
+ puts " Update took #{(Time.now - start_time).to_i} seconds."
+ end
+
def import_answers
puts "", "Importing solutions into post custom fields..."