<commit_before>require 'yaml'
require 'active_record'

namespace :disco do
  namespace :db do
    def disco_database_configuration(disco_config, database)
      disco_config.reduce({}) do |map, (key, value)|
        if value.is_a?(Hash) && value.has_key?(database)
          map[key] = value[database]
        end
        map
      end
    end

    task :load_config => :environment do
      migrate_path = 'db/migrate'
      ActiveRecord::Tasks::DatabaseTasks.db_dir = Rails.application.config.paths['db'].first
      ActiveRecord::Tasks::DatabaseTasks.seed_loader = Rails.application
      ActiveRecord::Base.clear_all_connections!

      if ENV['SYSTEM'] == 'domain'
        migrate_path = 'db/migrate_domain'
        ActiveRecord::Tasks::DatabaseTasks.env = ENV['DOMAIN_ENV'] || Rails.env
        disco_config = YAML.load_file(File.join Rails.root, 'config', 'disco.yml')
        ActiveRecord::Tasks::DatabaseTasks.database_configuration = disco_database_configuration(disco_config, 'domain_database')
        ActiveRecord::Tasks::DatabaseTasks.migrations_paths =
            Array(ENV['DOMAIN_MIGRATIONS_DIR'] || Rails.application.paths[migrate_path] || File.join(Rails.root, migrate_path))
        ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures_domain'
        ENV['SCHEMA'] = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema_domain.rb')
        ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'structure_domain.sql')

      else # 'projection' - normal rails
        ActiveRecord::Tasks::DatabaseTasks.env = ENV['PROJECTION_ENV'] || Rails.env
        ActiveRecord::Tasks::DatabaseTasks.database_configuration = Rails.application.config.database_configuration
        ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Array(Rails.application.paths[migrate_path])
        ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures'
        ENV['SCHEMA'] = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
        ENV['DB_STRUCTURE'] = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'structure.sql')
      end

      if defined?(ENGINE_PATH) && (engine = Rails::Engine.find(ENGINE_PATH))
        if engine.paths[migrate_path].existent
          ActiveRecord::Tasks::DatabaseTasks.migrations_paths += Array(engine.paths[migrate_path])
        elsif Dir.exists?(File.join engine.root, migrate_path)
          ActiveRecord::Tasks::DatabaseTasks.migrations_paths << File.join(engine.root, migrate_path)
        end
      end
      Rails.env = ActiveRecord::Tasks::DatabaseTasks.env

      #puts "env = #{ActiveRecord::Tasks::DatabaseTasks.env}"
      puts "system = #{ENV['SYSTEM']}"
      #puts "config = #{ActiveRecord::Tasks::DatabaseTasks.database_configuration[ActiveRecord::Tasks::DatabaseTasks.env]}"
    end

    def connect
      ActiveRecord::Base.establish_connection ActiveRecord::Tasks::DatabaseTasks.env
    end

    namespace :migrate do<commit_msg>added Rails 4.0.3 support<commit_after>require 'yaml'
require 'active_record'

namespace :disco do
  namespace :db do
    def disco_database_configuration(disco_config, database)
      disco_config.reduce({}) do |map, (key, value)|
        if value.is_a?(Hash) && value.has_key?(database)
          map[key] = value[database]
        end
        map
      end
    end

    task :load_config => :environment do
      migrate_path = 'db/migrate'
      ActiveRecord::Tasks::DatabaseTasks.db_dir = Rails.application.config.paths['db'].first
      ActiveRecord::Tasks::DatabaseTasks.seed_loader = Rails.application
      ActiveRecord::Base.clear_all_connections!

      if ENV['SYSTEM'] == 'domain'
        migrate_path = 'db/migrate_domain'
        ActiveRecord::Tasks::DatabaseTasks.env = ENV['DOMAIN_ENV'] || Rails.env
        disco_config = YAML.load_file(File.join Rails.root, 'config', 'disco.yml')
        ActiveRecord::Tasks::DatabaseTasks.database_configuration = disco_database_configuration(disco_config, 'domain_database')
        ActiveRecord::Tasks::DatabaseTasks.migrations_paths =
            Array(ENV['DOMAIN_MIGRATIONS_DIR'] || Rails.application.paths[migrate_path] || File.join(Rails.root, migrate_path))
        ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures_domain'
        ENV['SCHEMA'] = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema_domain.rb')
        ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'structure_domain.sql')

      else # 'projection' - normal rails
        ActiveRecord::Tasks::DatabaseTasks.env = ENV['PROJECTION_ENV'] || Rails.env
        ActiveRecord::Tasks::DatabaseTasks.database_configuration = Rails.application.config.database_configuration
        ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Array(Rails.application.paths[migrate_path])
        ActiveRecord::Tasks::DatabaseTasks.fixtures_path = File.join Rails.root, 'test', 'fixtures'
        ENV['SCHEMA'] = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
        ENV['DB_STRUCTURE'] = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'structure.sql')
      end

      if defined?(ENGINE_PATH) && (engine = Rails::Engine.find(ENGINE_PATH))
        if engine.paths[migrate_path].existent
          ActiveRecord::Tasks::DatabaseTasks.migrations_paths += Array(engine.paths[migrate_path])
        elsif Dir.exists?(File.join engine.root, migrate_path)
          ActiveRecord::Tasks::DatabaseTasks.migrations_paths << File.join(engine.root, migrate_path)
        end
      end
      Rails.env = ActiveRecord::Tasks::DatabaseTasks.env
      ActiveRecord::Tasks::DatabaseTasks.root = Rails.root if ActiveRecord::Tasks::DatabaseTasks.respond_to? :root

      #puts "env = #{ActiveRecord::Tasks::DatabaseTasks.env}"
      puts "system = #{ENV['SYSTEM']}"
      #puts "config = #{ActiveRecord::Tasks::DatabaseTasks.database_configuration[ActiveRecord::Tasks::DatabaseTasks.env]}"
    end

    def connect
      ActiveRecord::Base.establish_connection ActiveRecord::Tasks::DatabaseTasks.env
    end

    namespace :migrate do
{'fraction_non_alphanumeric': 0.12799218368343918, 'fraction_numerical': 0.0004885197850512946, 'mean_word_length': 4.281169389509889, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3921228', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1811, 'n_words': 267}