app/models/resource.rb
# TODO: this should be extracted and generalized so that a resource_id is options (thus allowing ALL contents to be
# fixed). TODO: I think the pluck at the beginning will need to be MANUALLY segmented, as it takes too long
# (285749.5ms on last go).
this should be extracted and generalized so that a resource_id is options (thus allowing ALL contents to be
fixed). TODO: I think the pluck at the beginning will need to be MANUALLY segmented, as it takes too long
(285749.5ms on last go).
this should be extracted and generalized so that a resource_id is options ( thus allowing ALL contents to be fixed ) . TODO : I think the pluck at the beginning will need to be MANUALLY segmented as it takes too long ( 285749 . 5ms on last go ) .
def fix_missing_page_contents_by_type(type, options = {})
    delete = options.key?(:delete) ? options[:delete] : false
    page_counts = {}
    type_table = type.table_name
    first_content_id = type.where(resource_id: id).first&.id
    last_content_id = type.where(resource_id: id).last&.id
    contents = PageContent.where(content_type: type.name, resource_id: id).
                           where(["content_id >= ? AND content_id <= ?", first_content_id, last_content_id])
    contents = contents.where(options[:clause]) if options[:clause]
    if delete
      contents.joins(
        %Q{LEFT JOIN #{type_table} ON (page_contents.content_id = #{type_table}.id)}
      ).where("#{type_table}.id IS NULL")
    else
      PageContent
    end
    contents.pluck(:page_id).each { |pid| page_counts[pid] ||= 0 ; page_counts[pid] += 1 }
    by_count = {}
    page_counts.each { |pid, count| by_count[count] ||= [] ; by_count[count] << pid }
    contents.delete_all if delete
    by_count.each do |count, pages|
      pages.in_groups_of(5_000, false) do |group|
        pages = Page.where(id: group)
        type_field = "#{type.table_name}_count"
        update =
          if delete
            "page_contents_count = IF(page_contents_count > #{count}, (page_contents_count - #{count}),0), "\
              "#{type_field} = IF(#{type_field} > #{count}, (#{type_field} - #{count}),0)"
          else
            "page_contents_count = page_contents_count + #{count}, #{type_field} = #{type_field} + #{count}"
          end
        pages.update_all(update)
      end
    end
  end
def fix_missing_page_contents_by_type ( type , options = { } ) delete = options . key? ( :delete ) ? options [ :delete ] : false page_counts = { } type_table = type . table_name first_content_id = type . where ( resource_id : id ) . first &. id last_content_id = type . where ( resource_id : id ) . last &. id contents = PageContent . where ( content_type : type . name , resource_id : id ) . where ( [ "content_id >= ? AND content_id <= ?" , first_content_id , last_content_id ] ) contents = contents . where ( options [ :clause ] ) if options [ :clause ] if delete contents . joins ( %Q{LEFT JOIN #{type_table} ON (page_contents.content_id = #{type_table}.id)} ) . where ( "#{type_table}.id IS NULL" ) else PageContent end contents . pluck ( :page_id ) . each { | pid | page_counts [ pid ] ||= 0 ; page_counts [ pid ] += 1 } by_count = { } page_counts . each { | pid , count | by_count [ count ] ||= [ ] ; by_count [ count ] << pid } contents . delete_all if delete by_count . each do | count , pages | pages . in_groups_of ( 5_000 , false ) do | group | pages = Page . where ( id : group ) type_field = "#{type.table_name}_count" update = if delete "page_contents_count = IF(page_contents_count > #{count}, (page_contents_count - #{count}),0), " "#{type_field} = IF(#{type_field} > #{count}, (#{type_field} - #{count}),0)" else "page_contents_count = page_contents_count + #{count}, #{type_field} = #{type_field} + #{count}" end pages . update_all ( update ) end end end
TODO: this should be extracted and generalized so that a resource_id is options (thus allowing ALL contents to be
fixed).
TODO : this should be extracted and generalized so that a resource_id is options ( thus allowing ALL contents to be fixed ) .
# .where('page_contents.id > 31617148').pluck(:page_id)
type
<not_specific>
options
<not_specific>