Permalink
Browse files

Added db/seeds.rb as a default file for storing seed data for the dat…

…abase. Can be loaded with rake db:seed (or created alongside the db with db:setup). (This is also known as the "Stop Putting Gawd Damn Seed Data In Your Migrations" feature) [DHH]
  • Loading branch information...
dhh committed May 11, 2009
1 parent 2e7409f commit 4932f7b38f72104819022abca0c952ba6f9888cb
View
@@ -1,3 +1,8 @@
*Edge*
* Added db/seeds.rb as a default file for storing seed data for the database. Can be loaded with rake db:seed (or created alongside the db with db:setup). (This is also known as the "Stop Putting Gawd Damn Seed Data In Your Migrations" feature) [DHH]
*2.3.2 [Final] (March 15, 2009)*
* Remove outdated script/plugin options [Pratik Naik]
View
@@ -200,11 +200,14 @@ task :copy_configs do
cp "configs/locales/en.yml", "#{PKG_DESTINATION}/config/locales/en.yml"
cp "configs/seeds.rb", "#{PKG_DESTINATION}/db/seeds.rb"
cp "environments/boot.rb", "#{PKG_DESTINATION}/config/boot.rb"
cp "environments/environment.rb", "#{PKG_DESTINATION}/config/environment.rb"
cp "environments/production.rb", "#{PKG_DESTINATION}/config/environments/production.rb"
cp "environments/development.rb", "#{PKG_DESTINATION}/config/environments/development.rb"
cp "environments/test.rb", "#{PKG_DESTINATION}/config/environments/test.rb"
end
task :copy_binfiles do
@@ -0,0 +1,7 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Major.create(:name => 'Daley', :city => cities.first)
@@ -125,6 +125,7 @@ def create_config_files(m)
create_database_configuration_file(m)
create_routes_file(m)
create_locale_file(m)
create_seeds_file(m)
create_initializer_files(m)
create_environment_files(m)
end
@@ -176,6 +177,10 @@ def create_routes_file(m)
m.file "configs/routes.rb", "config/routes.rb"
end
def create_seeds_file(m)
m.file "configs/seeds.rb", "db/seeds.rb"
end
def create_initializer_files(m)
%w(
backtrace_silencers
@@ -156,8 +156,8 @@ namespace :db do
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc 'Drops and recreates the database from db/schema.rb for the current environment.'
task :reset => ['db:drop', 'db:create', 'db:schema:load']
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
task :reset => [ 'db:drop', 'db:setup' ]
desc "Retrieves the charset for the current environment's database"
task :charset => :environment do
@@ -206,6 +206,15 @@ namespace :db do
end
end
desc 'Create the database, load the schema, and initialize with the seed data'
task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ]
desc 'Load the seed data from db/seeds.rb'
task :seed => :environment do
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file)
end
namespace :fixtures do
desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
task :load => :environment do

44 comments on commit 4932f7b

@pjammer

This comment has been minimized.

Show comment
Hide comment
@pjammer

pjammer May 11, 2009

Now this is an awesome commit. Thanks, as adding seed data was usually one of the first things i'd do in most of my apps.

pjammer replied May 11, 2009

Now this is an awesome commit. Thanks, as adding seed data was usually one of the first things i'd do in most of my apps.

@akahn

This comment has been minimized.

Show comment
Hide comment
@akahn

akahn May 11, 2009

What is "seed data"?

akahn replied May 11, 2009

What is "seed data"?

@rmoriz

This comment has been minimized.

Show comment
Hide comment
@rmoriz

rmoriz May 11, 2009

@akahn initial sql data you'll use in your app. e.g. country data, roles

rmoriz replied May 11, 2009

@akahn initial sql data you'll use in your app. e.g. country data, roles

@arthurschreiber

This comment has been minimized.

Show comment
Hide comment
@arthurschreiber

arthurschreiber May 11, 2009

Great change! :) Until now, I always came up with my own solutions for this, so I'm happy to see this in Rails Core.

arthurschreiber replied May 11, 2009

Great change! :) Until now, I always came up with my own solutions for this, so I'm happy to see this in Rails Core.

@supaspoida

This comment has been minimized.

Show comment
Hide comment
@supaspoida

supaspoida May 11, 2009

Glad to see this functionality built into rails. Been using seed-fu to keep migrations & data separate for some time now & the technique works really well.

supaspoida replied May 11, 2009

Glad to see this functionality built into rails. Been using seed-fu to keep migrations & data separate for some time now & the technique works really well.

@thiagopradi

This comment has been minimized.

Show comment
Hide comment
@thiagopradi

thiagopradi May 11, 2009

Contributor

great work!

