Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
37 lines (30 sloc) 1.12 KB
require 'active_record'
require 'yaml'
require 'logger'
require 'json'
namespace :db do
desc "Migrate the database"
task (:migrate) do
rack_env = ENV['RACK_ENV'] != nil ? ENV['RACK_ENV'] : 'development'
if rack_env == 'development'
File.open( 'config/database.yml' ) do |yf|
YAML.load_documents( yf ) do |ydoc|
#puts ENV.inspect
#puts ydoc['development'].inspect
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(ydoc)
end
end
else
services = JSON.parse(ENV['VCAP_SERVICES'])
puts services.inspect
mysql_key = services.keys.select { |svc| svc =~ /mysql/i }.first
mysql = services[mysql_key].first['credentials']
dbconfig = {:adapter => 'mysql2', :database => mysql['name'], :host => mysql['hostname'], :port => mysql['port'],
:username => mysql['user'], :password => mysql['password']}
ActiveRecord::Base.establish_connection(dbconfig)
end
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
end
end