/
db_setup
executable file
·40 lines (30 loc) · 1.17 KB
/
db_setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env ruby
require "bundler/setup"
require "active_record"
require "yaml"
require 'highline/import'
database_file = File.join(File.dirname(__FILE__), "/../", "database.yml")
$DB_CONFIG = YAML.load(File.read(database_file))
$ROOT_PW = ask("Provide root mySQL password to setup '#{$DB_CONFIG["database"]}': ") { |q| q.echo = false}
if !$ROOT_PW || $ROOT_PW.empty?
puts "error reading password"
exit 4
end
password_check = "echo 'show databases;' | mysql -u root -p'#{$ROOT_PW}' > /dev/null"
unless system password_check
puts "incorrect password"
exit 7
end
def execute_mysql(message, statement, user = nil, password = nil)
puts message
`echo '#{statement}' | mysql -u #{user} -p'#{password}'`
end
db = $DB_CONFIG["database"]
user = "root"
pass = $ROOT_PW
drop_statement = "drop database if exists #{db};"
create_statement = "create database #{db};"
grant_statement = "grant all on `#{db}`.* to `#{$DB_CONFIG["username"]}`@`localhost` identified by \"#{$DB_CONFIG["password"]}\";"
execute_mysql("dropping #{db}", drop_statement, user, pass)
execute_mysql("creating #{db}", create_statement, user, pass)
execute_mysql("granting on #{db}", grant_statement, user, pass)