I'm running multiple worker processes each in their own daemon (with the rails-daemon
gem) that should each have separate db roles / privileges (e.g. one should only be able to read table X, the other can write to X and Y), but it seems like Rails is set up to read the db credentials from one config/database.yml
. How do I set up configure multiple db users in rails so that each worker uses its own credentials?
You can inject code ERB into the database.yml file to serve your purposes by switching between DB users based on worker name, so you change the DB users based on the current worker.
The config/database.yml file can contain ERB tags <%= %>. Anything in the tags will be evaluated as Ruby code. You can use this to pull out data from an environment variable or to perform calculations to generate the needed connection information.
Reference:
http://edgeguides.rubyonrails.org/configuring.html
Example:
<%
def get_worker_credential
current_worker = "..." # You need to get the current worker name
case current_worker
when "worker-A"
return { username: "userA", password: "kjahskdjashd" }
when "worker-B"
return { username: "userB", password: "gtrgrttrtn" }
else
return { username: "guest", password: "fsdfdsdfsd" }
end
end
%>
test:
adapter: mysql
database: db
username: <%= get_worker_credential.username %>
password: <%= get_worker_credential.password %>
host: host_site