Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix initial revision for policy posts & improve seed error reporting #1201

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
processed = ERB.new(File.read(f)).result(binding)
data = YAML.load(processed)
created = 0
errored = 0
skipped = 0
updated = 0
data.each do |seed|
Expand All @@ -39,20 +40,43 @@
puts "Running full Posts update..."

seed['body'] = ApplicationController.helpers.render_markdown(seed['body_markdown'])

system_usr = User.find(-1)

Community.all.each do |c|
RequestContext.community = c
post = Post.find_by doc_slug: seed['doc_slug']
if post.present? && PostHistory.where(post: post)
.where.not(post_history_type:
PostHistoryType.find_by(name: 'initial_revision'))
.count.zero?

# post exists, still original version: update post
post.update(seed.merge('community_id' => c.id))

no_initial = PostHistory.where(post: post)
.where(post_history_type: PostHistoryType.find_by(name: 'initial_revision'))
.count.zero?

if no_initial
puts "[#{c.name}:#{seed['doc_slug']}] missing initial revision, creating..."
PostHistory.initial_revision(post, system_usr)
end

updated += 1
elsif post.nil?
# post doesn't exist: create post
Post.create seed.merge('community_id' => c.id)
created += 1
status = Post.create seed.merge('community_id' => c.id, 'user' => system_usr)

if status.errors.size
status.errors.full_messages.each do |msg|
puts "[#{c.name}:#{seed['doc_slug']}] invalid: #{msg}"
end

errored += 1
else
created += 1
end
else
# post exists, versions diverged: skip
skipped += 1
Expand Down Expand Up @@ -92,7 +116,7 @@
end
end
unless Rails.env.test?
puts "#{type}: Created #{created}, #{updated > 0 ? "updated #{updated}, " : ''}skipped #{skipped}"
puts "#{type}: Errored #{errored}, Created #{created}, #{updated > 0 ? "updated #{updated}, " : ''}skipped #{skipped}"
end
rescue StandardError => e
puts "Got error #{e}. Continuing..."
Expand Down