<commit_before>$: << File.expand_path('..', __FILE__)

def travis?
  !!ENV['TRAVIS']
end

if travis?
  require 'simplecov'
  require 'coveralls'

  SimpleCov.formatter = Coveralls::SimpleCov::Formatter
  SimpleCov.start do
    add_filter "spec/"
  end
end

require 'ridgepole'
require 'ridgepole/cli/config'
require 'active_support/core_ext/string/strip'
require 'string_ext'
require 'open3'
require 'tempfile'
require 'json'

TEST_SCHEMA = 'ridgepole_test'

if ENV['DEBUG']
  ActiveRecord::Migration.verbose = true
  logger = Ridgepole::Logger.instance
  logger.level = ::Logger::DEBUG
  ActiveRecord::Base.logger = logger
else
  ActiveRecord::Migration.verbose = false
  Ridgepole::Logger.instance.level = ::Logger::ERROR
end

RSpec.configure do |config|
  config.before(:each) do
    restore_database
  end
end

def restore_database
  if postgresql?
    restore_database_postgresql
  else
    restore_database_mysql
  end
end

def restore_database_mysql
  sql_file = File.expand_path('../mysql/ridgepole_test_database.sql', __FILE__)
  system("mysql -uroot < #{sql_file}")
end

def restore_database_postgresql
  sql_file = File.expand_path('../postgresql/ridgepole_test_database.sql', __FILE__)
  system("psql #{travis? ? '-U postgres' : ''} --set ON_ERROR_STOP=off -q -f #{sql_file} 2>/dev/null")
end

def restore_tables
  if postgresql?
    restore_tables_postgresql
  else
    restore_tables_mysql
  end
end

def restore_tables_mysql
  sql_file = File.expand_path('../mysql/ridgepole_test_tables.sql', __FILE__)
  system("mysql -uroot < #{sql_file}")
end

def restore_tables_postgresql
  sql_file = File.expand_path('../postgresql/ridgepole_test_tables.sql', __FILE__)
  system("psql #{travis? ? '-U postgres' : ''} -q -f #{sql_file} 2>/dev/null")
end

def client(options = {}, config = {})
  config = conn_spec(config)

  default_options = {
    :debug => !!ENV['DEBUG'],
  }

  if mysql_awesome_enabled?
    default_options[:enable_mysql_awesome] = true
    default_options[:dump_without_table_options] = true
    default_options[:mysql_awesome_unsigned_pk] = true
  end
<commit_msg>Give dbname explicitly to psql<commit_after>$: << File.expand_path('..', __FILE__)

def travis?
  !!ENV['TRAVIS']
end

if travis?
  require 'simplecov'
  require 'coveralls'

  SimpleCov.formatter = Coveralls::SimpleCov::Formatter
  SimpleCov.start do
    add_filter "spec/"
  end
end

require 'ridgepole'
require 'ridgepole/cli/config'
require 'active_support/core_ext/string/strip'
require 'string_ext'
require 'open3'
require 'tempfile'
require 'json'

TEST_SCHEMA = 'ridgepole_test'

if ENV['DEBUG']
  ActiveRecord::Migration.verbose = true
  logger = Ridgepole::Logger.instance
  logger.level = ::Logger::DEBUG
  ActiveRecord::Base.logger = logger
else
  ActiveRecord::Migration.verbose = false
  Ridgepole::Logger.instance.level = ::Logger::ERROR
end

RSpec.configure do |config|
  config.before(:each) do
    restore_database
  end
end

def restore_database
  if postgresql?
    restore_database_postgresql
  else
    restore_database_mysql
  end
end

def system_raise_on_fail(*args)
  unless system(*args)
    raise RuntimeError.new("Failed to run: #{args}")
  end
end

def restore_database_mysql
  sql_file = File.expand_path('../mysql/ridgepole_test_database.sql', __FILE__)
  system_raise_on_fail("mysql -uroot < #{sql_file}")
end

def restore_database_postgresql
  sql_file = File.expand_path('../postgresql/ridgepole_test_database.sql', __FILE__)
  system_raise_on_fail("psql ridgepole_test #{travis? ? '-U postgres' : ''} --set ON_ERROR_STOP=off -q -f #{sql_file} 2>/dev/null")
end

def restore_tables
  if postgresql?
    restore_tables_postgresql
  else
    restore_tables_mysql
  end
end

def restore_tables_mysql
  sql_file = File.expand_path('../mysql/ridgepole_test_tables.sql', __FILE__)
  system_raise_on_fail("mysql -uroot < #{sql_file}")
end

def restore_tables_postgresql
  sql_file = File.expand_path('../postgresql/ridgepole_test_tables.sql', __FILE__)
  system_raise_on_fail("psql ridgepole_test #{travis? ? '-U postgres' : ''} -q -f #{sql_file} 2>/dev/null")
end

def client(options = {}, config = {})
  config = conn_spec(config)

  default_options = {
    :debug => !!ENV['DEBUG'],
  }

  if mysql_awesome_enabled?
    default_options[:enable_mysql_awesome] = true
    default_options[:dump_without_table_options] = true
    default_options[:mysql_awesome_unsigned_pk] = true
  end

{'fraction_non_alphanumeric': 0.11966394187102634, 'fraction_numerical': 0.0013623978201634877, 'mean_word_length': 4.936657681940701, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '5789467', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1513, 'n_words': 305}