<commit_before>if defined? JRUBY_VERSION
  require 'activerecord-jdbcpostgresql-adapter'
  error_classes = [ActiveRecord::JDBCError]
else
  require "pg"
  error_classes = [PGError]
end

error_classes << ActiveRecord::NoDatabaseError if defined? ActiveRecord::NoDatabaseError

begin
  database_user = if ENV["TRAVIS"]
                    "postgres"
                  else
                    ENV["USER"]
                  end

  ActiveRecord::Base.establish_connection(:adapter  => 'postgresql',
                                          :database => 'pg_search_test',
                                          :username => database_user,
                                          :min_messages => 'warning')
  connection = ActiveRecord::Base.connection
  postgresql_version = connection.send(:postgresql_version)
  connection.execute("SELECT 1")
rescue *error_classes
  at_exit do
    puts "-" * 80
    puts "Unable to connect to database.  Please run:"
    puts
    puts "    createdb pg_search_test"
    puts "-" * 80
  end
  raise $ERROR_INFO
end

if ENV["LOGGER"]
  require "logger"
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

def install_extension_if_missing(name, query, expected_result) # rubocop:disable Metrics/AbcSize
  connection = ActiveRecord::Base.connection
  postgresql_version = connection.send(:postgresql_version)
  result = connection.select_value(query)
  raise "Unexpected output for #{query}: #{result.inspect}" unless result.downcase == expected_result.downcase
rescue
  begin
    if postgresql_version >= 90100
      extension = connection.execute "SELECT * FROM pg_catalog.pg_extension WHERE extname = '#{name}';"
      return unless extension.none?
      connection.execute "CREATE EXTENSION #{name};"
    else
      share_path = `pg_config --sharedir`.strip
      ActiveRecord::Base.connection.execute File.read(File.join(share_path, 'contrib', "#{name}.sql"))
      puts $ERROR_INFO.message
    end
  rescue => exception
    at_exit do
      puts "-" * 80
      puts "Please install the #{name} contrib module"
      puts "-" * 80
    end
    raise exception
  end
end

install_extension_if_missing("pg_trgm", "SELECT 'abcdef' % 'cdef'", "t")
unless postgresql_version < 90000
  install_extension_if_missing("unaccent", "SELECT unaccent('foo')", "foo")
end
install_extension_if_missing("fuzzystrmatch", "SELECT dmetaphone('foo')", "f")

def load_sql(filename)<commit_msg>Update logic for checking installed extensions<commit_after>if defined? JRUBY_VERSION
  require 'activerecord-jdbcpostgresql-adapter'
  error_classes = [ActiveRecord::JDBCError]
else
  require "pg"
  error_classes = [PGError]
end

error_classes << ActiveRecord::NoDatabaseError if defined? ActiveRecord::NoDatabaseError

begin
  database_user = if ENV["TRAVIS"]
                    "postgres"
                  else
                    ENV["USER"]
                  end

  ActiveRecord::Base.establish_connection(:adapter  => 'postgresql',
                                          :database => 'pg_search_test',
                                          :username => database_user,
                                          :min_messages => 'warning')
  connection = ActiveRecord::Base.connection
  postgresql_version = connection.send(:postgresql_version)
  connection.execute("SELECT 1")
rescue *error_classes
  at_exit do
    puts "-" * 80
    puts "Unable to connect to database.  Please run:"
    puts
    puts "    createdb pg_search_test"
    puts "-" * 80
  end
  raise $ERROR_INFO
end

if ENV["LOGGER"]
  require "logger"
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

def install_extension(name) # rubocop:disable Metrics/AbcSize
  connection = ActiveRecord::Base.connection
  postgresql_version = connection.send(:postgresql_version)
  if postgresql_version >= 90100
    extension = connection.execute "SELECT * FROM pg_catalog.pg_extension WHERE extname = '#{name}';"
    return unless extension.none?

    connection.execute "CREATE EXTENSION #{name};"
  else
    share_path = `pg_config --sharedir`.strip
    ActiveRecord::Base.connection.execute File.read(File.join(share_path, 'contrib', "#{name}.sql"))
    puts $ERROR_INFO.message
  end
rescue => exception
  at_exit do
    puts "-" * 80
    puts "Please install the #{name} contrib module"
    puts "-" * 80
  end
  raise exception
end

def install_extension_if_missing(name, query, expected_result)
  result = ActiveRecord::Base.connection.select_value(query)
  raise "Unexpected output for #{query}: #{result.inspect}" unless result.downcase == expected_result.downcase
rescue
  install_extension(name)
end

install_extension_if_missing("pg_trgm", "SELECT 'abcdef' % 'cdef'", "t")
unless postgresql_version < 90000
  install_extension_if_missing("unaccent", "SELECT unaccent('foo')", "foo")
end
install_extension_if_missing("fuzzystrmatch", "SELECT dmetaphone('foo')", "f")

def load_sql(filename)
{'fraction_non_alphanumeric': 0.10458452722063037, 'fraction_numerical': 0.007777322963569382, 'mean_word_length': 3.2755905511811023, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85536', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1421, 'n_words': 330}