Ran Seed Data on Production Mysql Database Cleardb Heroku Data randomly generated fabricator faker

568 views Asked by At

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: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "George Washington", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "John Adam", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)


Person.create(full_name: "Barack Obama", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "John Kennedy", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "Andrew Jackson", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)


Person.create(full_name: "Jimmy Carter", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
Person.create(full_name: "Gerald Ford", email: "[email protected]", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
Person.create(full_name: "Richard Nixon", email: "[email protected]", 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.

0

There are 0 answers