<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-9002259897671049588</id><updated>2011-04-21T19:48:20.599-07:00</updated><title type='text'>Andreas Raphael</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://aandresrafael.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9002259897671049588/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://aandresrafael.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Andres Rafael Aguilar Albores</name><uri>http://www.blogger.com/profile/00011332856621808214</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9002259897671049588.post-5033562695040902392</id><published>2007-06-15T10:06:00.000-07:00</published><updated>2007-08-03T05:41:12.209-07:00</updated><title type='text'>Rails Migrations Summary</title><content type='html'>In rails  the migrations be able to  keep  the database  schema versions, without   the complicated use  the DLL .&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Creating and runnig Migrations&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Creating&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;In  the  rails projects   you can  create  the  migrations  in the db\migrate directory. The migrations  are created with the  model generator. For  example:&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;application&gt; ruby script\generate  model  employees&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Or  you can also generate  a migration &lt;span onclick="dr4sdgryt2(event)" style="cursor: pointer;"&gt;directly:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;application&gt; ruby script\generate  migration add_adrress_column&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Runnig&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Migrations are run using: db:migrate rake&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;aaplication&gt; rake db:migrate&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Internally  the  migration code  maintains  a table called  schema_info in every rails database. This table  has only one  column called &lt;span style="font-style: italic;"&gt;version&lt;/span&gt;, with only one  record. When  you run rake db:migrate   take  the  version from  the table  if doesn't  exists  will be created .&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;You can force the database to a specific version by supplying the VERSION=parameter to the rake db:migrate command.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;application &gt;  rake db:migrate VERSION=23&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;Migration code example&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Managing Tables&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-style: italic;"&gt;This  is  a example code:&lt;br /&gt;&lt;br /&gt;class CreateWatches &lt; force=""&gt;true  do |t|&lt;br /&gt;             t.column :clv, :integer, :default=&gt;0&lt;br /&gt;             t.column :description, :string&lt;br /&gt;             t.column :price, :decimal, :precision=&gt;10, :scale=&gt;2&lt;br /&gt;        end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.down&lt;br /&gt;            drop_table :watches&lt;br /&gt;  end&lt;br /&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-style: italic;"&gt;Note : The types supported by migrations are :binary, :boolean, :date, :datetime, :decimal,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;:float, :integer, :string, :text, :time, and :timestamp.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Primary Keys&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;br /&gt;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:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    class CreateWatches &lt;&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;      def self.up&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            create_table :watches, :force=&gt;true, &lt;span style="font-weight: bold;"&gt;:primary_key=&gt; :number&lt;/span&gt;  do |t|&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;                 t.column :clv, :integer, :default=&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;                 t.column :description, :string&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;                 t.column :price, :decimal, :precision=&gt;10, :scale=&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            end&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;      end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;      def self.down&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;            drop_table :watches&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;      end&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Tables with No Primary Key&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;create_table :sales, :id =&gt; false do |t|&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;t.column :customer_id, :integer, :null =&gt; false&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;t.column :watch_id, :integer, :null =&gt; false&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;Migrations using native SQL&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;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.&lt;br /&gt;&lt;br /&gt;The next  example  if for   adding foreign key constraints to a child table.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;execute "alter table line_items&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;add constraint fk_line_item_products&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;foreign key (product_id) references products(id)"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9002259897671049588-5033562695040902392?l=aandresrafael.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aandresrafael.blogspot.com/feeds/5033562695040902392/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9002259897671049588&amp;postID=5033562695040902392' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9002259897671049588/posts/default/5033562695040902392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9002259897671049588/posts/default/5033562695040902392'/><link rel='alternate' type='text/html' href='http://aandresrafael.blogspot.com/2007/06/rails-migrations-summary.html' title='Rails Migrations Summary'/><author><name>Andres Rafael Aguilar Albores</name><uri>http://www.blogger.com/profile/00011332856621808214</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
