<commit_before>require 'hummingbird/plan_error'

require 'pathname'

class Hummingbird
  class Plan
    attr_reader :migration_dir, :planned_files

    def initialize(planfile, migration_dir)
      @planned_files = parse_plan(planfile)
      @migration_dir = migration_dir
    end

    def migration_files
      @migration_files ||= get_migration_files
    end

    def files_missing_from_plan
      migration_files - planned_files
    end

    def files_missing_from_migration_dir
      planned_files - migration_files
    end

    def migrations_to_be_run(already_run_migrations)
      to_be_run_migration_file_names(already_run_migrations).map do |f|
        {
          migration_name: f,
          sql: get_migration_contents(f)
        }
      end
    end

    def to_be_run_migration_file_names(already_run_migrations)
      return planned_files if already_run_migrations.empty?

      unless (run_migrations_missing_from_plan = already_run_migrations.map {|a| a[:migration_name]} - planned_files).empty?
        raise Hummingbird::PlanError.new("Plan is missing the following already run migrations: #{run_migrations_missing_from_plan.join(', ')}",planned_files,already_run_migrations)
      end

      files = planned_files.dup
      already_run_migrations.each do |f|
        if f[:migration_name] == files.first
          files.shift
        else
          first_out_of_sync_run_on = DateTime.strptime(f[:run_on].to_s, '%s')

          raise Hummingbird::PlanError.new("Plan has '#{files.first}' before '#{f[:migration_name]}' which was run on #{first_out_of_sync_run_on}",planned_files,already_run_migrations)
        end
      end

      files
    end

    def get_migration_contents(migration_file)
      File.read(File.absolute_path(migration_file, @migration_dir))
    end

    private

    def parse_plan(planfile)
      File.read(planfile).split("\n")
    end

    def get_migration_files
      listing = Dir[File.join(migration_dir,'**','*')].select {|f| File.file? f}

      migration_path = Pathname.new(migration_dir)
      listing.map do |f|
        Pathname.new(f).relative_path_from(migration_path).to_s
      end<commit_msg>Use File#expand_path instead of File#absolute_path to determine migration file path<commit_after>require 'hummingbird/plan_error'

require 'pathname'

class Hummingbird
  class Plan
    attr_reader :migration_dir, :planned_files

    def initialize(planfile, migration_dir)
      @planned_files = parse_plan(planfile)
      @migration_dir = migration_dir
    end

    def migration_files
      @migration_files ||= get_migration_files
    end

    def files_missing_from_plan
      migration_files - planned_files
    end

    def files_missing_from_migration_dir
      planned_files - migration_files
    end

    def migrations_to_be_run(already_run_migrations)
      to_be_run_migration_file_names(already_run_migrations).map do |f|
        {
          migration_name: f,
          sql: get_migration_contents(f)
        }
      end
    end

    def to_be_run_migration_file_names(already_run_migrations)
      return planned_files if already_run_migrations.empty?

      unless (run_migrations_missing_from_plan = already_run_migrations.map {|a| a[:migration_name]} - planned_files).empty?
        raise Hummingbird::PlanError.new("Plan is missing the following already run migrations: #{run_migrations_missing_from_plan.join(', ')}",planned_files,already_run_migrations)
      end

      files = planned_files.dup
      already_run_migrations.each do |f|
        if f[:migration_name] == files.first
          files.shift
        else
          first_out_of_sync_run_on = DateTime.strptime(f[:run_on].to_s, '%s')

          raise Hummingbird::PlanError.new("Plan has '#{files.first}' before '#{f[:migration_name]}' which was run on #{first_out_of_sync_run_on}",planned_files,already_run_migrations)
        end
      end

      files
    end

    def get_migration_contents(migration_file)
      File.read(File.expand_path(migration_file, @migration_dir))
    end

    private

    def parse_plan(planfile)
      File.read(planfile).split("\n")
    end

    def get_migration_files
      listing = Dir[File.join(migration_dir,'**','*')].select {|f| File.file? f}

      migration_path = Pathname.new(migration_dir)
      listing.map do |f|
        Pathname.new(f).relative_path_from(migration_path).to_s
      end
{'fraction_non_alphanumeric': 0.08470695970695971, 'fraction_numerical': 0.0, 'mean_word_length': 3.44908350305499, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3913831', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1411, 'n_words': 250}