Contributor

thiagopradi replied May 11, 2009

great work!

@Xac

This comment has been minimized.

Show comment
Hide comment
@Xac

Xac May 11, 2009

Very nice to see something like this in Rails core. It's about time we standardized the solution to seeding a db.

Xac replied May 11, 2009

Very nice to see something like this in Rails core. It's about time we standardized the solution to seeding a db.

@ryanb

This comment has been minimized.

Show comment
Hide comment
@ryanb

ryanb May 11, 2009

Contributor

I like the simplicity of this. It only loads a Ruby file and doesn't put any restrictions on how one generates seed data. What this does is create a standard everyone can go with (rake db:seed) and leave the implementation up to the developer. One could load the fixture files, use seed-fu, or even Factory Girl.

Contributor

ryanb replied May 11, 2009

I like the simplicity of this. It only loads a Ruby file and doesn't put any restrictions on how one generates seed data. What this does is create a standard everyone can go with (rake db:seed) and leave the implementation up to the developer. One could load the fixture files, use seed-fu, or even Factory Girl.

@jrmehle

This comment has been minimized.

Show comment
Hide comment
@jrmehle

jrmehle May 11, 2009

Contributor

It's good to see Rails take a side on the seed data debate. Fixtures, migrations, rake tasks, script/console... There are so many ways you could load seed data. Standardizing on one method should make things easier.

Contributor

jrmehle replied May 11, 2009

It's good to see Rails take a side on the seed data debate. Fixtures, migrations, rake tasks, script/console... There are so many ways you could load seed data. Standardizing on one method should make things easier.

@melvinram

This comment has been minimized.

Show comment
Hide comment
@melvinram

melvinram May 11, 2009

Sweet! I like it.

melvinram replied May 11, 2009

Sweet! I like it.

@adrianpacala

This comment has been minimized.

Show comment
Hide comment
@adrianpacala

adrianpacala May 11, 2009

Contributor

Me approves.

Contributor

adrianpacala replied May 11, 2009

Me approves.

@melvinram

This comment has been minimized.

Show comment
Hide comment
@melvinram

melvinram May 11, 2009

Question: I get that this will run when you run 'rake db:setup', 'rake db:seed' or 'rake db:reset' ... but will it also run when you do 'rake db:migrate' for the first time? And are there other rake tasks other than what I've listed that will cause the seed.rb to be run?

melvinram replied May 11, 2009

Question: I get that this will run when you run 'rake db:setup', 'rake db:seed' or 'rake db:reset' ... but will it also run when you do 'rake db:migrate' for the first time? And are there other rake tasks other than what I've listed that will cause the seed.rb to be run?

@tilsammans

This comment has been minimized.

Show comment
Hide comment
@tilsammans

tilsammans May 11, 2009

Contributor

Very nice.

Contributor

tilsammans replied May 11, 2009

Very nice.

@melvinram

This comment has been minimized.

Show comment
Hide comment
@melvinram

melvinram May 11, 2009

In response to my question earlier: Looking at the code for the migrate task, it doesn't look like it will trigger the seed.rb to be run.

melvinram replied May 11, 2009

In response to my question earlier: Looking at the code for the migrate task, it doesn't look like it will trigger the seed.rb to be run.

@bcardarella

This comment has been minimized.

Show comment
Hide comment
@bcardarella

bcardarella May 11, 2009

Contributor

That's pretty cool. No more messy migration files.

Contributor

bcardarella replied May 11, 2009

That's pretty cool. No more messy migration files.

@mrichman

This comment has been minimized.

Show comment
Hide comment
@mrichman

mrichman May 11, 2009

I'd love to see this backported to 2.3.x or 2.4!

mrichman replied May 11, 2009

I'd love to see this backported to 2.3.x or 2.4!

@pjkelly

This comment has been minimized.

Show comment
Hide comment
@pjkelly

pjkelly May 11, 2009

If you want something like this in pre-edge rails, check out http://github.com/mbleigh/seed-fu/tree/master.

pjkelly replied May 11, 2009

If you want something like this in pre-edge rails, check out http://github.com/mbleigh/seed-fu/tree/master.

@andres

This comment has been minimized.

Show comment
Hide comment
@andres

andres May 11, 2009

for backporting you can have a little rake tast
desc "Load initial base data"
task :load_base_data => :environment do |t|
require 'db/data_dump.rb' # a file with a class DataDump and a method load that holds your data written in "migration style"
DataDump.load #where initialize loads the data into the database
end

andres replied May 11, 2009

for backporting you can have a little rake tast
desc "Load initial base data"
task :load_base_data => :environment do |t|
require 'db/data_dump.rb' # a file with a class DataDump and a method load that holds your data written in "migration style"
DataDump.load #where initialize loads the data into the database
end

