after "deploy:migrate", "deploy:migrate_subdomains"
task :migrate_subdomains do
configs = YAML::load(File.open(File.expand_path("../../../config/database.yml", __FILE__)))
configs["subdomains"].each do |subdomain|
begin
run "cd #{current_path} && #{rake} db:migrate RAILS_ENV=#{subdomain}"
rescue Exception => e
Capistrano::CLI.ui.say "*** [err] #{e.message}"
end
end
end
before_filter :select_database
def select_database
if Rails.env == "production"
unless request.subdomain.empty?
begin
subdomain = request.subdomains.first
config = ActiveRecord::Base.configurations[subdomain]
if config
ActiveRecord::Base.establish_connection(config)
else
ActiveRecord::Base.establish_connection(Rails.env)
flash[:error] = "Database not found"
end
rescue
render :text => "Invalid subdomain"
end
end
end
end