views:

6697

answers:

11

I've got a rails application where users have to log in. Therefore in order for the application to be usable, there must be one initial user in the system for the first person to log in with (they can then create subsequent users). Up to now I've used a migration to add a special user to the database.

After asking this question, it seems that I should be using db:schema:load, rather than running the migrations, to set up fresh databases on new development machines. Unfortunately, this doesn't seem to include the migrations which insert data, only those which set up tables, keys etc.

My question is, what's the best way to handle this situation:

  1. Is there a way to get d:s:l to include data-insertion migrations?
  2. Should I not be using migrations at all to insert data this way?
  3. Should I not be pre-populating the database with data at all? Should I update the application code so that it handles the case where there are no users gracefully, and lets an initial user account be created live from within the application?
  4. Any other options? :)
A: 

I'd keep it in a migration. While it's recommended to use the schema for initial setups, the reason for that is that it's faster, thus avoiding problems. A single extra migration for the data should be fine.

You could also add the data into the schema file, as it's the same format as migrations. You'd just lose the auto-generation feature.

MattW.
+2  A: 

I guess the best option is number 3, mainly because that way there will be no default user which is a great way to render otherwise good security useless.

Vinko Vrsalovic
+1  A: 

Consider using the rails console. Good for one-off admin tasks where it's not worth the effort to set up a script or migration.

On your production machine:

script/console production

... then ...

User.create(:name => "Whoever", :password => "whichever")

If you're generating this initial user more than once, then you could also add a script in RAILS_ROOT/script/, and run it from the command line on your production machine, or via a capistrano task.

Trevor Stow
+12  A: 

Try a rake task. For example:

  1. Create the file /lib/tasks/bootstrap.rake
  2. In the file, add a task to create your default user:

    namespace :bootstrap do
      desc "Add the default user"
      task :default_user => :environment do
        User.create( :name => 'default', :password => 'password' )
      end

      desc "Create the default comment"
      task :default_comment => :environment do
        Comment.create( :title => 'Title', :body => 'First post!' )
      end

      desc "Run all bootstrapping tasks"
      task :all => [:default_user, :default_comment]
    end
  1. Then, when you're setting up your app for the first time, you can do rake db:migrate OR rake db:schema:load, and then do rake bootstrap:all.
Aaron Wheeler
doesn't work if /lib is RO, like if you are on shared hosting environment. Do you have a solution that would work in that case?
cbrulak
I'm pretty sure he's talking about your application's /lib directory.
Azeem.Butt
A: 

For users and groups, the question of pre-existing users should be defined with respect to the needs of the application rather than the contingencies of programming. Perhaps your app requires an administrator; then prepopulate. Or perhaps not - then add code to gracefully ask for a user setup at application launch time.

On the more general question, it is clear that many Rails Apps can benefit from pre-populated date. For example, a US address holding application may as well contain all the States and their abbreviations. For these cases, migrations are your friend, I believe.

NickR
+11  A: 

I recommend that you don't insert any new data in migrations. Instead, only modify existing data in migrations.

For inserting initial data, I recommend you use YML. In every Rails project I setup, I create a fixtures directory under the DB directory. Then I create YML files for the initial data just like YML files are used for the test data. Then I add a new task to load the data from the YML files.

lib/tasks/db.rake:

namespace :db do
  desc "This loads the development data."
  task :seed => :environment do
    require 'active_record/fixtures'
    Dir.glob(RAILS_ROOT + '/db/fixtures/*.yml').each do |file|
      base_name = File.basename(file, '.*')
      say "Loading #{base_name}..."
      Fixtures.create_fixtures('db/fixtures', base_name)
    end
  end

  desc "This drops the db, builds the db, and seeds the data."
  task :reseed => [:environment, 'db:reset', 'db:seed']
end

db/fixtures/users.yml:

test:
  customer_id: 1
  name: "Test Guy"
  email: "[email protected]"
  hashed_password: "656fc0b1c1d1681840816c68e1640f640c6ded12"
  salt: "188227600.754087929365988"
Jay Stramel
it didn't seem to like the 'say' method.. worked fine otherwise though
Kevin Davis
+2  A: 

That Rake task can be provided by the db-populate plugin:

http://github.com/joshknowles/db-populate/tree/master

pantulis
+5  A: 

Try the seed-fu plugin, which is quite a simple plugin that allows you to seed data (and change that seed data in the future), will also let you seed environment specific data and data for all environments.

DEfusion
+3  A: 

This is my new favorite solution, using the populator and faker gems:

http://railscasts.com/episodes/126-populating-a-database

+1  A: 

I thought I'd summarise some of the great answers I've had to this question, together with my own thoughts now I've read them all :)

There are two distinct issues here:

  1. Should I pre-populate the database with my special 'admin' user? Or should the application provide a way to set up when it's first used?
  2. How does one pre-populate the database with data? Note that this is a valid question regardless of the answer to part 1: there are other usage scenarios for pre-population than an admin user.

For (1), it seems that setting up the first user from within the application itself is quite a bit of extra work, for functionality which is, by definition, hardly ever used. It may be slightly more secure, however, as it forces the user to set a password of their choice. The best solution is in between these two extremes: have a script (or rake task, or whatever) to set up the initial user. The script can then be set up to auto-populate with a default password during development, and to require a password to be entered during production installation/deployment (if you want to discourage a default password for the administrator).

For (2), it appears that there are a number of good, valid solutions. A rake task seems a good way, and there are some plugins to make this even easier. Just look through some of the other answers to see the details of those :)

Luke Halliwell
+1  A: 

Great blog post on this: http://railspikes.com/2008/2/1/loading-seed-data

I was using Jay's suggestions of a special set of fixtures, but quickly found myself creating data that wouldn't be possible using the models directly (unversioned entries when I was using acts_as_versioned)

Kevin Davis