@kivanio

This comment has been minimized.

Show comment
Hide comment
@kivanio

kivanio May 11, 2009

Contributor

great!

Contributor

kivanio replied May 11, 2009

great!

@dpickett

This comment has been minimized.

Show comment
Hide comment
@dpickett

dpickett May 11, 2009

Contributor

very nice - good to now have a standard for all that data

Contributor

dpickett replied May 11, 2009

very nice - good to now have a standard for all that data

@labria

This comment has been minimized.

Show comment
Hide comment
@labria

labria May 11, 2009

Contributor

yay

Contributor

labria replied May 11, 2009

yay

@jstorimer

This comment has been minimized.

Show comment
Hide comment
@jstorimer

jstorimer May 11, 2009

Contributor

+1 woot

Contributor

jstorimer replied May 11, 2009

+1 woot

@edward

This comment has been minimized.

Show comment
Hide comment
@edward

edward May 11, 2009

Contributor

Awesome. I’ve been looking forward to this puppy for a while.

Contributor

edward replied May 11, 2009

Awesome. I’ve been looking forward to this puppy for a while.

@grzegorzkazulak

This comment has been minimized.

Show comment
Hide comment
@grzegorzkazulak

grzegorzkazulak replied May 11, 2009

+1 awesome :}

@joefiorini

This comment has been minimized.

Show comment
Hide comment
@joefiorini

joefiorini May 11, 2009

Contributor

Awesome idea, would love to see a test showing a sample seeds file and how it's expected to be used! :) All around a good feature to have in!

Contributor

joefiorini replied May 11, 2009

Awesome idea, would love to see a test showing a sample seeds file and how it's expected to be used! :) All around a good feature to have in!

@trevorturk

This comment has been minimized.

Show comment
Hide comment
@trevorturk

trevorturk May 11, 2009

Contributor

Awesome!

BTW, I think there's a typo in the comments in db/seeds.rb:
Major.create(:name => 'Daley', :city => cities.first)
...should be "Mayor" not "Major" right? :)

Contributor

trevorturk replied May 11, 2009

Awesome!

BTW, I think there's a typo in the comments in db/seeds.rb:
Major.create(:name => 'Daley', :city => cities.first)
...should be "Mayor" not "Major" right? :)

@chalkers

This comment has been minimized.

Show comment
Hide comment
@chalkers

chalkers May 11, 2009

How about rolling back or running the seeder twice when some seed data is already there?

chalkers replied May 11, 2009

How about rolling back or running the seeder twice when some seed data is already there?

@loe

This comment has been minimized.

Show comment
Hide comment
@loe

loe May 11, 2009

Contributor

+1 Opinion delivered.

Contributor

loe replied May 11, 2009

+1 Opinion delivered.

@tvdeyen

This comment has been minimized.

Show comment
Hide comment
@tvdeyen

tvdeyen May 11, 2009

Contributor

great

Contributor

tvdeyen replied May 11, 2009

great

@matharvard

This comment has been minimized.

Show comment
Hide comment
@matharvard

matharvard replied May 11, 2009

Thanks :)

@samgranieri

This comment has been minimized.

Show comment
Hide comment
@samgranieri

samgranieri May 11, 2009

Contributor

Thanks DHH!

Contributor

samgranieri replied May 11, 2009

Thanks DHH!

@aussiegeek

This comment has been minimized.

Show comment
Hide comment
@aussiegeek

aussiegeek May 11, 2009

Contributor

Thanks, one of the project I'm working on we've been using fixtures, which never felt like fun

Contributor

aussiegeek replied May 11, 2009

Thanks, one of the project I'm working on we've been using fixtures, which never felt like fun

@radar

This comment has been minimized.

Show comment
Hide comment
@radar

radar May 11, 2009

Contributor

This is very awesome!

Contributor

radar replied May 11, 2009

This is very awesome!

@jinzhu

This comment has been minimized.

Show comment
Hide comment
@jinzhu

jinzhu May 12, 2009

awesome commit !
right now I am using config/load_data.rb .

jinzhu replied May 12, 2009

awesome commit !
right now I am using config/load_data.rb .

@deepthawtz

This comment has been minimized.

Show comment
Hide comment
@deepthawtz

deepthawtz May 12, 2009

would I be stabbed if I didn't agree this was awesome? hypothetical question for ya.

deepthawtz replied May 12, 2009

would I be stabbed if I didn't agree this was awesome? hypothetical question for ya.

@Knack

This comment has been minimized.

Show comment
Hide comment
@Knack

