viernes, 15 de junio de 2007

Rails Migrations Summary

In rails the migrations be able to keep the database schema versions, without the complicated use the DLL .

Creating and runnig Migrations

Creating

In the rails projects you can create the migrations in the db\migrate directory. The migrations are created with the model generator. For example:

application> ruby script\generate model employees

Or you can also generate a migration directly:

application> ruby script\generate migration add_adrress_column

Runnig

Migrations are run using: db:migrate rake

aaplication> rake db:migrate


Internally the migration code maintains a table called schema_info in every rails database. This table has only one column called version, with only one record. When you run rake db:migrate take the version from the table if doesn't exists will be created .

You can force the database to a specific version by supplying the VERSION=parameter to the rake db:migrate command.

application > rake db:migrate VERSION=23

In this case the migration will be executed if the version is greather than the current version in the database. But if the command line version is less than the current database version, rails look for the migration file whose number match in the database and undoes it.


Migration code example


The migrations are subclasses of the Rails class Active:Record ::Migration. The class that you create should have two methods up and down. The up method apply the schema changes for this migration, in contrast the down method unodes those changes.

Managing Tables

This is a example code:

class CreateWatches < force="">true do |t|
t.column :clv, :integer, :default=>0
t.column :description, :string
t.column :price, :decimal, :precision=>10, :scale=>2
end
end

def self.down
drop_table :watches
end
end


Note : The types supported by migrations are :binary, :boolean, :date, :datetime, :decimal,
:float, :integer, :string, :text, :time, and :timestamp.

Primary Keys

Rails assumes that every table has a numeric primary key (normally called id). In the last example we don't specific id column, however rails create it by default.

Rails really doesn't work too well unless each table has a numeric primary key. But if you want to experiment, you can start by using a different name for the primary key column(keeping it as incrementing integer). See the next example:

class CreateWatches <>
def self.up
create_table :watches, :force=>true, :primary_key=> :number do |t|
t.column :clv, :integer, :default=>0
t.column :description, :string
t.column :price, :decimal, :precision=>10, :scale=>2
end
end

def self.down
drop_table :watches
end
end


Tables with No Primary Key

Sometimes you need to declarate a table without primary key. The example is when you want to create tables with just two columns where each column is a foreign key to another table. In this case you need to tell Rails not to automatically add an id column.

create_table :sales, :id => false do |t|
t.column :customer_id, :integer, :null => false
t.column :watch_id, :integer, :null => false
end


Migrations using native SQL

Rails supply a lot of methods for Managing the database schema. However, if migrations don’t contain the methods you need to be able to do what you need to do, you’ll need to drop down to database-specific code. To do this, use the execute method.

The next example if for adding foreign key constraints to a child table.

execute "alter table line_items
add constraint fk_line_item_products
foreign key (product_id) references products(id)"








1 comentario:

arimoon dijo...

Hola, soy headhunter, ando buscando ruby on rails developer para irse un rato a San Francisco Californa, sabrĂ¡s de alguien que pueda estar interesado? Gracias!!!