A platform for community discussion. Free, open, simple.
MT: Enable topic voting on flagged categories in generic bulk import
Gerhard Schlager4932fc36966c
diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rbindex bcc7a7e01f0..6864c1c4e2b 100644--- a/script/bulk_import/generic_bulk.rb+++ b/script/bulk_import/generic_bulk.rb@@ -677,6 +677,7 @@ class BulkImport::Generic < BulkImport::Baseimport_engagementimport_post_voting_votesimport_topic_voting_votes+ import_category_topic_votingimport_answersimport_gamification_scoresimport_post_events@@ -4163,6 +4164,30 @@ class BulkImport::Generic < BulkImport::Baseputs " Update took #{(Time.now - start_time).to_i} seconds."end+ # Runs after the votes are imported because creating a CategorySetting unarchives+ # votes of the category's existing open topics.+ def import_category_topic_voting+ unless defined?(DiscourseTopicVoting)+ puts "", "Skipping topic voting categories, because the topic voting plugin is not installed."+ return+ end++ return if table_column_names("categories").exclude?("topic_voting")++ puts "", "Enabling topic voting on categories..."++ rows = query("SELECT id FROM categories WHERE topic_voting = 1 ORDER BY id")++ rows.each do |row|+ category_id = category_id_from_imported_id(row["id"])+ next unless category_id++ DiscourseTopicVoting::CategorySetting.find_or_create_by!(category_id:)+ end++ rows.close+ end+def import_answersunless defined?(DiscourseSolved)puts " Skipping import of solved topics"diff --git a/spec/script/bulk_import/generic_bulk_spec.rb b/spec/script/bulk_import/generic_bulk_spec.rbindex 507ce6eba71..3d1a2c61d4d 100644--- a/spec/script/bulk_import/generic_bulk_spec.rb+++ b/spec/script/bulk_import/generic_bulk_spec.rb@@ -537,6 +537,63 @@ if generic_import_dependencies_availableendend+ describe "#import_category_topic_voting" do+ fab!(:voting_category, :category)+ fab!(:other_category, :category)++ let(:source_db) { SQLite3::Database.new(":memory:", results_as_hash: true) }+ let(:importer) do+ described_class.allocate.tap do |instance|+ instance.instance_variable_set(:@source_db, source_db)+ instance.instance_variable_set(+ :@categories,+ { 1 => voting_category.id, 2 => other_category.id },+ )+ end+ end++ after { source_db.close }++ context "when the topic voting plugin is installed" do+ before do+ skip "requires the topic voting plugin" unless defined?(DiscourseTopicVoting)+ SiteSetting.topic_voting_enabled = true+ end++ it "enables voting only on flagged categories, also when run again" do+ source_db.execute("CREATE TABLE categories (id INTEGER, topic_voting BOOLEAN)")+ source_db.execute("INSERT INTO categories VALUES (1, 1), (2, NULL), (3, 1)")++ importer.import_category_topic_voting+ importer.import_category_topic_voting++ expect(DiscourseTopicVoting::CategorySetting.pluck(:category_id)).to contain_exactly(+ voting_category.id,+ )+ expect(Category.can_vote?(voting_category.id)).to eq(true)+ expect(Category.can_vote?(other_category.id)).to eq(false)+ end++ it "skips intermediate databases without a topic_voting column" do+ source_db.execute("CREATE TABLE categories (id INTEGER)")+ source_db.execute("INSERT INTO categories VALUES (1)")++ expect { importer.import_category_topic_voting }.not_to raise_error+ expect(DiscourseTopicVoting::CategorySetting.count).to eq(0)+ end+ end++ it "skips categories when the plugin is missing" do+ hide_const("DiscourseTopicVoting")+ source_db.execute("CREATE TABLE categories (id INTEGER, topic_voting BOOLEAN)")+ source_db.execute("INSERT INTO categories VALUES (1, 1)")++ expect { importer.import_category_topic_voting }.to output(+ /plugin is not installed/,+ ).to_stdout+ end+ end+describe "mapping selection" dofab!(:canonical_user, :user)fab!(:other_user, :user)