0

I have a simple rails app hosted on heroku using their cleardb mysql add on. I was developing on mysql locally. I am having problem with seeding data on production db on heroku.

my seed data looks like this in db/seeds.rb. The app is using faker and fabrication gems. I have a user model and a person model.

Fabricator(:user) do 
  first_name { Faker::Name.first_name }
  last_name { Faker::Name.last_name }
  birthday { Faker::Date.backward(14000) }
  phone_number { Faker::PhoneNumber.phone_number }
  street { Faker::Address.street_address }
  city { Faker::Address.city }
  state { Faker::Address.state_abbr }
  zip_code { Faker::Address.zip }
  education { "Bachelor" }
  image { File.open("app/assets/images/helloworld.jpg")}
end 

Fabricator(:person) do 
  full_name { Faker::Name.name }
  email { Faker::Internet.email }
  image { File.open("app/assets/images/puppy.jpg")}
  friend_test { 0 }
  user
end 

#40.times { Fabricate(:user)} #comment this out because running the below command will generate user already
40.times { Fabricate(:person)}

Person.create(full_name: "Ben Franklin", email: "ben@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "George Washington", email: "george@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "John Adam", email: "john@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)


Person.create(full_name: "Barack Obama", email: "barack@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "John Kennedy", email: "john@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "Andrew Jackson", email: "andrew@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)


Person.create(full_name: "Jimmy Carter", email: "jimmy@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
Person.create(full_name: "Gerald Ford", email: "gerald@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
Person.create(full_name: "Richard Nixon", email: "richard@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)

Initially I thought it didn't seed correctly because I was not clearing the db correctly (since heroku don't play well with mysql) . I ran through all of these steps and I think I narrow down to the problem with the seeding action instead.

heroku run rake db:drop
heroku run rake db:create
heroku run rake db:migrate 
heroku run rake db:setup
heroku run rake db:seed #problem

when I run

heroku run rails console

and do a User.all

the seed data created User_id =1 then jump to User_id = 11 and then User_id = 21. This is the same with the Person model. This is really odd since locally when I run those commands locally I would just get user id from 1 to 40 and same with person_id from 1 to 40. My local db is fine. I have logic in the view that depends on the records to go 1 by 1 and not randomly.

also its a rails 4.1.8 app.

Updated as of Dec 8 2014

Yes I am using mysql both locally and in production, here is my database.yml file

development:
  adapter: mysql2
  encoding: utf8
  database: mini_facebook_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

staging:
  adapter: mysql2
  encoding: utf8
  database: mini_facebook_staging
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

test:
  adapter: mysql2
  encoding: utf8
  database: mini_facebook_test
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

and is

gem 'mysql2', '~> 0.3.17'

I don't have production set here. I am using heroku config and from heroku docs, there will be a database.yml generated anyway.

Jngai1297
  • 2,415
  • 5
  • 29
  • 63
  • This is a good question answered w all the different rake db options: http://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload In your case I think you want rake db:reset – Corey Dec 06 '14 at 01:46
  • sorry that didn't work and I updated the question. – Jngai1297 Dec 06 '14 at 02:12
  • Are you using the same version of mysql locally and from cleardb? Something else that has bit me in the past with Heroku Postgres (which I know you aren't using but the issue may be the same) is that I can't actually drop and create the database. Their permissions won't let me do it and it is likely that cleardb won't either. If you are running a reset, the first two steps are to drop and create so that may be what is biting you. – Paul Elliott Dec 07 '14 at 01:06
  • Guessing it won't do anything, but the Heroku docs say "After running a migration you’ll want to restart your app with heroku restart to reload the schema and pickup any schema changes." As @PaulElliott laid out, reset doesn't work. Another idea is taking your fabricators out of the seed file and the line that creates 40, and manually creating 10 seed users and see if they increment properly. That will reduce variables in this file and potentially lead you to the underlying issue. I usually put my fabricators in spec/fabricators/model_fabricator.rb, not sure if that matters. – Corey Dec 07 '14 at 01:41
  • The seeds file is independent of a reset. Running reset runs setup, which runs the seed automatically. Running the seed does not run any other tasks though. You may want to try invoking `database_cleaner` manually at the beginning of your `seeds.rb` and just running `rake db:seed` in your staging environment instead of `rake db:reset`. Even in dev if you run `rake db:reset`, the `database_cleaner` operation just won't have any effect. – Paul Elliott Dec 07 '14 at 13:09
  • @PaulElliott I am using mysql2, and using mysql2 both locally and on heroku production. I made sure to set the cleardb database url with mysql2. I can drop the cleardb db doing a `heroku run rake db:drop`. I then try to do `heroku run rails console` and can confirmed that the db no longer exist and is waiting for me to recreate it. – Jngai1297 Dec 08 '14 at 16:05
  • @Corey yes `heroku restart` didn't do the trick after I `drop`, `create` , `migrate` and `seed` on heroku all over again. I know that fabricators are for specs but I cheated a little since I don't want to manually write the queries on seed file to seed production db. Don't forget that I am trying to cleardb production and not a testing environment with fabricators. – Jngai1297 Dec 08 '14 at 16:07

0 Answers0