Rails Study(VI)Migrations
1 Anatomy of a Migration
A primary key column called id will be added, this is the default we do not need to ask for this.
The timestamp columns created_at and updated_at which Active Record populates automatically will also be added.
1.1 Migrations are Classes
A migration is a subclass of ActiveRecord::Migration that implements two class methods:
up ----- perform the required transformations
down-- revert them
1.2 What's in a Name
1.3 Changing Migrations
db:rollback ---> db:migrate
2 Creating a Migration
2.1 Creating a Model
>rails generate model Product name:string description:text
The migration class will be created.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
or
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :products
end
end
2.2 Creating a Standalone Migration
If you are creating migrations for other purposes (for example to add a column to an existing table)
>rails generate migration AddPartNumberToProducts
or
>rails generate migration AddPartNumberToProducts part_number:string
class AddPartNumberToProduct < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
end
end
>rails generate migration RemovePartNumberFromProducts part_number:string
class RemovePartNumberFromProduct < ActiveRecord::Migration
def up
remove_column :products, :part_number
end
def down
add_column :products, :part_number, :string
end
end
>rails generate migration AddDetailsToProducts part_number:string price:decimal
class AddDetailToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_column :products, :price, :decimal
end
end
3. Writing a Migration
3.1 Creating a Table
create_table :products do |t|
t.string :name
t.text :description
t.string :nickname
t.timestamps
end
By default create_table will create a primary key called id. You can change the name of the primary key with the :primary_key option
If you don't want a primary key at all, you can pass :id => false.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products, :primary_key => "productId", :force => true,
:primary_column => {:type => :integer, :limit => 2, :scale=>3} do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
If you need to pass database specific options you can place an SQL fragment in the :options option.
create_table :products, :options => "ENGINE=BLACKHOLE" do |t|
t.string :name, :null => false
end
will append ENGINE=BLACKHOLE to the SQL statement used to create the table (when using MySQL the default is ENGINE=InnoDB).
The types supported by Active Record are
:primary_key
:string
:text
:integer
:float
:decimal
:datetime
:timestamp
:time
:date
:binary
:boolean
3.2 Changing Tables
change_table :products do |t|
t.remove :description, :name #removes the description and name columns
t.string :part_number
t.index :part_number #add index on part_number
t.rename :upccode, :upc_code
end
eq
remove_column :products, :description
remove_column :products, :name
add_column :products, :part_number, :string
add_index :products, :part_number
rename_column :products, :upccode, :upc_code
3.3 Special Helpers
create_table :products do |t|
t.references :category # one category many products
end
It will create a category_id column.
3.4 Writing Your down Method
4. Running Migrations
Running the db:migrate also invokes the db:schema:dump task, which will update our db/schema.rb file to match the structure of our database.
>rake db:migrate VERSION=20110804142910
This will run the up method on all migrations up to and including 20110804142910. If migrating downwards this will run the down method on all the migrations down to, but not including, 20110804142910.
4.1 Rolling Back
>rake db:rollback
A common task is to rollback the last migration.
>rake db:rollback STEP=3
Undo serveral migrations.
>rake db:reset # This will drop the database, recreate it and load the current schema into it.
4.2 Being Specific
>rake db:migrate:up VERSION=20110804142910
4.3 Being Talkative
To make some output in migration class.
5. Using Models in Your Migrations
5.1 Dealing with Changing Models
class AddPartNumberToProducts < ActiveRecord::Migration
class Product < ActiveRecord::Base
end
def self.up
add_column :product, :part_number, :string
Product.reset_column_information
...
end
def self.down
...
end
end
6. Schema Dumping and You
6.1 What are Schema Files for?
6.2 Types of Schema Dumps
db/schema.rb
6.3 Schema Dumps and Source Control
references:
http://guides.rubyonrails.org/migrations.html
1 Anatomy of a Migration
A primary key column called id will be added, this is the default we do not need to ask for this.
The timestamp columns created_at and updated_at which Active Record populates automatically will also be added.
1.1 Migrations are Classes
A migration is a subclass of ActiveRecord::Migration that implements two class methods:
up ----- perform the required transformations
down-- revert them
1.2 What's in a Name
1.3 Changing Migrations
db:rollback ---> db:migrate
2 Creating a Migration
2.1 Creating a Model
>rails generate model Product name:string description:text
The migration class will be created.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
or
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :products
end
end
2.2 Creating a Standalone Migration
If you are creating migrations for other purposes (for example to add a column to an existing table)
>rails generate migration AddPartNumberToProducts
or
>rails generate migration AddPartNumberToProducts part_number:string
class AddPartNumberToProduct < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
end
end
>rails generate migration RemovePartNumberFromProducts part_number:string
class RemovePartNumberFromProduct < ActiveRecord::Migration
def up
remove_column :products, :part_number
end
def down
add_column :products, :part_number, :string
end
end
>rails generate migration AddDetailsToProducts part_number:string price:decimal
class AddDetailToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_column :products, :price, :decimal
end
end
3. Writing a Migration
3.1 Creating a Table
create_table :products do |t|
t.string :name
t.text :description
t.string :nickname
t.timestamps
end
By default create_table will create a primary key called id. You can change the name of the primary key with the :primary_key option
If you don't want a primary key at all, you can pass :id => false.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products, :primary_key => "productId", :force => true,
:primary_column => {:type => :integer, :limit => 2, :scale=>3} do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
If you need to pass database specific options you can place an SQL fragment in the :options option.
create_table :products, :options => "ENGINE=BLACKHOLE" do |t|
t.string :name, :null => false
end
will append ENGINE=BLACKHOLE to the SQL statement used to create the table (when using MySQL the default is ENGINE=InnoDB).
The types supported by Active Record are
:primary_key
:string
:text
:integer
:float
:decimal
:datetime
:timestamp
:time
:date
:binary
:boolean
3.2 Changing Tables
change_table :products do |t|
t.remove :description, :name #removes the description and name columns
t.string :part_number
t.index :part_number #add index on part_number
t.rename :upccode, :upc_code
end
eq
remove_column :products, :description
remove_column :products, :name
add_column :products, :part_number, :string
add_index :products, :part_number
rename_column :products, :upccode, :upc_code
3.3 Special Helpers
create_table :products do |t|
t.references :category # one category many products
end
It will create a category_id column.
3.4 Writing Your down Method
4. Running Migrations
Running the db:migrate also invokes the db:schema:dump task, which will update our db/schema.rb file to match the structure of our database.
>rake db:migrate VERSION=20110804142910
This will run the up method on all migrations up to and including 20110804142910. If migrating downwards this will run the down method on all the migrations down to, but not including, 20110804142910.
4.1 Rolling Back
>rake db:rollback
A common task is to rollback the last migration.
>rake db:rollback STEP=3
Undo serveral migrations.
>rake db:reset # This will drop the database, recreate it and load the current schema into it.
4.2 Being Specific
>rake db:migrate:up VERSION=20110804142910
4.3 Being Talkative
To make some output in migration class.
5. Using Models in Your Migrations
5.1 Dealing with Changing Models
class AddPartNumberToProducts < ActiveRecord::Migration
class Product < ActiveRecord::Base
end
def self.up
add_column :product, :part_number, :string
Product.reset_column_information
...
end
def self.down
...
end
end
6. Schema Dumping and You
6.1 What are Schema Files for?
6.2 Types of Schema Dumps
db/schema.rb
6.3 Schema Dumps and Source Control
references:
http://guides.rubyonrails.org/migrations.html