LHM migrations

What is LHM?

  • LHM is a MYSQL schema migration for Rails

How do you create a migration?

  • Rails guide to migrations

Example of a migration

bin/rails generate migration AddCountryOriginAndHarvestDateToFruits country_origin:string, harvest_date:string
  • This will add two columns to the fruits table called country_origin and harvest_date
  • Both of these columns will be string basic data types

Migration created

class AddCountryOriginAndHarvestDateToFruits < ActiveRecord::Migration
  def self.up
    Lhm.change_table :fruits do |m|
      m.add_column :country_origin, :string
      m.add_column :harvest_date, :string

  def self.down
    Lhm.change_table :fruits do |m|
      m.remove_column :country_origin, :string
      m.remove_column :harvest_date, :string

Things to remember

  • There are NO commas at the end of lines where we are invoking #add_column and #remove_column
    • If you do include a comma, there will be errors when you try to run the migration
  • If you run into migration errors, it’s always good to reset your database and then run the migration
    • ruby bundle exec rake db:reset

Now migrate it locally

bundle exec rake db:migrate

Migration done

  • All done!