Knack May 12, 2009

@chalkers I'm using the plugin 'mbleigh/seed-fu' wich address this issue, because it either creates or updates records, checking the existence of the record based on one or several fields you specify.

In fact, seed_fu already defines a db:seed rake task (maybe a potential conflict).

I like the idea of seeds.rb being just a ruby file, so you can write inside whatever you want: use seed_fu, load one or several yaml files, etc.

Knack replied May 12, 2009

@chalkers I'm using the plugin 'mbleigh/seed-fu' wich address this issue, because it either creates or updates records, checking the existence of the record based on one or several fields you specify.

In fact, seed_fu already defines a db:seed rake task (maybe a potential conflict).

I like the idea of seeds.rb being just a ruby file, so you can write inside whatever you want: use seed_fu, load one or several yaml files, etc.

@chalkers

This comment has been minimized.

Show comment
Hide comment
@chalkers

chalkers May 12, 2009

@Knack Thanks for the reply. Couldn't "checking the existence of the record based on one or several fields you specify" be expensive?

chalkers replied May 12, 2009

@Knack Thanks for the reply. Couldn't "checking the existence of the record based on one or several fields you specify" be expensive?

@blj

This comment has been minimized.

Show comment
Hide comment
@blj

blj May 12, 2009

@chalkers, it is expensive only if you want it to be.

blj replied May 12, 2009

@chalkers, it is expensive only if you want it to be.

@sermoa

This comment has been minimized.

Show comment
Hide comment
@sermoa

sermoa May 12, 2009

NICE! thanks for that! :)

sermoa replied May 12, 2009

NICE! thanks for that! :)

@softprops

This comment has been minimized.

Show comment
Hide comment
@softprops

softprops May 16, 2009

+1 for a standardization of a common pattern in migrations.

softprops replied May 16, 2009

+1 for a standardization of a common pattern in migrations.

@fosrias

This comment has been minimized.

Show comment
Hide comment
@fosrias

fosrias Oct 19, 2009

Contributor

This is cool. Now really cool would be this:

script/generate scaffold MyModule::MyModel --seed
or
script/generate scaffold MyModule::MyModel --seed

creates the file:

..\db\seeds\my_module\my_model.rb

and then the command

rake db:seed Module::model

only runs the model seed file and

rake db:seed

runs all the seed files.

Then you've nailed the functionality.

Contributor

fosrias replied Oct 19, 2009

This is cool. Now really cool would be this:

script/generate scaffold MyModule::MyModel --seed
or
script/generate scaffold MyModule::MyModel --seed

creates the file:

..\db\seeds\my_module\my_model.rb

and then the command

rake db:seed Module::model

only runs the model seed file and

rake db:seed

runs all the seed files.

Then you've nailed the functionality.

@fosrias

This comment has been minimized.

Show comment
Hide comment
@fosrias

fosrias Oct 19, 2009

Contributor

Second generate was meant to be:

script/generate model MyModule::MyModel --seed

and the first rake was meant to be:

rake db:seed MyModule::my_model

GitHub needs to let you edit your comments.

Contributor

fosrias replied Oct 19, 2009

Second generate was meant to be:

script/generate model MyModule::MyModel --seed

and the first rake was meant to be:

rake db:seed MyModule::my_model

GitHub needs to let you edit your comments.

@fosrias

This comment has been minimized.

Show comment
Hide comment
@fosrias

fosrias Oct 26, 2009

Contributor

Broke down and created a plugin to do it. Manages seeds like models and allows rake db:seed to run individual model seeds, not just a batch.

http://github.com/markwfoster/seeded-generation

Contributor

fosrias replied Oct 26, 2009

Broke down and created a plugin to do it. Manages seeds like models and allows rake db:seed to run individual model seeds, not just a batch.

http://github.com/markwfoster/seeded-generation

@rafaelp

This comment has been minimized.

Show comment
Hide comment
@rafaelp

rafaelp Jan 18, 2011

The purpose of Rails task db:seed is for "Seed once" data or "Always seed" data? Do you encourage running db:seed after deploy?

UPDATED:
DHH response on Twitter: "@rafaelp Seed data once. The first time you setup the database. Not every time you deploy. (At least that was the design directive)."
http://twitter.com/dhh/statuses/27425849821110272

rafaelp replied Jan 18, 2011

The purpose of Rails task db:seed is for "Seed once" data or "Always seed" data? Do you encourage running db:seed after deploy?

UPDATED:
DHH response on Twitter: "@rafaelp Seed data once. The first time you setup the database. Not every time you deploy. (At least that was the design directive)."
http://twitter.com/dhh/statuses/27425849821110272

Please sign in to comment.