<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Lucid &#187; migrations</title>
	<atom:link href="http://thelucid.com/tag/migrations/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelucid.com</link>
	<description>The Lightweight Ramblings of Jamie Hill</description>
	<lastBuildDate>Sun, 11 Mar 2012 19:52:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Maintaining database column order with migrations</title>
		<link>http://thelucid.com/2006/09/30/maintaining-database-column-order-with-migrations/</link>
		<comments>http://thelucid.com/2006/09/30/maintaining-database-column-order-with-migrations/#comments</comments>
		<pubDate>Sat, 30 Sep 2006 18:24:00 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[migrations]]></category>

		<guid isPermaLink="false">http://d5459de2-4ee7-4d52-9a8a-b967ccbdeec7</guid>
		<description><![CDATA[If you&#8217;re anything like me, you like to keep database columns in a reasonably logical order. In my case I generally keep primary keys as the first column, then content columns, then special usage columns, then foreign keys e.g. id name description created_on updated_on account_id Now, say I added a &#8216;slug&#8217; column with a migration: [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re anything like me, you like to keep database columns in a reasonably logical order. In my case I generally keep primary keys as the first column, then content columns, then special usage columns, then foreign keys e.g.</p>
<pre>
id
name
description
created_on
updated_on
account_id
</pre>
<p>Now, say I added a &#8216;slug&#8217; column with a migration:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
  add_column <span style="color:#996600;">&quot;projects&quot;</span>, <span style="color:#996600;">&quot;slug&quot;</span>, :<span style="color:#CC0066; font-weight:bold;">string</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That&#8217;s fine, I have my new column but it&#8217;s after my foreign keys:</p>
<pre>
id
name
description
created_on
updated_on
account_id
slug
</pre>
<p>What&#8217;s it doing down there? I want it after the &#8216;name&#8217; column! &#8230;wouldn&#8217;t it be nice if you could specify an <code>:after</code> option for <code>add_column</code>.</p>
<p>We can make this possible by monkey patching the add_column method at the top of our migration file (I will make this into a plugin when I get time):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">module</span> ConnectionAdapters <span style="color:#008000; font-style:italic;"># :nodoc:</span>
    <span style="color:#9966CC; font-weight:bold;">module</span> SchemaStatements
      <span style="color:#9966CC; font-weight:bold;">def</span> add_column<span style="color:#006600; font-weight:bold;">&#40;</span>table_name, column_name, type, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        add_column_sql = <span style="color:#996600;">&quot;ALTER TABLE #{table_name} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}&quot;</span>
        after_column = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:after</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        add_column_options!<span style="color:#006600; font-weight:bold;">&#40;</span>add_column_sql, options<span style="color:#006600; font-weight:bold;">&#41;</span>
        add_column_sql <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; AFTER #{after_column}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> after_column
        execute<span style="color:#006600; font-weight:bold;">&#40;</span>add_column_sql<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This lets us do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
  add_column <span style="color:#996600;">&quot;projects&quot;</span>, <span style="color:#996600;">&quot;slug&quot;</span>, :<span style="color:#CC0066; font-weight:bold;">string</span>, <span style="color:#ff3333; font-weight:bold;">:after</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'name'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Which gives us:</p>
<pre>
id
name
slug
description
created_on
updated_on
account_id
</pre>
<p>Much better&#8230; this may seem a little petty, however as your tables get more and more columns it makes things much easier to follow.</p>
<p>Please note: I have only tested this with MySQL</p>
<p>h4. Update: this is now a plugin and can be &#8220;downloaded here&#8221;:http://svn.soniciq.com/public/rails/plugins/iq_migration_extensions/</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2006/09/30/maintaining-database-column-order-with-migrations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

