DEV: Add import support for Topic Voting plugin (#29442)
Some checks are pending
Licenses / run (push) Waiting to run
Linting / run (push) Waiting to run
Tests / core frontend (${{ matrix.browser }}) (Chrome) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (annotations, core) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (backend, core) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (backend, plugins) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (frontend, plugins) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (frontend, themes) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, chat) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, core) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, plugins) (push) Waiting to run
Tests / ${{ matrix.target }} ${{ matrix.build_type }} (system, themes) (push) Waiting to run
Tests / core frontend (${{ matrix.browser }}) (Firefox ESR) (push) Waiting to run
Tests / core frontend (${{ matrix.browser }}) (Firefox Evergreen) (push) Waiting to run

DEV: Add import support for Topic Voting plugin
- Implemented bulk import functionality specifically for topic_voting_votes.
- Updated the method name from import_votes to import_post_voting_votes for improved clarity and maintainability.
- Co-authored-by: Gerhard Schlager <gerhard.schlager@discourse.org>
This commit is contained in:
Ruben Oussoren 2024-10-31 08:30:28 -04:00 committed by GitHub
parent 4b533c47f0
commit 4b8b7b2fb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 2 deletions

View File

@ -761,6 +761,8 @@ class BulkImport::Base
POST_VOTING_VOTE_COLUMNS = %i[user_id votable_type votable_id direction created_at] 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[ BADGE_COLUMNS = %i[
id id
name name
@ -1051,6 +1053,10 @@ class BulkImport::Base
create_records(rows, "post_voting_vote", POST_VOTING_VOTE_COLUMNS, &block) create_records(rows, "post_voting_vote", POST_VOTING_VOTE_COLUMNS, &block)
end 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) def create_post_custom_fields(rows, &block)
create_records(rows, "post_custom_field", POST_CUSTOM_FIELD_COLUMNS, &block) create_records(rows, "post_custom_field", POST_CUSTOM_FIELD_COLUMNS, &block)
end end
@ -1558,6 +1564,12 @@ class BulkImport::Base
vote vote
end 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) def process_user_avatar(avatar)
avatar[:id] = @last_user_avatar_id += 1 avatar[:id] = @last_user_avatar_id += 1
avatar[:created_at] ||= NOW avatar[:created_at] ||= NOW

View File

@ -91,7 +91,8 @@ class BulkImport::Generic < BulkImport::Base
import_topic_allowed_groups import_topic_allowed_groups
import_likes import_likes
import_votes import_post_voting_votes
import_topic_voting_votes
import_answers import_answers
import_gamification_scores import_gamification_scores
import_post_events import_post_events
@ -1949,7 +1950,7 @@ class BulkImport::Generic < BulkImport::Base
topic_tags.close topic_tags.close
end end
def import_votes def import_post_voting_votes
puts "", "Importing votes for posts..." puts "", "Importing votes for posts..."
unless defined?(::PostVoting) unless defined?(::PostVoting)
@ -2006,6 +2007,78 @@ class BulkImport::Generic < BulkImport::Base
puts " Update took #{(Time.now - start_time).to_i} seconds." puts " Update took #{(Time.now - start_time).to_i} seconds."
end 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 def import_answers
puts "", "Importing solutions into post custom fields..." puts "", "Importing solutions into post custom fields..."