<commit_before>require 'digest/md5'
require 'feed2email/entry'
require 'feed2email/feed'
require 'feed2email/migrate/migration'

module Feed2Email
  module Migrate
    class HistoryImportMigration < Migration
      def apply
        applicable? && migrate
      end

      private

      def applicable?
        table_empty?
      end

      def feed_history_data(feed_uri)
        YAML.load(open(feed_history_path(feed_uri)))
      end

      def feed_history_path(feed_uri)
        root.join("history-#{Digest::MD5.hexdigest(feed_uri)}.yml")
      end

      def migrate
        Feed.each do |feed|
          if feed_history_path(feed.uri).exist?
            feed_history_data(feed.uri).each do |entry_uri|
              Entry.create(feed_id: feed.id, uri: entry_uri)
            end
          end
        end
      end

      def table_empty?
        Entry.empty?
      end
    end
  end
end<commit_msg>Avoid unique constraint exception if entry exists<commit_after>require 'digest/md5'
require 'feed2email/entry'
require 'feed2email/feed'
require 'feed2email/migrate/migration'

module Feed2Email
  module Migrate
    class HistoryImportMigration < Migration
      def apply
        applicable? && migrate
      end

      private

      def applicable?
        table_empty?
      end

      def feed_history_data(feed_uri)
        YAML.load(open(feed_history_path(feed_uri)))
      end

      def feed_history_path(feed_uri)
        root.join("history-#{Digest::MD5.hexdigest(feed_uri)}.yml")
      end

      def migrate
        Feed.each do |feed|
          if feed_history_path(feed.uri).exist?
            feed_history_data(feed.uri).each do |entry_uri|
              Entry.find_or_create(feed_id: feed.id, uri: entry_uri)
            end
          end
        end
      end

      def table_empty?
        Entry.empty?
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07758620689655173, 'fraction_numerical': 0.00646551724137931, 'mean_word_length': 2.1852487135506005, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5319301', 'n_tokens_mistral': 592, 'n_tokens_neox': 565, 'n_words': 121}