Wednesday, January 15, 2014

Ready to deploy my Rails App but there are last minute database changes - how to make everything work.

Sometimes things happen. A last minute change to the underlying database was made a week before I was ready to deploy my Ruby on Rails app. We are talking about terrabytes of data in the database and the change was made for speedy data transformation. This change was neccessary and needed. 

I thought to document this process for future reference. 

Make sure your code is checked into a source code control:

I use git so I would ran:
    git add .
   git commit -m "Snapshot before changing database schema"
   git push origin master

Dropping the current database & loading in the new development database dump:

I am using a MySQL client called Sequel Pro.  It's easy to "delete the database", "add" the database back (with the same database name so I don't have to change my database.yml file).  Then "import" the database dump file. 

     In MySQL admin tool:

        mysql> drop database [database name];
    mysql> create database [databasename];

   Then restore the database from a database dump file:

    mysql -u root -p[root_pwd] [database name] < dumpfilename.sql 

Create a new schema.rb file in rails:
  
    rake db:schema:dump
    (you may need to run: bundle exec rake db:schema:dump)



Add, Delete or Update models:

My app reads from the database so I only needed to add new models for the new tables in the database.  For example, if we had a new histories table I created a new file in the models directory:

      projectfolder/app/models/history.rb

Edit the history.rb file and add:

    class History < ActiveRecord::Bas
    end 

The table name is plural and the model name is singular (both are lowercase titles). The object name is capitalized and singular.

Now to work on the controller  to extract data from these new tables...