<commit_before>class CreateGenericItems < ActiveRecord::Migration
  def self.up  
    subqueries = Array.new
    [:article, :image, :artic_file, :audio, :video, :publication].each do |model|
      table_name = model.to_s.pluralize
      model_name = model.to_s.classify
      subqueries << %{
        SELECT
          '#{model_name}' as item_type,
          id,
          user_id,
          title,
          description,
          created_at,
          updated_at,
          ( SELECT COUNT(*)
           FROM comments
           WHERE
            #{table_name}.id = comments.commentable_id AND
            comments.commentable_type = '#{model_name}'
           ) AS number_of_comments,
          ( SELECT GROUP_CONCAT(workspaces.name)
            FROM items, workspaces
            WHERE
              #{table_name}.id = items.itemable_id AND
              items.itemable_type = '#{model_name}' AND
              workspaces.id = items.workspace_id
          ) AS workspaces,
          ( SELECT AVG(rating)
            FROM ratings
            WHERE
              #{table_name}.id = ratings.rateable_id AND
              ratings.rateable_type = '#{model_name}'
            GROUP BY rateable_id
          ) AS average_rate
        FROM #{table_name} }
    end
    
    execute "CREATE OR REPLACE VIEW generic_items AS #{subqueries.join(' UNION ALL ')}"
  end

  def self.down
    execute('DROP VIEW generic_items;')
  end
end<commit_msg>Migration: smart ouput<commit_after>class CreateGenericItems < ActiveRecord::Migration
  def self.up  
    subqueries = Array.new
    [:article, :image, :artic_file, :audio, :video, :publication].each do |model|
      table_name = model.to_s.pluralize
      model_name = model.to_s.classify
      subqueries << %{
        SELECT
          '#{model_name}' as item_type,
          id,
          user_id,
          title,
          description,
          created_at,
          updated_at,
          ( SELECT COUNT(*)
           FROM comments
           WHERE
            #{table_name}.id = comments.commentable_id AND
            comments.commentable_type = '#{model_name}'
           ) AS number_of_comments,
          ( SELECT GROUP_CONCAT(workspaces.name)
            FROM items, workspaces
            WHERE
              #{table_name}.id = items.itemable_id AND
              items.itemable_type = '#{model_name}' AND
              workspaces.id = items.workspace_id
          ) AS workspaces,
          ( SELECT AVG(rating)
            FROM ratings
            WHERE
              #{table_name}.id = ratings.rateable_id AND
              ratings.rateable_type = '#{model_name}'
            GROUP BY rateable_id
          ) AS average_rate
        FROM #{table_name} }
    end
    
    execute "CREATE OR REPLACE VIEW generic_items AS #{subqueries.join(' UNION ALL ')}".tr_s(" \n", ' ')
  end

  def self.down
    execute('DROP VIEW generic_items;')
  end
end
{'fraction_non_alphanumeric': 0.09046961325966851, 'fraction_numerical': 0.0, 'mean_word_length': 1.807170542635659, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6264755', 'n_tokens_mistral': 860, 'n_tokens_neox': 837, 'n_words': 191}