<?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>John&#039;s Tidbits &#187; Programming</title>
	<atom:link href="http://inodes.org/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://inodes.org</link>
	<description>Moo - Development, Trouble-shooting and Random thoughts...</description>
	<lastBuildDate>Thu, 19 Apr 2012 10:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Taking your cucumber tests back to the future with Delorean</title>
		<link>http://inodes.org/2010/03/31/taking-your-cucumber-tests-back-to-the-future-with-delorean/</link>
		<comments>http://inodes.org/2010/03/31/taking-your-cucumber-tests-back-to-the-future-with-delorean/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 02:28:35 +0000</pubDate>
		<dc:creator>johnf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[chronic]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[delorean]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[vquence]]></category>

		<guid isPermaLink="false">http://inodes.org/?p=241</guid>
		<description><![CDATA[I&#8217;m currently working on an API for Vquence&#8217;s VQdata product which allows our customers to use a REST interface to retrieve videos with certain keywords they have previously stored. While writing tests I need to be able to mock out the Time object so that my tests were deterministic relative to time. I remembered listening [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on an API for Vquence&#8217;s VQdata product which allows our customers to use a REST interface to retrieve videos with certain keywords they have previously stored. While writing tests I need to be able to mock out the Time object so that my tests were deterministic relative to time.</p>
<p>I remembered listening to a <a href="http://ruby5.envylabs.com/episodes/56-episode-54-february-26-2010">Ruby5 podcast</a> which mentioned a great little gem called <a href="http://github.com/bebanjo/delorean">Delorean</a>.</p>
<p>Delorean easily allows you to mock time in your tests. In no time I had hooked it up to cucumber.</p>
<p>In features/support/delorean.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'delorean'</span>  
&nbsp;
<span style="color:#008000; font-style:italic;"># Make sure we fix the time up after each scenario</span>
After <span style="color:#9966CC; font-weight:bold;">do</span>
  Delorean.<span style="color:#9900CC;">back_to_the_present</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>and then in features/step_definitions/delorean_steps.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^I time travel to <span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>period<span style="color:#006600; font-weight:bold;">|</span>
  Delorean.<span style="color:#9900CC;">time_travel_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>period<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>this lets me create steps like</p>

<div class="wp_syntax"><div class="code"><pre class="cucumber" style="font-family:monospace;">  Scenario: Link attributes are correct for yesterday
    Given I time travel to 2010-02-01 05:00
    When I GET the videos keywords feeds page
    Then I should see &quot;start_time=2010-02-01T00:00:00&quot;</pre></div></div>

<p>Some other examples you can use with Delorean are</p>
<ul>
<li> 2 weeks ago
  </li>
<li> tomorrow
  </li>
<li> next tuesday 5pm
</li>
</ul>
<p>You can find more examples in the <a href="http://chronic.rubyforge.org/">Chronic gem documentation</a> which Delorean uses to achieve this functionality. </p>
]]></content:encoded>
			<wfw:commentRss>http://inodes.org/2010/03/31/taking-your-cucumber-tests-back-to-the-future-with-delorean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing the type on a legacy table in ActiveRecord</title>
		<link>http://inodes.org/2009/07/14/changing-the-type-on-a-legacy-table-in-activerecord/</link>
		<comments>http://inodes.org/2009/07/14/changing-the-type-on-a-legacy-table-in-activerecord/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 03:04:40 +0000</pubDate>
		<dc:creator>johnf</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://inodes.org/blog/?p=150</guid>
		<description><![CDATA[I&#8217;m doing some work for a client which involves extracting some data from a legacy database and displaying it in a web interface. One of the fields in the table is the number of megabytes included in the quota. For some crazy reason this is defined as follows: CREATE TABLE quota &#40; bandwidth_in_included DECIMAL&#40;8,2&#41; &#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m doing some work for a client which involves extracting some data from a legacy database and displaying it in a web interface. One of the fields in the table is the number of megabytes included in the quota. For some crazy reason this is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> quota <span style="color: #FF00FF;">&#40;</span>
  bandwidth_in_included <span style="color: #999900; font-weight: bold;">DECIMAL</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">8</span><span style="color: #000033;">,</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>This means that in the web interface I get <strong>10,000.0 MB</strong> instead of <strong>10,000 MB</strong>. Notice the decimal point. Also I wanted bytes rather than MB since the legacy app was a bit all over the place in this regard.</p>
<p>My first solution was to simple create a virtual attribute in the model to override the type.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Quota <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># We need it as an int and in bytes</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> bandwidth_in_included
    attributes<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bandwidth_in_included].to_i * 1000
  end
&nbsp;
end</span></pre></div></div>

<p>This worked great except that I&#8217;m actually rendering the data to XML to be accessed over a REST service so this was generating XML elements like</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bandwidth_in_included</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;BigDecimal&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>10000<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bandwidth_in_included<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Eventually I discovered that you can tell ActiveRecord to override the type, so I ended up with</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Quota <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># We want to treat the bandwidth_included a an integer</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> columns_hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bandwidth_in_included'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> type
      <span style="color:#ff3333; font-weight:bold;">:integer</span>
    <span style="color:#9966CC; font-weight:bold;">end</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span> 
&nbsp;
  <span style="color:#008000; font-style:italic;"># We need it as an int and in bytes</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> bandwidth_in_included
    attributes<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bandwidth_in_included].to_i * 1000
  end
