summaryrefslogtreecommitdiff
path: root/build/scripts/db.rake
blob: 4237156f5b3e4621c203605c2dd9addd18f680f4 (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
56

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']
    host = configuration['host']
    #password = configuration['password']
    #command = "CREATE USER #{username} WITH password '#{password}'"
    #sh "psql -c '#{command}'"
    sh "psql -h #{host} -c 'CREATE DATABASE #{database} WITH OWNER #{username}'"
    sh "psql -h #{host} -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