From 8d5c90014268fa9e625930740db2a17e2d018784 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 10 Apr 2019 18:09:35 +0530 Subject: [PATCH] DEV: add unique missing uploads index in post custom fields https://review.discourse.org/t/feature-send-missing-post-uploads-stat-to-prometheus/2609/6?u=vinothkannans --- app/models/post.rb | 10 +++++++--- ..._add_missing_uploads_index_to_post_custom_fields.rb | 5 +++++ lib/tasks/posts.rake | 5 ++--- 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20190410122835_add_missing_uploads_index_to_post_custom_fields.rb diff --git a/app/models/post.rb b/app/models/post.rb index f2a93c50237..c7c57d44113 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require_dependency 'pretty_text' require_dependency 'rate_limiter' require_dependency 'post_revisor' @@ -60,12 +61,15 @@ class Post < ActiveRecord::Base # We can pass several creating options to a post via attributes attr_accessor :image_sizes, :quoted_post_numbers, :no_bump, :invalidate_oneboxes, :cooking_options, :skip_unique_check, :skip_validation - LARGE_IMAGES ||= "large_images".freeze - BROKEN_IMAGES ||= "broken_images".freeze - DOWNLOADED_IMAGES ||= "downloaded_images".freeze + LARGE_IMAGES ||= "large_images" + BROKEN_IMAGES ||= "broken_images" + DOWNLOADED_IMAGES ||= "downloaded_images" + MISSING_UPLOADS ||= "missing uploads" SHORT_POST_CHARS ||= 1200 + register_custom_field_type(MISSING_UPLOADS, :json) + scope :private_posts_for_user, ->(user) { where("posts.topic_id IN (SELECT topic_id FROM topic_allowed_users diff --git a/db/migrate/20190410122835_add_missing_uploads_index_to_post_custom_fields.rb b/db/migrate/20190410122835_add_missing_uploads_index_to_post_custom_fields.rb new file mode 100644 index 00000000000..7cca4548ba4 --- /dev/null +++ b/db/migrate/20190410122835_add_missing_uploads_index_to_post_custom_fields.rb @@ -0,0 +1,5 @@ +class AddMissingUploadsIndexToPostCustomFields < ActiveRecord::Migration[5.2] + def change + add_index :post_custom_fields, :post_id, unique: true, where: "name = 'missing uploads'" + end +end diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake index 083afc0750c..d01a409a944 100644 --- a/lib/tasks/posts.rake +++ b/lib/tasks/posts.rake @@ -390,8 +390,7 @@ end desc 'Finds missing post upload records from cooked HTML content' task 'posts:missing_uploads' => :environment do - name = "missing_uploads" - PostCustomField.where(name: name).destroy_all + PostCustomField.where(name: Post::MISSING_UPLOADS).destroy_all posts = Post.have_uploads.select(:id, :cooked) count = 0 @@ -409,7 +408,7 @@ task 'posts:missing_uploads' => :environment do end if missing.present? - missing.each { |src| PostCustomField.create!(post_id: post.id, name: name, value: src) } + PostCustomField.create!(post_id: post.id, name: Post::MISSING_UPLOADS, value: missing.to_json) count += missing.count end