<commit_before>namespace :db do
  
  ADMIN_NAME = "Primary Admin"
  ADMIN_EMAIL = "<EMAIL>"
  
  def verbosely_drop_database
    db_name = Config.drop_database
    puts "Dropped database: #{db_name}."
  end
  
  def verbosely_create_user(params, phrase="user")
    api_key = params.delete("primary_api_key")
    user = User.create(params)
    user.add_api_key!({
      :api_key  => api_key,
      :key_type => "primary"
    })
    puts "Created #{phrase}:"
    verbosely_display_users([user])
  end
  
  def verbosely_display_users(users)
    users.each do |user|
      puts "  * name  : #{user.name}"
      puts "    id    : #{user.id}"
      puts "    email : #{user.email}"
      user.api_keys.each do |api_key|
        puts "    - API key : #{api_key.api_key} #{api_key.key_type}"
      end
    end
  end
  
  desc "Create the primary admin user if not present"
  task :ensure_admin => ["environment:models"] do
    users = User.find(:all, :conditions => {
      :name  => ADMIN_NAME,
      :admin => true
    })
    if users.length > 0
      users.each { |u| u.update_attributes(:email => ADMIN_EMAIL) }
      puts "Found #{users.length} users in database:"
      verbosely_display_users(users)
    else
      verbosely_create_user({
        :name  => ADMIN_NAME,
        :email => ADMIN_EMAIL,
        :admin => true
      }, "admin user")
    end
  end

  desc "Create default users if not present"
  task :ensure_default_users => ["environment:models"] do
    default_users = Config.environment_config["default_users"]
    if default_users && default_users.length > 0
      default_users.each do |default_user|
        existing_user = User.find(:first, :conditions => {
          :name => default_user["name"]
        })
        if existing_user
          puts "Found user:"
          verbosely_display_users([existing_user])
        else
          verbosely_create_user(default_user)
        end
      end
    else
      puts "No default users specified in config.yml"
    end
  end
  
  desc 'Drop database for current environment (development unless RACK_ENV is set)'
  task :reset do
    verbosely_drop_database
  end

  namespace :reset do
    desc 'Drop databases defined in config/config.yml'<commit_msg>Use .first or .all instead of .find<commit_after>namespace :db do
  
  ADMIN_NAME = "Primary Admin"
  ADMIN_EMAIL = "<EMAIL>"
  
  def verbosely_drop_database
    db_name = Config.drop_database
    puts "Dropped database: #{db_name}."
  end
  
  def verbosely_create_user(params, phrase="user")
    api_key = params.delete("primary_api_key")
    user = User.create(params)
    user.add_api_key!({
      :api_key  => api_key,
      :key_type => "primary"
    })
    puts "Created #{phrase}:"
    verbosely_display_users([user])
  end
  
  def verbosely_display_users(users)
    users.each do |user|
      puts "  * name  : #{user.name}"
      puts "    id    : #{user.id}"
      puts "    email : #{user.email}"
      user.api_keys.each do |api_key|
        puts "    - API key : #{api_key.api_key} #{api_key.key_type}"
      end
    end
  end
  
  desc "Create the primary admin user if not present"
  task :ensure_admin => ["environment:models"] do
    users = User.all(:conditions => {
      :name  => ADMIN_NAME,
      :admin => true
    })
    if users.length > 0
      users.each { |u| u.update_attributes(:email => ADMIN_EMAIL) }
      puts "Found #{users.length} users in database:"
      verbosely_display_users(users)
    else
      verbosely_create_user({
        :name  => ADMIN_NAME,
        :email => ADMIN_EMAIL,
        :admin => true
      }, "admin user")
    end
  end

  desc "Create default users if not present"
  task :ensure_default_users => ["environment:models"] do
    default_users = Config.environment_config["default_users"]
    if default_users && default_users.length > 0
      default_users.each do |default_user|
        existing_user = User.first(:conditions => {
          :name => default_user["name"]
        })
        if existing_user
          puts "Found user:"
          verbosely_display_users([existing_user])
        else
          verbosely_create_user(default_user)
        end
      end
    else
      puts "No default users specified in config.yml"
    end
  end
  
  desc 'Drop database for current environment (development unless RACK_ENV is set)'
  task :reset do
    verbosely_drop_database
  end

  namespace :reset do
    desc 'Drop databases defined in config/config.yml'
{'fraction_non_alphanumeric': 0.10593792172739541, 'fraction_numerical': 0.000899685110211426, 'mean_word_length': 2.6037277147487843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '1299820', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1427, 'n_words': 353}