blob: ff2b089cfb85866de2328823dbed53a59a7b26b5 (
plain)
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
|
namespace :db do
task :migrate do
Database.new.migrate('development')
end
task 'migrate:test' do
Database.new.migrate('test')
end
task :create do
Database.new.create('development')
Rake::Task['db:migrate'].invoke
end
task 'create:test' do
Database.new.create('test')
Rake::Task['db:migrate:test'].invoke
end
end
class Database
def create(environment)
configuration = load_configuration_for(environment)
database = configuration['database']
username = configuration['username']
#password = configuration['password']
#command = "CREATE USER #{username} WITH password '#{password}'"
#sh "psql -c '#{command}'"
sh "psql -c 'CREATE DATABASE #{database} WITH OWNER #{username}'"
sh "psql -c 'GRANT ALL PRIVILEGES ON DATABASE #{database} TO #{username}'"
end
def migrate(environment)
connection_string = ENV["DATABASE_URL"] || create_connection_string_for(environment)
sh "bundle exec sequel -m build/db/migrations -E #{connection_string}"
end
private
def sh(command)
Kernel::system(command)
end
def load_configuration_for(environment)
all_configuration = YAML.load_file(File.join(File.dirname(__FILE__), '../db/configuration.yml'))
all_configuration[ENV["BOOTY_ENV"] || environment]
end
def create_connection_string_for(environment)
configuration = load_configuration_for(environment)
"postgres://#{configuration['username']}:#{configuration['password']}@#{configuration['host']}/#{configuration['database']}"
end
end
|