Updating columns in heroku production vs local

Question!

I have a Ruby on Rails (v 4.2.0) app this is deployed to Heroku, with a PostgresQL database.

I have a number of objects in my db that I populate with my seeds.rb file:

Item.create([
  {name: "Kale", type: "veggie"},
  {name: "Apple", type: "fruit"},
  {name: "MF Kombucha", type: "drink"},
  {name: "Cider", type: "drink"},
  {name: "Carrot", type: "veggie"}
  ])

I need to add a column to my items table, :price:

rails g migration AddPriceToItems

class AddPriceToItems < ActiveRecord::Migration
  def change
    add_column :items, :price, :integer
  end
end

I then update my seeds file:

Item.create([
  {name: "Kale", type: "veggie", price: 2},
  {name: "Apple", type: "fruit", price: 1},
  {name: "MF Kombucha", type: "drink", price: 10000},
  {name: "Cider", type: "drink", price: 2},
  {name: "Carrot", type: "veggie", price: 1}
  ])

Locally, this is totally fine. I sync this new data with other data in the seeds file:

rake db:drop

rake db:create

rake db:migrate

rake db:seed

However, in production, the database now has user entered data. Dropping the database would mean dropping the user data.

Is there a way to sync the new seeds file with the production database, without having to drop the database? Ideally, this would be more efficient than heroku run rails c and manually editing all the info in my console.



Answers

You can make use of find_or_create_by. Just create another migration and loop over the seeds. As for heroku this will be a safer way to change seeds without losing production data.

class AddPriceToItemSeeds < ActiveRecord::Migration
  def change
    items= [
      {name: "Kale", type: "veggie", price: 2},
      {name: "Apple", type: "fruit", price: 1},
      {name: "MF Kombucha", type: "drink", price: 10000},
      {name: "Cider", type: "drink", price: 2},
      {name: "Carrot", type: "veggie", price: 1}
    ]
    items.each do |item|
      Item.find_or_create_by(name: item[:name], type: item[:type]) do |record|
        record.price= item[:price]
      end
    end
  end
end
By : Deepak


do this

while (i < 333) {
  b <- b + (nrow(as.data.frame(allInfo[[i]]))==0)
  i <- i + 1
 }

Rich's answer is the best.

By : Stephen


This video can help you solving your question :)
By: admin