Heroku: How to push seeds.rb to existing rails app?
Asked Answered
A

5

22

I store all my app's data in seeds.rb locally. However, after I pushing everything to Heroku, the app works well, but without any data. I do not want to re-input the mass data again, so does anyone have any ways to help me?

Antenna answered 23/4, 2014 at 1:49 Comment(2)
Is there a problem pushing seeds.rb to heroku?Indeterminable
@Indeterminable I am very new to rails app and heroku. I am not sure if the seeds.rb has been pushed to heroku or not. Do you mean that in normal case, the seeds.rb should be pushed to heroku automiticatlly by: 'git push heroku master' and 'heroku run rake db:migrate' without any more action?Antenna
I
51

If you push the app to heroku, you can seed the database with the following command.

heroku run rake db:seed
Indeterminable answered 23/4, 2014 at 2:15 Comment(1)
rake aborted! No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)Sciuroid
V
24

If you have changed migrations then first you need to do is run migration

heroku run rake db:migrate

then

heroku run rake db:seed

If you don't have any data in database the I would suggest following is, But caution it will remove all current data from heroku database.

heroku run rake db:setup

Hope this helps you

Variant answered 23/4, 2014 at 2:36 Comment(0)
K
0

you have to be sure no migration is pending if no pending

just do

heroku run rake db:migrate

and it will word perfectly

Kind answered 27/11, 2018 at 9:32 Comment(0)
L
0

I'll just add, because I haven't seen it yet. heroku run rails db:seed Will accomplish the same task.

Louvenialouver answered 7/12, 2020 at 19:13 Comment(0)
D
0

The accepted answer of

heroku run rake db:seed

certainly used to work. However, as of May 2024 with Rails-7.0.8 and Ruby-3.1, it failed in my case and in a silent manner with no messages. It did not print out anything on the terminal (which my seeding script should even in a successful run), nor created any DB entries. Then I found

heroku run rails db:seed

worked as it should, like a charm!

Just don't forget DB migratons prior to seeding with:

heroku run rake db:migrate:status  # to see the current migration status
heroku run rake db:migrate

Note

Interestingly, when I tried the rake command when there were still pending DB migrations, it issued a warning message like

You have 2 pending migrations:
  20240423111111 AddChannelToPosts
  20240514111111 CreatePostAssocs
Run `bin/rails db:migrate` to update your database then try again.

So, the heroku ran rake db:seed command must have worked to some degree. However, for some reason it did not complete its job… Possibly my seeding script contains something the (old) rake does not handle well? In my local environments, I haven't used rake at all since Rails 5, and exclusively use bin/rails. It is good heroku accepts heroku run rails .

Drogin answered 17/5 at 3:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.