Sha256: 9e97173258a0632c04654ecd52c3650e52ad272b6aa7df25c6b73c3cde21bd08

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

#!/usr/bin/env ruby
require "bundler/setup"
require "yaml"
require 'highline/import'

def read_database_config
  database_file = File.join(File.dirname(__FILE__), "/../", "database.yml")
  $DB_CONFIG = YAML.load(File.read(database_file))["mysql"]
end

def get_root_password
  $ROOT_PW = ask("Provide root mySQL password to setup '#{$DB_CONFIG["database"]}': ") { |q| q.echo = false}
  if !$ROOT_PW || $ROOT_PW.nil?
    puts "error reading password"
    exit 4
  end
end

def check_root_password
  password_check = "echo 'show databases;' | mysql -u root -p'#{$ROOT_PW}' > /dev/null"
  unless system password_check
    puts "incorrect password"
    exit 7
  end
end

def execute_mysql(message, statement, user = nil, password = nil)
  puts message
  `echo '#{statement}' | mysql -u #{user} -p'#{password}'`
end

def drop_existing(db, user, pass)
  drop_statement   = "drop database if exists #{db};"
  execute_mysql("dropping #{db}", drop_statement, user, pass)
end

def create_db(db, user, pass)
  create_statement =  "create database #{db};"
  execute_mysql("creating #{db}", create_statement, user, pass)
end

def grant_permission_on_db(db, user, pass)
  grant_statement  =  "grant all on `#{db}`.* to `#{$DB_CONFIG["username"]}`@`localhost` identified by \"#{$DB_CONFIG["password"]}\";"
  execute_mysql("granting on #{db}", grant_statement, user, pass)
end

read_database_config
get_root_password
check_root_password

db   = $DB_CONFIG["database"]
user = "root"
pass = $ROOT_PW

drop_existing(db, user, pass)
create_db(db, user, pass)
grant_permission_on_db(db, user, pass)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_archival-0.6.0 test/script/db_setup