Improve base import script

* Make sure the category description is imported correctly
  (the about topic usually had the wrong excerpt).
* Allow import scripts to mark topics as closed or archived.
* Allow import scripts to store the topic's original id.
  It will be stored in topic_custom_fields as import_topic_id.
This commit is contained in:
Gerhard Schlager 2018-03-16 22:31:33 +01:00
parent 6c736a1fa4
commit 2834705bd1
1 changed files with 43 additions and 1 deletions

View File

@ -48,6 +48,7 @@ class ImportScripts::Base
puts "" puts ""
unless @skip_updates unless @skip_updates
update_topic_status
update_bumped_at update_bumped_at
update_last_posted_at update_last_posted_at
update_last_seen_at update_last_seen_at
@ -436,7 +437,6 @@ class ImportScripts::Base
name: opts[:name], name: opts[:name],
user_id: opts[:user_id] || opts[:user].try(:id) || Discourse::SYSTEM_USER_ID, user_id: opts[:user_id] || opts[:user].try(:id) || Discourse::SYSTEM_USER_ID,
position: opts[:position], position: opts[:position],
description: opts[:description],
parent_category_id: opts[:parent_category_id], parent_category_id: opts[:parent_category_id],
color: opts[:color] || "AB9364", color: opts[:color] || "AB9364",
text_color: opts[:text_color] || "FFF", text_color: opts[:text_color] || "FFF",
@ -446,6 +446,12 @@ class ImportScripts::Base
new_category.custom_fields["import_id"] = import_id if import_id new_category.custom_fields["import_id"] = import_id if import_id
new_category.save! new_category.save!
if opts[:description].present?
changes = { raw: opts[:description] }
opts = { skip_revision: true, skip_validations: true, bypass_bump: true }
new_category.topic.first_post.revise(Discourse.system_user, changes, opts)
end
add_category(import_id, new_category) add_category(import_id, new_category)
post_create_action.try(:call, new_category) post_create_action.try(:call, new_category)
@ -522,6 +528,13 @@ class ImportScripts::Base
opts[:custom_fields] ||= {} opts[:custom_fields] ||= {}
opts[:custom_fields]['import_id'] = import_id opts[:custom_fields]['import_id'] = import_id
unless opts[:topic_id]
opts[:meta_data] = meta_data = {}
meta_data["import_closed"] = true if opts[:closed]
meta_data["import_archived"] = true if opts[:archived]
meta_data["import_topic_id"] = opts[:import_topic_id] if opts[:import_topic_id]
end
opts[:guardian] = STAFF_GUARDIAN opts[:guardian] = STAFF_GUARDIAN
if @bbcode_to_md if @bbcode_to_md
opts[:raw] = opts[:raw].bbcode_to_md(false) rescue opts[:raw] opts[:raw] = opts[:raw].bbcode_to_md(false) rescue opts[:raw]
@ -594,6 +607,35 @@ class ImportScripts::Base
end end
end end
def update_topic_status
puts "", "updating topic status"
Topic.exec_sql(<<~SQL)
UPDATE topics AS t
SET closed = TRUE
WHERE EXISTS(
SELECT 1
FROM topic_custom_fields AS f
WHERE f.topic_id = t.id AND f.name = 'import_closed' AND f.value = 't'
)
SQL
Topic.exec_sql(<<~SQL)
UPDATE topics AS t
SET archived = TRUE
WHERE EXISTS(
SELECT 1
FROM topic_custom_fields AS f
WHERE f.topic_id = t.id AND f.name = 'import_archived' AND f.value = 't'
)
SQL
TopicCustomField.exec_sql(<<~SQL)
DELETE FROM topic_custom_fields
WHERE name IN ('import_closed', 'import_archived')
SQL
end
def update_bumped_at def update_bumped_at
puts "", "updating bumped_at on topics" puts "", "updating bumped_at on topics"
Post.exec_sql("update topics t set bumped_at = COALESCE((select max(created_at) from posts where topic_id = t.id and post_type = #{Post.types[:regular]}), bumped_at)") Post.exec_sql("update topics t set bumped_at = COALESCE((select max(created_at) from posts where topic_id = t.id and post_type = #{Post.types[:regular]}), bumped_at)")