8.2 Script the upgrade and downgrade of a database

Learning Objectives

By the end of this lesson you will be able to:

Apply database changes

  1. Rollback changes that were previously applied to a database.
    cd /var/www/rails
    sudo rake db:rollback
  2. The example below is part of the sample application for Ruby on Rails Tutorial: Learn Rails by Example by Michael Hartl. You can also define the database schema in a script.
     
    class CreateUsers < ActiveRecord::Migration
      def self.up
        create_table :users do |t|
        t.string :name
        t.string :email
    
        t.timestamps
      end
    end
    
      def self.down
        drop_table :users
      end
    end
    			
  3. The example below is using a tool called Liquibase for database change management.
    <changeSet id="4" author="paul">
      <addColumn tableName="student">
        <column name="twitter" type="varchar(255)"/>
      </addColumn> 
    </changeSet>