&nbsp;
end</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://inodes.org/2009/07/14/changing-the-type-on-a-legacy-table-in-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>bzr-svn and svn revisions</title>
		<link>http://inodes.org/2008/06/14/bzr-svn-and-svn-revisions/</link>
		<comments>http://inodes.org/2008/06/14/bzr-svn-and-svn-revisions/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 09:19:26 +0000</pubDate>
		<dc:creator>johnf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bzr]]></category>
		<category><![CDATA[revision]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://inodes.org/blog/?p=74</guid>
		<description><![CDATA[I was updating an svn branch today using bzr, thanks to bzr-svn, and I wanted to know what svn revision I was at. You can easily see the bzr revision by running johnf@zoot:~/trunk$ bzr revno 34 But it gives no indication of where you are in SVN land. After a bit of rummaging around I [...]]]></description>
			<content:encoded><![CDATA[<p>I was updating an svn branch today using bzr, thanks to bzr-svn, and I wanted to know what svn revision I was at.</p>
<p>You can easily see the bzr revision by running</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">johnf<span style="color: #000000; font-weight: bold;">@</span>zoot:~<span style="color: #000000; font-weight: bold;">/</span>trunk$ bzr revno
<span style="color: #000000;">34</span></pre></div></div>

<p>But it gives no indication of where you are in SVN land. After a bit of rummaging around I discovered the following</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">johnf<span style="color: #000000; font-weight: bold;">@</span>zoot:~<span style="color: #000000; font-weight: bold;">/</span>trunk$ bzr version-info
revision-id: svn-v3-trunk0:90e61fa5-<span style="color: #000000;">4541</span>-0410-a685-e5b9dba3c764:trunk:<span style="color: #000000;">74</span>
<span style="color: #c20cb9; font-weight: bold;">date</span>: <span style="color: #000000;">2008</span>-05-<span style="color: #000000;">29</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">24</span>:<span style="color: #000000;">44</span> +0000
build-date: <span style="color: #000000;">2008</span>-06-<span style="color: #000000;">14</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">10</span>:<span style="color: #000000;">59</span> +<span style="color: #000000;">1000</span>
revno: <span style="color: #000000;">34</span>
branch-nick: trunk</pre></div></div>

<p>The <strong>revision-id</strong> field seems to be the key and seems to indicate I&#8217;m using SVN revision 74. Checking the branch via the web confirmed that.</p>
]]></content:encoded>
			<wfw:commentRss>http://inodes.org/2008/06/14/bzr-svn-and-svn-revisions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails, ActiveRecord, MySQL, GUIDs and the rename_column bug</title>
		<link>http://inodes.org/2008/01/11/rails-activerecord-mysql-guids-and-the-rename_column-bug/</link>
		<comments>http://inodes.org/2008/01/11/rails-activerecord-mysql-guids-and-the-rename_column-bug/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 10:25:01 +0000</pubDate>
		<dc:creator>johnf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[guid]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[vquence]]></category>

		<guid isPermaLink="false">http://inodes.org/blog/2008/01/11/rails-activerecord-mysql-guids-and-the-rename_column-bug/</guid>
		<description><![CDATA[Since I wasted over 4 hours of my life today working my way through this problem I feel the need to share. Since it seems to be the in thing in the Web 2.0 space, just to be cool, we use GUIDs to identify different objects in our URLs at Vquence. For example my randomly [...]]]></description>
			<content:encoded><![CDATA[<p>Since I wasted over 4 hours of my life today working my way through this problem I feel the need to share.</p>
<p>Since it seems to be the in thing in the Web 2.0 space, just to be cool, we use <a href="http://en.wikipedia.org/wiki/Globally_Unique_Identifier">GUIDs</a> to identify different objects in our URLs at <a href="http://vquence.com">Vquence</a>. For example my randomly created vquence on on <a href="http://www.vqslices.com/vq/cDuIhGWb8r3lDxaby-aaea">Rails</a> has a GUID of</p>
<pre>cDuIhGWb8r3lDxaby-aaea</pre>
<p>Andy Singleton has written a rails plugin called funnily enough <a href="http://tools.assembla.com/breakout/wiki/FreeSoftware">guid</a>. This allows you to do the following in your model.</p>
<pre class="ruby">class Vquence &lt; ActiveRecord::Base
  usesguid :column =&gt; 'guid'
end</pre>
<p>Once you do this you will automatically get GUID looking identifiers in the db and your application. The <strong>guid</strong> column in the DB gets mapped to <strong>Vquence.id</strong> so you can do things like</p>
<pre class="ruby">Vquence.find('cDuIhGWb8r3lDxaby-aaea');</pre>
<p>We used to use <a href="http://www.sphinxsearch.com/">Sphinx</a> as our search index, we now use <a href="http://lucene.apache.org/">Lucene</a>. Sphinx requires that you have an integer id for each document in your index. This is to make your SQL queries much faster. The dumb way to create your index is to use queries like the following.</p>
<pre class="sql">SELECT * FROM videos LIMIT 0,10000
SELECT * FROM videos LIMIT 10000,10000
...
SELECT * FROM videos LIMIT 990000,10000</pre>
<p>I know this as its what we originally used with Lucene. This works fine until you reach about 1,000,000 rows. The problem is that since there is no implicit ordering or range in the above query it means that for the final query MySQL needs to workout what the first 1,000,000 rows are and then return you the last 10,000.</p>
<p>A much better way to do it is the following</p>
<pre class="sql">SELECT * FROM videos WHERE integer_id &gt;= 1 and integer_id &lt; = 10000
SELECT * FROM videos WHERE integer_id &gt;= 10001 and integer_id &lt; = 20000
...
SELECT * FROM videos WHERE integer_id &gt;= 990000 and integer_id &lt; = 1000000</pre>
<p>This is fast as long as <strong>integer_id</strong> is indexed.</p>
<p>So to accommodate this in Rails we began using migrations like the following.</p>
<pre class="ruby">class Videos &lt; ActiveRecord::Migration
  def self.up
    create_table :videos do |t|
      t.column :uuid, :string, :limit =&gt;22, :null =&gt; false
      ...

      t.timestamps
    end
    add_index :videos, :uuid, :unique =&gt; true
    rename_column :videos, :id, :integer_id
  end

  def self.down
    drop_table :videos
  end
end</pre>
<p>This was all done months ago and the repercussions didn&#8217;t rear their ugly head until today. Previously everything in the videos table had been created by our external crawler and Rails never needed to insert into the table. Today I wrote some code that inserted into the videos table and everything broke horribly.</p>
<p>The problem is that ActiveRecord can still see the <strong>integer_id</strong> field and tries to insert a 0 value into it. It isn&#8217;t clever enough to realise that it is an auto increment field and to leave it alone. After some help from <em>bitsweat</em> on #RoR I implemented a dirty hack to hide the <strong>integer_id</strong> column from ActiveRecord. Thanks to Ruby overriding the ActiveRecord internals is really easy and I added the following to our guid plugin.</p>
<pre class="ruby">  # HACK (JF) - This is too evil to even blog about
  # When we use guid as a primary key we usually rename the original 'id'
  # field to 'integer_id'. We need to hide this from rails so it doesn't
  # mess with it. WARNING: This means once you use usesguid anywhere you can
  # never access a column in any table anywhere called 'integer_id'

class ActiveRecord::Base
  private
    alias <img src='http://inodes.org/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> riginal_attributes_with_quotes :attributes_with_quotes

    def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true)
      quoted = original_attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true)
      quoted.delete('integer_id')
      quoted
    end
