-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_db.rb
81 lines (69 loc) · 2.21 KB
/
setup_db.rb
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
current_app_name = Dir.pwd.split("/").last
dbname_entry = ask("Database name [#{current_app_name[0..64]}_development]: ")
while dbname_entry.size > (64-12)
puts "*Name of db is too long (#{dbname_entry.size}. The limit is 64.)"
dbname_entry = ask("Database name [#{current_app_name[0..64]}_development]: ")
end
if dbname_entry.nil? || dbname_entry.size == 0
dbname = current_app_name[0..64]
dbname = "#{dbname}_development"
else
dbname = dbname_entry
end
# Name dbs of other environment
dbname_prod = "#{dbname}_production"
dbname_test = "#{dbname}_test"
# Ask for username for db to be created, defaulting to app name plus _dev
username = ask("Username [#{current_app_name}_dev]: ")
username = "#{current_app_name}_dev" if username.nil? || username.size == 0
# Ask for password for db to be created, defaulting to username
pass = ask("Password [#{username}]: ")
pass = username if pass.nil? || pass.size == 0
contents = ""
file "config/database.yml",
%q{development:
adapter: mysql
encoding: utf8
reconnect: false
database: $dbname
pool: 5
username: $username
password: $password
test:
adapter: mysql
encoding: utf8
reconnect: false
database: $dbname_test
pool: 5
username:
password:
socket: /tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
reconnect: false
database: $dbname_prod
pool: 5
username:
password:
socket: /tmp/mysql.sock
}
source_file = File.expand_path(File.dirname(__FILE__)) + "/config/database.yml"
File.open(source_file, 'r') do |f1|
while line = f1.gets
if line.include?("$")
line.gsub!(/\$dbname/, dbname) if !line.nil? && !dbname.nil?
line.gsub!(/\$username/, username) if !line.nil? && !username.nil?
line.gsub!(/\$password/, pass) if !line.nil? && !pass.nil?
line.gsub!(/\$dbname_prod/, dbname_prod) if !line.nil? && !pass.nil?
line.gsub!(/\$dbname_test/, dbname_test) if !line.nil? && !pass.nil?
end
contents += line
end
end
File.open('config/database.yml', 'w') do |f2|
f2.puts contents
end
mysql_command = "mysql -u root -p -e \"create database #{dbname}; grant all on #{dbname}.* to #{username}@'localhost' identified by '#{pass}'\";"
puts "Executing #{mysql_command}"
system mysql_command