Forums/Cloud Foundry Forums/CloudFoundry.com Q&A

Answered

Anyway to run 'rake db:seed' for rails app?

Adam
asked this on May 29, 2011 11:56

I'm working my way through the Depot app from 'Agile Web Development with Rails'.  I've figured out how to deploy to Cloud Foundry (finally got all my gem dependencies right and performed a 'bundle package' prior to 'vmc update...').

Now I'm wondering if there's anyway to populate the sample data, (in my db/seeds.rb file) from my development environment to cloud foundry.  This would typically be done using 'rake db:seed'. Anyway to have this command run against Cloud Foundry?

Thanks,

 

Comments

User photo
Michael Latta

You could try invoking `rake db:seed` from within your app on startup if the database is empty.  This could be put in an initializer.  Just a guess, my invite has not come through yet.

May 29, 2011 15:10
User photo
Glenn Oppegard
Cloud Foundry

For example:

$ vim config/initializers/data_loader.rb

if Product.count.zero?

  load "#{RAILS_ROOT}/Rakefile"

  Rake::Task['db:seed'].invoke

end

 

Does that answer your question?

May 29, 2011 23:50
User photo
Adam

Thank you both, that did answer my question.  The data loader approach worked.  

It would be nice if there were some way of issuing rake commands through the vmc application, kind of like in heroku:

http://devcenter.heroku.com/articles/rake

May 30, 2011 05:21
User photo
Brian Wu

I had some issues doing that. The application could load perfectly without the data loader but as soon as I introduced the data loader I ran into problems.

 

From terminal:

...

Stopping Application: OK

Staging Application: OK

Starting Application: .......Error: Application 'meddiesite's state is undetermined, not enough information available.

 

Were there any require's that were needed in the Rakefile? I might have been missing something else. My application is on github... (https://github.com/bwu/AcapocoNewbieRailsSite ). I didn't include the data_loader.rb file since it wouldn't load to cloud foundry with it.

August 18, 2011 16:45
User photo
Glenn Oppegard
Cloud Foundry

Hi Brian,

After testing your application and looking at output from 'vmc files <appname> logs/migration.log', I found that the DB migrations haven't finished running before data_loader.rb is called. In migration.log you'll see an error about the table current_members not existing. There are a couple options:

1. Add the seed data to the relevant migration. For example, add the following in db/migrate/20110722213133_create_current_members.rb after create_table:

CurrentMember.create(:first_name => 'Foo',
:last_name => 'Bar',
:year => 2013,
:bio =>
%{<p> This is a test</p>},
:image_url => '/images/foobar.jpg')

 

2. Create an action to seed the DB from a URL.

Add to app/routes.rb:

match 'seed' => 'application#seed'

Add to app/controllers/application_controller.rb:

def seed
load "#{RAILS_ROOT}/Rakefile"
Rake::Task['db:seed'].invoke
redirect_to('/home/index')
end

Then point your browser to appname.cloudfoundry.com/seed.

August 22, 2011 15:25
User photo
Brian Wu

Thanks for your help. I added all of my data in the migrations and everything is running smoothly.

I didn't get the 2nd option to work because of two before_filters that I put in the application_controller. With a little more troubleshooting I'm sure it would have worked fine, but I wanted the databases to be full upon launching.

Thanks again. Cloud foundry is great.

August 23, 2011 05:28
User photo
Thomas Boltze

Just ran into the same issue. I make extesnsive use of seeding database during testing and also for productions / demo sites. 

It would be really nice to have control over that from the vmc command line, e.g.

vmc rake app_name db:seed

or something like that, so I don't have to write a controller, just to be able to run rake tasks.... 

September 20, 2011 07:11
User photo
Glenn Oppegard
Cloud Foundry

Hi Thomas,

Cloud Foundry Engineering is aware of the issue, but we don't have an ETA yet on when this might be available. Stay tuned.

September 20, 2011 10:32
User photo
Thomas Boltze

Thanks Glenn,

Looking forward to the fix.

Thomas

September 20, 2011 21:41
User photo
John Eberly

I have the same question.  Trying to get a rails app working where we use sunspot gem with solr.  Need to do rake sunspot:solr:reindex in different environments.

December 10, 2011 14:34
User photo
Glenn Oppegard
Cloud Foundry
Ajax_loader_small Answer

To seed your DB, we recommend using Caldecott to set up a tunnel from your local dev machine to the remote Cloud Foundry service: http://blog.cloudfoundry.com/post/12928974099/now-you-can-tunnel-in...

Once the tunnel is established, you'll be given connection information that you can supply to your app's DB config, and tasks like 'rake db:seed' should work.

December 13, 2011 19:52