<commit_before>module Alchemy
  class Upgrader < Alchemy::Seeder
    class << self

      # Runs ugrades
      #
      # Set UPGRADE env variable to only run a specific task.
      def run!
        if ENV['UPGRADE']
          ENV['UPGRADE'].split(',').each do |task|
            self.send(task)
          end
        else
          run_all
        end
        display_todos
      end

      def run_all
        Rake::Task['alchemy:install:migrations'].invoke
        Rake::Task['db:migrate'].invoke
        Seeder.seed!
        copy_new_config_file
      end

    private

      def copy_new_config_file
        desc "Copy configuration file."
        config_file = Rails.root.join('config/alchemy/config.yml')
        default_config = File.join(File.dirname(__FILE__), '../../config/alchemy/config.yml')
        if FileUtils.identical? default_config, config_file
          log "Configuration file already present.", :skip
        else
          log "Custom configuration file found."
          FileUtils.cp default_config, Rails.root.join('config/alchemy/config.yml.defaults')
          log "Copied new default configuration file."
          todo "Check the default configuration file (./config/alchemy/config.yml.defaults) for new configuration options and insert them into your config file."
        end
      end<commit_msg>Adds upgrader for new dragonfly based attachments storage.<commit_after>module Alchemy
  class Upgrader < Alchemy::Seeder
    class << self

      # Runs ugrades
      #
      # Set UPGRADE env variable to only run a specific task.
      def run!
        if ENV['UPGRADE']
          ENV['UPGRADE'].split(',').each do |task|
            self.send(task)
          end
        else
          run_all
        end
        display_todos
      end

      def run_all
        Rake::Task['alchemy:install:migrations'].invoke
        Rake::Task['db:migrate'].invoke
        Seeder.seed!
        convert_attachment_storage
        copy_new_config_file
      end

    private

      def convert_attachment_storage
        desc "Convert the attachment storage"
        converted_files = []
        files = Dir.glob Rails.root.join 'uploads/attachments/**/*.*'
        if files.blank?
          log "No attachments found", :skip
        else
          files.each do |file|
            file_uid = file.gsub(/#{Rails.root.to_s}\/uploads\/attachments\//, '')
            file_id = file_uid.split('/')[1].to_i
            attachment = Alchemy::Attachment.find_by_id(file_id)
            if attachment && attachment.file_uid.blank?
              attachment.file_uid = file_uid
              attachment.file_size = File.new(file).size
              if attachment.save!
                log "Converted #{file_uid}"
              end
            else
              log "Attachment with id #{file_id} not found or already converted.", :skip
            end
          end
        end
      end

      def copy_new_config_file
        desc "Copy configuration file."
        config_file = Rails.root.join('config/alchemy/config.yml')
        default_config = File.join(File.dirname(__FILE__), '../../config/alchemy/config.yml')
        if FileUtils.identical? default_config, config_file
          log "Configuration file already present.", :skip
        else
          log "Custom configuration file found."
          FileUtils.cp default_config, Rails.root.join('config/alchemy/config.yml.defaults')
          log "Copied new default configuration file."
          todo "Check the default configuration file (./config/alchemy/config.yml.defaults) for new configuration options and insert them into your config file."
        end
      end
{'fraction_non_alphanumeric': 0.09195717814987647, 'fraction_numerical': 0.0002744990392533626, 'mean_word_length': 2.0341382181515404, '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': '4084933', 'n_tokens_mistral': 1022, 'n_tokens_neox': 982, 'n_words': 259}