开发者

appending to rake db:seed in rails and running it without duplicating data

Rake db:seed populates your db with default database values for an app right? So what if you already have a seed and you need to 开发者_开发知识库add to it(you add a new feature that requires the seed). In my experience, when I ran rake db:seed again, it added the existing content already so existing content became double.

What I need is to add some seeds and when ran, it should just add the newest ones, and ignore the existing seeds. How do I go about with this? (the dirty, noob way I usually do it is to truncate my whole db then run seed again, but that's not very smart to do in production, right?)


A cleaner way to do this is by using find_or_create_by, as follows:

User.find_or_create_by_username_and_role(
  :username => "admin",
  :role => "admin",
  :email => "me@gmail.com")

Here are the possible outcomes:

  1. A record exists with username "admin" and role "admin". This record will NOT be updated with the new e-mail if it already exists, but it will also NOT be doubled.
  2. A record does not exist with username "admin" and role "admin". The above record will be created.
  3. Note that if only one of the username/role criteria are satisfied, it will create the above record. Use the right criteria to ensure you aren't duplicating something you want to remain unique.


I do something like this.... When I need to add a user

in seeds.rb:

if User.count == 0
  puts "Creating admin user"
  User.create(:role=>:admin, :username=>'blagh', :etc=>:etc)
end

You can get more interesting than that, but in this case, you could run it over again as needed.


Another option that might have a slight performance benefit:

# This example assumes that a role consists of just an id and a title.

roles = ['Admin', 'User', 'Other']
existing_roles = Role.all.map { |r| r.title }

roles.each do |role|
  unless existing_roles.include?(role)
    Role.create!(title: role)
  end
end

I think that doing it this way, you only have to do one db call to get an array of what exists, then you only need to call again if something isn't there and needs to be created.



Adding


from

departments = ["this", "that"]
departments.each{|d| Department.where(:name => d).first_or_create}

to

departments = ["this", "that", "there", "then"]
departments.each{|d| Department.where(:name => d).first_or_create}

this is a simple example,


Updating/rename


from

departments = ["this", "that", "there", "then"]
departments.each{|d| Department.where(:name => d).first_or_create}

to

departments = ["these", "those", "there", "then"]
new_names = [['these', 'this'],['those','that']]

new_names.each do |new| 
  Department.where(:name => new).group_by(&:name).each do |name, depts|
    depts.first.update_column :name, new[0] if new[1] == name # skips validation
    # depts[1..-1].each(&:destroy) if depts.size > 1 # paranoid mode
  end
end

departments.each{|d| Department.where(:name => d).first_or_create}

IMPORTANT: You need to update the elements of departments array else duplication will surely happen.

Work around: Add a validates_uniqueness_of validation or a validation of uniqueness comparing all necessary attributes BUT don't use methods skipping validations.


My preference for this sort of thing is to create a custom rake task rather than use the seeds.rb file.

If you're trying to bulk create users I'd create a .csv files with the data then create a rake task called import_users and pass it the filename. Then loop through it to create the user records.

In lib/tasks/import_users.rake:

namespace :my_app do
  desc "Import Users from a .csv"
  task :import_users => :environment do
    # loop through records and create users
  end
end

Then run like so: rake bundle exec my_app:import_users path/to/.csv

If you need to run it in production: RAILS_ENV=production bundle exec rake my_app:import_users /path/to/.csv


Another alternative is to use the #first_or_create.

categories = [
    [ "Category 1", "#e51c23" ],
    [ "Category 2", "#673ab7" ]
]

categories.each do |name, color|
  Category.where( name: name, color: color).first_or_create
end


A really hackable way would be to comment out the existing data, that's how i did it, and it worked fine for me

=begin

#Commented Out these lines since they where already seeded 
   PayType.create!(:name => "Net Banking")
   PayType.create!(:name => "Coupouns Pay")

=end
#New data to be used by seeds

PayType.create!(:name => "Check")
PayType.create!(:name => "Credit card")
PayType.create!(:name => "Purchase order")
PayType.create!(:name => "Cash on delivery")

Once done just remove the comments


Another trivial alternative:

#categories => name, color 
categories = [
    [ "Category 1", "#e51c23" ],
    [ "Category 2", "#673ab7" ]
]

categories.each do |name, color|
  if ( Category.where(:name => name).present? == false )
    Category.create( name: name, color: color )
  end
end


Just add User.delete_all and for all the models that you have included in your application at the beginning of your seed.rb file. There will not be any duplicate values for sure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