Migrate with different user account?

In my environment, the application logins do not have the ability to
alter the schema to create tables, etc. There is a separate schema
owner account with that capability. So I cannot run “rake migrate”
using the database.yml configuration. Is there a way to run migrations
with a different account?

Thanks,

  • Mark.

On Tue, Sep 04, 2007 at 07:19:10PM -0000, Mark T. wrote:

In my environment, the application logins do not have the ability to
alter the schema to create tables, etc. There is a separate schema
owner account with that capability. So I cannot run “rake migrate”
using the database.yml configuration. Is there a way to run migrations
with a different account?

There isn’t any magical way to do more than the permissions you have
allow you to do, if that’s what you’re asking. If you have a different
db user account that has sufficient permissions, then just plug it into
the database.yml file.


Greg D.
Cyberfusion Consulting
http://cyberfusionconsulting.com/

Let me be more specific. My rails app is not allowed to use the schema
owner account, so I cannot just put it in the database.yml file. In
addition, I cannot leave the password for that account on the
filesystem. So even if there was a way to select a different
database.yml file for the migrations only, I cannot do that. (Although
I’d still be interested to know if there’s a way to select a different
database.yml on the fly).

What would be ideal: a front end to rake db:migrate that asks me for
the username/password to the account, and overrides the database.yml
entries for them. Is there a way to do this via a new rake task? Or
maybe it would be easier to modify the existing db:migrate task?

Huh? YAML files are run through ERB? I wouldn’t have thought this to
be the case. Nevertheless, I’ll try it.

Mark T. wrote:

Let me be more specific. My rails app is not allowed to use the schema
owner account, so I cannot just put it in the database.yml file. In
addition, I cannot leave the password for that account on the
filesystem. So even if there was a way to select a different
database.yml file for the migrations only, I cannot do that. (Although
I’d still be interested to know if there’s a way to select a different
database.yml on the fly).

What would be ideal: a front end to rake db:migrate that asks me for
the username/password to the account, and overrides the database.yml
entries for them. Is there a way to do this via a new rake task? Or
maybe it would be easier to modify the existing db:migrate task?

how about this solution;

database.yml

<%
def as_db_admin?
defined?(AS_DB_ADMIN) && AS_DB_ADMIN == “true”
end
%>
production:
adapter: mysql
database: databasename
host: localhost
username: <%= as_db_admin? ? “root” : “normal” %>
password: <%= as_db_admin? ? “root” : “normal” %>

============

“rake db:migrate AS_DB_ADMIN=true”

I belive that’ll set the global constant AS_DB_ADMIN,
and hence do what you want.

Otherwise it sets ENV[“AS_DB_ADMIN”]
but I think it actually sets the constant directly.

Give it a try.

Mark T. wrote:

Huh? YAML files are run through ERB? I wouldn’t have thought this to
be the case. Nevertheless, I’ll try it.

yeah.
it’s not obvious, until you look at the code.

I use it all the time, so I can do host specific databases
(namely I have 3 different environments I want to run the tests on, some
of them have the database running on localhost, but the others have
dedicated db servers with different names)

it works proper fine.

<%
def sharon_or_localhost
if [“punky”, “munky”].include? Socket.gethostname
return “sharon”
else
return “localhost”
end
end
%>

test:
adapter: mysql
database: testdb
username: test_user
password: th1si5aTESt
host: <%= sharon_or_localhost %>

On Sep 5, 9:43 am, Mark T. [email protected] wrote:

Huh? YAML files are run through ERB? I wouldn’t have thought this to
be the case. Nevertheless, I’ll try it.

Nope, it is as I thought–you can’t put ERB into database.yml.

I’m going down the path of trying a custom rake task, but I have no
idea where database.yml exists as a variable after it has been loaded
by Rails. Does anyone know?

and here’s the code out of the rails initializer

Loads and returns the contents of the #database_configuration_file.

The

contents of the file are processed via ERB before being sent through

YAML::load.

def database_configuration
YAML::load(ERB.new(IO.read(database_configuration_file)).result)
end

see railties/lib/initializer.rb – don’t know which line.

Mark T. wrote:

On Sep 5, 9:43 am, Mark T. [email protected] wrote:

Huh? YAML files are run through ERB? I wouldn’t have thought this to
be the case. Nevertheless, I’ll try it.

Nope, it is as I thought–you can’t put ERB into database.yml.

hmm…
well I haven’t bothered to go any further back than revision 2115,
but ERB parsing of database.yml has been in rails trunk since at least
September 2005, maybe before then even.

Matthew R. wrote:

Matthew R. wrote:

Mark T. wrote:

On Sep 5, 9:43 am, Mark T. [email protected] wrote:

Huh? YAML files are run through ERB? I wouldn’t have thought this to
be the case. Nevertheless, I’ll try it.

Nope, it is as I thought–you can’t put ERB into database.yml.

and equally;
ActiveRecord::Base.configurations is the hash of the database
connections.

and I think this rake task will do what you want also

================
/lib/tasks/migrate_as_root.rake

ROOT_USER_PASS_HASH = {“username” => “root”, “password” => “rootpass”}

desc “run your migration as db root”
task “db:migrate_as_root” do
original_config = ActiveRecord::Base.configurations[RAILS_ENV ||
“development”]
config = original_config.merge(ROOT_USER_PASS_HASH)
ActiveRecord::Base.establish_connection(config)

Rake::Task[“db:migrate”].invoke
end

well I haven’t bothered to go any further back than revision 2115,
but ERB parsing of database.yml has been in rails trunk since at least
September 2005, maybe before then even.

Well then I apologize. My quick test didn’t work and I guess I jumped
to the conclusion.

and equally;
ActiveRecord::Base.configurations is the hash of the database
connections.

Thanks!

original_config = ActiveRecord::Base.configurations[RAILS_ENV ||
“development”]
config = original_config.merge(ROOT_USER_PASS_HASH)
ActiveRecord::Base.establish_connection(config)

Rake::Task[“db:migrate”].invoke
end

Yes! This is very close to what I need. I’ll take it from here. Thank
you very much!

  • Mark.

Matthew R. wrote:

Mark T. wrote:

On Sep 5, 9:43 am, Mark T. [email protected] wrote:

Huh? YAML files are run through ERB? I wouldn’t have thought this to
be the case. Nevertheless, I’ll try it.

Nope, it is as I thought–you can’t put ERB into database.yml.

and equally;
ActiveRecord::Base.configurations is the hash of the database
connections.

Mark T. wrote:

Yes! This is very close to what I need. I’ll take it from here. Thank
you very much!

  • Mark.

Yeah, no wuzz…
I don’t like being wrong.

Take it easy…

btw, do you happen to know of Mark T. the english
comedian/activist… he’s pretty darn cool.

Mark T. wrote in post #550700:

well I haven’t bothered to go any further back than revision 2115,
but ERB parsing of database.yml has been in rails trunk since at least
September 2005, maybe before then even.

Well then I apologize. My quick test didn’t work and I guess I jumped
to the conclusion.

and equally;
ActiveRecord::Base.configurations is the hash of the database
connections.

Thanks!

original_config = ActiveRecord::Base.configurations[RAILS_ENV ||
“development”]
config = original_config.merge(ROOT_USER_PASS_HASH)
ActiveRecord::Base.establish_connection(config)

Rake::Task[“db:migrate”].invoke
end

Yes! This is very close to what I need. I’ll take it from here. Thank
you very much!

  • Mark.

Were you able to do this successfully? And how did you handle
capistrano tasks?