How (and will) populate the rails application with raw data

I have a rail application in which users must log in. Therefore, in order for the application to be usable, the system must have one initial user for whom the first user must log in (then they can create users). So far, I have used the transition to add a special user to the database.

After asking this question, it seems that I should use db: schema: load, and not start the migration, to configure fresh databases on new development machines. Unfortunately, this does not seem to include migrations that insert data, only those that set up tables, keys, etc.

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

  • Is there a way to get d: s: l to enable data entry migration?
  • Should I not use migrations to insert data this way?
  • Should I not populate the database with data at all? Should I update the application code so that it handles the case when there are no users gracefully and allows me to create an initial user account in real time from the application?
  • Any other options? :)
+59
ruby-on-rails data-migration
Sep 15 '08 at 11:25
source share
13 answers

I thought I would summarize some of the wonderful answers that I had to this question, along with my own thoughts, now I read them all :)

There are two different questions here:

  • Should I pre-populate the database with my special admin user? Or should the application provide a way to configure it for the first time?
  • How to pre-populate a database with data? Please note that this is the correct question, regardless of the answer to part 1: there are other usage scenarios for prepopulation than the admin user.

For (1), it seems that setting up the first user from the application itself is quite a bit of additional work, since functionality, by definition, is almost never used. However, it can be somewhat more secure, as it forces the user to set a password of their choice. The best solution is between these two extremes: you have a script (or rake task or something else) to set up the initial user. Then the script can be configured to automatically fill in the default password during development and require a password to be entered during production installation / deployment (if you want to cancel the default administrator password).

For (2), there seems to be a number of good, valid solutions. Robbery seems like a good way, and there are some plugins to make this even easier. Just check out some other answers to see their details :)

+4
Sep 16 '08 at 19:42
source share

Try the rake command. For example:

  • Create the file /lib/tasks/bootstrap.rake
  • In the file, add a task to create the 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 set up your application for the first time, you can do rake db: transfer OR rake db: schema: load, and then do rake bootstrap: all.
+44
Sep 15 '08 at 12:52
source share

I recommend that you do not embed any new data in the migration. Instead, just modify existing data in migrations.

For input, I recommend using YML. In every Rails project that I configure, I create a fixture catalog in the DB directory. Then I create the YML files for the source data, just as the YML files are used for the test data. Then I add a new task to load data from YML files.

Library / 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 / appliances / users.yml:

 test: customer_id: 1 name: "Test Guy" email: "test@example.com" hashed_password: "656fc0b1c1d1681840816c68e1640f640c6ded12" salt: "188227600.754087929365988" 
+32
Sep 15 '08 at 17:14
source share

Use db/seed.rb found in every Rails application.

Although some of the answers above from 2008 may work well, they are fairly outdated and they are no longer Rails conventions.

Filling of the source data in the database should be done using the db/seed.rb .

It just works as a Ruby file.

To create and save an object, you can do something like:

User.create(:username => "moot", :description => "king of /b/")

Once you have the finished file, you can do the following

rake db:migrate

rake db:seed

Or in one step

rake db:setup

Your database should be populated with any objects you want to create in seed.rb

+31
Feb 13 '13 at 22:01
source share

This is my new favorite solution using a puller and fireworks:

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

+9
Sep 16 '08 at 12:37
source share

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

+6
Sep 16 '08 at 9:17
source share

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

+4
Sep 15 '08 at 11:36
source share

Consider using a rail console. Good for one-time admin tasks where you should not try to configure a script or port.

On your production machine:

 script/console production 

... then ...

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

If you create this initial user several times, you can also add the script to the RAILS_ROOT / script / file and run it from the command line on your production machine or using the capistrano task.

+3
Sep 15 '08 at 12:04
source share

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

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

+3
Sep 15 '08 at 21:04
source share

Great blog entry: http://railspikes.com/2008/2/1/loading-seed-data

I used Jay's suggestions in a special set of instruments, but quickly found that I was creating data that would not be possible using models directly (untranslated records when I used act_as_versioned)

+3
Sep 10 2018-10-10T00:
source share

I would save it during the migration process. Although it is recommended that you use the circuit for initial setup, the reason for this is that it is faster, which avoids problems. One additional migration for data should be good.

You can also add data to the schema file, as well as in the migration format. You just lose the auto-generation feature.

+2
15 Sep '08 at 11:33
source share

For users and groups, the question of existing users should be determined depending on the needs of the application, and not on unforeseen programming situations. Perhaps your application requires an administrator; then pre-fill. Or maybe not - add code to gracefully ask for customization when the application starts.

In a more general question, it is clear that many Rails applications can benefit from a pre-populated date. For example, an application for posting addresses in the United States may also contain all states and their abbreviations. I suppose for these cases, migration is your friend.

+2
Sep 15 '08 at 17:00
source share

Some of the answers are out of date. Since Rails 2.3.4 there is a simple Seed function available in db/seed.rb :

 #db/seed.rb User.create( :name => 'default', :password => 'password' ) Comment.create( :title => 'Title', :body => 'First post!' ) 

It provides a new rake task that you can use after your migrations to load data:

 rake db:seed 

Seed.rb is a classic Ruby file, feel free to use any classic data structure (array, hashes, etc.) and iterators to add your data:

 ["bryan", "bill", "tom"].each do |name| User.create(:name => name, :password => "password") end 

If you want to add data with UTF-8 characters (very common in French, Spanish, German, etc.), do not forget to add at the beginning of the file:

 # ruby encoding: utf-8 

This Railscast is a good introduction: http://railscasts.com/episodes/179-seed-data

+1
Jan 21 '14 at 11:08
source share



All Articles