end</pre>
<p>So this worked like a charm and after 4 hours I thought my pain was over, but then I tried to add second row to my test database. This resulted in the following.</p>
<pre> Mysql::Error: Duplicate entry '0' for key 1: INSERT INTO `videos` (`updated_at`, `sort_order`, `guid`, `description`,
 `user_id`, `created_at`) VALUES('2008-01-11 16:45:05', NULL, 'bcOMPqWaGr3k5CabxfFyeK', '', 5, '2008-01-11 16:44:28');</pre>
<p>I ran the same SQL with MySQL client and got the same error. I then looked at the table and saw the following</p>
<pre>mysql&gt; show columns from moo;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| integer_id | int(11)     | NO   | PRI | 0       |       |
| guid       | varchar(22) | NO   | UNI |         |       |
+------------+-------------+------+-----+---------+-------+</pre>
<p>What I expected to see was</p>
<pre>mysql&gt; show columns from moo;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| integer_id | int(11)     | NO   | PRI | NULL    | auto_increment |
| guid       | varchar(22) | NO   | UNI |         |                |
+------------+-------------+------+-----+---------+----------------+</pre>
<p>The difference is that when the column was renamed it seems to have lost its auto increment and NOT NULL properties. Some investigation showed that the SQL being used to rename the column was</p>
<pre class="sql">ALTER TABLE `videos` CHANGE `id` `integer_id` int(11)</pre>
<p>when it should be</p>
<pre class="sql">ALTER TABLE `videos` CHANGE `id` `integer_id` int(11) NOT NULL AUTO_INCREMENT</pre>
<p>It seems that this is already filled as a <a href="http://dev.rubyonrails.org/ticket/6999">bug</a> on the rails site, including a patch.</p>
<p>Funnily enough that bug is owned by <strong>bitsweat</strong>. It seems he&#8217;s managed to help me out twice in one day <img src='http://inodes.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It doesn&#8217;t seem that it made it into Rails 2.0 though so until then be careful about renaming columns using migrations.</p>
]]></content:encoded>
			<wfw:commentRss>http://inodes.org/2008/01/11/rails-activerecord-mysql-guids-and-the-rename_column-bug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

