[LRUG] database account used by migrations

James Adam james.adam at gmail.com
Fri Dec 14 05:26:20 PST 2007


On 12/14/07, Scott Rutherford <scott at cominded.com> wrote:
> Hi Dafydd,
>
> Alter config/database.yml
>

I Think Dafydd's question was more related to security - it seems he
doesn't want the account that ActiveRecord normally uses to have ANY
permission to alter the schema. Or something like that.

You can achieve this by working with database.yml:

  development_defaults: &development_defaults
    adapter: mysql
    database: my_app_dev
    host: localhost

  development:
    <<: *development_defaults
    username: user_with_read_access
    password: password_for_that_user

  development_migration
    <<: *development_defaults
    username: super_user_with_alter_access
    password: password_for_that_super_user


.... and so on for the other environments that you care about. Then,
when you want to migrate, you just need to set the environment like
so:

  $ RAILS_ENV=development_migration rake db:migrate

And it should connect with the creditials you've given.

This isn't fool-proof, however - if you rely on the environment
variable to load any other configuration (i.e. via
config/environments/development.rb), you'll need to deal with that
somehow too.

-~*~-

Here's another (probably better) approach using environment variables
and the fact that database.yml is parsed through ERB:

  development:
    adapter: mysql
    database: my_app_dev
    host: localhost
    username: <%= ENV['RAILS_USER'] || 'default_username' %>
    password: <%= ENV['RAILS_PASSWORD'] || 'default_password' %>

That way if you run

  $ RAILS_USER=super_user RAILS_PASSWORD=their_password rake db:migrate

...it will pick up the creditials you've given on the command line.

HTH,

James

-- 
* J *
  ~



More information about the Chat mailing list