-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
60 lines (52 loc) · 1.63 KB
/
Rakefile
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
require 'rake'
task 'db:setup' do
require 'active_record'
require 'pg'
create = ENV['DB_SETUP']
if create.nil?
print "Is PostgreSQL already setup? Including user, password and (empty) database? [y/n] "
answer = $stdin.gets.chomp.downcase
case answer
when 'y'
create = false
when 'n'
create = true
else
puts "Incorrect input. Terminate..."
exit
end
end
db_setup = {
:adapter => 'postgresql',
:host => ENV['DB_HOST'] || 'localhost',
:port => ENV['DB_PORT'] || '5432'
}
%w[user password database].each do |k|
var = "DB_#{k.upcase}"
if ENV[var].nil?
print "\tEnter PostgreSQL #{k}: "
db_setup[k.to_sym] = $stdin.gets.chomp
else
db_setup[k.to_sym] = ENV[var]
puts "\tUsing PostgreSQL #{k}='#{ENV[var]}'..."
end
end
if create
puts "\nCreating PostgreSQL user. Enter sudo password if asked to."
sh "sudo -u postgres psql -c \"CREATE USER #{db_setup[:user]} WITH PASSWORD '#{db_setup[:password]}'\""
puts "Creating database. Enter sudo password for 'postgres' if asked to."
sh "sudo -u postgres createdb -O #{db_setup[:user]} #{db_setup[:database]}"
end
begin
ActiveRecord::Base.establish_connection(db_setup)
require './database/schema.rb'
rescue PG::Error => e
puts "Database setup failed. Exiting..."
exit
end
puts "\nCreating foreman .env file..."
File.open('.env', 'w') do |file|
file.write("DATABASE_URL=postgres://#{db_setup[:user]}:#{db_setup[:password]}@#{db_setup[:host]}:#{db_setup[:port]}/#{db_setup[:database]}")
end
puts "\nDONE! Run app with 'foreman start'."
end