<?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>Complicated Simplicity</title>
	<atom:link href="http://complicated-simplicity.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://complicated-simplicity.com</link>
	<description>I&#039;m addicted to good abstractions</description>
	<lastBuildDate>Sun, 17 Jul 2011 09:44:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gaining performance boost with Rails, Ruby 1.9, PostgreSQL and concurrency sauce</title>
		<link>http://complicated-simplicity.com/2011/07/gaining-performance-boost-with-rails-ruby-1-9-postgresql-and-concurrency-sauce/</link>
		<comments>http://complicated-simplicity.com/2011/07/gaining-performance-boost-with-rails-ruby-1-9-postgresql-and-concurrency-sauce/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 09:44:15 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[concurrency]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=126</guid>
		<description><![CDATA[
Recently I was trying to resolve performance issue in a back-end Rails app. The challenge was to reduce response time of one computational heavy back-end request. Target app is using PostgreSQL 9 for the database and running on Ruby 1.9 MRI. Logic for that particular request naturally breaks down into two parts. The first part [...]]]></description>
			<content:encoded><![CDATA[<p>
Recently I was trying to resolve performance issue in a back-end Rails app. The challenge was to reduce response time of one computational heavy back-end request. Target app is using PostgreSQL 9 for the database and running on Ruby 1.9 MRI. Logic for that particular request naturally breaks down into two parts. The first part is search, which is a combination of different filters applied together in order to reduce available dataset. There are filters that run SQL queries and filters making HTTP requests to other services (3rd party and our own). The second part is all about computing additional data based on search results. The fact that search filters can be made independent of each other was a clear hint for adding concurrency to the search step.
</p>
<p>
But first, I was trying to squeeze out as much performance as I can from single-threaded code itself. Ruby garbage collector is getting quite bad rap these days, but I didn&#8217;t want to go to the dark side right from the beginning and mess with reducing allocations or controlling GC cycles manually. My first targets were SQL queries.
</p>
<p><span id="more-126"></span></p>
<p>
Search filters operate on relatively big dataset, and their results can contain nothing more than just collections of database record ids. There is no need to pull all the stuff or instantiate real models because until all filters are applied, we don&#8217;t know whether given record would appear in result. So SQL based filters tend to run rather fast queries that return integer arrays of significant size.
</p>
<p>
It&#8217;s no surprise that ActiveRecord spends fair amount of time on object instantiation. ORM plumbing comes at price. There is well known way to avoid paying that cost by calling connection adapter&#8217;s <code>select_values</code> directly. It appears however, that with PostgreSQL we can do better. <a href="http://rubygems.org/gems/pg">pg gem</a> has <code>column_values</code> function written in C which returns array of string values for any given column of query result. Here are some benchmarks that compare <code>find</code> with <code>select_values</code> and my <code>column_values</code> wrapper:
</p>
<pre>
  Test sample contains 20000 records
  Benchmarking find
  Time: 34.165s 0.6833s per run
  Benchmarking select_values
  Time: 3.686s 0.0737s per run
  Benchmarking to_int_array
  Time: 2.426s 0.0485s per run
</pre>
<p>
I&#8217;ve made an attempt to put some envelope around <code>column_values</code> for accessing it from ActiveRecord in more convenient way. The <a href="http://github.com/bgipsy/column_queries">column_queries gem</a> gives ability to quickly pull id columns and is compatible with scopes and other ActiveRecord native facilities.
</p>
<p>
Ok, that helped, but still response time wasn&#8217;t perfect. No matter how hard I tried to optimize filters that run on app&#8217;s own database, there were always 2-3 filters making HTTP calls to other components, and at least one of those going to 3rd party service. So by just summing up time spent on those external calls, it easily approached 800ms, which was still far from the goal. The next logical step was to run filters in parallel, but GIL and poor MRI support for concurrency is a well known story. I was rather skeptical at that point, but still wanted to try.
</p>
<p>
Though one obvious way to get real concurrency is to switch to JRuby, I sticked to MRI. I may try JRuby on that project some day, however at that moment I was still looking at other options that wouldn&#8217;t bring significant infrastructure change to the production environment.
</p>
<p>
After <code>column_values</code> discovery I was looking into pg gem sources for asynchronous interfaces to be used with threads or fibers. And there were such, but one thing looked even more interesting: <code>async_exec</code> method. It&#8217;s interesting because despite its name it is a synchronous call. It blocks and returns only after it gets response from database. However internally, it uses asynchronous libpq interface and uses Ruby <code>rb_thread_select</code> call to release global mutex and wait for data on connection socket. So this method behaves just like regular exec, but releases GIL while blocked on I/O. Moreover, there is already relevant ActiveRecord code in place to work with that, it can be enabled by <code>allow_concurrency: true</code> entry in database.yml:
</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">  development:
    adapter: postgresql
    encoding: unicode
    database: sandbox
    username: postgres
    allow_concurrency: true
    pool: 10</pre></div></div>

<p>
I&#8217;ve tested that discovery with this simple test:
</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Starting&quot;</span>
&nbsp;
  threads = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  bm = <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">realtime</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    10.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      threads <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">select_values</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'SELECT pg_sleep(2)'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    threads.<span style="color:#5A0A0A; font-weight:bold;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:join<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Time: #{&quot;</span><span style="color:#006600; font-weight:bold;">%</span>0.3f<span style="color:#996600;">&quot; % bm}s&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># with :allow_concurrency =&gt; true</span>
  <span style="color:#008000; font-style:italic;"># Starting</span>
  <span style="color:#008000; font-style:italic;"># Time: 2.372s</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#008000; font-style:italic;"># with :allow_concurrency =&gt; false</span>
  <span style="color:#008000; font-style:italic;"># Starting</span>
  <span style="color:#008000; font-style:italic;"># Time: 20.337s</span></pre></div></div>

<p>
That meant I could actually get acceptable level of concurrency in Rails and Ruby 1.9 with pg gem and PostgreSQL database. The only thing left was ActiveResource client or HTTP requests in general.
</p>
<p>
Can threads with SQL queries run concurrently with threads that make ActiveResource calls? It appears that ActiveResource uses Net::HTTP and native I/O which releases GIL with <code>rb_thread_blocking_region</code>, just like <code>rb_thread_select</code>. So the thing works right out of the box:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># dummy_server.rb:</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
&nbsp;
  get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">2</span>
    <span style="color:#996600;">&quot;Hello world!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
  <span style="color:#008000; font-style:italic;"># test.rb:</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Starting&quot;</span>
&nbsp;
  threads = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  bm = <span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">realtime</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    10.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      threads <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">select_values</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'SELECT pg_sleep(2)'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    10.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      threads <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'localhost'</span>, <span style="color:#996600;">'/'</span>, <span style="color:#006666;">4567</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    threads.<span style="color:#5A0A0A; font-weight:bold;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:join<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Time: #{&quot;</span><span style="color:#006600; font-weight:bold;">%</span>0.3f<span style="color:#996600;">&quot; % bm}s&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Starting</span>
  <span style="color:#008000; font-style:italic;"># Time: 2.350s</span></pre></div></div>

<p>
Note that ActiveRecord connection pool needs to to be tuned carefully: the number of concurrent connections to the database shouldn&#8217;t be less than the number of filter threads that run in parallel. Also, each thread should play nicely with pool, so it makes sense to package code in separate Scheduler module:
</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">module</span> Scheduler
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">run_group_and_reduce</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>workers, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      group = <span style="color:#CC00FF; font-weight:bold;">ThreadGroup</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span>
      workers.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>w<span style="color:#006600; font-weight:bold;">|</span>
        group.<span style="color:#5A0A0A; font-weight:bold;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#9966CC; font-weight:bold;">begin</span>
            w.<span style="color:#5A0A0A; font-weight:bold;">call</span>
          <span style="color:#9966CC; font-weight:bold;">ensure</span>
            <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">clear_active_connections</span>!
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      group.<span style="color:#9900CC;">list</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:join<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">yield</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The above scheduler method gets an array of callable worker procs and regular <code>do...end</code> block. It first runs all the supplied procs in separate threads, waits for them to complete and then runs its own block. For spawned threads scheduler also takes care of releasing connections to pool. Such semantics of barrier synchronized blocks with dedicated &#8220;reduce&#8221; step allowed me to experiment with threads, fibers and single-threaded sequential execution. Here&#8217;s how scheduler looked like during those experiments:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> Scheduler
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">run_group_and_reduce_threaded</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>workers, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      group = <span style="color:#CC00FF; font-weight:bold;">ThreadGroup</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span>
      workers.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>w<span style="color:#006600; font-weight:bold;">|</span>
        group.<span style="color:#5A0A0A; font-weight:bold;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#9966CC; font-weight:bold;">begin</span>
            w.<span style="color:#5A0A0A; font-weight:bold;">call</span>
          <span style="color:#9966CC; font-weight:bold;">ensure</span>
            <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">clear_active_connections</span>!
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      group.<span style="color:#9900CC;">list</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:join<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">yield</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">run_group_and_reduce_sequential</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>workers, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      workers.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>w<span style="color:#006600; font-weight:bold;">|</span>
        w.<span style="color:#5A0A0A; font-weight:bold;">call</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">yield</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">run_group_and_reduce_fibered</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>workers, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
      counter = <span style="color:#006666;">0</span>
      fibers = workers.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>w<span style="color:#006600; font-weight:bold;">|</span>
        Fiber.<span style="color:#5A0A0A; font-weight:bold;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          <span style="color:#9966CC; font-weight:bold;">begin</span>
            w.<span style="color:#5A0A0A; font-weight:bold;">call</span>
            counter <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
            EventMachine.<span style="color:#9900CC;">stop</span> <span style="color:#9966CC; font-weight:bold;">if</span> counter == <span style="color:#006666;">0</span>
          <span style="color:#9966CC; font-weight:bold;">ensure</span>
            <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">clear_active_connections</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>
&nbsp;
      counter = fibers.<span style="color:#5A0A0A; font-weight:bold;">size</span>
      EventMachine.<span style="color:#9900CC;">run</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        fibers.<span style="color:#5A0A0A; font-weight:bold;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:resume<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">yield</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
      <span style="color:#008000; font-style:italic;"># Select desired strategy:</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># alias_method :run_group_and_reduce, :run_group_and_reduce_sequential</span>
      <span style="color:#008000; font-style:italic;"># alias_method :run_group_and_reduce, :run_group_and_reduce_fibered</span>
      alias_method <span style="color:#ff3333; font-weight:bold;">:run_group_and_reduce</span>, <span style="color:#ff3333; font-weight:bold;">:run_group_and_reduce_threaded</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
As you may see, I&#8217;ve also played with EventMachine and fibers inspired by <a href="http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers">findings</a> of Ilya Grigorik. That even ended up with a bug report and <a href="http://redmine.ruby-lang.org/issues/4612">one line fix to Ruby core</a>. I&#8217;ve turned back to threads though, mainly because of the discomfort with the amount of monkey patching in PostgreSQL ActiveRecord connection adapter and ActiveResource. EM Reactor pattern is great at I/O handling but under circumstances that are different from mine. Starting EM event loop and shutting it down just for search phase in serving each request seemed a bit unnatural to me. So by putting all my filters into separate threads I was able to come closer to the desired response time which was about 200ms.
</p>
<p>
Use of threads in Rails is an intriguing endeavor. Rails is declared thread safe, there is plenty of relevant code that takes care of synchronization, but still the threading practice should be approached with care. While threads aren&#8217;t that common in Rails applications, some configuration may be suboptimal for multi-threaded code. Some code can get broken just because regression testing for thread safety is a hard thing to achieve. And some deadlocks and race conditions can only reveal themselves under load or when scheduling becomes more &#8220;fair&#8221; as MRI progresses. Despite all the dangers, I&#8217;ve came across just several minor tweaks and tricks along the way of getting my code into production.
</p>
<p>
One problem that I&#8217;ve stepped into is that <code>BufferedLogger</code> default configuration can result in skipping log messages from the production log. The tricky part is that it is only production mode which is affected:
</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># railties/lib/rails/application/bootstrap.rb:</span>
  <span style="color:#008000; font-style:italic;">#</span>
  <span style="color:#008000; font-style:italic;"># Initialize the logger early in the stack in case we need to log some deprecation.</span>
  initializer <span style="color:#ff3333; font-weight:bold;">:initialize_logger</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    Rails.<span style="color:#9900CC;">logger</span> <span style="color:#006600; font-weight:bold;">||</span>= config.<span style="color:#9900CC;">logger</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#9966CC; font-weight:bold;">begin</span>
      path = config.<span style="color:#9900CC;">paths</span>.<span style="color:#9900CC;">log</span>.<span style="color:#5A0A0A; font-weight:bold;">to_a</span>.<span style="color:#5A0A0A; font-weight:bold;">first</span>
      logger = <span style="color:#6666ff; font-weight:bold;">ActiveSupport::BufferedLogger</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
      logger.<span style="color:#9900CC;">level</span> = <span style="color:#6666ff; font-weight:bold;">ActiveSupport::BufferedLogger</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>config.<span style="color:#9900CC;">log_level</span>.<span style="color:#5A0A0A; font-weight:bold;">to_s</span>.<span style="color:#9900CC;">upcase</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      logger.<span style="color:#9900CC;">auto_flushing</span> = <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> Rails.<span style="color:#9900CC;">env</span>.<span style="color:#9900CC;">production</span>?
      logger
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">StandardError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      logger = <span style="color:#6666ff; font-weight:bold;">ActiveSupport::BufferedLogger</span>.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDERR<span style="color:#006600; font-weight:bold;">&#41;</span>
      logger.<span style="color:#9900CC;">level</span> = <span style="color:#6666ff; font-weight:bold;">ActiveSupport::BufferedLogger::WARN</span>
      logger.<span style="color:#9900CC;">warn</span><span style="color:#006600; font-weight:bold;">&#40;</span>
        <span style="color:#996600;">&quot;Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. &quot;</span> <span style="color:#006600; font-weight:bold;">+</span>
        <span style="color:#996600;">&quot;The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.&quot;</span>
      <span style="color:#006600; font-weight:bold;">&#41;</span>
      logger
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
As you may see, production mode disables buffer auto flushing by default. If threads produce less than <code>MAX_BUFFER_SIZE = 1000</code> log messages, which is very likely given most logging is off in production, all their legacy remains hidden and that can result in memory leak. The cure is rather simple:
</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  config.<span style="color:#9900CC;">after_initialize</span> <span style="color:#006600; font-weight:bold;">&#123;</span> Rails.<span style="color:#9900CC;">logger</span>.<span style="color:#9900CC;">auto_flushing</span> = <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>
Another interesting side note that may be useful is that when you use <code>config.threadsafe!</code> it shuts off ActiveSupport dependency loader. That makes total sense, as it has been test proven that all the magic present in that module can&#8217;t cope with multi-threaded environment. So booting app by requiring files in proper order is a good thing and the only option for such applications. The &#8220;proper order&#8221; part is achieved by <code>ActiveSupport::Dependency</code> still, so Rails does something like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  Application.<span style="color:#9900CC;">eager_load</span>!
  <span style="color:#6666ff; font-weight:bold;">ActiveSupport::Dependencies</span>.<span style="color:#9900CC;">unhook</span>!</pre></div></div>

<p>
That is the reason of problems in rake tasks: with <code>config.threadsafe!</code> you have to manually require all the necessary files in right order. Eager loading doesn&#8217;t work there because rails dependency loader is already unhooked. One possible way to boot up app in rake task is this:
</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  task <span style="color:#ff3333; font-weight:bold;">:some_task</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#6666ff; font-weight:bold;">ActiveSupport::Dependencies</span>.<span style="color:#9900CC;">hook</span>!
    <span style="color:#6666ff; font-weight:bold;">MyApp::Application</span>.<span style="color:#9900CC;">eager_load</span>!
    <span style="color:#6666ff; font-weight:bold;">ActiveSupport::Dependencies</span>.<span style="color:#9900CC;">unhook</span>!
&nbsp;
    <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>
Hope that helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2011/07/gaining-performance-boost-with-rails-ruby-1-9-postgresql-and-concurrency-sauce/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>There is nothing new except what is forgotten</title>
		<link>http://complicated-simplicity.com/2011/06/there-is-nothing-new-except-what-is-forgotten/</link>
		<comments>http://complicated-simplicity.com/2011/06/there-is-nothing-new-except-what-is-forgotten/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 12:35:36 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[ood]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=104</guid>
		<description><![CDATA[Isn&#8217;t it fun that we had &#8220;blog in 15 minutes&#8221; video for many years now, but industry standard blog engine is written in &#8220;fugly&#8221; PHP?

There are always interesting things happening in Ruby and Rails ecosystem. Our community embraces change, even when change means redefining conventional wisdom. alias_method_chain has stopped being considered a good practice, and [...]]]></description>
			<content:encoded><![CDATA[<p><em>Isn&#8217;t it fun that we had &#8220;blog in 15 minutes&#8221; video for many years now, but industry standard blog engine is written in &#8220;fugly&#8221; PHP?</em></p>
<p>
There are always interesting things happening in Ruby and Rails ecosystem. Our community embraces change, even when change means redefining conventional wisdom. <code>alias_method_chain</code> has <a href="http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/">stopped being considered a good practice</a>, and it was only beginning. Today we refer to classics of <a href="http://en.oreilly.com/rails2011/public/schedule/detail/19579">SOLID design principles</a>, while people continue revisiting patterns and practices that have made Ruby stand out as highly dynamic runtime. Take <code>method_missing</code> as another example: <a href="http://www.slideshare.net/paoloperrotta/the-revenge-of-methodmissing">it can be harmful</a> just like any other powerful technique. What&#8217;s next? Dynamic dependency loading? (It&#8217;s evil indeed)
</p>
<p>
Our <a href="http://gmoeck.github.com/2011/03/10/sproutcore-mvc-vs-rails-mvc.html">understanding of MVC is changing</a> as well. Frontends aren&#8217;t what they were before. Driven by desire to <a href="http://thechangelog.com/post/5666652108/scrollability-native-scrolling-for-the-mobile-web">match platform native user experience</a>, people find ways to do <a href="http://techreport.com/discussions.x/21037">crazy</a> and <a href="http://bellard.org/jslinux/tech.html">beautiful</a> things.
</p>
<p>
I can&#8217;t help myself but <del datetime="2011-06-14T11:17:17+00:00">rant</del> play with some fundamentals too, so I&#8217;d like to focus on OO design and SOLID theme in particular. I don&#8217;t think that all five principles are equally applicable to Ruby because of dynamic typing. SRP (Single Responsibility Principle) may be the only one that can be taken as it is. OCP (Open Closed Principle) makes sense as well, but given Ruby open classes, needs an updated statement (i.e. what is meant by modification and extension).
</p>
<p>
LSP (Liskov Substitution Principle) is much more curious to think of in Ruby. Why would anyone want to consider such boring thing as <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">this</a>:
</p>
<blockquote><p>Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.
</p></blockquote>
<p>
when Ruby offers such wonderful and fun version as <a href="http://en.wikipedia.org/wiki/Duck_typing">Duck Typing</a>:
</p>
<blockquote><p>When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.
</p></blockquote>
<p>
I&#8217;m being sarcastic here, of course I don&#8217;t think that Duck Typing can substitute LSP. Have a look at behavioral conditions section in the wikipedia article for reasons. The fact that LSP is attracting attention right now can be pretty telling. I&#8217;m excited to witness it. Community is finally seeking for some grownup approaches, great! However, the question is: doesn&#8217;t that get us closer to some limitations of our beloved highly dynamic language?
</p>
<p>
Rails code is rapidly evolving, the same is true for the community and ideas behind it. The fact that such static typing rituals as DIP (Dependency Inversion Principle) and ISP (Interface Segregation Principle) start being considered may mean that within the current pace of growth, it becomes more and more of a challenge to manage complexity. It is well known that in order to successfully &#8220;Duck Type&#8221; one&#8217;s way through code, he or she should often mind more details.
</p>
<p>
Rituals are what we hate in Java and C++, it&#8217;s what gets in our way when we want to take the shortest path from an idea to it&#8217;s expression. But as codebase becomes bigger and more complex, rituals become inevitable. I think everyone who has got past &#8220;15 minutes blog app&#8221; state and the euphoria of &#8220;I&#8217;ll put together a dozen of gems and my CMS is done, than add some more and here is my CRM&#8221; knows that in order to build (<em>and scale</em>) apps beyond the framework, some set of carefully picked rituals is very important. Often the very same set that was in the way at the beginning. An extreme example of building beyond framework is Twitter and <a href="http://www.artima.com/scalazine/articles/twitter_on_scala.html">their reasons</a> behind switching to Scala.
</p>
<p>
The fact that you can call any private method you like, or access any instance variable, or reopen class and do whatever you want is fun. But doesn&#8217;t that lead to less time spent on thinking about fundamentals like encapsulation? How often do we have thoughts like &#8220;should this method that I write be public, private or protected&#8221; ?
</p>
<p>
Ruby is famous for its well thought OO paradigm. It has been influenced by Smalltalk design, which seems to mean a lot in the world of OO. The reality however is that de-facto standard <code>attr_accessor</code> doesn&#8217;t contribute at all to good design. And even if we put some opinionated decision and use <code>attr_reader</code> instead, doesn&#8217;t that still feel like we&#8217;re pretending encapsulation? For example if we expose an array trough <code>attr_reader :x</code>, we can&#8217;t do direct assignment, but what about <code>x</code> elements? Being able to ensure immutability on language level is good for VM performance optimizations and very important for various concurrency patterns. There are plenty of opportunities to break encapsulation. Of course being able to do something doesn&#8217;t necessarily mean having to do that, which brings us to certain aspects of code discipline. But, isn&#8217;t it all about rituals again? Implicit rituals unfortunately.
</p>
<p>
I heard a phrase at Railsconf 2011, that we end up writing more tests because of interface specs (Lint), and it&#8217;s ok to do so. It may be true in that particular case, but generally, is it okay to write more code especially when it does the work that interpreter or runtime can do better?</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2011/06/there-is-nothing-new-except-what-is-forgotten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quickly Copy Current File Path in TextMate</title>
		<link>http://complicated-simplicity.com/2010/08/quickly-copy-current-file-path-in-textmate/</link>
		<comments>http://complicated-simplicity.com/2010/08/quickly-copy-current-file-path-in-textmate/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 09:38:23 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[productivity]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=99</guid>
		<description><![CDATA[
I work with remote teams most of the time. And wile I do respect co-location practices, from my experience a phone and an instant messenger together can serve as a pretty good communication channel as well. Given this, I often need to get someone&#8217;s attention to a particular piece of code, so that we&#8217;re looking [...]]]></description>
			<content:encoded><![CDATA[<p>
I work with remote teams most of the time. And wile I do respect co-location practices, from my experience a phone and an instant messenger together can serve as a pretty good communication channel as well. Given this, I often need to get someone&#8217;s attention to a particular piece of code, so that we&#8217;re looking at the same file in order to discuss it. One way to achieve that is to paste file path into a chat room. But in order to paste, I need to get that path into the clipboard first. There are some some things like this that we just have to accept.
</p>
<p>
I&#8217;ve found this <a href="http://ciaranwal.sh/2007/11/27/textmate-tip-where-am-i">beautiful article</a> on the topic. It has nice explanation of different ways to access current file location in TextMate. I&#8217;ve chosen the last option for myself and created a custom bundle, but did a small change in script however. Having file path relative to the file&#8217;s project directory is more convenient for me, I don&#8217;t want to distract people with details of my home folder organization. Here&#8217;s my version of the script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #007800;">FILEPATH</span>=<span style="color: #800000;">${TM_FILEPATH/#$TM_PROJECT_DIRECTORY\//}</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$FILEPATH</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> pbcopy <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Path copied to clipboard: <span style="color: #007800;">$FILEPATH</span>&quot;</span></pre></div></div>

</p>
<p>
You can create it yourself using Ciarán&#8217;s guide and this code snippet, or just grab it from github: <a href="http://github.com/bgipsy/files-tmbundle">http://github.com/bgipsy/files-tmbundle</a></p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2010/08/quickly-copy-current-file-path-in-textmate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two Things I Avoid</title>
		<link>http://complicated-simplicity.com/2010/07/two-things-i-avoid/</link>
		<comments>http://complicated-simplicity.com/2010/07/two-things-i-avoid/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 13:09:33 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=92</guid>
		<description><![CDATA[
There are two things that smell bad to me: when database data leaks into code and when app code leaks into database. I&#8217;m not talking about SQL views, stored procedures, foreign keys, and other well known holy war stuff. I&#8217;d like to bring up some of &#8220;misplaced constants&#8221; problems.


Code constants that belong to database

Consider this [...]]]></description>
			<content:encoded><![CDATA[<p>
There are two things that smell bad to me: when database data leaks into code and when app code leaks into database. I&#8217;m not talking about SQL views, stored procedures, foreign keys, and other well known holy war stuff. I&#8217;d like to bring up some of &#8220;misplaced constants&#8221; problems.
</p>
<p><span id="more-92"></span></p>
<h2>Code constants that belong to database</h2>
<p>
Consider this example:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
SPONSORED_PARTNER_IDS = <span style="color:#006600; font-weight:bold;">&#91;</span>
    <span style="color:#006666;">1234</span>, <span style="color:#006666;">5678</span>, <span style="color:#006666;">9101</span> <span style="color:#008000; font-style:italic;">#, ... etc</span>
  <span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Here, prod DB was taken in order to look up the ids, and than those were hard coded into the code constant. It&#8217;s wonderful to observe how this seemingly harmless approach expands with time. Sometimes magic numbers don&#8217;t become less magic even after being put into well named constants.
</p>
<h2>Database tables that belong to code</h2>
<p>
Here&#8217;s the inverse pattern. Have you ever seen anything like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">&nbsp;
 <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> content_statuses;
&nbsp;
 id <span style="color: #66cc66;">|</span>   name    
<span style="color: #808080; font-style: italic;">----+-----------</span>
  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> Draft
  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> Published
  <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> Hidden
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> rows<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Here we have a content status dictionary in the database. But for what reason? For consistency enforced by foreign keys maybe, but I&#8217;m not sure it worth the burden of maintaining the table. The problem from design perspective here is more important however. What makes all those statuses different is code itself. If you do an <code>INSERT</code> and add another status, say &#8220;rejected&#8221;, it won&#8217;t change anything until you drop in some code changes that do all the associated special treatment to your content. This approach often breaks Common Closure Principle of component design.
</p>
<p>
Another way code leaks into database is &#8220;table algorithms&#8221;. Have you ever seen a table that contains pieces of SQL/PHP/Perl/Ruby code that are used by <em>agile and generic</em> algorithm? Often it comes as anticipation for future changes. The Holy Grail of code reuse probably is to make an algorithm that is so generic that it can handle any business request without change. All you have to do is update some data in the database. I&#8217;d rather call it <em>code that leaked into configuration</em>. Sometimes it transforms into a belief that marketing/content/admin folks will be able to make changes without even touching developers team. But more often than not, it ends up with everyone being too afraid to touch any of such code, and only one person being able to make &#8220;configuration changes&#8221; &#8211; the author of the code. With new requirements often come additional generalization and complexity. Same effort put into well thought modular, object oriented design could yield in good code that is complaint with Open Closed Principle (OCP) and located where it belongs &#8211; in files that are under control of VCS. It would still require some updates with new marketing requests, but those would land in code, and not DB migrations.</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2010/07/two-things-i-avoid/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Models In Rails Migrations</title>
		<link>http://complicated-simplicity.com/2010/05/using-models-in-rails-migrations/</link>
		<comments>http://complicated-simplicity.com/2010/05/using-models-in-rails-migrations/#comments</comments>
		<pubDate>Wed, 19 May 2010 07:54:07 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[migrations]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=78</guid>
		<description><![CDATA[Every once in a while we may want to do something like this:

&#160;
class CreateUsers &#60; ActiveRecord::Migration
  def self.up
    create_table :users do &#124;t&#124;
      t.string :name, :null =&#62; false
      t.string :email, :null =&#62; false
      t.string :hashed_password, :null =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while we may want to do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:users</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#9900CC;">string</span> <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      t.<span style="color:#9900CC;">string</span> <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      t.<span style="color:#9900CC;">string</span> <span style="color:#ff3333; font-weight:bold;">:hashed_password</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      t.<span style="color:#9900CC;">string</span> <span style="color:#ff3333; font-weight:bold;">:salt</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    add_index <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:unique</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
    User.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Default User'</span>,
      <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'user@example.com'</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'demo'</span>,
      <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'demo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:users</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Does it look good? If the answer is yes, then the rest of this small post worth reading.</p>
<p><span id="more-78"></span></p>
<h2>Once upon a time</h2>
<p>Let&#8217;s pay closer attention to the line where we&#8217;re creating default user record. The fact that it works and does its job well doesn&#8217;t mean that we&#8217;re safe. And that is due to the fact that we&#8217;re referencing a piece of application code from the database migration script. Migration files and application code have different life cycles. While application code evolves, migration code, once committed to the master branch (i.e. shared with the rest of the team/world), should stay the same forever (well, we&#8217;re doing grown up development, right?). We need to be extra careful when referencing any application code from migrations. For example, months after that migration was successfully committed and pushed, <code>User</code> model changes:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># new migration gets added...</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> AddUsersBirthday <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <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:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:birthday</span>, <span style="color:#ff3333; font-weight:bold;">:date</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    remove_column, <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:birthday</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;"># and User model gets a new validation:</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># ...</span>
&nbsp;
  <span style="color:#5A0A0A; font-weight:bold;">validates_presence_of</span> <span style="color:#ff3333; font-weight:bold;">:birthday</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># ...</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, if we try to recreate database from scratch, old migration which creates users table will not work for obvious reasons. Better yet, things could get even more interesting as <code>User</code> could have been renamed to <code>Admin</code> at this point.</p>
<h2>Special cases</h2>
<p>Sometimes using models for data migration is tempting. SQL often gets not as pretty as we would like to; seldom we just can&#8217;t do data conversions in SQL at all (consider hashing operations on passwords as an example). But in all such cases validations, relationships and algorithms that we may want to utilize in migrations can easily change as application develops.</p>
<p>One obvious solution for this problem is not to use models in migrations at all. Just DDL, plain SQL updates with <code>execute</code> is not such a bad idea actually. Another way is to declare migration local models like this:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
    <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Behaviors::HashedPassword</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    User.<span style="color:#9900CC;">reset_column_information</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:users</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># ...</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    User.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Default User'</span>,
      <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'user@example.com'</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'demo'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:users</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And if data conversion requires some piece of model implementation, it&#8217;s better to copy that code into migration local model class. In such cases it&#8217;s not a violation of DRY. It&#8217;s rather a code revision freeze, as migration should always run the same way.</p>
<h2>Just don&#8217;t do it</h2>
<p>If all migration data manipulation gravitates around bootstrapping your database with some starter content (like the above example with creating a user instance), then it may better fit into a dedicated rake task. Migrations should deal with schema changes, not data population. And often having to deal with static table content (which is often used as options for various selects, status id/name dictionary for example) can be a sign of more general design problem of putting code constants into db.</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2010/05/using-models-in-rails-migrations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Postgres: Thread safe insert/update</title>
		<link>http://complicated-simplicity.com/2009/11/postgres-thread-safe-insertupdate/</link>
		<comments>http://complicated-simplicity.com/2009/11/postgres-thread-safe-insertupdate/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 12:06:41 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[postgres]]></category>
		<category><![CDATA[multithreaded]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=64</guid>
		<description><![CDATA[I don&#8217;t like stored procedures, but sometimes creating a materialized view can be reasonable. Business logic doesn&#8217;t belong in database procedural code, but, I guess, everyone has a few stories about how easily performance considerations overcome design principles. I think I had just one materialized view that I consider nice in my carrier until now. [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t like stored procedures, but sometimes creating a materialized view can be reasonable. Business logic doesn&#8217;t belong in database procedural code, but, I guess, everyone has a few stories about how easily performance considerations overcome design principles. I think I had just one materialized view that I consider <em>nice</em> in my carrier until now. It was maintaining aggregated view of one quite heavy table. That materialized view  was used instead of queries like <code>SELECT ... GROUP BY event_time::date</code> It was long time ago, but up to present day I think that it was performance optimization in its purest form. No business logic was involved.</p>
<p>I&#8217;d like to share a trick that served me well in those triggers and I think it was not the last time. Every once in a while we need to create a row in a table if it isn&#8217;t there yet or update it otherwise. It can be implemented as easy as it sounds, with solid feeling of accomplishment, until one day you face process races.<br />
<span id="more-64"></span></p>
<p>Let&#8217;s suppose that we have two processes running the same code. Then the following sequence is possible. You probably already know that if you came here by googling, looking for a solution. But anyway:</p>
<ul>
<li>process A does <code>SELECT</code> and finds out that row does not exist</li>
<li>process B does <code>SELECT</code> &#8211; still nothing there</li>
<li>B inserts a row</li>
<li>A tries to insert the same row</li>
<li>BOOM! depending on whether you have unique constraints or not, process A gets an exception or database gets an extra row</li>
</ul>
<p>Both outcomes, extra row and exception, may not be the worst things that can happen to some &#8220;blog&#8221; application, but fixing such bugs is good for self esteem. So let&#8217;s see what can be done here.</p>
<p>Race conditions are fixed with proper synchronization, and proper synchronization is based on locks. Postgres has nice implementation of shared and exclusive locks. We need an exclusive lock here which is gained by performing <code>SELECT * FROM table WHERE conditions FOR UPDATE</code>. After such query, transaction owns the lock up to it&#8217;s end. There is no other pretty way to release locks.</p>
<p>We have locks implementation at hand, but, in above scenario, we may not have a row to lock yet. Okay, let&#8217;s make one. Hello, <em>Giant Mutex</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;">&nbsp;
<span style="color: #080; font-style: italic;">-- somewhere in schema initialization / migration</span>
&nbsp;
<span style="color: #00F;">CREATE</span> <span style="color: #00F;">TABLE</span> locks<span style="color: #00F;">&#40;</span>
  name <span style="color: #00F;">VARCHAR</span> PRIMARY KEY
<span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span>
&nbsp;
<span style="color: #00F;">INSERT</span> <span style="color: #00F;">INTO</span> locks <span style="color: #00F;">VALUES</span><span style="color: #00F;">&#40;</span><span style="color: #F00;">'MATVIEW_INSERT'</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span>
&nbsp;
<span style="color: #080; font-style: italic;">-- and then somewhere in materialized view update trigger ...</span>
&nbsp;
<span style="color: #00F;">SELECT</span> <span style="color: #800;">1</span> <span style="color: #00F;">FROM</span> locks <span style="color: #00F;">WHERE</span> name<span style="color: #00F;">=</span><span style="color: #F00;">'MATVIEW_INSERT'</span> <span style="color: #00F;">FOR</span> <span style="color: #00F;">UPDATE</span><span style="color: #00F;">;</span>
<span style="color: #080; font-style: italic;">-- triggers are run inside transactions, so everything below is run under global exclusive lock</span>
&nbsp;
<span style="color: #00F;">SELECT</span> <span style="color: #00F;">INTO</span> id_ id <span style="color: #00F;">FROM</span> my_matview <span style="color: #00F;">WHERE</span> <span style="color: #00F;">....</span>
<span style="color: #00F;">IF</span> <span style="color: #00F;">NOT</span> FOUND <span style="color: #00F;">THEN</span>
  <span style="color: #00F;">INSERT</span> <span style="color: #00F;">INTO</span> my_matview <span style="color: #00F;">VALUES</span><span style="color: #00F;">&#40;</span><span style="color: #00F;">...</span><span style="color: #00F;">&#41;</span>
  <span style="color: #00F;">RETURN</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span> <span style="color: #00F;">IF</span><span style="color: #00F;">;</span>
<span style="color: #00F;">UPDATE</span> my_matview <span style="color: #00F;">SET</span> <span style="color: #00F;">...</span> <span style="color: #00F;">WHERE</span> id <span style="color: #00F;">=</span> id_<span style="color: #00F;">;</span></pre></div></div>

<p>Well, that&#8217;s nice. But we killed concurrency quite a bit.  And that&#8217;s not nice because rows actually do <em>exist most of the time</em>. There is a pattern which is called &#8220;double checked locking&#8221; and often used in singleton resources initialization. An adaptation for our example would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;">&nbsp;
<span style="color: #00F;">SELECT</span> <span style="color: #00F;">INTO</span> id_ id <span style="color: #00F;">FROM</span> my_matview <span style="color: #00F;">WHERE</span> <span style="color: #00F;">....</span>
<span style="color: #00F;">IF</span> <span style="color: #00F;">NOT</span> FOUND <span style="color: #00F;">THEN</span>
  <span style="color: #00F;">SELECT</span> <span style="color: #800;">1</span> <span style="color: #00F;">FROM</span> locks <span style="color: #00F;">WHERE</span> name<span style="color: #00F;">=</span><span style="color: #F00;">'MATVIEW_INSERT'</span> <span style="color: #00F;">FOR</span> <span style="color: #00F;">UPDATE</span><span style="color: #00F;">;</span>
  <span style="color: #00F;">SELECT</span> <span style="color: #00F;">INTO</span> id_ id <span style="color: #00F;">FROM</span> my_matview <span style="color: #00F;">WHERE</span> <span style="color: #00F;">....</span>
  <span style="color: #00F;">IF</span> <span style="color: #00F;">NOT</span> FOUND <span style="color: #00F;">THEN</span>
    <span style="color: #00F;">INSERT</span> <span style="color: #00F;">INTO</span> my_matview <span style="color: #00F;">VALUES</span><span style="color: #00F;">&#40;</span><span style="color: #00F;">...</span><span style="color: #00F;">&#41;</span>
    <span style="color: #00F;">RETURN</span><span style="color: #00F;">;</span>
  <span style="color: #00F;">END</span> <span style="color: #00F;">IF</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span> <span style="color: #00F;">IF</span><span style="color: #00F;">;</span>
<span style="color: #00F;">UPDATE</span> my_matview <span style="color: #00F;">SET</span> <span style="color: #00F;">...</span> <span style="color: #00F;">WHERE</span> id <span style="color: #00F;">=</span> id_<span style="color: #00F;">;</span></pre></div></div>

<p>Note how we obtain lock only when first &#8220;easy&#8221; attempt to find row fails. Then we go &#8220;serious&#8221;, and when we do, we need to perform <code>SELECT</code> check once again. That&#8217;s because, like at the very beginning, several threads can get down to serious business at the same time.</p>
<p>The next things to worry about are deadlocks. But that probably deserves a separate post.</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2009/11/postgres-thread-safe-insertupdate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Factory Pattern &#8211; Ruby Style</title>
		<link>http://complicated-simplicity.com/2009/10/factory-pattern-ruby-style/</link>
		<comments>http://complicated-simplicity.com/2009/10/factory-pattern-ruby-style/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 12:04:09 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[plugins]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ood]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=42</guid>
		<description><![CDATA[Recently I was working on a framework for front end sites. The framework is packaged as a rails plugin, but unlike regular plugins it contains large blocks of logic. Framework models and controller classes are used to build a reusable abstraction layer above one particular API. A front end application then can subclass framework controller [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a framework for front end sites. The framework is packaged as a rails plugin, but unlike regular plugins it contains large blocks of logic. Framework models and controller classes are used to build a reusable abstraction layer above one particular API. A front end application then can subclass framework controller template classes to incorporate shared UI patterns in it&#8217;s own controllers. At the same time such applications introduce some variations in user experience. That&#8217;s the reason behind having many of them instead of one. And now I&#8217;m quizzed to find a nice way to make all the generic framework code play nicely with app specific extensions.</p>
<p><span id="more-42"></span></p>
<h2>Monkey Patching</h2>
<p>One possible way to add logic to library classes is to reopen class and add new methods directly, i.e. monkey-patch a class:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">UserTracking::SiteVisitor</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize_with_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
    initialize_without_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> = api_get_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">api_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  alias_method_chain <span style="color:#ff3333; font-weight:bold;">:initialize</span>, <span style="color:#ff3333; font-weight:bold;">:fancy_links</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> featured_links_with_fancy_links
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> <span style="color:#006600; font-weight:bold;">+</span> featured_links_without_fancy_links
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  alias_method_chain <span style="color:#ff3333; font-weight:bold;">:featured_links</span>, <span style="color:#ff3333; font-weight:bold;">:fancy_links</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This approach has some limitations. First, the default dependency loader doesn&#8217;t really know how to handle classes that are defined in several files. I had to add some explicit require statements in order to get properly loaded models. Second, I had to use <code>alias_method_chain</code> to add logic to methods defined in framework parts of classes. That sometimes produced rather ugly effects. Look how it affected constructor for example.</p>
<h2>Injecting a Module</h2>
<p>As a slightly modified version of previous approach, I was considering a style taken from active_support&#8217;s core extensions:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> SiteVisitorExtension
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">alias_method_chain</span> <span style="color:#ff3333; font-weight:bold;">:initialize</span>, <span style="color:#ff3333; font-weight:bold;">:fancy_links</span>
    base.<span style="color:#9900CC;">alias_method_chain</span> <span style="color:#ff3333; font-weight:bold;">:featured_links</span>, <span style="color:#ff3333; font-weight:bold;">:fancy_links</span>
    base.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:attr_accessor</span>, <span style="color:#ff3333; font-weight:bold;">:fancy_links</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize_with_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
    initialize_without_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> = api_get_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">api_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> featured_links_with_fancy_links
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> <span style="color:#006600; font-weight:bold;">+</span> featured_links_without_fancy_links
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> find_in_cache<span style="color:#006600; font-weight:bold;">&#40;</span>cookie_key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#008000; font-style:italic;"># ...</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>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">UserTracking::SiteVisitor</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>, SiteVisitorExtension<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It looks just a bit more clever, but adds extra syntax overhead for defining class methods or calling macro methods. Such things all go into <code>def self.included</code> and become messy with time.</p>
<h2>So What&#8217;s The Problem?</h2>
<p>But it wasn&#8217;t extra lines of code that kept me looking for other options. I didn&#8217;t like the fact that the inherent difference of one particular front end application, the very incentive to create one, was implemented by some kind of monkey patch. I think that described techniques are very good in their own domain. When, for example, one wants to add some formatting to log messages or stick caching into objects that should be unaware of the fact, these methods do their trick. But implementing parts of business logic by alias_method_chains is something I&#8217;d really like to avoid.</p>
<h2>Subclassing</h2>
<p>We don&#8217;t create application models by reopening <code>ActiveRecord::Base</code>, we subclass it. All the above examples get simplified dramatically if we use boring inheritance instead of fancy tricks with patching classes. Also, standard dependency loader works perfectly with such classes because they&#8217;re just plain regular.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MySiteVisitor <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">UserTracking::SiteVisitor</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:fancy_links</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span>
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> = api_get_fancy_links<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">api_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> featured_links
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#9966CC; font-weight:bold;">super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_in_cache</span><span style="color:#006600; font-weight:bold;">&#40;</span>cookie_key<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But can I easily use inheritance if framework creates instances of models inside itself? How can it know about my new shiny subclasses? People were facing such problems for quite a long time now. And for statically typed languages the answer always was often found near <em>generative patterns</em> or <em>Factories</em>. I used to think that in ruby I will never need a factory. Now I&#8217;m going to use this pattern and see how it works out.</p>
<h2>Factory Module</h2>
<p>Here&#8217;s a slightly adapted Factory pattern. I&#8217;m trying to be aware that I&#8217;m still using a highly dynamic language, and inventory of clever tricks is always at hand. But at the same time I don&#8217;t want to place any magic inside model classes &#8211; neither framework ancestors, nor app descendants. I mean that I don&#8217;t probably want to see some class <code>BaseClass</code> to magically appear as <code>DescendantClass</code>. Tricks like that can be fun but I&#8217;d like to see an explicit notion of whether framework code refers to some fixed class or is willing to instantiate it with the help of a factory.</p>
<p>A possible use case example may look like this:</p>
<h3>framework code</h3>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># in user_tracking_plugin/app/models/user_tracking/site_visitor.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">UserTracking::SiteVisitor</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">api_find_or_create_by_request</span><span style="color:#006600; font-weight:bold;">&#40;</span>http_request<span style="color:#006600; font-weight:bold;">&#41;</span>
    api_response = ...
    <span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>api_response<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>api_response<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> featured_links
    <span style="color:#008000; font-style:italic;"># ...</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;"># in user_tracking_plugin/app/models/user_tracking/factory.rb</span>
<span style="color:#9966CC; font-weight:bold;">module</span> UserTracking
  create_factory
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># in user_tracking_plugin/app/constrollers/user_tracking/controller_base.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">UserTracking::ControllerBase</span>
  <span style="color:#5A0A0A; font-weight:bold;">before_filter</span> <span style="color:#ff3333; font-weight:bold;">:track_visitor</span>
&nbsp;
  protected
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> track_visitor
    <span style="color:#0066ff; font-weight:bold;">@visitor</span> = <span style="color:#6666ff; font-weight:bold;">UserTracking::Factory::SiteVisitor</span>.<span style="color:#9900CC;">api_find_or_create_by_request</span><span style="color:#006600; font-weight:bold;">&#40;</span>request<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></pre></div></div>

<h3>application code</h3>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># in config/environment.rb</span>
config.<span style="color:#9900CC;">after_initialize</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#6666ff; font-weight:bold;">UserTracking::Factory</span>.<span style="color:#9900CC;">add_mapping</span> <span style="color:#ff3333; font-weight:bold;">:site_visitor</span>, <span style="color:#ff3333; font-weight:bold;">:fancy_visitor</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># in app/models/fancy_visitor.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> FancyVisitor <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">UserTracking::SiteVisitor</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span>
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> = another_api_get_very_special_links<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">api_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> featured_links
    <span style="color:#0066ff; font-weight:bold;">@fancy_links</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#008000; font-style:italic;"># I love super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Another option for application code would be to place factory mappings setup in separate initializer instead of <code>config.after_initialize</code> block.</p>
<p>One important thing is to be aware how the factory module gets loaded. Standard class reloading mechanism that us used in development can drop all factory mappings. To avoid that rb file which creates factory module must be loaded explicitly (or just be placed in initializer) or reside under one of the <code>load_once_paths</code> (that is true for plugins by default).</p>
<h2>Install</h2>
<p>Implementation can be found on <a href="http://github.com/bgipsy/factory_module">github</a>. It is packaged as a separate plugin, so you can play with it by installing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>plugin <span style="color: #c20cb9; font-weight: bold;">install</span> git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>bgipsy<span style="color: #000000; font-weight: bold;">/</span>factory_module.git</pre></div></div>

</p>
<p>Additional details can be found by looking at <code>spec/lib/proto_factory_spec.rb</code> under the plugin tree.</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2009/10/factory-pattern-ruby-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Dark Side of Duck Typing</title>
		<link>http://complicated-simplicity.com/2009/10/the-dark-side-of-duck-typing/</link>
		<comments>http://complicated-simplicity.com/2009/10/the-dark-side-of-duck-typing/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 08:21:56 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[curiuos bugs]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[duck typing]]></category>
		<category><![CDATA[ood]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=27</guid>
		<description><![CDATA[On the Origin of Species
What walks like duck and quacks like a duck is a duck. That what duck typing is about. It makes development easier. We don&#8217;t need interfaces and abstract classes. Many object oriented patterns look much simpler because of such dynamic typing. They come more naturally in ruby code and often we [...]]]></description>
			<content:encoded><![CDATA[<h2>On the Origin of Species</h2>
<p>What walks like duck and quacks like a duck is a duck. That what duck typing is about. It makes development easier. We don&#8217;t need interfaces and abstract classes. Many object oriented patterns look much simpler because of such dynamic typing. They come more naturally in ruby code and often we don&#8217;t even notice them until we see explicit names like <code>Visitor</code>, <code>Decorator</code>, etc.</p>
<p>After I&#8217;ve got used with such a powerful tool, it was too easy to step into  dangerous territory without even noticing. Sometimes things do walk and quack like ducks all the time&#8230; well, <em>almost</em> all the time. And sometimes in a pack of ducks there are some species that do quack (and that makes you feel safe), but that doesn&#8217;t make them ducks.</p>
<p><span id="more-27"></span></p>
<h2>When all ducks are quacking well</h2>
<p>Making real world examples is hard, and the following one is somewhat synthetic. But anyway. Let&#8217;s suppose that we have a system with <code>Project</code> model and our system should generate reports by aggregating project instances. We have different types of parametrized reports, and these types share some common features. Let&#8217;s see how it fits in code.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Project <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:categories</span>
&nbsp;
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:items</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ReportFilter <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> filter_project_ids<span style="color:#006600; font-weight:bold;">&#40;</span>project_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># implemented in descendants</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> DueDateFilter <span style="color:#006600; font-weight:bold;">&lt;</span> ReportFilter
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> filter_project_ids<span style="color:#006600; font-weight:bold;">&#40;</span>project_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    irrelevant_projects = Project.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>project_ids,
      <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'due_date &gt; ?'</span>, <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">cut_off_date</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    project_ids <span style="color:#006600; font-weight:bold;">-</span> irrelevant_projects.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:id<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ComplexFilter <span style="color:#006600; font-weight:bold;">&lt;</span> ReportFilter
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> filter_project_ids<span style="color:#006600; font-weight:bold;">&#40;</span>project_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    connection.<span style="color:#9900CC;">select_values</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>SQL
      SELECT <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">id</span> FROM projects <span style="color:#CC0066; font-weight:bold;">p</span>
        JOIN project_estimates e ON <span style="color:#006600; font-weight:bold;">&#40;</span>e.<span style="color:#9900CC;">project_id</span> = <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        JOIN metrics m ON <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">id</span> = m.<span style="color:#9900CC;">project_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        WHERE m.<span style="color:#9900CC;">coverage_level</span> <span style="color:#006600; font-weight:bold;">&lt;</span> average_coverage_in_group<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">group_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          AND e.<span style="color:#9900CC;">author_id</span> IN <span style="color:#006600; font-weight:bold;">&#40;</span>SELECT id FROM team_members WHERE statisfaction_level <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#008000; font-style:italic;">#{desired_level})</span>
        AND <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">id</span> IN <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#008000; font-style:italic;">#{project_ids.join ','})</span>
    SQL
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> CategoryReport <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:category</span>
&nbsp;
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:report_filters</span>, <span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'complexity_level'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> projects
    project_ids = category.<span style="color:#9900CC;">project_ids</span>
&nbsp;
    report_items.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      project_ids = i.<span style="color:#9900CC;">filter_project_ids</span><span style="color:#006600; font-weight:bold;">&#40;</span>project_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    Project.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>project_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Note how <code>CategoryReport</code> generates it&#8217;s project set. It iterates through it&#8217;s project filters to trim category project list down to some relevant report specific subset. All the report filters implement common interface. Each filter accepts <code>project_ids</code> array and reduces it according to its parameters and semantics. Some filters are fairly easy to calculate, other can be computationally complex. We run simple filters first to reduce set and then refer to complex ones.</p>
<p>Most of our filter implementations should look like <code>DueDateFilter</code>. But we live in real world, and several filters look like <code>ComplexFilter</code>. Don&#8217;t look deep into SQL query in it, it was added for dramatic effect only. My point is that due to some <small>(often premature)</small> optimization, complex filters can implement some real black magic under the hood.</p>
<h2>And here comes the time for a <em>closer look</em></h2>
<p>Let&#8217;s suppose that we always run <code>ComplexFilter</code> as the last filter in our chain. But one day a very good reason for changing that order arises. We place <code>DueDateFilter</code> after <code>ComplexFilter</code>. System works, all the filter rspecs pass, and rspec for <code>Report</code> passes as well. It checks <code>Report</code> behavior with great success by mocking everything except <code>Report</code> (a model&#8217;s rspec should be focused on the model it&#8217;s testing, right?)</p>
<p>Some time passes and we discover a great surprise. It appears that reports have been working incorrectly for some time now. Some filters that come after <code>ComplexFilter</code> don&#8217;t filter projects any more. No exceptions are raised. They&#8217;ve just silently became transparent.</p>
<p>Please note how <code>DueDateFilter</code> operates on array of ids that are <code>Fixnums</code>. Then note how <code>ComplexFilter</code> returns an array of strings. They&#8217;re still ids, but that doesn&#8217;t help when it comes to&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
project_ids <span style="color:#006600; font-weight:bold;">-</span> irrelevant_projects.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:id<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>&#8230; in other filters.</p>
<h2>And a few reflections</h2>
<p>A type of bug that is easy to fix, easy to debug, but hard to predict. Who would notice such a thing on code review when all these filters did work previously? And another big question: what can we do to make such bugs harder to sneak into our code?</p>
<p>Well, I don&#8217;t know. There&#8217;s probably no silver bullet for it. But there&#8217;s something we can do however. It&#8217;s not new, in fact it&#8217;s plain old OOD.</p>
<p>Arrays of fixnums (and arrays of strings) are not domain models for our system. Yes, we don&#8217;t want to instantiate full projects for the sake of performance, but passing integers around instead of project models is not the only option that we have.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ProjectSet
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@project_ids</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#5A0A0A; font-weight:bold;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>ids<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span>ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@project_ids</span> <span style="color:#006600; font-weight:bold;">|</span>= ids.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#5A0A0A; font-weight:bold;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> exclude<span style="color:#006600; font-weight:bold;">&#40;</span>ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@project_ids</span> <span style="color:#006600; font-weight:bold;">-</span> ids.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#5A0A0A; font-weight:bold;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> project_ids
    <span style="color:#0066ff; font-weight:bold;">@project_ids</span>.<span style="color:#9900CC;">dup</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> projects
    Project.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>@project_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_sql_list
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">'NULL'</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@project_ids</span>.<span style="color:#9900CC;">empty</span>?
    <span style="color:#0066ff; font-weight:bold;">@project_ids</span>.<span style="color:#9900CC;">join</span> <span style="color:#996600;">','</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Looks like we&#8217;ve avoided the temptation to implement domain specific operations through overriding operators <small>(especially arithmetic)</small>. Cool books say that&#8217;s not something that grownups do very often. So our methods are named <code>add</code> and <code>exclude</code>. Now we can change our report code to pass an instance of <code>ProjectSet</code> and have a bit more control of operations that are essential to our problem domain.</p>
<p><code>ProjectSet</code> adds the following benefits:</p>
<ul>
<li>we can encapsulate type checks and explicit conversions in it, and keep filter code clean of such details</li>
<li>we can raise an exception if supplied ids are of incorrect type and be informed about problems earlier by QA (500-page is easier to notice than tricky changes in reports behavior)</li>
<li>we can add other specific code that otherwise would be scattered across different filters (note how <code>to_sql_list</code> handles empty lists)</li>
<li>we can open paths to other caching/performance optimizations that can be hidden under the hood of <code>ProjectSet</code></li>
</ul>
<p>Not all <code>.rb</code> files that live under <code>app/models</code> directory have to be AR models. Many applications can benefit from having additional models. And those can be <em>no less domain specific</em> than conventional <code>ActiveRecord::Base</code> descendants.</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2009/10/the-dark-side-of-duck-typing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding foreign key constraints in rails migrations</title>
		<link>http://complicated-simplicity.com/2009/06/fk-constraints/</link>
		<comments>http://complicated-simplicity.com/2009/06/fk-constraints/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 17:32:29 +0000</pubDate>
		<dc:creator>serge</dc:creator>
				<category><![CDATA[plugins]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[constraints]]></category>
		<category><![CDATA[foreign keys]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://complicated-simplicity.com/?p=5</guid>
		<description><![CDATA[More often than not models have associations. And foreign key constraints are sometimes used for keeping them in good shape. The role of such constraints can be indispensable or trivial, depending on how your database is managed.
Here&#8217;s a rails plugin to add foreign keys for associations. It doesn&#8217;t have any dependencies. And it&#8217;s code is [...]]]></description>
			<content:encoded><![CDATA[<p>More often than not models have associations. And foreign key constraints are sometimes used for keeping them in good shape. The role of such constraints can be indispensable or trivial, depending on how your database is managed.</p>
<p>Here&#8217;s a rails <a href="http://github.com/bgipsy/fk_constraints/tree/master" target="_blank">plugin</a> to add foreign keys for associations. It doesn&#8217;t have any dependencies. And it&#8217;s code is simple so it can be read in one take. These are the two things in plugins world that I appreciate the most.</p>
<p>In migrations, when a table is created, it recognizes columns which are foreign keys by <code>_id</code> suffix. Then it adds foreign key declarations. Referred table name is guessed from column&#8217;s name according to rails conventions. This can be disabled by adding <code>:references =&gt; false</code> option.</p>
<p>If a column should refer to a table which name can&#8217;t be inferred that way, you can explicitly specify it by using a symbol or a string.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">create_table <span style="color:#ff3333; font-weight:bold;">:comments</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
  t.<span style="color:#9900CC;">text</span> <span style="color:#ff3333; font-weight:bold;">:body</span>
  t.<span style="color:#9900CC;">integer</span> <span style="color:#ff3333; font-weight:bold;">:commented_post_id</span>, <span style="color:#ff3333; font-weight:bold;">:references</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:posts</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>will produce</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> comments<span style="color: #66cc66;">&#40;</span>
  id SERIAL <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
  body TEXT<span style="color: #66cc66;">,</span>
  commented_post_id INTEGER <span style="color: #993333; font-weight: bold;">REFERENCES</span> posts
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And with strings you can do all the things that you shouldn&#8217;t want to do:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">create_table <span style="color:#ff3333; font-weight:bold;">:comments</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
  t.<span style="color:#9900CC;">text</span> <span style="color:#ff3333; font-weight:bold;">:body</span>
  t.<span style="color:#9900CC;">integer</span> <span style="color:#ff3333; font-weight:bold;">:commented_post_id</span>, <span style="color:#ff3333; font-weight:bold;">:references</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'whatever(strange_pk) ON DELETE RESTRICT'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>will become</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> comments<span style="color: #66cc66;">&#40;</span>
  id SERIAL <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
  body TEXT<span style="color: #66cc66;">,</span>
  commented_post_id INTEGER <span style="color: #993333; font-weight: bold;">REFERENCES</span> whatever<span style="color: #66cc66;">&#40;</span>strange_pk<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> RESTRICT
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That can be handy when dealing with nasty legacy schema.</p>
<p>And you can add foreign key constraints for already created tables:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">add_foreign_key_constraint<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:posts</span>, <span style="color:#ff3333; font-weight:bold;">:comment_id</span>, <span style="color:#ff3333; font-weight:bold;">:comments</span>, <span style="color:#ff3333; font-weight:bold;">:p_id</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> posts <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>comment_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> comments<span style="color: #66cc66;">&#40;</span>p_id<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>You get the idea. For more examples please see <code>spec/migration_spec.rb</code> <a href="http://github.com/bgipsy/fk_constraints/blob/e06fd4f32d4a4c80f80b3d74366f26519591e399/spec/migration_spec.rb" target="_blank">here</a>
</p>
<p>So the next thing you may want after you&#8217;ve packed your database with foreign key constraints is to get rid of them. In your tests. Rails disables referential integrity checks before loading fixtures and enables them after that. So missing references in fuxtures are ok. And if you need to disable foreign key constraints for certain table you can always make a call.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">remove_all_foreign_key_constraints<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'comments'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Another option is to automatically drop all constraints for tables that are referenced by test suite fixtures statement. That is what another branch <code>with_fixture_patch</code> of the plugin contains. I&#8217;m still considering if it can be useful&#8230;</p>
<p>To install run</p>
<pre>
./script/plugin install git://github.com/bgipsy/fk_constraints.git
</pre>
<p>from your app root directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://complicated-simplicity.com/2009/06/fk-constraints/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

