PouriaSterling/ShowMark

Don't lose production data on schema changes

Closed this issue · 3 comments

Come up with a way to update existing data with database changes instead of deleting and starting again! This is very important because the application is actually functional as of now, but since new functionality can only be used by deleting and starting again, I'm not actually using it yet.

In the same vein, if I change validations logic for something such as which image to use, there should be a way to update all current database entries with it. This may require refactoring or moving the application logic to somewhere better.

Say I've added a new column to my shows model called "show_id" and i want to update all existing database entries with a non-nil show_id. I can define a method in the show.rb model as,

def addShowID
   res = Tmdb::Search.tv(self.name)
   self.update(show_id: res.results[0].id)
end

and then in the rails console (only tested on cloud9 so far), I can write (Note that it has to be a one-liner because of the way rails console works),

Show.all.each {|show| show.addShowID}

which will update all the database objects without losing any information

Note the following:

  • update_attributes = assign_attributes + save
  • attributes= = alias of assign_attributes
  • update = alias of update_attributes

Only deliberately save attributes when you want.

This issue was predominantly regarding information gathering, so it is considered resolved now