Title: Some TextMate snippets for Rails Migrations
Date: February 18, 2006
Timestamp: 1140331680
Author: sjs
Tags: textmate, rails, hacking, rails, snippets, textmate
----


  <a href="http://web.archive.org/web/20060615034816/http://sami.samhuri.net/admin/content/edit/10" class="admintools" id="admin_article" style="display: none;">edit</a>
  <h2>Some TextMate snippets for Rails Migrations</h2>
  <p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
  <span class="typo_date" title="Sun, 19 Feb 2006 06:48:00 GMT">on Saturday, February 18, 2006</span></p>
  <p>My arsenal of snippets and macros in TextMate is building as I read through the rails canon, <a href="http://web.archive.org/web/20060615034816/http://www.pragmaticprogrammer.com/titles/rails/" title="Agile Web Development With Rails">Agile Web Development…</a> I’m only 150 pages in so I haven’t had to add much so far because I started with the bundle found on the <a href="http://web.archive.org/web/20060615034816/http://wiki.rubyonrails.org/rails/pages/TextMate">rails wiki</a>. The main ones so far are for migrations.</p><p>Initially I wrote a snippet for adding a table and one for dropping a table, but I don’t want to write it twice every time! If I’m adding a table in <strong>up</strong> then I probably want to drop it in <strong>down</strong>.</p>

<p>What I did was create one snippet that writes both lines, then it’s just a matter of cut &amp; paste to get it in <strong>down</strong>. The drop_table line should be inserted in the correct method, but that doesn’t seem possible. I hope I’m wrong!</p>

<p>Scope should be <em>source.ruby.rails</em> and the triggers I use are above the snippets.  </p>

<p>&nbsp;</p>

<p>mcdt: <strong>M</strong>igration <strong>C</strong>reate and <strong>D</strong>rop <strong>T</strong>able</p>

<pre><code>create_table "${1:table}" do |t|
    $0
end
${2:drop_table "$1"}
</code></pre>

<p>mcc: <strong>M</strong>igration <strong>C</strong>reate <strong>C</strong>olumn</p>

<pre><code>t.column "${1:title}", :${2:string}
</code></pre>

<p>marc: <strong>M</strong>igration <strong>A</strong>dd and <strong>R</strong>emove <strong>C</strong>olumn</p>

<pre><code>add_column "${1:table}", "${2:column}", :${3:string}
${4:remove_column "$1", "$2"}
</code></pre>

<p>I realize this might not be for everyone, so here are my original 4 snippets that do the work of <em>marc</em> and <em>mcdt</em>.</p>

<p>&nbsp;</p>

<p>mct: <strong>M</strong>igration <strong>C</strong>reate <strong>T</strong>able</p>

<pre><code>create_table "${1:table}" do |t|
    $0
end
</code></pre>

<p>mdt: <strong>M</strong>igration <strong>D</strong>rop <strong>T</strong>able</p>

<pre><code>drop_table "${1:table}"
</code></pre>

<p>mac: <strong>M</strong>igration <strong>A</strong>dd <strong>C</strong>olumn</p>

<pre><code>add_column "${1:table}", "${2:column}", :${3:string}
</code></pre>

<p>mrc: <strong>M</strong>igration <strong>R</strong>remove <strong>C</strong>olumn</p>

<pre><code>remove_column "${1:table}", "${2:column}"
</code></pre>

<p>I’ll be adding more snippets and macros. There should be a central place where the rails bundle can be improved and extended. Maybe there is…</p>

