Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
[[TableOfContents]] | <<TableOfContents>> |
Line 72: | Line 72: |
* [http://weblog.jamisbuck.org/2005/9/27/getting-started-with-activerecord-migrations Getting started with Migrations] * [http://garrettsnider.backpackit.com/pub/367902 Garrett Snider's cheat sheet] * [http://www.rubyrailways.com/ruby-on-rails-migrations Peter Szinek's first] and [http://www.rubyrailways.com/ruby-on-rails-migrations-reloaded/ second] posts on migrations |
* [[http://weblog.jamisbuck.org/2005/9/27/getting-started-with-activerecord-migrations|Getting started with Migrations]] * [[http://garrettsnider.backpackit.com/pub/367902|Garrett Snider's cheat sheet]] * [[http://www.rubyrailways.com/ruby-on-rails-migrations|Peter Szinek's first]] and [[http://www.rubyrailways.com/ruby-on-rails-migrations-reloaded/|second]] posts on migrations |
Contents
Actions
- create_table(name, options)
- drop_table(name)
- rename_table(old_name, new_name)
- add_column(table_name, column_name, type, options)
- rename_column(table_name, column_name, new_column_name)
- change_column(table_name, column_name, type, options)
- remove_column(table_name, column_name)
- add_index(table_name, column_name, index_type)
- remove_index(table_name, column_name)
Columns
Types
integer, float, datetime, date, timestamp, time, text, string, binary and boolean
Options
- limit
:limit => "50"
- default
:default => "blah"
- null
:null => false implies NOT NULL
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
Peter Szinek's first and second posts on migrations