<commit_before>require 'logger'
require 'pathname'
require_relative '../gemfile_selector'

module SchemaDev
  module Rspec
    module Db
      extend self

      def setup
        @db = GemfileSelector.infer_db
        set_logger
        connect
        RSpec.configure do |config|
          config.include Helpers
          config.filter_run_excluding :postgresql => :only unless Helpers.postgresql?
          config.filter_run_excluding :postgresql => :skip if Helpers.postgresql?
          config.filter_run_excluding :mysql => :only unless Helpers.mysql?
          config.filter_run_excluding :mysql => :skip if Helpers.mysql?
          config.filter_run_excluding :sqlite3 => :only unless Helpers.sqlite3?
          config.filter_run_excluding :sqlite3 => :skip if Helpers.sqlite3?
        end
      end

      def tmproot
        @tmproot ||= Pathname.new('tmp').tap { |path| path.mkpath }
      end

      def configuration
        case @db
        when 'mysql'
          {
            :adapter => 'mysql',
            :database => 'schema_plus_test',
            :username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
            :encoding => 'utf8',
            :min_messages => 'warning'
          }
        when 'mysql2'
          {
            :adapter => 'mysql2',
            :database => 'schema_plus_test',
            :username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
            :encoding => 'utf8',
            :min_messages => 'warning'
          }
        when 'postgresql'
          {
            :adapter => 'postgresql',
            :username => ENV['POSTGRESQL_DB_USER'],
            :database => 'schema_plus_test',
            :min_messages => 'warning'
          }
        when 'sqlite3'
          {
            :adapter => 'sqlite3',
            :database => tmproot.join('schema_plus.sqlite3').to_s
          }
        else
          raise "Unknown db adapter #{@db.inspect}"
        end
      end

      def connect
        ActiveRecord::Base.configurations = { 'schema_dev' => configuration }
        ActiveRecord::Base.establish_connection :schema_dev
        case @db
        when 'sqlite3'
          ActiveRecord::Base.connection.execute "PRAGMA synchronous = OFF"
        end
      end

      def set_logger
        ruby = "#{RUBY_ENGINE}-#{RUBY_VERSION}"
        activerecord = "activerecord-#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
        ActiveRecord::Base.logger = Logger.new(tmproot.join("#{ruby}.#{activerecord}.#{@db}.log").open("w"))
      end

      module Helpers
        extend self

        def mysql?
          ActiveRecord::Base.connection.adapter_name =~ /^mysql/i
        end

        def postgresql?
          ActiveRecord::Base.connection.adapter_name =~ /^postgresql/i
        end

        def sqlite3?<commit_msg>log files go in log/<commit_after>require 'logger'
require 'pathname'
require_relative '../gemfile_selector'

module SchemaDev
  module Rspec
    module Db
      extend self

      def setup
        @db = GemfileSelector.infer_db
        set_logger
        connect
        RSpec.configure do |config|
          config.include Helpers
          config.filter_run_excluding :postgresql => :only unless Helpers.postgresql?
          config.filter_run_excluding :postgresql => :skip if Helpers.postgresql?
          config.filter_run_excluding :mysql => :only unless Helpers.mysql?
          config.filter_run_excluding :mysql => :skip if Helpers.mysql?
          config.filter_run_excluding :sqlite3 => :only unless Helpers.sqlite3?
          config.filter_run_excluding :sqlite3 => :skip if Helpers.sqlite3?
        end
      end

      def tmproot
        @tmproot ||= Pathname.new('tmp').tap { |path| path.mkpath }
      end

      def logroot
        @logroot ||= Pathname.new('log').tap { |path| path.mkpath }
      end

      def configuration
        case @db
        when 'mysql'
          {
            :adapter => 'mysql',
            :database => 'schema_plus_test',
            :username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
            :encoding => 'utf8',
            :min_messages => 'warning'
          }
        when 'mysql2'
          {
            :adapter => 'mysql2',
            :database => 'schema_plus_test',
            :username => ENV.fetch('MYSQL_DB_USER', 'schema_plus'),
            :encoding => 'utf8',
            :min_messages => 'warning'
          }
        when 'postgresql'
          {
            :adapter => 'postgresql',
            :username => ENV['POSTGRESQL_DB_USER'],
            :database => 'schema_plus_test',
            :min_messages => 'warning'
          }
        when 'sqlite3'
          {
            :adapter => 'sqlite3',
            :database => tmproot.join('schema_plus.sqlite3').to_s
          }
        else
          raise "Unknown db adapter #{@db.inspect}"
        end
      end

      def connect
        ActiveRecord::Base.configurations = { 'schema_dev' => configuration }
        ActiveRecord::Base.establish_connection :schema_dev
        case @db
        when 'sqlite3'
          ActiveRecord::Base.connection.execute "PRAGMA synchronous = OFF"
        end
      end

      def set_logger
        ruby = "#{RUBY_ENGINE}-#{RUBY_VERSION}"
        activerecord = "activerecord-#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
        ActiveRecord::Base.logger = Logger.new(logroot.join("#{ruby}.#{activerecord}.#{@db}.log").open("w"))
      end

      module Helpers
        extend self

        def mysql?
          ActiveRecord::Base.connection.adapter_name =~ /^mysql/i
        end

        def postgresql?
          ActiveRecord::Base.connection.adapter_name =~ /^postgresql/i
        end

        def sqlite3?
{'fraction_non_alphanumeric': 0.11321416053277251, 'fraction_numerical': 0.004556607080266386, 'mean_word_length': 1.9786012526096033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '7430227', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1646, 'n_words': 330}