<?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>Pivotal Labs &#187; postgres</title>
	<atom:link href="http://pivotallabs.com/tag/postgres/feed/" rel="self" type="application/rss+xml" />
	<link>http://pivotallabs.com</link>
	<description>Agility Developed</description>
	<lastBuildDate>Fri, 24 May 2013 21:58:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Access your database&#8217;s best features with Sequel</title>
		<link>http://pivotallabs.com/access-your-databases-best-features-with-sequel/</link>
		<comments>http://pivotallabs.com/access-your-databases-best-features-with-sequel/#comments</comments>
		<pubDate>Mon, 06 May 2013 06:58:53 +0000</pubDate>
		<dc:creator>Brandon Liu</dc:creator>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[bloggerdome]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/?p=18892</guid>
		<description><![CDATA[<p>Sequel is a wonderful library for interacting with relational databases. Some of my favorite aspects: 1. Out-of-the box support for foreign key constraints. 2. Straightforward migration DSL. 3. Support for all major free RDBMSes and even some proprietary ones. Also JRuby support through JDBC. (although JDBC doesn&#8217;t necessarily give you all the features a native driver would. Projects like jruby-pg are making progress though.) 4. &#8220;One way to do it&#8221; for common operations such as inserts and updates. Parsimonious API (compared to ActiveRecord). 5. bin/sequel executable gives you an interactive ruby session directly from a database URL. 6. Extremely powerful Dataset abstraction lets you write general query code. 7. The source code itself is a pleasure to work with and the maintainer (jeremyevans) is extremely diligent about pull requests. One feature i&#8217;d like to expand upon is the ability to call raw SQL functions from Sequel&#8217;s DSL. Here&#8217;s a migration&#8230;</p><p>The post <a href="http://pivotallabs.com/access-your-databases-best-features-with-sequel/">Access your database&#8217;s best features with Sequel</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="https://github.com/jeremyevans/sequel">Sequel</a> is a wonderful library for interacting with relational databases. Some of my favorite aspects:<br />
1. Out-of-the box support for foreign key constraints.<br />
2. Straightforward migration DSL.<br />
3. Support for all major free RDBMSes and even some proprietary ones. Also JRuby support through JDBC. (although JDBC doesn&#8217;t necessarily<br />
give you all the features a native driver would. Projects like <a href="https://github.com/headius/jruby-pg">jruby-pg </a>are making progress though.)<br />
4. &#8220;One way to do it&#8221; for common operations such as inserts and updates. Parsimonious API (compared to ActiveRecord).<br />
5. bin/sequel executable gives you an interactive ruby session directly from a database URL.<br />
6. Extremely powerful Dataset abstraction lets you write general query code.<br />
7. The source code itself is a pleasure to work with and the maintainer (<a href="https://github.com/jeremyevans">jeremyevans</a>) is extremely diligent about pull requests.</p>
<p>One feature i&#8217;d like to expand upon is the ability to call raw SQL functions from Sequel&#8217;s DSL.</p>
<p>Here&#8217;s a migration where I create a speeches table with some full-text search columns:</p>
<pre><code>
Sequel.migration do
  up do
    create_table :speeches do
      primary_key :id
      String :text
      String :speaker
      Integer :year
    end

    run "ALTER TABLE speeches ADD COLUMN ts_text tsvector;"
    run "CREATE INDEX ts_text_idx ON speeches USING gin(ts_text);"
    run "CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
         ON speeches FOR EACH ROW EXECUTE PROCEDURE
         tsvector_update_trigger(ts_text, 'pg_catalog.english', text, speaker);"
  end
end
</code></pre>
<p>&nbsp;</p>
<p>And here&#8217;s some sample code to query it:</p>
<pre><code>
require 'sequel'

def search(dataset, term)
  results = dataset.select do
    [ts_headline('english',
                 :text,
                 to_tsquery('english', term),
                 'MaxFragments=2').as(headline),
     id,
     speaker]
  end
  results.filter("ts_text @@ to_tsquery('english', ?::text)", term)
end

DB = Sequel.connect('postgres://localhost/blogpost')

DB[:speeches].insert(:year =&gt; 1865, :speaker =&gt; 'President Lincoln', :text =&gt; 'Fourscore and seven years ago')
DB[:speeches].insert(:year =&gt; 1963, :speaker =&gt; 'President Kennedy', :text =&gt; 'I am a Jelly Donut')

p search(DB[:speeches], 'President').all # returns both speeches
p search(DB[:speeches].where("year &gt; ?", 1900), 'President').all # returns only the Kennedy speech
</code></pre>
<p>The search function takes in a dataset and a query term, and returns a new dataset filtered with a body or speaker name matching the query term,<br />
as well as a &#8220;headline&#8221;, or the context around the search term to display in search results. This takes full advantage of a full-text search index.</p>
<p>Sequel is also great as glue code for calling PostGIS functions. I&#8217;ve found it especially useful for DRY-ing up long sets of raw SQL queries into maintainable scripts.</p>
<p>&nbsp;</p>
<p>The post <a href="http://pivotallabs.com/access-your-databases-best-features-with-sequel/">Access your database&#8217;s best features with Sequel</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/access-your-databases-best-features-with-sequel/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rails and SQL Views &#8211; Part 1</title>
		<link>http://pivotallabs.com/rails-sql-views-1/</link>
		<comments>http://pivotallabs.com/rails-sql-views-1/#comments</comments>
		<pubDate>Mon, 11 Feb 2013 18:29:41 +0000</pubDate>
		<dc:creator>Dirk Kelly</dc:creator>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/?p=15205</guid>
		<description><![CDATA[<p>The project we&#8217;re currently working on has a fairly system wide requirement to have objects in different &#8220;states&#8221; as a result of user actions. The client wanted to be able to see both the current status, and a history of all the statuses each object has been in. These two requirements felt very closely tied, to the point where really the status is just the most recent action. This could be fairly easily modelled in a Rails application as such. class Rocket &#60; ActiveRecord::Base has_many :activities, class_name: "RocketActivity" def current_activity activities.order("created_at DESC").first end end rocket.activities.map(&#38;:status) # [landed, landing, cruising, launch, landed] rocket.current_activity.status # landed This seems like a fairly typical solution, and would provide you with a fairly easy to use accessor. rocket.current_activity.status But what if you wanted to view all the current activities of all the rockets you have in your fleet. @rockets.each do &#124;rocket&#124; rocket.current_activity.status end Works great,&#8230;</p><p>The post <a href="http://pivotallabs.com/rails-sql-views-1/">Rails and SQL Views &#8211; Part 1</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>The project we&#8217;re currently working on has a fairly system wide requirement to have objects in different &#8220;states&#8221; as a result of user actions. </p>
<p>The client wanted to be able to see both the current status, and a history of all the statuses each object has been in.</p>
<p>These two requirements felt very closely tied, to the point where really the status is just the most recent action.</p>
<p>This could be fairly easily modelled in a Rails application as such.</p>
<pre><code>class Rocket &lt; ActiveRecord::Base
  has_many :activities, class_name: "RocketActivity"

  def current_activity
    activities.order("created_at DESC").first
  end
end</code></pre>
<pre><code>rocket.activities.map(&amp;:status) # [landed, landing, cruising, launch, landed]
rocket.current_activity.status # landed</code></pre>
<p>This seems like a fairly typical solution, and would provide you with a fairly easy to use accessor.</p>
<pre><code>rocket.current_activity.status</code></pre>
<p>But what if you wanted to view all the current activities of all the rockets you have in your fleet.</p>
<pre><code>@rockets.each do |rocket|
  rocket.current_activity.status
end</code></pre>
<p>Works great, but creates an n+1 problem, where every line is executing a query to find all the activities and return the latest. </p>
<p>A solution to this is to use includes, you could define rockets as</p>
<pre><code>@rockets = Rocket.scope.includes(:activities)</code></pre>
<p>Still, your custom logic inside the <code>#current_activity</code> method is then going to have to perform more sql on that collection.</p>
<p>Also it shouldn&#8217;t be the Rocket&#8217;s responsibility to know how to find it&#8217;s most recent activity, that&#8217;s more of a class concern.</p>
<p>Given that, in Rails land as long as you had a class to take care of it, you could define that relationship as.</p>
<pre><code>class Rocket &lt; ActiveRecord::Base
  has_many :activities, class_name: "RocketActivity"
  has_one :current_activity, class_name: "RocketCurrentActivity"
end</code></pre>
<p>Leaving us to include the current_activity and output more efficiently.</p>
<pre><code>@rockets = Rocket.scope.includes(:current_activity)</code></pre>
<p>And keep our view the same.</p>
<pre><code>@rockets.each do |rocket|
  rocket.current_activity.status
end</code></pre>
<p>We solved this with <a href="http://en.wikipedia.org/wiki/View_(database)" title="SQL views" target="_blank">SQL views (Wikipedia)</a>, and next week I&#8217;m going to show you how.</p>
<p>The post <a href="http://pivotallabs.com/rails-sql-views-1/">Rails and SQL Views &#8211; Part 1</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/rails-sql-views-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iFrame you frame&#8230;</title>
		<link>http://pivotallabs.com/iframe-you-frame/</link>
		<comments>http://pivotallabs.com/iframe-you-frame/#comments</comments>
		<pubDate>Tue, 05 Feb 2013 17:24:57 +0000</pubDate>
		<dc:creator>George Dean</dc:creator>
				<category><![CDATA[Standup]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[capybara]]></category>
		<category><![CDATA[homebrew]]></category>
		<category><![CDATA[mediaquery]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[sqllite]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/?p=15045</guid>
		<description><![CDATA[<p>Helps undefined method `fields&#39; for nil We keep getting this error across multiple projects &#38; machines, with different versions of the pg gem and the PostgreSQL server, with varying consistency. Switching the tests to sqlite seems to fix it. Failure/Error: Unable to find matching line from backtrace ActiveRecord::StatementInvalid: NoMethodError: undefined method `fields&#39; for nil:NilClass: SELECT &#34;locations&#34;.* FROM &#34;locations&#34; WHERE &#34;locations&#34;.&#34;id&#34; IN (11, 10, 5, 4, 1, 3, 2) It appears that PG::Connection#async_exec is returning nil for some queries. It was suggested to add a &#34;fields&#34; method for the nil class. Interestings Homebrew is Kickstarting an automated test bot http://www.kickstarter.com/projects/homebrew/brew-test-bot £50 to get a pair of pint glasses&#8230; Resizing the window to test media queries If you want to test your media query css as part of your acceptance suite, you can just ask Capybara to resize the firefox window. Don&#39;t use the iframe to do this, because it will be&#8230;</p><p>The post <a href="http://pivotallabs.com/iframe-you-frame/">iFrame you frame&#8230;</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<h2>Helps</h2>
<h3>undefined method `fields&#39; for nil</h3>
<p>We keep getting this error across multiple projects &amp; machines, with different versions of the pg gem and the PostgreSQL server, with varying consistency. Switching the tests to sqlite seems to fix it.</p>
<pre><code>Failure/Error: Unable to find matching line from backtrace
ActiveRecord::StatementInvalid:
  NoMethodError: undefined method `fields&#39; for nil:NilClass: SELECT &quot;locations&quot;.* FROM &quot;locations&quot;  WHERE &quot;locations&quot;.&quot;id&quot; IN (11, 10, 5, 4, 1, 3, 2)
</code></pre>
<p>It appears that PG::Connection#async_exec is returning nil for some queries.</p>
<p>It was suggested to add a &quot;fields&quot; method for the nil class.</p>
<h2>Interestings</h2>
<h3>Homebrew is Kickstarting an automated test bot</h3>
<p><a href="http://www.kickstarter.com/projects/homebrew/brew-test-bot">http://www.kickstarter.com/projects/homebrew/brew-test-bot</a> £50 to get a pair of pint glasses&#8230;</p>
<h3>Resizing the window to test media queries</h3>
<p>If you want to test your media query css as part of your acceptance suite, you can just ask Capybara to resize the firefox window.<br />
Don&#39;t use the iframe to do this, because it will be buggy. Note you will need to use min/max-width not min/max-device-width when defining your media queries. Otherwise resizing the browser window will not trigger the media query.</p>
<p>Capybara.current_session.driver.browser.manage.window.resize_to(width, height)<br />
Capybara.current_session.driver.browser.manage.window.position = Struct.new(:x, :y).new(session_name == :default ? 0 : width, 0)</p>
<p>Thanks to Ryan for the tip.</p>
<p>The post <a href="http://pivotallabs.com/iframe-you-frame/">iFrame you frame&#8230;</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/iframe-you-frame/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New York Standup 11/24/2008</title>
		<link>http://pivotallabs.com/new-york-standup-11-24-2008/</link>
		<comments>http://pivotallabs.com/new-york-standup-11-24-2008/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 21:05:00 +0000</pubDate>
		<dc:creator>Mike Grafton</dc:creator>
				<category><![CDATA[Standup]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/new-york-standup-11-24-2008/</guid>
		<description><![CDATA[<p><h1>Interesting</h1>

<ul>
<li><p>Rails 2.2.2 <a href="http://weblog.rubyonrails.org/2008/11/21/rails-2-2-i18n-http-validators-thread-safety-jruby-1-9-compatibility-docs">is released</a>!</p></li>
<li><p>Even Rails 2.2.2 isn't always threadsafe.  I found this out by running a script with JRuby from the command line.  The script loaded the Rails environment and then launched two threads that simply tried to resolve an ActiveRecord class constant.  Fireworks &#40;in the form of LoadError&#41; ensued deep inside of const_missing.  I'll post the full example later today.  </p></li>
<li><p>Tsearch2 is now built into Postgres &#40;as of 8.3&#41;.  This means you must remove the metadata from your tables, since Postgres now stores it in a separate place.</p></li>
</ul> <a href="http://pivotallabs.com/new-york-standup-11-24-2008/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>The post <a href="http://pivotallabs.com/new-york-standup-11-24-2008/">New York Standup 11/24/2008</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<h1>Interesting</h1>
<ul>
<li>
<p>Rails 2.2.2 <a href="http://weblog.rubyonrails.org/2008/11/21/rails-2-2-i18n-http-validators-thread-safety-jruby-1-9-compatibility-docs">is released</a>!</p>
</li>
<li>
<p>Even Rails 2.2.2 isn&#8217;t always threadsafe.  I found this out by running a script with JRuby from the command line.  The script loaded the Rails environment and then launched two threads that simply tried to resolve an ActiveRecord class constant.  Fireworks &#40;in the form of LoadError&#41; ensued deep inside of const_missing.  I&#8217;ll post the full example later today.  </p>
</li>
<li>
<p>Tsearch2 is now built into Postgres &#40;as of 8.3&#41;.  This means you must remove the metadata from your tables, since Postgres now stores it in a separate place.</p>
</li>
</ul>
<p>The post <a href="http://pivotallabs.com/new-york-standup-11-24-2008/">New York Standup 11/24/2008</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/new-york-standup-11-24-2008/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using apc
Database Caching 3/13 queries in 0.027 seconds using apc
Object Caching 648/680 objects using apc

 Served from: pivotallabs.com @ 2013-05-24 22:19:42 by W3 Total Cache -->