Migrate with different user account?

12 views
Skip to first unread message

Mark Thomas

unread,
Sep 4, 2007, 3:19:10 PM9/4/07
to Ruby on Rails: Talk
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.

Greg Donald

unread,
Sep 4, 2007, 4:05:22 PM9/4/07
to rubyonra...@googlegroups.com

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 Donald
Cyberfusion Consulting
http://cyberfusionconsulting.com/

Mark Thomas

unread,
Sep 4, 2007, 4:32:03 PM9/4/07
to Ruby on Rails: Talk
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?


Matthew Rudy

unread,
Sep 4, 2007, 7:25:17 PM9/4/07
to rubyonra...@googlegroups.com

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.
--
Posted via http://www.ruby-forum.com/.

Mark Thomas

unread,
Sep 5, 2007, 9:43:20 AM9/5/07
to Ruby on Rails: Talk
Huh? YAML files are run through ERB? I wouldn't have thought this to
be the case. Nevertheless, I'll try it.

Matthew Rudy

unread,
Sep 5, 2007, 11:24:57 AM9/5/07
to rubyonra...@googlegroups.com
Mark Thomas 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 %>

Matthew Rudy

unread,
Sep 5, 2007, 11:27:03 AM9/5/07
to rubyonra...@googlegroups.com
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 Thomas

unread,
Sep 5, 2007, 11:28:22 AM9/5/07
to Ruby on Rails: Talk
On Sep 5, 9:43 am, Mark Thomas <r...@thomaszone.com> 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?

Matthew Rudy

unread,
Sep 5, 2007, 11:42:15 AM9/5/07
to rubyonra...@googlegroups.com
Mark Thomas wrote:
> On Sep 5, 9:43 am, Mark Thomas <r...@thomaszone.com> 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 Rudy

unread,
Sep 5, 2007, 11:45:30 AM9/5/07
to rubyonra...@googlegroups.com
Matthew Rudy wrote:
> Mark Thomas wrote:
>> On Sep 5, 9:43 am, Mark Thomas <r...@thomaszone.com> 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.

Matthew Rudy

unread,
Sep 5, 2007, 12:00:02 PM9/5/07
to rubyonra...@googlegroups.com
Matthew Rudy wrote:
> Matthew Rudy wrote:
>> Mark Thomas wrote:
>>> On Sep 5, 9:43 am, Mark Thomas <r...@thomaszone.com> 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

Mark Thomas

unread,
Sep 5, 2007, 12:14:27 PM9/5/07
to Ruby on Rails: Talk
> 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!

> 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

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

- Mark.

Matthew Rudy

unread,
Sep 5, 2007, 6:07:56 PM9/5/07
to rubyonra...@googlegroups.com
Mark Thomas 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 Thomas the english
comedian/activist... he's pretty darn cool.

Anirudhan J.

unread,
Jun 6, 2013, 9:29:41 AM6/6/13
to rubyonra...@googlegroups.com
Mark Thomas 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?
Reply all
Reply to author
Forward
0 new messages