1st Dec, 2007

Rails DB Migaration

Rails ActiveRecordMigration manages changes to a database schema. Migrations allows a developer to manage rollout, and rollback, of database schema changes in a controlled and consistent manner.

Sample Script:

class SampleMigration < ActiveRecord::Migration

def self.up

end

def self.down

end

end

Within this migration’s up method, you’d place all of the code necessary to make one logical database change. You’d also capture any change to undo any schema changes. By encapsulating up and down, Rails development and production tools can automate the process of deploying and backing out any change involving persistent object models. These database changes can include:

* Adding or dropping any new tables.

* Adding or dropping any new columns.

* Changing the database in other ways, including adding, dropping, or modifying indexes or other constraints.

* Modifying database data.

Creating the migration

ruby script/generate migration migration_name

create_table

Creates a table on the database.

create_table :table_name, { options } do |t|

t.column :column_name, :column_type, :options

end

:force => true - forces drop of an existing table of the same name before creation the new one

:id => false - defines a table with no primary key, for example when you need to define a join table

:primary_key => :new_primary_key_name - overrides the default name of :id for the primary column, use this to specify the name of the column in the database that Rails will use to store the primary key

:options => “” - e.g. “auto_increment = 1000″

add_column

Creates a new column on the specified table.

add_column :table_name, :column_name, :column_type, { options }

:null => true or false - if false, the underlying column has a not null constraint added by the database engine

:limit => size - set a limit on the size of the field

:default => value - set a default value for the column

remove_column

Removes column from the specified table.

remove_column :table_name, :column_name

add_index

Creates an index for the specified table, the name of which defaults to table_column_index.

add_index :table_name, :column_name, :unique => true, :name => “chosen_index_name”

change_column

Change the data type of the specified column

change_column :table_name, :column_name, :new_type, { options as add_column }

rename_table

Renames the specified table.

rename_table :new_table_name, :old_table_name

rename_column

Renames the old_column_name to new_column_name

rename_column :old_column_name, :new_column_name

rename_table

Renames the specified table.

rename_table :new_table_name, :old_table_name

remove_index

Remove an index for the specified table.

remove_index :table_name, :column_name

execute

Execute SQL command directly

execute “alter table expenses_tags add constraint expense_fk foreign key (expense_id) references expenses(id)”

execute “alter table expenses_tags add constraint tags_fk foreign key (tag_id) references tags(id)”

execute “alter table expenses engine=innodb”

execute “Insert into tags(name) values (’restaurant’);”

Mapping

:binary – blob

:boolean – tinyint(1)

:date – date

:datetime – datetime

:decimal – decimal

:float – float

:integer – int(11)

:string – varchar(255)

:text – text

:time – time

:timestamp – datetime

To run all unapplied migrations

rake db:migrate

To migrate database to specific version

rake db:migrate VERSION=18

To use migrations in development,testing or production databases

rake db:migrate RAILS_ENV=production

Create a db/schema.rb file that can be portably used against any database supported by ActiveRecord

rake db:schema:dump

Load a schema.rb file into the database

rake db:schema:load

Example code

1) class CreateExpenses < ActiveRecord::Migration

def self.up

create_table :expenses, :id => false, :options => “auto_increment = 10000″ do |t|

t.column :amount, :float, :null => false, :limit => 10, :default => 1234

t.column :updated_at, :datetime

end

add_column :expenses, :book, :primary_key

add_index :expenses, :updated_at, :unique => true, :name => “updates”

end

def self.down

drop_table :expenses

end

end

2) class AlterExpenses < ActiveRecord::Migration

def self.up

remove_column :expenses, :book

add_column :expenses, :id, :primary_key

execute “alter table expenses_tags add constraint expense_fk foreign key (expense_id) references expenses(id)”

execute “alter table expenses_tags add constraint tags_fk foreign key (tag_id) references tags(id)”

execute “alter table expenses engine=innodb”

end

def self.down

remove_column :expenses, :id

add_column :expenses_tags, :book, :primary_key

end

end

3) class InsertTags < ActiveRecord::Migration

def self.up

execute “Insert into tags(name) values (’food’);”

execute “Insert into tags(name) values (’restaurant’);”

execute “Insert into tags(name) values (’lodging’);”

end

def self.down

end

end

Leave a response

Your response:

Categories and Tags

Advertising