Actions

Columns

Types

integer, float, datetime, date, timestamp, time, text, string, binary and boolean

Options

Steps to start a new database

Create database using DDL

> mysql -u root -p

mysql> create database myproject_development;
mysql> create database myproject_test;
mysql> create database myproject_production;
mysql> grant all on myproject_development.* to 'railsdev'@'localhost';
mysql> grant all on myproject_test.* to 'railsdev'@'localhost';
mysql> grant all on myproject_production.* to 'prod'@'localhost' identified by 'username';
exit

Edit config/database.yml to match

Generate initial migration script

> ruby script/generate migration initial

This creates db/migrate/001_initial.rb

Edit db/migrate/001_initial.rb

class Initial < ActiveRecord::Migration
  def self.up
    create_table :products do |table|
      table.column :title,       :string, :limit => 100, :null => false      
      table.column :description, :text,                  :null => false      
      table.column :image_url,   :string, :limit => 200, :null => false
      table.column :price,       :float,                 :null => false
    end
  end
  
  def self.down
    drop_table :products
  end
end

Run the migration

> rake db:migrate

or

> rake db:migrate version=1

See Also


CategoryCheatSheet

iDIAcomputing: RailsMigrationsCheatSheet (last edited 2009-07-27 18:25:11 by localhost)