<commit_before>module Tickle
  extend self

  def load_environment
    return if @loaded
    puts "Loading Rails.."
    ENV["RAILS_ENV"] = "test"
    Object.const_set :RAILS_ENV, "test"

    require(File.join(RAILS_ROOT, 'config', 'environment'))
    $: << "#{Rails.root}/test"
    require "#{Rails.root}/test/test_helper"

    @loaded = true
  end

  def run_tests(dir, n = 2)
    dir = "#{Rails.root}/test/#{dir}/**/*_test.rb" unless dir.index(Rails.root)
    groups = Dir[dir].sort.in_groups(n, false)

    pids = []
    n.times do |i|
      pids << Process.fork do
        prepare_databse(:"test_#{i+1}")
        groups[i].each {|f| load(f) unless f =~ /^-/  }
      end
    end

    Signal.trap 'SIGINT', lambda { pids.each {|p| Process.kill("KILL", p)}; exit 1 }

    # Wait...
    Process.waitall
  end
  def prepare_databse(db)
    ActiveRecord::Base.establish_connection(db) unless postgres? 
    conf = ActiveRecord::Base.configurations
    
    adapter = ActiveRecord::Base.connection
    recreate_db(conf[db.to_s]["database"], adapter, conf)
    
    ActiveRecord::Base.establish_connection(conf[db.to_s])
    ActiveRecord::Schema.verbose = false
    file = ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb"
    load(file)
  end
  
  private
  def postgres?
    @postgres ||= ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
  end
  
  def recreate_db(db, adapter, options = {})
    if postgres?
      system("dropdb #{db}")
      adapter.create_database(db)
    else
      adapter.recreate_database(db, options[db.to_s])
    end
  end
end<commit_msg>Make it work with mysql again and some code cleanup<commit_after>module Tickle
  extend self

  def load_environment
    return if @loaded
    puts "Loading Rails.."
    ENV["RAILS_ENV"] = "test"
    Object.const_set :RAILS_ENV, "test"

    require(File.join(RAILS_ROOT, 'config', 'environment'))
    $: << "#{Rails.root}/test"
    require "#{Rails.root}/test/test_helper"

    @loaded = true
  end

  def run_tests(dir, n = 2)
    dir = "#{Rails.root}/test/#{dir}/**/*_test.rb" unless dir.index(Rails.root)
    groups = Dir[dir].sort.in_groups(n, false)

    pids = []
    n.times do |i|
      pids << Process.fork do
        prepare_databse(:"test_#{i+1}")
        groups[i].each {|f| load(f) unless f =~ /^-/  }
      end
    end

    Signal.trap 'SIGINT', lambda { pids.each {|p| Process.kill("KILL", p)}; exit 1 }

    # Wait...
    Process.waitall
  end

  def prepare_databse(db)
    recreate_db(config[db.to_s])

    ActiveRecord::Base.establish_connection(config[db.to_s])
    ActiveRecord::Schema.verbose = false
    file = ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb"
    load(file)
  end

  def recreate_db(db_config)
    db = db_config["database"]

    if postgres?
      system("dropdb #{db}")
      ActiveRecord::Base.connection.create_database(db)
    else
      ActiveRecord::Base.establish_connection(db_config)
      ActiveRecord::Base.connection.recreate_database(db, db_config)
    end
  end

  def postgres?
    defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
  end

  def config
    ActiveRecord::Base.configurations
  end
end
{'fraction_non_alphanumeric': 0.1456221198156682, 'fraction_numerical': 0.0018433179723502304, 'mean_word_length': 3.289855072463768, 'pattern_counts': {'":': 0, '<': 11, '<?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': '3983524', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1133, 'n_words': 213}