<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[theory.pm]]></title>
  <link href="http://theory.pm/atom.xml" rel="self"/>
  <link href="http://theory.pm/"/>
  <updated>2016-07-11T13:37:26+01:00</updated>
  <id>http://theory.pm/</id>
  <author>
    <name><![CDATA[David E. Wheeler]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
    <entry>
      




<title type="html"><![CDATA[Wanted: New SVN::Notify Maintainer]]></title>
<link href="http://theory.pm/2016/07/11/svn-notify-maintainer-wanted/"/>
<updated>2016-07-11T13:22:00+01:00</updated>
<id>http://theory.pm/2016/07/11/svn-notify-maintainer-wanted</id>

      <content type="html"><![CDATA[<p>I&rsquo;ve used Subversion very occasionally <a href="http://justatheory.com/computers/vcs/git/bricolage-svn-to-git.html" title="Just a Theory: “Migrating Bricolage Subversion to Git”">since 2009</a>, and <a href="https://metacpan.org/release/SVN-Notify" title="SVN-Notify on MetaCPAN">SVN::Notify</a> at
all. Over the years, I&rsquo;ve fixed minor issues with it now and then, and made
the a couple of releases to address issues fixed by others. But it&rsquo;s past the
point where I feel qualified to maintain it. Hell, the repository for
SVN::Notify has been <a href="https://github.com/theory/svn-notify/" title="svn-notify Project on GitHub">hosted on GitHub</a> ever <a href="https://github.com/theory/svn-notify/commit/32347ad" title="SVN-Notify Commit: “Note move to GitHub.”">since 2011</a>. I don&rsquo;t have an
instance of Subversion against which to test it; nor do I have any SMTP
servers to throw test messages at.</p>

<p>In short, it&rsquo;s past time I relinquished maintenance of this module to someone
with a vested interest in its continued use. Is that you? Do you need to keep SVN::Notify running for your projects, and have a few <a href="https://en.wiktionary.org/wiki/tuit" title="Wictionary: “tuit”">TUITs</a> to fix the occasional bug or security issue? If so, drop me a line (david @ this domain). I’d be happy to transfer the repository.</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[The Blockchain Hype Cycle →]]></title>
<link href="http://techcrunch.com/2016/05/11/the-blockchain-is-the-new-google/"/>
<updated>2016-05-12T11:32:00+01:00</updated>
<id>http://theory.pm/security/2016/05/12/the-blockchain-hype-cycle</id>

      <content type="html"><![CDATA[<p>Excerpt from William Mougayar&rsquo;s new book on TechCrunch:</p>

<blockquote><p>At its core, the blockchain is a technology that permanently records transactions in a way that cannot be later erased but can only be sequentially updated, in essence keeping a never-ending historical trail. This seemingly simple functional description has gargantuan implications. It is making us rethink the old ways of creating transactions, storing data, and moving assets, and that’s only the beginning.</p>

<p>The blockchain cannot be described just as a revolution. It is a tsunami-like phenomenon, slowly advancing and gradually enveloping everything along its way by the force of its progression. Plainly, it is the second significant overlay on top of the Internet, just as the Web was that first layer back in 1990. That new layer is mostly about trust, so we could call it the trust layer.</p></blockquote>

<p>What a steaming pile of hype and nonsense. I find it hard to take such revolutionary fervor seriously, as if people forget the Web in the 90s or real estate in 2006. Given that <a href="https://www.linkedin.com/in/williammougayar">the author is a venture capitalist</a> invested in a <a href="https://www.crunchbase.com/organization/openbazaar">blockchain startup</a>, it just feels like a way to try to inflate the value of his investments for short-term gain. A piece like this is snake oil.</p>

<p>Blockchains are inarguably useful tools, like databases or encryption algorithms, and we in the technology business should do our best to understand how thet work and figure out the applications for which they make sense. I’m still trying to wrap my mind around blockchains, but one thing I understand very well: <strong>they&rsquo;re not a panacea.</strong> The industry overall won’t see true benefits from blockchains for a couple of years, once the practicalities have been worked out and the nonsense has subsided. We should learn and contribute to those practicalities, but as for the <a href="https://en.wikipedia.org/wiki/Hype_cycle">hype cycle</a>, for now I just hold my nose.</p>
<p><a rel="bookmark" href="http://theory.pm/security/2016/05/12/the-blockchain-hype-cycle/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[A Porous “Privacy Shield” →]]></title>
<link href="http://arstechnica.com/tech-policy/2016/02/privacy-shield-doomed-from-get-go-nsa-bulk-surveillance-waved-through/"/>
<updated>2016-02-29T11:34:00+00:00</updated>
<id>http://theory.pm/privacy/2016/02/29/privacy-shield</id>

      <content type="html"><![CDATA[<p>Glyn Moody, in <em>Ars Technica,</em> on the proposed replacement for the recently
struck-down Safe Harbor framework:</p>

<blockquote><p>However, with what seems like extraordinarily bad timing, President Obama
has just made winning the trust of EU citizens even harder. As
<a href="http://arstechnica.com/tech-policy/2016/02/obama-administration-closing-in-on-rules-to-let-nsa-share-more-freely-with-fbi-cia/" title="Obama administration closing in on rules to let NSA share more freely with FBI, CIA">Ars reported last week</a>, the Obama administration is close to allowing the
NSA to share more of the private communications it intercepts with other
federal agencies, including the FBI and the CIA, without removing
identifying information first.</p>

<p>In other words, not only will the new Privacy Shield allow the NSA to
continue to scoop up huge quantities of personal data from EU citizens, it
may soon be allowed to share them widely. That&rsquo;s unlikely to go down well
with Europeans, the Article 29 Working Party, or the CJEU&mdash;all of which
ironically increases the likelihood that the new Privacy Shield will suffer
the same fate as the Safe Harbour scheme it has been designed to replace.</p></blockquote>

<p>So let me get this straight. Under this proposal:</p>

<ul>
<li>The NSA can continue to bulk collect EU citizen data.</li>
<li>That data may be shared with other agencies in the US government.</li>
<li>Said collection must fall under six allowed case, one of wich is
undefined &ldquo;counter-terrorism&rdquo; purposes. No one ever abused <em>that</em> kind of
thing before.</li>
<li>The US claims there is no more bulk surveillance, except that there is under those six cases.</li>
<li>The appointed &ldquo;independent ombudsman&rdquo; to address complaints by EU citizens will be a single US Undersecretary of State.</li>
<li>Complaints can also be addressed to US companies housing EU citizen data, even though, in the absence of another Snowden-scale whistle-blowing, they may have no idea their data is being surveilled.</li>
</ul>


<p>Color me skeptical that this would work, let alone not be thrown out by another case similar to the one that killed Safe Harbor.</p>

<p>I have a better idea. How about eliminating mass surveillance?</p>
<p><a rel="bookmark" href="http://theory.pm/privacy/2016/02/29/privacy-shield/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Do We Have Right to Security? →]]></title>
<link href="https://securosis.com/blog/do-we-have-a-right-to-security"/>
<updated>2016-02-19T15:26:00+00:00</updated>
<id>http://theory.pm/security/2016/02/19/right-to-security</id>

      <content type="html"><![CDATA[<p>Rich Mogull:</p>

<blockquote><p>Don’t be distracted by the technical details. The model of phone, the method
of encryption, the detailed description of the specific attack technique,
and even the feasibility are all irrelevant.</p>

<p>Don’t be distracted by the legal wrangling. By the timing, the courts, or
the laws in question. Nor by politicians, proposed legislation, Snowden, or
speeches at think tanks or universities.</p>

<p>Don’t be distracted by who is involved. Apple, the FBI, dead terrorists, or
common drug dealers.</p>

<p>Everything, all of it, boils down to a single question.</p>

<p><em>Do we have a right to security?</em></p></blockquote>

<p>How about we introduce a bill guaranteeing a right to security. <a href="https://twitter.com/theory/status/700823603122540544">Senator Wyden</a>?</p>

<p>(<a href="http://daringfireball.net/linked/2016/02/19/right-to-security">Via DF</a>)</p>
<p><a rel="bookmark" href="http://theory.pm/security/2016/02/19/right-to-security/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Anthem Breach Harms Consumers →]]></title>
<link href="https://digitalguardian.com/blog/judge-anthem-case-rules-breach-harmed-patients"/>
<updated>2016-02-19T13:56:00+00:00</updated>
<id>http://theory.pm/security/2016/02/19/anthem-breach</id>

      <content type="html"><![CDATA[<p>Paul Roberts in Digital Guardian:</p>

<blockquote><p>Whether or not harm has occurred to plaintiffs is critical for courts to
decide whether the plaintiff has a right – or “standing” – to sue in the
first place. But proving that data exposed in a breach has actually been
used for fraud is notoriously difficult.</p>

<p>In her decision in the Anthem case, [U.S. District Judge Lucy] Koh reasoned
that the theft of personal identification information is harm to consumers
in itself, regardless of whether any subsequent misuse of it can be proven.
Allegations of a “concrete and imminent threat of future harm&#8221; are enough
to establish an injury and standing in the early stages of a breach suit,
she said.</p></blockquote>

<p>Seems like a no-brainer to me. Personal information is just that: <em>personal.</em>
Organizations that collect and store personal information must take every
step they can to protect it. Failure to do so harms their users, exposing
them to increased risk of identity theft, fraud, surveillance, and abuse. It&rsquo;s
reasonable to expect that firms not be insulated from litigation for failing
to protect user data.</p>
<p><a rel="bookmark" href="http://theory.pm/security/2016/02/19/anthem-breach/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Apple Challenges FBI Decryption Demand →]]></title>
<link href="https://www.apple.com/customer-letter/"/>
<updated>2016-02-17T11:30:00+00:00</updated>
<id>http://theory.pm/encryption/2016/02/17/apple-encryption-stand</id>

      <content type="html"><![CDATA[<p>Incredible post from Apple, signed by Tim Cook:</p>

<blockquote><p>The government is asking Apple to hack our own users and undermine decades
of security advancements that protect our customers — including tens of
millions of American citizens — from sophisticated hackers and
cybercriminals. The same engineers who built strong encryption into the
iPhone to protect our users would, ironically, be ordered to weaken those
protections and make our users less safe.</p>

<p>We can find no precedent for an American company being forced to expose its
customers to a greater risk of attack. For years, cryptologists and national
security experts have been warning against weakening encryption. Doing so
would hurt only the well-meaning and law-abiding citizens who rely on
companies like Apple to protect their data. Criminals and bad actors will
still encrypt, using tools that are readily available to them.</p></blockquote>

<p>I only wish there was a place to co-sign. Companies must do all they can to
safeguard the privacy of their users, preferably such only users can unlock
and access their personal information. It&rsquo;s in the interest of the government
to ensure that private data remain private. Forcing Apple to crack its own
encryption sets a dangerous precedent likely to be exploited by
cybercriminals for decades to come. Shame on the FBI.</p>
<p><a rel="bookmark" href="http://theory.pm/encryption/2016/02/17/apple-encryption-stand/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[theory.so is No More]]></title>
<link href="http://theory.pm/blog/2015/10/03/theory-dot-so-is-no-more/"/>
<updated>2015-10-03T13:14:00+01:00</updated>
<id>http://theory.pm/blog/2015/10/03/theory-dot-so-is-no-more</id>

      <content type="html"><![CDATA[<p>Until last week, I had two newish blogs. This one, <a href="http://theory.pm/">theory.pm</a>, was to be my Perl blog. The other one, theory.so, was my database blog. I thought it would be a good idea to have separate blogs for separate audiences, but it turns out I don&rsquo;t post enough to make much difference. And now, as of last week, I let the theory.so domain expire. Control the <a href="https://en.wikipedia.org/wiki/.so">.so</a> domain was turned over to Somalia a few months ago, and domain renwal fees went <em>way</em> up. Since I had so few posts over there (14 since August, 2013), I decided it was a good time to just merge it with theory.pm and be done with it.</p>

<p>So my apologies if a bunch of my old posts just showed up in your RSS readers. (You all still use RSS readers, right?). This is a one-time merging of the two blogs, so should not happen again.</p>

<p>Well…maybe. Now I have a total of 25 posts on theory.pm (since July 2013), which is still pretty paltry. I&rsquo;m thinking it&rsquo;s silly to have this thing separate from my original blog, <a href="http://justatheory.com/">Just a Theory</a>, so I might eventually merge that blog, too. Not sure what domain I&rsquo;ll use for it. Maybe I&rsquo;ll go back to justatheory.com. Or maybe I&rsquo;ll use one of the other domains I registered, like the recently added theory.one. Or maybe theory dot something else.</p>

<p>Not that you care. Good on you for reading this far. I would have stopped before now. You&rsquo;re a better person than I.</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Please Test Pod::Simple 3.29_3]]></title>
<link href="http://theory.pm/2015/02/11/please-test-pod-simple-3-dot-29-3/"/>
<updated>2015-02-11T10:53:00+00:00</updated>
<id>http://theory.pm/2015/02/11/please-test-pod-simple-3-dot-29-3</id>

      <content type="html"><![CDATA[<p><img class="left" src="http://theory.pm/images/cpan.png" width="250" height="250" title="'Pod Book'" ></p>

<p>I&rsquo;ve just pushed <a href="https://metacpan.org/release/Pod-Simple/">Pod-Simple</a> 3.29_v3 to CPAN. <a href="https://metacpan.org/author/KHW">Karl Williamson</a> did a lot of
hacking on this release, finally adding support for EBCDIC. But as part of
that work, and in coordination with Pod::Simple&rsquo;s original author, <a href="http://interglacial.com/">Sean
Burke</a>, as well as <a href="http://lists.perl.org/list/pod-people.html">pod-people</a>, we have switched the default encoding from
<a href="http://en.wikipedia.org/wiki/ISO/IEC_8859-1">Latin-1</a> to <a href="http://en.wikipedia.org/wiki/Windows-1252">CP-1252</a>.</p>

<p>On the surface, that might sound like a big change, but in truth, it&rsquo;s pretty
straight-forward. CP-1252 is effectively a superset of Latin-1, repurposing
30 or so unused control characters from Latin-1. Those characters are pretty
common on Windows (the home of the CP family of encodings), especially in
pastes from Word. It&rsquo;s nice to be able to pick those up essentially for free.</p>

<p>Still, Karl&rsquo;s done more than that. He also updated the encoding detection to
do a better job at detecting UTF-8. This is the <em>real</em> default. Pod::Simple
only falls back on CP1252 if there are no obvious UTF-8 byte sequences in
your Pod.</p>

<p>Overall these changes should be a great improvement. Better encoding support
is always a good idea. But it is a pretty significant change, including a
change to <a href="https://metacpan.org/pod/distribution/perl/pod/perlpodspec.pod">the Pod spec</a>. Hence the test release. Please make sure it works
well with your code by installing it today:</p>

<pre><code>cpan D/DW/DWHEELER/Pod-Simple-3.29_3.tar.gz
cpanm DWHEELER/Pod-Simple-3.29_3.tar.gz
</code></pre>

<p>Oh, and one last thing: If Pod::Simple fails to properly recognize the encoding in your Pod file, you can always use the <code>=encoding</code> command early in your Pod file to make it explicit:</p>

<pre><code>=encoding CP1254
</code></pre>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Build Modern Perl RPMs with rpmcpan]]></title>
<link href="http://theory.pm/rpm/2014/09/21/build-modern-perl-rpms-with-rpmcpan/"/>
<updated>2014-09-21T14:45:00+01:00</updated>
<id>http://theory.pm/rpm/2014/09/21/build-modern-perl-rpms-with-rpmcpan</id>

      <content type="html"><![CDATA[<p><img class="center" src="http://theory.pm/images/ioperllove.png" width="640" title="iovation + Perl = Love" alt="iovation + Perl = Love"></p>

<p>We&rsquo;ve been using the CentOS Perl RPMs at <a href="http://iovation.com/">iovation</a> to run all of our Perl
applications. This has been somewhat painful, because the version of Perl,
5.10.1, is quite old &mdash; it shipped in August 2009. In fact, it consists
mostly of bug fixes against Perl 5.10.0, which shipped in December 2007! Many
of the modules provided by CentOS core and <a href="https://fedoraproject.org/wiki/EPEL" title="Extra Packages for Enterprise Linux">EPEL</a> are quite old, as well, and
we had built up quite the collection of customized module RPMs managed by a
massive spaghetti-coded Jenkins job. When we recently ran into a Unicode
issue that would best have been addressed by running a more modern Perl &mdash;
rather than a <a href="http://grokbase.com/t/perl/perl5-porters/147gfvrd2n/encode-vs-json#20140723oncbjv4rddo66735xess5wo77a" title="“Encode vs. JSON” on Perl 5 Porters">hinky workaround</a> &mdash; I finally sat down and knocked out a way
to get a solid set of Modern Perl and related CPAN RPMs.</p>

<p>I gave it the rather boring name <code>rpmcpan</code>, and now <a href="https://github.com/iovation/rpmcpan" title="rpmcpan on GitHub">you can use it, too</a>.
Turns out, <a href="http://twitter.com/aaronblew" title="Aaron Blew: SRE Manager (dun dun duuuuun!)">DevOps</a> doesn&rsquo;t myopically insist on using core RPMs in the name
of some abstract idea about stability. Rather, we just need a way to easily
deploy our stuff as RPMs. If the same applies to your organization, you can
get Modern Perl RPMs, too.</p>

<p>Here&rsquo;s how we do it. We have a new Jenkins job that runs both nightly and
whenever the <a href="https://github.com/iovation/rpmcpan" title="rpmcpan on GitHub"><code>rpmcpan</code> Git repository</a> updates. It uses the <a href="https://metacpan.org/">MetaCPAN</a> API
to build the latest versions of everything we need. Here&rsquo;s how to get it to
build the latest version of Perl, 5.20.1:</p>

<pre><code>./bin/rpmcpan --version 5.20.1
</code></pre>

<p>That will get you a nice, modern Perl RPM, named <code>perl520</code>, completely encapsulated in <code>/usr/local/perl520</code>. Want 5.18 instead: Just change the version:</p>

<pre><code>./bin/rpmcpan --version 5.18.2
</code></pre>

<p>That will give you <code>perl518</code>. But that&rsquo;s not all. You want to build CPAN
distributions against that version. Easy. Just edit the <a href="https://github.com/iovation/rpmcpan/blob/master/etc/dists.json"><code>dists.json</code> file</a>.
Its contents are a JSON object where the keys name CPAN distributions (not
modules), and the values are objects that customize our RPMs get built. Most
of the time, the objects can be empty:</p>

<pre><code>"Try-Tiny": {},
</code></pre>

<p>This results in an RPM named <code>perl520-Try-Tiny</code> (or <code>perl518-Try-Tiny</code>,
etc.). Sometimes you might need additional information to customize the CPAN
spec file generated to build the distribution. For example, since this is
Linux, we need to exclude a Win32 dependency in the <a href="http://search.cpan.org/dist/Encode-Locale" title="Encode-Locale on CPAN">Encode-Locale</a>
distribution:</p>

<pre><code>"Encode-Locale": { "exclude_requires": ["Win32::Console"] },
</code></pre>

<p>Other distributions might require additional RPMs or environment variables,
like <a href="http://search.cpan/org/dist/DBD-Pg/" title="DBD-Pg on CPAN">DBD-Pg</a>, which requires the <a href="http://yum.postgresql.org" title="PostgreSQL Yum Repository">PostgreSQL RPMs</a>:</p>

<pre><code>"DBD-Pg": {
    "build_requires": ["postgresql93-devel", "postgresql93"],
    "environment": { "POSTGRES_HOME": "/usr/pgsql-9.3" }
},
</code></pre>

<p>See the <a href="https://github.com/iovation/rpmcpan/blob/master/README.md" title="`rpmcpan README`">README</a> for a complete list of customization options. Or just get
started with our <a href="https://github.com/iovation/rpmcpan/blob/master/etc/dists.json"><code>dists.json</code> file</a>, which so far builds the bare minimum we
need for one of our Perl apps. Add new distributions? Send a <a href="https://github.com/iovation/rpmcpan/pulls">pull request</a>!
We&rsquo;ll be doing so as we integrate more of our Perl apps with a Modern Perl
and leave the sad RPM past behind.</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Sqitch on FLOSS Weekly →]]></title>
<link href="https://twit.tv/shows/floss-weekly/episodes/309"/>
<updated>2014-09-17T20:42:00+01:00</updated>
<id>http://theory.pm/podcast/2014/09/17/sqitch-on-floss-weekly</id>

      <content type="html"><![CDATA[<p>Yours truly was feature in this week&rsquo;s episode of <a href="http://twit.tv/floss">FLOSS Weekly</a>, talking
about Sqitch. I feel pretty good about this interview, despite continually
banging on my legs, the table, and the mic. It&rsquo;s interesting to try to
communicate what Sqitch is about purely by talking.</p>

<p><video x-webkit-airplay="allow" webkit-playsinline="" src="http://www.podtrac.com/pts/redirect.mp4/twit.cachefly.net/video/floss/floss0309/floss0309_h264m_864x480_500.mp4" width="100%" height="100%" controls></video></p>

<p>If it&rsquo;s enough to get you interested in giving a try, try <a href="http://sqitch.org/">installing it</a> and
using working through one of the tutorials:</p>

<ul>
<li><a href="https://metacpan.org/module/sqitchtutorial%20%22Sqitch%20PostgreSQL%20Tutorial">PostgreSQL</a></li>
<li><a href="https://metacpan.org/module/sqitchtutorial-sqlite%20%22Sqitch%20SQLite%20Tutorial">SQLite</a></li>
<li><a href="https://metacpan.org/module/sqitchtutorial-oracle%20%22Sqitch%20Oracle%20Tutorial">Oracle</a></li>
<li><a href="https://metacpan.org/module/sqitchtutorial-mysql%20%22Sqitch%20MySQL%20Tutorial">MySQL</a></li>
<li><a href="https://metacpan.org/module/sqitchtutorial-firebird%20%22Sqitch%20Firebird%20Tutorial">Firebird</a></li>
<li><a href="https://metacpan.org/module/sqitchtutorial-vertica%20%22Sqitch%20Vertica%20Tutorial">Vertica</a></li>
</ul>

<p><a rel="bookmark" href="http://theory.pm/podcast/2014/09/17/sqitch-on-floss-weekly/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Localize Your Perl Apps with this One Weird Trick]]></title>
<link href="http://theory.pm/l10n/2014/09/06/localize-your-perl-apps-with-this-one-weird-trick/"/>
<updated>2014-09-06T18:10:00+01:00</updated>
<id>http://theory.pm/l10n/2014/09/06/localize-your-perl-apps-with-this-one-weird-trick</id>

      <content type="html"><![CDATA[<p>Nota Bene: This is a republication of a <a href="http://perladvent.org/2013/2013-12-09.html">post that originally appeared in the
2013 Perl Advent Calendar</a>.</p>

<hr />

<p>These days, <a href="http://en.wikipedia.org/wiki/Gettext" title="Wikipedia: “gettext”">gettext</a> is far and away the most widely-used localization
(<a href="http://en.wikipedia.org/wiki/Language_localisation" title="Wikipedia: “Localization”">l10n</a>) and internationalization (<a href="http://en.wikipedia.org/wiki/Internationalization_and_localization" title="Wikipedia: “Internationalization and Localization”">i18n</a>) library for open-source software. So
far, it has not been widely used in the Perl community, even though it&rsquo;s the
most flexible, capable, and easy-to use solution, thanks to
<a href="https://metacpan.org/module/Locale::TextDomain" title="Locale::TextDomain on CPAN">Locale::TextDomain</a>.<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> How easy? Let&rsquo;s get started!</p>

<h3>Module Internationale</h3>

<p>First, just <em>use</em> Locale::TextDomain. Say you&rsquo;re creating an awesome new
module, Awesome::Module. These CPAN distribution will be named
<code>Awesome-Module</code>, so that&rsquo;s the &ldquo;domain&rdquo; to use for its localizations. Just let
Locale::TextDomain know:</p>

<figure class='code'><figcaption><span>Establish Dominion</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="k">use</span> <span class="nn">Locale::</span><span class="n">TextDomain</span> <span class="s">&#39;Awesome-Module&#39;</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Locale::TextDomain will later use this string to look for the appropriate
translation catalogs. But don&rsquo;t worry about that just yet. Instead, start using
it to translate user-visible strings in your code. With the assistance of the
Locale::TextDomain&rsquo;s [comprehensive documentation], you&rsquo;ll find it second nature
to internationalize your modules in no time. For example, simple strings are
denoted with <code>__</code>:</p>

<figure class='code'><figcaption><span>Trekkie Greetings</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="n">say</span> <span class="n">__</span> <span class="s">&#39;Greetings puny human!&#39;</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you need to specify variables, use <code>__x</code>:</p>

<figure class='code'><figcaption><span>Animal Thanks</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="n">say</span> <span class="n">__x</span><span class="p">(</span>
</span><span class='line'>   <span class="s">&#39;Thank you {sir}, may I have another?&#39;</span><span class="p">,</span>
</span><span class='line'>   <span class="n">sir</span> <span class="o">=&gt;</span> <span class="nv">$username</span><span class="p">,</span>
</span><span class='line'><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Need to manage plurals? Use <code>__n</code>:</p>

<figure class='code'><figcaption><span>Obligatory Python Reference</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="n">say</span> <span class="n">__n</span><span class="p">(</span>
</span><span class='line'>    <span class="s">&#39;I will not buy this record, it is scratched.&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="s">&#39;I will not buy these records, they are scratched.&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nv">$num_records</span>
</span><span class='line'><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>If <code>$num_records</code> is 1, the first phrase will be used. Otherwise the second.</p>

<p>Sometimes you gotta do both, mix variables and plurals. <code>__nx</code> has got you
covered there:</p>

<figure class='code'><figcaption><span>Deeper Understanding</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="n">say</span> <span class="n">__nx</span><span class="p">(</span>
</span><span class='line'>    <span class="s">&#39;One item has been grokked.&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="s">&#39;{count} items have been grokked.&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="nv">$num_items</span><span class="p">,</span>
</span><span class='line'>    <span class="n">count</span> <span class="o">=&gt;</span> <span class="nv">$num_items</span><span class="p">,</span>
</span><span class='line'><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Congratulations! Your module is now internationalized. Wasn&rsquo;t that easy? Make a
habit of using these functions in all the modules in your distribution, always
with the <code>Awesome-Module</code> domain, and you&rsquo;ll be set.</p>

<h4>Encode da Code</h4>

<p>Locale::TextDomain is great, but it dates from a time when Perl character
encoding was, shall we say, sub-optimal. It therefore took it upon itself to
try to do the right thing, which is to to detect the locale from the runtime
environment and automatically encode as appropriate. Which might work okay if
all you ever do is print localized messages &mdash; and never anything else.</p>

<p>If, on the other hand, you will be manipulating localized strings in your code,
or emitting unlocalized text (such as that provided by the user or read from a
database), then it&rsquo;s probably best to coerce Locale::TextDomain to return Perl
strings, rather than encoded bytes. There&rsquo;s no formal interface for this in
Locale::TextDomain, so we have to hack it a bit: set the <code>$OUTPUT_CHARSET</code>
environment variable to &ldquo;UTF-8&rdquo; and then bind a filter. Don&rsquo;t know what that
means? Me neither. Just put this code somewhere in your distribution where it
will always run early, before anything gets localized:</p>

<figure class='code'><figcaption><span>Control Decode</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="k">use</span> <span class="nn">Locale::</span><span class="n">Messages</span> <span class="sx">qw(bind_textdomain_filter)</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="n">Encode</span><span class="p">;</span>
</span><span class='line'><span class="k">BEGIN</span> <span class="p">{</span>
</span><span class='line'>    <span class="nv">$ENV</span><span class="p">{</span><span class="n">OUTPUT_CHARSET</span><span class="p">}</span> <span class="o">=</span> <span class="s">&#39;UTF-8&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="n">bind_textdomain_filter</span> <span class="s">&#39;Awesome-Module&#39;</span> <span class="o">=&gt;</span> <span class="o">\&amp;</span><span class="nn">Encode::</span><span class="n">decode_utf8</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You only have to do this once per domain. So even if you use Locale::TextDomain
with the <code>Awesome-Module</code> domain in a bunch of your modules, the presence of
this code in a single early-loading module ensures that strings will always be
returned as Perl strings by the localization functions.</p>

<h4>Environmental Safety</h4>

<p>So what about output? There&rsquo;s one more bit of boilerplate you&rsquo;ll need to throw
in. Or rather, put this into the <code>main</code> package that uses your modules to begin
with, such as the command-line script the user invokes to run an application.</p>

<p>First, on the shebang line, follow <a href="http://stackoverflow.com/a/6163129/79202" title="Perl UTF-8 Recommendations on Stack Overflow">Tom Christiansen&rsquo;s advice</a> and put <code>-CAS</code>
in it (or set the <code>$PERL_UNICODE</code> environment variable to <code>AS</code>). Then use the
<a href="https://metacpan.org/pod/POSIX#setlocale" title="POSIX on CPAN">POSIX <code>setlocale</code></a> function to the appropriate locale for the runtime
environment. How? Like this:</p>

<figure class='code'><figcaption><span>Localization Boilerplate</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="c1">#!/usr/bin/perl -CAS</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="n">v5</span><span class="mf">.12</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="n">warnings</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="n">utf8</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="n">POSIX</span> <span class="sx">qw(setlocale)</span><span class="p">;</span>
</span><span class='line'><span class="k">BEGIN</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="vg">$^O</span> <span class="ow">eq</span> <span class="s">&#39;MSWin32&#39;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nb">require</span> <span class="nn">Win32::</span><span class="n">Locale</span><span class="p">;</span>
</span><span class='line'>        <span class="n">setlocale</span> <span class="nn">POSIX::</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="nn">Win32::Locale::</span><span class="n">get_locale</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">setlocale</span> <span class="nn">POSIX::</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nn">Awesome::</span><span class="n">Module</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Locale::TextDomain will notice the locale and select the appropriate translation
catalog at runtime.</p>

<h3>Is that All There Is?</h3>

<p>Now what? Well, you could do nothing. Ship your code and those
internationalized phrases will be handled just like any other string in your
code.</p>

<p>But what&rsquo;s the point of that? The real goal is to get these things translated.
There are two parts to that process:</p>

<ol>
<li><p>Parsing the internationalized strings from your modules and creating
language-specific translation catalogs, or &ldquo;<a href="https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html" title="GNU gettext: PO Files">PO files</a>&rdquo;, for translators to
edit. These catalogs should be maintained in your source code repository.</p></li>
<li><p>Compiling the PO files into binary files, or &ldquo;<a href="https://www.gnu.org/software/gettext/manual/html_node/MO-Files.html" title="GNU gettext: MO Files">MO files</a>&rdquo;, and distributing
them with your modules. These files should <em>not</em> be maintained in
your source code repository.</p></li>
</ol>


<p>Until a year ago, there was no Perl-native way to manage these processes.
Locale::TextDomain ships with a <a href="https://metacpan.org/source/GUIDO/libintl-perl-1.23/sample/simplecal/po/Makefile" title="Sample Locale::TextDomain Makefile">sample Makefile</a> demonstrating the appropriate
use of the <a href="https://www.gnu.org/software/gettext/">GNU gettext</a> command-line tools, but that seemed a steep price for
a Perl hacker to pay.</p>

<p>A better fit for the Perl hacker&rsquo;s brain, I thought, is Dist::Zilla. So I wrote
<a href="https://metacpan.org/module/Dist::Zilla::LocaleTextDomain" title="Dist::Zilla::LocaleTextDomain on CPAN">Dist::Zilla::LocaleTextDomain</a> to encapsulate the use of the gettext
utiltiies. Here&rsquo;s how it works.</p>

<p>First, configuring Dist::Zilla to compile localization catalogs for
distribution: add these lines to your <code>dist.ini</code> file:</p>

<figure class='code'><figcaption><span>LocaleTextDomain is your dist.ini</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ini'><span class='line'><span class="k">[ShareDir]</span>
</span><span class='line'><span class="k">[LocaleTextDomain]</span>
</span></code></pre></td></tr></table></div></figure>


<p>There are <a href="https://metacpan.org/pod/Dist::Zilla::Plugin::LocaleTextDomain#Configuration" title="Dist::Zilla LocaleTextDomain Configuration">configuration attributes</a> for the <code>LocaleTextDomain</code> plugin, such as
where to find the PO files and where to put the compiled MO files. In case you
didn&rsquo;t use your distribution name as your localization domain in your modules,
for example:</p>

<figure class='code'><figcaption><span>Go with a Java-y Domain</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="k">use</span> <span class="nn">Locale::</span><span class="n">TextDomain</span> <span class="s">&#39;com.example.perl-libawesome&#39;</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then you&rsquo;d set the <code>textdomain</code> attribute so that the <code>LocaleTextDomain</code> plugin
can find the translation catalogs:</p>

<figure class='code'><figcaption><span>The Domain in DNS</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ini'><span class='line'><span class="k">[LocaleTextDomain]</span>
</span><span class='line'><span class="na">textdomain</span> <span class="o">=</span> <span class="s">com.example.perl-libawesome</span>
</span></code></pre></td></tr></table></div></figure>


<p>Check out the <a href="https://metacpan.org/pod/Dist::Zilla::Plugin::LocaleTextDomain#Configuration" title="Dist::Zilla LocaleTextDomain Configuration">configuration docs</a> for details on all available attributes.</p>

<p>At this point, the plugin doesn&rsquo;t do much, because there are no translation
catalogs yet. You might see this line from <code>dzil build</code>, though:</p>

<figure class='code'><figcaption><span>Nothing to See Here</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="o">[</span>LocaleTextDomain<span class="o">]</span> Skipping language compilation: directory po does not exist
</span></code></pre></td></tr></table></div></figure>


<p>Let&rsquo;s give it something to do!</p>

<h3>Locale Motion</h3>

<p>To add a French translation file, use the <a href="https://metacpan.org/pod/Dist::Zilla::App::Command::msg_init" title="dzil msg-init documentation on CPAN"><code>msg-init</code></a> command<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>:</p>

<figure class='code'><figcaption><span>French Neuveau</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-init fr
</span><span class='line'>Created po/fr.po.
</span></code></pre></td></tr></table></div></figure>


<p>The <code>msg-init</code> command uses the <a href="https://www.gnu.org/software/gettext/">GNU gettext</a> utilities to scan your Perl
source code and initialize the French catalog, <code>po/fr.po</code>. This file is now
ready translation! Commit it into your source code repository so your
agile-minded French-speaking friends can find it. Use <code>msg-init</code> to create as
many language files as you like:</p>

<figure class='code'><figcaption><span>Multilingualism</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-init de ja.JIS en_US.UTF-8 en_UK.UTF-8
</span><span class='line'>Created po/de.po.
</span><span class='line'>Created po/ja.po.
</span><span class='line'>Created po/en_US.po.
</span><span class='line'>Created po/en_UK.po.
</span></code></pre></td></tr></table></div></figure>


<p>Each language has its on PO file. You can even have region-specific catalogs,
such as the <code>en_US</code> and <code>en_UK</code> variants here. Each time a catalog is updated,
the changes should be committed to the repository, like code. This allows the
latest translations to always be available for compilation and distribution.
The output from <code>dzil build</code> now looks something like:</p>

<figure class='code'><figcaption><span>Catalogs Plugged</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>po/fr.po: 10 translated messages, 1 fuzzy translation, 0 untranslated messages.
</span><span class='line'>po/ja.po: 10 translated messages, 1 fuzzy translation, 0 untranslated messages.
</span><span class='line'>po/en_US.po: 10 translated messages, 1 fuzzy translation, 0 untranslated messages.
</span><span class='line'>po/en_UK.po: 10 translated messages, 1 fuzzy translation, 0 untranslated messages.
</span></code></pre></td></tr></table></div></figure>


<p>The resulting MO files will be in the shared directory of your distribution:</p>

<figure class='code'><figcaption><span>She Want Mo Mo Mo</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% find Awesome-Module-0.01/share -type f
</span><span class='line'>Awesome-Module-0.01/share/LocaleData/de/LC_MESSAGES/Awesome-Module.mo
</span><span class='line'>Awesome-Module-0.01/share/LocaleData/en_UK/LC_MESSAGES/Awesome-Module.mo
</span><span class='line'>Awesome-Module-0.01/share/LocaleData/en_US/LC_MESSAGES/Awesome-Module.mo
</span><span class='line'>Awesome-Module-0.01/share/LocaleData/ja/LC_MESSAGES/Awesome-Module.mo
</span></code></pre></td></tr></table></div></figure>


<p>From here <a href="https://metacpan.org/module/Module::Build" title="Module::Build on CPAN">Module::Build</a> or <a href="https://metacpan.org/module/ExtUtils::MakeMaker" title="ExtUtils::MakeMaker on CPAN">ExtUtils::MakeMaker</a> will install these MO files
with the rest of your distribution, right where Locale::TextDomain can find
them at runtime. The PO files, on the other hand, won&rsquo;t be used at all, so you
might as well exclude them from the distribution. Add this line to your
<code>MANIFEST.SKIP</code> to prevent the <code>po</code> directory and its contents from being
included in the distribution:</p>

<figure class='code'><figcaption><span>No PO</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='perl'><span class='line'><span class="o">^</span><span class="n">po</span><span class="o">/</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Mergers and Acquisitions</h3>

<p>Of course no code base is static. In all likelihood, you&rsquo;ll change your code
&mdash; and end up adding, editing, and removing localizable strings as a result.
You&rsquo;ll need to periodically merge these changes into all of your translation
catalogs so that your translators can make the corresponding updates. That&rsquo;s
what the the <a href="https://metacpan.org/pod/Dist::Zilla::App::Command::msg_merge" title="dzil msg-merge documentation on CPAN"><code>msg-merge</code></a> command is for:</p>

<figure class='code'><figcaption><span>Merge Management </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-merge
</span><span class='line'>extracting gettext strings
</span><span class='line'>Merging gettext strings into po/de.po
</span><span class='line'>Merging gettext strings into po/en_UK.po
</span><span class='line'>Merging gettext strings into po/en_US.po
</span><span class='line'>Merging gettext strings into po/ja.po
</span></code></pre></td></tr></table></div></figure>


<p>This command re-scans your Perl code and updates all of the language files. Old
messages will be commented-out and new ones added. Commit the changes and give
your translators a holler so they can keep the awesome going.</p>

<h4>Template Scan</h4>

<p>The <code>msg-init</code> and <code>msg-merge</code> commands don&rsquo;t actually scan your source code.
Sort of lied about that. Sorry. What they actually do is merge a template file
into the appropriate catalog files. If this template file does not already
exist, a temporary one will be created and discarded when the initialization or
merging is done.</p>

<p>But projects commonly maintain a permanent template file, stored in the source
code repository along with the translation catalogs. For this purpose, we have
the <a href="https://metacpan.org/pod/Dist::Zilla::App::Command::msg_scan" title="dzil msg-scan documentation on CPAN"><code>msg-scan</code></a> command. Use it to create or update the template, or POT file:</p>

<figure class='code'><figcaption><span>Scanners</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-scan
</span><span class='line'>extracting gettext strings into po/Awesome-Module.pot
</span></code></pre></td></tr></table></div></figure>


<p>From here on in, the resulting <code>.pot</code> file will be used by <code>msg-init</code> and <code>msg-merge</code> instead of scanning your code all over again. But keep in mind
that, if you do maintain a POT file, future merges will be a two-step process:
First run <code>msg-scan</code> to update the POT file, then <code>msg-merge</code> to merge its
changes into the PO files:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-scan
</span><span class='line'>extracting gettext strings into po/Awesome-Module.pot
</span><span class='line'>% dzil msg-merge
</span><span class='line'>Merging gettext strings into po/de.po
</span><span class='line'>Merging gettext strings into po/en_UK.po
</span><span class='line'>Merging gettext strings into po/en_US.po
</span><span class='line'>Merging gettext strings into po/ja.po
</span></code></pre></td></tr></table></div></figure>


<h3>Lost in Translation</h3>

<p>One more thing, a note for translators. They can, of course, also use
<code>msg-scan</code> and <code>msg-merge</code> to update the catalogs they&rsquo;re working on. But how
do they test their translations? Easy: use the <a href="https://metacpan.org/pod/Dist::Zilla::App::Command::msg_compile" title="dzil msg-compile documentation on CPAN"><code>msg-compile</code></a> command to
compile a single catalog:</p>

<figure class='code'><figcaption><span>Translation Comilation</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-compile po/fr.po
</span><span class='line'><span class="o">[</span>LocaleTextDomain<span class="o">]</span> po/fr.po: 195 translated messages.
</span></code></pre></td></tr></table></div></figure>


<p>The resulting compiled catalog will be saved to the <code>LocaleData</code> subdirectory
of the current directory, so it&rsquo;s easily available to your app for testing.
Just be sure to tell Perl to include the current directory in the search path,
and set the <code>$LANGUAGE</code> environment variable for your language. For example,
here&rsquo;s how I test the [Sqitch] French catalog:</p>

<figure class='code'><figcaption><span>Translation Comilation</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>% dzil msg-compile po/fr.po
</span><span class='line'><span class="o">[</span>LocaleTextDomain<span class="o">]</span> po/fr.po: 148 translated messages, 36 fuzzy translations, 27 untranslated messages.
</span><span class='line'>% <span class="nv">LANGUAGE</span><span class="o">=</span>fr perl -Ilib -CAS -I. bin/sqitch foo
</span><span class='line'><span class="s2">&quot;foo&quot;</span> n<span class="err">&#39;</span>est pas une commande valide
</span></code></pre></td></tr></table></div></figure>


<p>Just be sure to delete the <code>LocaleData</code> directory when you&rsquo;re done &mdash; or at
least don&rsquo;t commit it to the repository.</p>

<h3>TL;DR</h3>

<p>This may seem like a lot of steps, and it is. But once you have the basics in
place &mdash; Configuring the <a href="https://metacpan.org/module/Dist::Zilla::LocaleTextDomain" title="Dist::Zilla::LocaleTextDomain on CPAN">Dist::Zilla::LocaleTextDomain</a> plugin, setting up
the &ldquo;textdomain filter&rdquo;, setting and the locale in the application &mdash; there
are just a few habits to get into:</p>

<ul>
<li>Use the functions <code>__</code>, <code>__x</code>, <code>__n</code>, and <code>__nx</code> to internationalize
user-visible strings</li>
<li>Run <code>msg-scan</code> and <code>msg-merge</code> to keep the catalogs up-to-date</li>
<li>Keep your translators in the loop.</li>
</ul>


<p>The <a href="https://metacpan.org/module/Dist::Zilla::LocaleTextDomain" title="Dist::Zilla::LocaleTextDomain on CPAN">Dist::Zilla::LocaleTextDomain</a> plugin will do the rest.</p>
<div class="footnotes">
<hr/>
<ol>
<li id="fn:1">
<p>What about <a href="https://metacpan.org/module/Locale::Maketext">Locale::Maketext</a>, you ask? It has not, alas, withsthood the test of time. For details, see Nikolai Prokoschenko&rsquo;s epic 2009 polemic, &ldquo;<a href="http://rassie.org/archives/247">On the state of i18n in Perl</a>.&rdquo; See also Steffen Winkler&rsquo;s presentation, <a href="http://download.steffen-winkler.de/dpws2010/I18N_STEFFENW.pod">Internationalisierungs-Framework auswählen</a> (and the <a href="https://gist.github.com/ap/909197">English translation</a> by <a href="http://blogs.perl.org/users/aristotle/2011/04/stop-using-maketext.html">Aristotle Pagaltzis</a>), from <a href="http://conferences.yapceurope.org/gpw2010/">German Perl Workshop 2010</a>.<a href="#fnref:1" rev="footnote">&#8617;</a></p></li>
<li id="fn:2">
<p>The <code>msg-init</code> function &mdash; like all of the <code>dzil msg-*</code> commands &ndash; uses the <a href="https://www.gnu.org/software/gettext/">GNU gettext</a> utilities under the hood. You&rsquo;ll need a reasonably modern version in your path, or else it won&rsquo;t work.<a href="#fnref:2" rev="footnote">&#8617;</a></p></li>
</ol>
</div>

]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Sqitch Goes Vertical]]></title>
<link href="http://theory.pm/2014/09/05/sqitch-goes-vertical/"/>
<updated>2014-09-05T17:46:00+01:00</updated>
<id>http://theory.pm/2014/09/05/sqitch-goes-vertical</id>

      <content type="html"><![CDATA[<p>I released <a href="http://sqitch.org/">Sqitch</a> v0.996 today. Despite the minor version increase, this is
a pretty big release. I&rsquo;m busy knocking out all the stuff I want to get done
for 1.0, but the version space is running out, so just a minor version jump
from v0.995 to v0.996. But a lot changed. A couple the biggies:</p>

<h3>Goodbye Mouse and Moose, Hello Moo</h3>

<p>If you&rsquo;re not a Perl programmer, you probably aren&rsquo;t familiar with <a href="https://metacpan.org/module/Moose">Moose</a> or
its derivatives <a href="https://metacpan.org/module/Mouse">Mouse</a> and <a href="https://metacpan.org/module/Moo">Moo</a>. Briefly, it&rsquo;s an object system. Great
interface and features, but freaking <em>huge</em>—and <em>slow</em>. Mouse is a lighter
version, and when we (mostly) switched to it <a href="https://github.com/theory/sqitch/pull/73">last year</a>, it yielded a 20-30% speed
improvement.</p>

<p>Still wasn&rsquo;t great, though. So on a day off recently, I switched
to Moo, which implements most of Moose but without a lot of the baggage. At
first, there wasn&rsquo;t much difference in performance, but as I profiled it
(<a href="https://metacpan.org/module/Devel::NYTProf">Devel::NYTProf</a> is indispensable for profiling Perl apps, BTW), I was able
to root out all trace of Moose or Mouse, including in CPAN modules Sqitch
depends on. The result is around a 40% speedup over what we had before.
Honestly, it feels like a new app, it&rsquo;s so fast. I&rsquo;m really happy with how it
turned out, and to have shed some of the baggage from the code base.</p>

<p>The downside is that package maintainers will need to do some work to get the
new dependencies built. Have a look at <a href="https://github.com/theory/sqitch/compare/v0.995...v0.996#diff-4">the RPM spec changes</a> I made to get
our internal Sqitch RPMs to build v0.996.</p>

<h3>MySQL Password Handling</h3>

<p>The handling of MySQL passwords has also been improved. Sqitch now uses the
<code>$MYSQL_PWD</code> environment variable if a password is provided in a target. This
should simplify authentication when running MySQL change scripts through the
<code>mysql</code> client client.</p>

<p>Furthermore, if <a href="https://metacpan.org/module/MySQL::Config">MySQL::Config</a> is installed, Sqitch will look for passwords
in the <code>client</code> and <code>mysql</code> sections of your MySQL configuration files
(<code>~/.my.cnf</code>, <code>/etc/my.cnf</code>). This should already happen automatically when
executing scripts, but Sqitch now tries to replicate that behavior when
connecting to the database via <a href="https://metacpan.org/module/DBI">DBI</a>.</p>

<p>Spotting the <code>$MYSQL_PWD</code> commit, Ștefan Suciu updated the Firebird engine to
use the <code>$ISC_PASSWORD</code> when running scripts. Awesome.</p>

<h3>Vertically Integrated</h3>

<p>And finally, another big change: I added support for <a href="https://my.vertica.com/">Vertica</a>, a very nice
commercial column-store database that features partitioning and sharding,
among other OLAP-style functionality. It was originally forked from
<a href="http://www.postgresql.org/">PostgreSQL</a>, so it was fairly straight-forward to port, though I did have to
borrow a bit from the Oracle and SQLite engines, too. This port was essential
for <a href="http://www.iovation.com/">work</a>, as we&rsquo;re starting to use Vertical more and more, and need ways to
manage changes.</p>

<p>If you&rsquo;re using Vertica, peruse <a href="https://github.com/theory/sqitch/blob/master/lib/sqitchtutorial-vertica.pod">the tutorial</a> to get a feel for what it&rsquo;s
all about. If you want to install it, you can get it from CPAN:</p>

<pre><code>cpan install App::Sqitch BDD::ODBC
</code></pre>

<p>Or, if you&rsquo;re on Homebrew:</p>

<pre><code>brew tap theory/sqitch
brew install sqitch_vertica
</code></pre>

<p>Be warned that there&rsquo;s a minor bug in v0.996, though. Apply this diff to fix
it:</p>

<pre><code>@@ -16,7 +16,7 @@ our $VERSION = '0.996';

 sub key    { 'vertica' }
 sub name   { 'Vertica' }
-sub driver { 'DBD::Pg 2.0' }
+sub driver { 'DBD::ODBC 1.43' }
 sub default_client { 'vsql' }

 has '+destination' =&gt; (
</code></pre>

<p>That fix will be in the next release, of course, as will <a href="https://github.com/theory/sqitch/commit/4f8dbaa236a04f6dd1ec762250ffd8481078691a">support for Vertica 6</a>.</p>

<h3>What Next?</h3>

<p>I need to focus on some other work stuff for a few weeks, but then I expect
to come back to Sqitch again. I&rsquo;d like to get 1.0 shipped before the end of
the year. To that end, next up I will be <a href="https://github.com/theory/sqitch/issues/153">rationalizing configuration hierarchies</a>
to make engine selection and deploy-time configuration more sensible. I hope
to get that done by early October.</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Managing Sqitch with Make →]]></title>
<link href="http://www.modernperlbooks.com/mt/2014/02/managing-sqitch-with-make.html"/>
<updated>2014-02-09T13:58:00+00:00</updated>
<id>http://theory.pm/sqitch/2014/02/09/managing-sqitch-with-make</id>

      <content type="html"><![CDATA[<p>chromatic:</p>

<blockquote><p>This saves me a few dozen keystrokes and a few seconds every time I make a
database change. If that sounds trivial to you, good. A few keystrokes and a
few seconds <em>are</em> trivial. My brainpower <em>isn&rsquo;t</em> trivial. Those keystrokes
and seconds mean the difference between staying in the zone and fumbling
around trying to remember commands I don&rsquo;t use all day every day. They save
me minutes every time I use them, if you count the friction of switching
between &ldquo;How do I do this in Sqitch again? What&rsquo;s the directory layout here?&rdquo;
and &ldquo;What was I really working on?&rdquo;</p></blockquote>

<p>Nice application of a <code>Makefile</code> to eliminate boilerplate. A couple of notes, though:</p>

<p>Nice post. A couple comments and questions:</p>

<ul>
<li><p>As of Sqitch v0.990, you can pass the <code>--open-editor</code> option to the <code>add</code>
command to have the new files opened in your editor.</p></li>
<li><p>If you want to add a pgTAP test with a new change, see <a href="http://theory.pm/sqitch/2014/01/13/templating-tests-with-sqitch/" title="Templating Tests with Sqitch">this post</a>.</p></li>
<li><p>What is the call to <code>sqitch status</code> for? Since its output just goes to
<code>/dev/null</code>, I don&rsquo;t understand the point.</p></li>
<li><p>Also as of v0.990, you can <a href="http://theory.pm/sqitch/2014/01/09/sqitch-on-target/" title="Sqitch on Target">specify Sqitch targets</a>. The <code>-d</code>, <code>-u</code>, and
other options then override values in the target URI.</p></li>
<li><p>I <em>really</em> want to get Sqitch to <a href="https://github.com/theory/sqitch/issues/25" title="Add VCS Integration to Sqitch">better understand and work with VCSs</a>. An
example would be to have it automatically <code>git add</code> files created by
<code>sqitch add</code>. Another might be a Git config setting pointing to the Sqitch
config file. Alas, I don&rsquo;t know when I will have the tuits to work on that.</p></li>
</ul>


<p>Lots of room for growth and improvement in Sqitch going forward. You post provides more food for thought.</p>
<p><a rel="bookmark" href="http://theory.pm/sqitch/2014/02/09/managing-sqitch-with-make/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Templating Tests with Sqitch]]></title>
<link href="http://theory.pm/sqitch/2014/01/13/templating-tests-with-sqitch/"/>
<updated>2014-01-13T14:11:00+00:00</updated>
<id>http://theory.pm/sqitch/2014/01/13/templating-tests-with-sqitch</id>

      <content type="html"><![CDATA[<p>Back in September, <a href="http://theory.pm/sqitch/2013/09/06/sqitch-templating/">I described</a> how to create custom deploy, revert, and
verify scripts for various types of <a href="http://sqitch.org/" title="Sane database schema change management">Sqitch</a> changes, such as adding a new
table. Which is cool and all, but what I&rsquo;ve found while developing databases
at <a href="http://iovation.com/">work</a> is that I nearly always want to create a test script with the same
name as a newly-added change.</p>

<p>So for the recent v0.990 release, the <a href="https://metacpan.org/pod/sqitch-add"><code>add</code> command</a> gained the ability to
generate arbitrary script files from templates. To get it to work, we just
have to create template files. Templates can go into <code>~/.sqitch/templates</code>
(for personal use) or in <code>$(sqitch --etc-path)/templates</code> (for use by
everyone on a system). The latter is where templates are installed by
default. Here&rsquo;s what it looks like:</p>

<figure class='code'><figcaption><span>List Default Templates</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>&gt; ls <span class="k">$(</span>sqitch --etc-path<span class="k">)</span>/templates
</span><span class='line'>deploy  revert  verify
</span><span class='line'>&gt; ls <span class="k">$(</span>sqitch --etc-path<span class="k">)</span>/templates/deploy
</span><span class='line'>firebird.tmpl  mysql.tmpl  oracle.tmpl  pg.tmpl  sqlite.tmpl
</span></code></pre></td></tr></table></div></figure>


<p>Each directory defines the type of script and the name of the directory in
which it will be created. The contents are default templates, one for each
engine.</p>

<p>To create a default test template, all we have to do is create a template for our preferred engine in a directory named <code>test</code>. So I created <code>~/.sqitch/templates/test/pg.tmpl</code>. Here it is:</p>

<figure class='code'><figcaption><span>Default pgTAP template</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="k">SET</span> <span class="n">client_min_messages</span> <span class="k">TO</span> <span class="n">warning</span><span class="p">;</span>
</span><span class='line'><span class="k">CREATE</span> <span class="n">EXTENSION</span> <span class="n">IF</span> <span class="k">NOT</span> <span class="k">EXISTS</span> <span class="n">pgtap</span><span class="p">;</span>
</span><span class='line'><span class="k">RESET</span> <span class="n">client_min_messages</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">BEGIN</span><span class="p">;</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">no_plan</span><span class="p">();</span>
</span><span class='line'><span class="c1">-- SELECT plan(1);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">pass</span><span class="p">(</span><span class="s1">&#39;Test [% change %]!&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">finish</span><span class="p">();</span>
</span><span class='line'><span class="k">ROLLBACK</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is my standard boilerplate for tests, more or less. It just loads
<a href="http://pgtap.org/">pgTAP</a>, sets the plan, runs the tests, finishes and rolls back. See this
template in action:</p>

<figure class='code'><figcaption><span>Add a change.</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>&gt; sqitch add whatever -n <span class="s1">&#39;Adds whatever.&#39;</span>
</span><span class='line'>Created deploy/whatever.sql
</span><span class='line'>Created revert/whatever.sql
</span><span class='line'>Created <span class="nb">test</span>/whatever.sql
</span><span class='line'>Created verify/whatever.sql
</span><span class='line'>Added <span class="s2">&quot;whatever&quot;</span> to sqitch.plan
</span></code></pre></td></tr></table></div></figure>


<p>Cool, it added the test script. Here’s what it looks like:</p>

<figure class='code'><figcaption><span>Generated test script</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="k">SET</span> <span class="n">client_min_messages</span> <span class="k">TO</span> <span class="n">warning</span><span class="p">;</span>
</span><span class='line'><span class="k">CREATE</span> <span class="n">EXTENSION</span> <span class="n">IF</span> <span class="k">NOT</span> <span class="k">EXISTS</span> <span class="n">pgtap</span><span class="p">;</span>
</span><span class='line'><span class="k">RESET</span> <span class="n">client_min_messages</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">BEGIN</span><span class="p">;</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">no_plan</span><span class="p">();</span>
</span><span class='line'><span class="c1">-- SELECT plan(1);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">pass</span><span class="p">(</span><span class="s1">&#39;Test whatever!&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">finish</span><span class="p">();</span>
</span><span class='line'><span class="k">ROLLBACK</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that it replaced the <code>change</code> variable in the call to <code>pass()</code>. All
ready to start writing tests! Nice, right? If we don&rsquo;t want the test script
created &ndash; for example when adding a column to a table for which a test
already exists &ndash; we use the <code>--without</code> option to omit it:</p>

<figure class='code'><figcaption><span>Add change without test</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>&gt; sqitch add add_timestamp_column --without <span class="nb">test</span> -n <span class="s1">&#39;Adds whatever.&#39;</span>
</span><span class='line'>Created deploy/add_timestamp_column.sql
</span><span class='line'>Created revert/add_timestamp_column.sql
</span><span class='line'>Created verify/add_timestamp_column.sql
</span><span class='line'>Added <span class="s2">&quot;add_timestamp_column&quot;</span> to sqitch.plan
</span></code></pre></td></tr></table></div></figure>


<p>Naturally you&rsquo;ll want to update the existing test to validate the new column.</p>

<p>In the <a href="http://theory.pm/sqitch/2013/09/06/sqitch-templating/">previous templating post</a>, we added custom scripts as for <code>CREATE
TABLE</code> changes; now we can add a test template, too. This one takes advantage
of the advanced features of <a href="http://tt2.org/">Template Toolkit</a>. We name it
<code>~/.sqitch/templates/test/createtable.tmpl</code> to complement the deploy,
revert, and verify scripts created previously:</p>

<figure class='code'><figcaption><span>CREATE TABLE test template</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="c1">-- Test [% change %]</span>
</span><span class='line'><span class="k">SET</span> <span class="n">client_min_messages</span> <span class="k">TO</span> <span class="n">warning</span><span class="p">;</span>
</span><span class='line'><span class="k">CREATE</span> <span class="n">EXTENSION</span> <span class="n">IF</span> <span class="k">NOT</span> <span class="k">EXISTS</span> <span class="n">pgtap</span><span class="p">;</span>
</span><span class='line'><span class="k">RESET</span> <span class="n">client_min_messages</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">BEGIN</span><span class="p">;</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">no_plan</span><span class="p">();</span>
</span><span class='line'><span class="c1">-- SELECT plan(1);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SET</span> <span class="n">search_path</span> <span class="k">TO</span> <span class="p">[</span><span class="o">%</span> <span class="n">IF</span> <span class="k">schema</span> <span class="o">%</span><span class="p">][</span><span class="o">%</span> <span class="k">schema</span> <span class="o">%</span><span class="p">],[</span><span class="o">%</span> <span class="k">END</span> <span class="o">%</span><span class="p">]</span><span class="k">public</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_table</span><span class="p">(</span><span class="s1">&#39;[% table or change %]&#39;</span><span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_pk</span><span class="p">(</span> <span class="s1">&#39;[% table or change %]&#39;</span> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="p">[</span><span class="o">%</span> <span class="n">FOREACH</span> <span class="n">col</span> <span class="k">IN</span> <span class="k">column</span> <span class="o">-%</span><span class="p">]</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_column</span><span class="p">(</span>        <span class="s1">&#39;[% table or change %]&#39;</span><span class="p">,</span> <span class="s1">&#39;[% col %]&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_type_is</span><span class="p">(</span>       <span class="s1">&#39;[% table or change %]&#39;</span><span class="p">,</span> <span class="s1">&#39;[% col %]&#39;</span><span class="p">,</span> <span class="s1">&#39;[% type.item( loop.index ) or &#39;</span><span class="nb">text</span><span class="s1">&#39; %]&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_not_null</span><span class="p">(</span>      <span class="s1">&#39;[% table or change %]&#39;</span><span class="p">,</span> <span class="s1">&#39;[% col %]&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_hasnt_default</span><span class="p">(</span> <span class="s1">&#39;[% table or change %]&#39;</span><span class="p">,</span> <span class="s1">&#39;[% col %]&#39;</span> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="p">[</span><span class="o">%</span> <span class="k">END</span> <span class="o">%</span><span class="p">]</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">finish</span><span class="p">();</span>
</span><span class='line'><span class="k">ROLLBACK</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>As <a href="http://theory.pm/sqitch/2013/09/06/sqitch-templating/">before</a>, we tell the <a href="https://metacpan.org/pod/sqitch-add"><code>add</code> command</a> to use the <code>createtable</code> templates:</p>

<figure class='code'><figcaption><span>Create table with typed columns</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>&gt; sqitch add corp_widgets --template createtable <span class="se">\</span>
</span><span class='line'>  -s <span class="nv">schema</span><span class="o">=</span>corp -s <span class="nv">table</span><span class="o">=</span>widgets <span class="se">\</span>
</span><span class='line'>  -s <span class="nv">column</span><span class="o">=</span>id -s <span class="nb">type</span><span class="o">=</span>SERIAL <span class="se">\</span>
</span><span class='line'>  -s <span class="nv">column</span><span class="o">=</span>name -s <span class="nb">type</span><span class="o">=</span>TEXT <span class="se">\</span>
</span><span class='line'>  -s <span class="nv">column</span><span class="o">=</span>quantity -s <span class="nb">type</span><span class="o">=</span>INTEGER <span class="se">\</span>
</span><span class='line'>  -n <span class="s1">&#39;Add corp.widgets table.&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This yields a very nice test script to get you going:</p>

<figure class='code'><figcaption><span>Generated Table Test</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="c1">-- Test corp_widgets</span>
</span><span class='line'><span class="k">SET</span> <span class="n">client_min_messages</span> <span class="k">TO</span> <span class="n">warning</span><span class="p">;</span>
</span><span class='line'><span class="k">CREATE</span> <span class="n">EXTENSION</span> <span class="n">IF</span> <span class="k">NOT</span> <span class="k">EXISTS</span> <span class="n">pgtap</span><span class="p">;</span>
</span><span class='line'><span class="k">RESET</span> <span class="n">client_min_messages</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">BEGIN</span><span class="p">;</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">no_plan</span><span class="p">();</span>
</span><span class='line'><span class="c1">-- SELECT plan(1);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SET</span> <span class="n">search_path</span> <span class="k">TO</span> <span class="n">corp</span><span class="p">,</span><span class="k">public</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_table</span><span class="p">(</span><span class="s1">&#39;widgets&#39;</span><span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_pk</span><span class="p">(</span> <span class="s1">&#39;widgets&#39;</span> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_column</span><span class="p">(</span>        <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_type_is</span><span class="p">(</span>       <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="s1">&#39;SERIAL&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_not_null</span><span class="p">(</span>      <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_hasnt_default</span><span class="p">(</span> <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_column</span><span class="p">(</span>        <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_type_is</span><span class="p">(</span>       <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;TEXT&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_not_null</span><span class="p">(</span>      <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_hasnt_default</span><span class="p">(</span> <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;name&#39;</span> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">has_column</span><span class="p">(</span>        <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;quantity&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_type_is</span><span class="p">(</span>       <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;quantity&#39;</span><span class="p">,</span> <span class="s1">&#39;INTEGER&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_not_null</span><span class="p">(</span>      <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;quantity&#39;</span> <span class="p">);</span>
</span><span class='line'><span class="k">SELECT</span> <span class="n">col_hasnt_default</span><span class="p">(</span> <span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="s1">&#39;quantity&#39;</span> <span class="p">);</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="k">SELECT</span> <span class="n">finish</span><span class="p">();</span>
</span><span class='line'><span class="k">ROLLBACK</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>I don&rsquo;t know about you, but I&rsquo;ll be using this functionality <em>a lot.</em></p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Brent Simmons is Not Wrong →]]></title>
<link href="http://inessential.com/2014/01/02/vesper_sync_diary_7_audibles"/>
<updated>2014-01-10T11:02:00+00:00</updated>
<id>http://theory.pm/theory/2014/01/10/brent-simmons-is-not-wrong</id>

      <content type="html"><![CDATA[<p>Brent Simmons:</p>

<blockquote><p>Database people are already gasping for air, because they know what’s coming.
Instead of creating a separate table for attachment metadata, I created an
attachments column in the notes table and just encoded the attachment
metadata there.</p>

<p>On iOS it uses Core Data’s built-in object archiving feature. On the server
it’s stored as JSON.</p>

<p>This is wrong, surely; it’s not how to do this. Except, in this case, it is.
Incomplete object graphs are wrong; inefficient and slower syncing with more
complex server-side code is also wrong.</p>

<p>This is <em>less wrong than the alternatives.</em></p></blockquote>

<p>Some database folks might be gasping for air, but not those of us steeped in
relational theory. In <em><a href="http://www.amazon.com/Database-Depth-Relational-Theory-Practitioners/dp/0596100124/justatheory-20">Database in Depth</a></em>, relational theorist <a href="http://en.wikipedia.org/wiki/Christopher_J._Date">C.J. Date</a>
poses a question:</p>

<p><a href="http://www.amazon.com/Database-Depth-Relational-Theory-Practitioners/dp/0596100124/justatheory-20"><img class="right" src="http://ecx.images-amazon.com/images/I/41Z2NjCztCL.jpg" width="300" title="“Database in Depth,” by C.J. Date" ></a></p>

<blockquote><p>In Chapter 1, I said that 1NF meant that every tuple in every relation
contains just a single value (of the appropriate type, of course) in every
attribute position&mdash;and it’s usual to add that those “single values” are
supposed to be atomic. But this latter requirement raises the obvious
question: what does it mean for data to be atomic?</p>

<p>Well, on page 6 of the book mentioned earlier, Codd defines atomic data as
data that “cannot be decomposed into smaller pieces by the DBMS (excluding
certain special functions).” But even if we ignore that parenthetical
exclusion, this definition is a trifle puzzling, and not very precise. For
example, what about character strings? Are character strings atomic? Every
product I know provides several operators on such strings—LIKE, SUBSTR
(substring), “||” (concatenate), and so on—that clearly rely on the fact that
character strings in general can be decomposed by the DBMS. So are those
strings atomic? What do you think?</p></blockquote>

<p>The whole book is worth a read, especially the first four chapters, as it
does an excellent job of dispelling the myth that complex data types are
verboten in a properly normalized relational model. Another gem:</p>

<blockquote><p>But I could have used any number of different examples to make my point: I
could have shown attributes (and therefore domains) that contained arrays; or
bags; or lists; or photographs; or audio or video recordings; or X rays; or
fingerprints; or XML documents; or any other kind of value, “atomic” or
“nonatomic,” that you might care to think of. Attributes, and therefore
domains, can contain anything (any values, that is). All of which goes a long
way, incidentally, toward explaining why a true “object/relational” system
would be nothing more nor less than a true relational system—which is to say,
a system that supports the relational model, with all that such support
entails.</p></blockquote>

<p>This is exactly why PostgreSQL offers <a href="http://www.postgresql.org/docs/current/static/arrays.html">array</a>, <a href="http://www.postgresql.org/docs/current/static/datatype-xml.html">XML</a>, and <a href="http://www.postgresql.org/docs/current/static/datatype-json.html">JSON</a> data types:
because sometimes, they are exactly what you need to properly model your
system.</p>

<p>So you go, Brent, you&rsquo;re doing it exactly right.</p>
<p><a rel="bookmark" href="http://theory.pm/theory/2014/01/10/brent-simmons-is-not-wrong/">§</a></p>]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Sqitch on Target]]></title>
<link href="http://theory.pm/sqitch/2014/01/09/sqitch-on-target/"/>
<updated>2014-01-09T15:27:00+00:00</updated>
<id>http://theory.pm/sqitch/2014/01/09/sqitch-on-target</id>

      <content type="html"><![CDATA[<p>At the end of the day last week, I released <a href="http://sqitch.org/">Sqitch</a> v0.990. This was a
pretty big release, with lots of changes. The most awesome addition, in my
opinion, is <em>Named Deployment targets.</em></p>

<p>In previous versions of Sqitch, one
could set default values for the database to deploy to, but needed to use the
<code>--db-*</code> options to deploy to another database. This was fine for
development: just set the default on <code>localhost</code> and go. But when it came
time to deploy to other servers for testing, QA, or production, it was a bit
of a PITA. At <a href="http://www.iovation.com/">work</a>, I ended up writing deployment docs that defined a slew
of environment variables, and our operations team needed to adjust those
variables to deploy to various servers. It was ugly, and frankly a bit of a
pain in the ass.</p>

<p>I thought it&rsquo;d be better to have named deployment targets, so instead of
changing a bunch of environment variables in order to set a bunch of options,
we could just name a target and go. I borrowed the idea from <a href="http://git-scm.com/docs/git-remote">Git remotes</a>,
and started a <a href="https://github.com/theory/uri-db">database URI spec</a> (mentioned <a href="http://theory.pm/rfc/2013/11/26/toward-a-database-uri-standard/">previously</a>) to simplify things
a bit. Here&rsquo;s how it works. Say you have a PostgreSQL Sqitch project called
&ldquo;Flipr&rdquo;. While doing development, you&rsquo;ll want to have a local database to
deploy to. There is also a QA database and a production database. Use the
<a href="https://metacpan.org/pod/sqitch-target"><code>target</code></a> command to set them up:</p>

<figure class='code'><figcaption><span>Sqitch Targeting</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch target add dev db:pg:flipr
</span><span class='line'>sqitch target add qa db:pg://sqitch@qa.example.com/flipr
</span><span class='line'>sqitch target add prod db:pg://sqitch@db.example.com/flipr
</span></code></pre></td></tr></table></div></figure>


<p>Like <a href="http://git-scm.com/docs/git-remote">Git remotes</a>, we just have names and URIs. To <a href="https://metacpan.org/pod/sqitch-deploy"><code>deploy</code></a> to a database,
just name it:</p>

<figure class='code'><figcaption><span>Deploy to Dev</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch deploy dev
</span></code></pre></td></tr></table></div></figure>


<p>Want to deploy to QA? Name it:</p>

<figure class='code'><figcaption><span>Deploy to QA</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch deploy qa
</span></code></pre></td></tr></table></div></figure>


<p>This works with any of the commands that connect to a database, including
<a href="https://metacpan.org/pod/sqitch-revert"><code>revert</code></a> and <a href="https://metacpan.org/pod/sqitch-status"><code>status</code></a>:</p>

<figure class='code'><figcaption><span>Targeted revert, Status</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch revert --to @HEAD^^ dev
</span><span class='line'>sqitch status prod
</span></code></pre></td></tr></table></div></figure>


<p>The great thing about this feature is that the configuration is all stored
in the project Sqitch configuration file. That means you can commit all the
connection URIs for all likely targets in directly to the project repository.
If they change, just change them in the config, commit, and push.</p>

<p>Targets don&rsquo;t always have to be configured in advance, of course. The names
essentially stand in for the URIs, so you can connect to an unnamed target
just by using a URI:</p>

<figure class='code'><figcaption><span>URI Targeting</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch log db:postgres://db1.example.net/flipr_export
</span></code></pre></td></tr></table></div></figure>


<p>Of course there are still defaults specific to each engine. I generally like
to set the &ldquo;dev&rdquo; target as the default deploy target, like so:</p>

<figure class='code'><figcaption><span>Configure Default Target</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch config core.pg.target dev
</span></code></pre></td></tr></table></div></figure>


<p>This sets the &ldquo;dev&rdquo; target as the default for the PostgreSQL engine. So now I
can do stuff with the &ldquo;dev&rdquo; target without mentioning it at all:</p>

<figure class='code'><figcaption><span>Rebase Dev Target</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>sqitch rebase --onto HEAD^4
</span></code></pre></td></tr></table></div></figure>


<p>Named targets may also have a couple other attributes associated with them:</p>

<ul>
<li><code>client</code>: The command-line client to use for a target.</li>
<li><code>registry</code>: The name of the Sqitch registry schema or database, which defaults to, simply, <code>sqitch</code>.</li>
</ul>


<p>Now that I&rsquo;ve started using it, I can think of other things I&rsquo;d like to add to targets in the future, including:</p>

<ul>
<li><a href="https://github.com/theory/sqitch/issues/143">Setting other attributes</a>, such as the deployment mode, whether to verify changes, and variables.</li>
<li><a href="https://github.com/theory/sqitch/issues/135">Allowing multiple URIs</a>, for concurrent database deployments!</li>
</ul>


<p>Pretty cool stuff ahead, IMO. I&rsquo;m grateful to <a href="http://www.iovation.com/">work</a> for letting me hack on
Sqitch.</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Toward A Database URI Standard]]></title>
<link href="http://theory.pm/rfc/2013/11/26/toward-a-database-uri-standard/"/>
<updated>2013-11-26T20:14:00+00:00</updated>
<id>http://theory.pm/rfc/2013/11/26/toward-a-database-uri-standard</id>

      <content type="html"><![CDATA[<p>As part of my effort to improve <a href="http://sqitch.org/" title="Sane database change management">Sqitch</a>, I plan to add support for <a href="https://github.com/theory/sqitch/issues/100" title="Issue #100: “Add target command to configure target databases”">specifying
deployment targets via URIs</a>. Inspired by <a href="http://git-scm.com/book/en/Git-Basics-Working-with-Remotes" title="Git Basics - Working with Remotes">Git remotes</a>, targets will greatly
simplify the specification of databases to update &mdash; especially when stored as
named targets in the configuration file.</p>

<p>Before implementing it, though, I started casting about for a standard
<a href="http://en.wikipedia.org/wiki/URI_scheme" title="Wikipedia: “URI Scheme”">URI Scheme</a> for database connections. Imagine my surprise<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> to find that
there is none! The closest thing to a standard is <a href="http://www.jguru.com/faq/view.jsp?EID=690" title="jGuru: “What is a database URL?”">JDBC URLs</a>. Formally, their
format is simply:</p>

<pre><code>jdbc:&lt;jdbc-specific-stuff&gt;
</code></pre>

<p>Turns out that JDBC URLs are <a href="https://groups.google.com/forum/#!topic/comp.lang.java.programmer/twkIYNaDS64" title="comp.lang.java.programmer: ”JDBC URLs ...not really URLs?“">barely URLs at all</a>. I mean, fine, according to
<a href="http://www.ietf.org/rfc/rfc3986.txt" title="Uniform Resource Identifier (URI): Generic Syntax">RFC 3986</a> they start with the <code>jdbc:</code> scheme followed by whatever. According
to the <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/getstart/connection.html#997649" title="Getting Started with the JDBC API: “JDBC URLs”">JDBC docs</a>, what comes after the scheme is defined as follows:</p>

<pre><code>jdbc:&lt;subprotocol&gt;:&lt;subname&gt;
</code></pre>

<p>The &ldquo;subprotocol&rdquo; is simply a driver name, while the the format of the &ldquo;subname
can vary, depending on the subprotocol, and it can have any internal syntax the
driver writer chooses, including a subsubname.&rdquo; In other words, it can be
anything at all. Not very satisfying, or particularly &ldquo;standard.&rdquo;<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup></p>

<p>In poking around the net, however, I found a fair number of database URI
formats defined by various projects:</p>

<ul>
<li><p><a href="http://www.postgresql.org/docs/9.3/static/libpq-connect.html#LIBPQ-CONNSTRING" title="PostgreSQL Documentation: “Connection Strings”">PostgreSQL libpq URIs</a>
<code>postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&amp;...]</code></p></li>
<li><p><a href="http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#database-urls" title="SQLAlchemy Documentation: “Database Urls”">SQLAlchemy URLs</a>:
<code>dialect[+driver:]//[username[:password]@host[:port]/database</code></p></li>
<li><p><a href="http://docs.stackato.com/3.0/user/services/data-services.html#database-url" title="Stackato Documentation: “DATABASE_URL”">Stackato database URLs</a>:
<code>protocol://[username[:password]@host[:port]/database_name</code></p></li>
<li><p><a href="https://github.com/kennethreitz/dj-database-url" title="DJ-Database-URL on GitHub">Django database URLs</a>:
<code>ENGINE://[USER[:PASSWORD]@][HOST][:PORT]/DATABASE</code></p></li>
<li><p><a href="https://github.com/glenngillen/rails-database-url" title="rails-database-url on GitHub">Rails database URLs</a>:
<code>adapter://[userinfo@][hostname][:port]/database</code></p></li>
</ul>


<p>All very similar, right? Most database engines support all or a subset of these
connection parts in common:</p>

<ul>
<li>username</li>
<li>password</li>
<li>host address</li>
<li>port</li>
<li>database name</li>
<li>configuration parameters</li>
</ul>


<p>So why not define a standard database URI format with all those parts, and use
them where appropriate for each engine? It&rsquo;s all right there, just like
<a href="http://tools.ietf.org/html/rfc2616#page-19" title="RFC 2616: “http URL”">http URLs</a>.</p>

<h2>The Proposal</h2>

<p>Here&rsquo;s <a href="https://github.com/theory/uri-db" title="Database URI on GitHub">my proposal</a>. Formally, it&rsquo;s an opaque URI like JDBC. All database URIs
start with the scheme <code>db:</code>. But in this case, the opaque part is an embedded
URI that may be in one of two formats:</p>

<pre><code>engine://[username[:password]@]host[:port][/dbname][?params]
engine:[dbname][?params]
</code></pre>

<p>In other words, a pretty typical http- or mailto-style URI format. We embed it
in a <code>db:</code> URI in order to identify the URI as a database URI, and to have a
single reasonable scheme to register. Informally, it&rsquo;s simplest to think of a
database URI as a single URI starting with the combination of the scheme and
the engine, e.g., <code>db:mysql</code>.</p>

<p>Some notes on the formats:</p>

<ul>
<li><p>The Database URI <em>scheme</em> is <code>db</code>. Consequently, database URIs always start
with <code>db:</code>. This is the <a href="http://en.wikipedia.org/wiki/URI_scheme" title="Wikipedia: “URI Scheme”">URI scheme</a> that defines a database URI.</p></li>
<li><p>Next comes the database <em>engine</em>. This part is a string naming the type of
database engine for the database. It must always be followed by a colon, <code>:</code>.
There is no formal list of supported engines, though certain implementations
may specify engine-specific semantics, such as a default port.</p></li>
<li><p>The <em>authority</em> part is separated from the engine by a double slash, <code>//</code>,
and terminated by the next slash or end of the URI. It consists of an
optional user-information part, terminated by <code>@</code> (e.g.,
<code>username:password@</code>); a required host address (e.g., domain name or IP
address); and an optional port number, preceded by a colon, <code>:</code>.</p></li>
<li><p>The <em>path</em> part specifies the database name or path. For URIs that contain
an authority part, a path specifying a file name must be absolute. URIs
without an authority may use absolute or relative paths.</p></li>
<li><p>The optional <em>query</em> part, separated by a question mark, <code>?</code>, contains
<code>key=value</code> pairs separated by a semicolon, <code>;</code>, or ampersand, <code>&amp;</code>. These
parameters may be used to configure a database connection with parameters not
directly supported by the rest of the URI format.</p></li>
</ul>


<h3>Examples</h3>

<p>Here are some database URIs without an authority part, which is typical for
non-server engines such as <a href="http://sqlite.org/" title="SQLite Home Page">SQLite</a>, where the path part is a relative or
absolute file name:</p>

<ul>
<li><code>db:sqlite:</code></li>
<li><code>db:sqlite:foo.db</code></li>
<li><code>db:sqlite:../foo.db</code></li>
<li><code>db:sqlite:/var/db/foo.sqlite</code></li>
</ul>


<p>Other engines may use a database name rather than a file name:</p>

<ul>
<li><code>db:ingres:mydb</code></li>
<li><code>db:postgresql:template1</code></li>
</ul>


<p>When a URI includes an authority part, it must be preceded by a double slash:</p>

<ul>
<li><code>db:postgresql://example.com</code></li>
<li><code>db:mysql://root@localhost</code></li>
<li><code>db:pg://postgres:secr3t@example.net</code></li>
</ul>


<p>To add the database name, separate it from the authority by a single slash:</p>

<ul>
<li><code>db:postgresql://example.com/template1</code></li>
<li><code>db:mongodb://localhost:27017/myDatabase</code></li>
<li><code>db:oracle://scott:tiger@foo.com/scott</code></li>
</ul>


<p>Some databases, such as Firebird, take both a host name and a file path.
These paths must be absolute:</p>

<ul>
<li><code>db:firebird://localhost/tmp/test.gdb</code></li>
<li><code>db:firebird://localhost/C:/temp/test.gdb</code></li>
</ul>


<p>Any URI format may optionally have a query part containing key/value pairs:</p>

<ul>
<li><code>db:sqlite:foo.db?foreign_keys=ON;journal_mode=WAL</code></li>
<li><code>db:pg://localhost:5433/postgres?client_encoding=utf8;connect_timeout=10</code></li>
</ul>


<h2>Issues</h2>

<p>In discussing this proposal with various folks, I&rsquo;ve become aware of a few
challenges to standardization.</p>

<p>First, the requirement that the authority part must include a host address
prevents the specification of a URI with a username that can be used to connect
to a Unix socket. PostgreSQL and MySQL, among others provide authenticated
socket connections. While <a href="http://www.ietf.org/rfc/rfc3986.txt" title="Uniform Resource Identifier (URI): Generic Syntax">RFC 3986</a> requires the host name, its predecessor,
<a href="http://www.ietf.org/rfc/rfc3986.txt" title="Uniform Resource Identifiers (URI): Generic Syntax">RFC 2396</a>, does not. Furthermore, as a precedent, neither do <a href="http://en.wikipedia.org/wiki/File_URI_scheme#Examples" title="Wikipedia: “File URI Scheme: Examples”">file URIs</a>. So
I&rsquo;m thinking of allowing something like this to connect to a PostgreSQL database</p>

<pre><code>db:pg://postgres:secr3t@/
</code></pre>

<p>In short, it makes sense to allow the user information without a host name.</p>

<p>The second issue is the disallowing of relative file names in the path part
following an authority part. The problem here is that most database engines
don&rsquo;t use paths for database names, so a leading slash makes no sense. For
example, in <code>db:pg:localhost/foo</code>, the PostgreSQL database name is <code>foo</code>, not
<code>/foo</code>. Yet in <code>db:firebird:localhost/foo</code>, the Firebird database name <em>is</em> a
path, <code>/foo</code>. So each engine implementation must know whether or not the path
part is a file name.</p>

<p>But some databases may in fact allow a path to be specified for a local
connection, and a name for a remote connection. <a href="https://metacpan.org/pod/DBD::Informix#INFORMIX-CONNECTION-SEMANTICS" title="MetaCPAN: “Informix Connection Semantics”">Informix</a> appears to support
such variation. So how is one to know whether the path is a file path or a
named database? The two variants cannot be distinguished.</p>

<p><a href="http://www.ietf.org/rfc/rfc3986.txt" title="Uniform Resource Identifiers (URI): Generic Syntax">RFC 2396</a> is quite explicit that the path part must be absolute when following
an authority part. But <a href="http://www.ietf.org/rfc/rfc3986.txt" title="Uniform Resource Identifier (URI): Generic Syntax">RFC 3986</a> forbids the double slash only when there is
no authority part. Therefore, I think it might be best to require a second
slash for absolute paths. Engines that use a simple name or relative path can
have it just after the slash, while an absolute path could use a second slash:</p>

<ul>
<li>Absolute: db:firebird://localhost//tmp/test.gdb</li>
<li>Relative: db:firebird://localhost/db/test.gdb</li>
<li>Name: db:postgresql://localhost/template1</li>
</ul>


<h2>That&rsquo;s It</h2>

<p>The path issue aside, I feel like this is a pretty simple proposal, and could
have wide utility. I&rsquo;ve already knocked out a Perl reference implementation,
<a href="https://github.com/theory/uri-db/blob/master/lib/URI/db.pm" title="URI::db on GitHub">URI::db</a>. Given the wide availability of URI parsers in various programming
languages, I wouldn&rsquo;t expect it to be difficult to port, either.</p>

<p>The <a href="https://github.com/theory/uri-db/" title="uri-db on GitHub">uri-db project</a> is the canonical home for the proposal for now, so check
there for updates. And your feedback would be appreciated! What other issues
have I overlooked? What have I got wrong? Let me know!</p>
<div class="footnotes">
<hr/>
<ol>
<li id="fn:1">
<p>As in not surprised at all. Though I was hoping!<a href="#fnref:1" rev="footnote">&#8617;</a></p></li>
<li id="fn:2">
<p>DSNs for Perl&rsquo;s <a href="https://metacpan.org/module/DBI" title="MetaCPAN: DBI">DBI</a> aren&rsquo;t much better: <code>dbi:&lt;driver&gt;:&lt;driver-specific-stuff&gt;</code>.<a href="#fnref:2" rev="footnote">&#8617;</a></p></li>
</ol>
</div>

]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Indexing Nested hstore]]></title>
<link href="http://theory.pm/pg/2013/10/25/indexing-nested-hstore/"/>
<updated>2013-10-25T14:36:00+01:00</updated>
<id>http://theory.pm/pg/2013/10/25/indexing-nested-hstore</id>

      <content type="html"><![CDATA[<p>In my first <a href="http://theory.pm/pg/2013/10/23/testing-nested-hstore/">Nested hstore</a> post yesterday, I ran a query against unindexed
hstore data, which required a table scan. But hstore is able to take
advantage of <a href="http://www.postgresql.org/docs/current/static/gin.html">GIN indexes</a>. So let&rsquo;s see what that looks like. Connecting to
the same database, I indexed the <code>review</code> column:</p>

<figure class='code'><figcaption><span>Indexing reviews</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="n">reviews</span><span class="o">=#</span> <span class="k">CREATE</span> <span class="k">INDEX</span> <span class="n">idx_reviews_gin</span> <span class="k">ON</span> <span class="n">reviews</span> <span class="k">USING</span> <span class="n">GIN</span><span class="p">(</span><span class="n">review</span><span class="p">);</span>
</span><span class='line'><span class="k">CREATE</span> <span class="k">INDEX</span>
</span><span class='line'><span class="n">Time</span><span class="p">:</span> <span class="mi">360448</span><span class="p">.</span><span class="mi">426</span> <span class="n">ms</span>
</span><span class='line'><span class="n">reviews</span><span class="o">=#</span> <span class="k">SELECT</span> <span class="n">pg_size_pretty</span><span class="p">(</span><span class="n">pg_database_size</span><span class="p">(</span><span class="n">current_database</span><span class="p">()));</span>
</span><span class='line'> <span class="n">pg_size_pretty</span>
</span><span class='line'><span class="c1">----------------</span>
</span><span class='line'> <span class="mi">421</span> <span class="n">MB</span>
</span></code></pre></td></tr></table></div></figure>


<p>Well, that takes a while, and makes the database a lot bigger (it was 277 MB
unindexed). But is it worth it? Let&rsquo;s find out. Oleg and Teodor&rsquo;s patch adds
support for a nested hstore value on the right-hand-side of the <code>@&gt;</code>
operator. In practice, that means we can specify the full path to a nested
value as an hstore expression. In our case, to query only for Books, instead
of using this expression:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="k">WHERE</span> <span class="n">review</span> <span class="o">#&gt;</span> <span class="s1">&#39;{product,group}&#39;</span> <span class="o">=</span> <span class="s1">&#39;Book&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can use an hstore value with the entire path, including the value:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="k">WHERE</span> <span class="n">review</span> <span class="o">@&gt;</span> <span class="s1">&#39;{product =&gt; {group =&gt; Book}}&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Awesome, right? Let&rsquo;s give it a try:</p>

<figure class='code'><figcaption><span>Query Book Reviews</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="n">reviews</span><span class="o">=#</span> <span class="k">SELECT</span>
</span><span class='line'>    <span class="n">width_bucket</span><span class="p">(</span><span class="k">length</span><span class="p">(</span><span class="n">review</span> <span class="o">#&gt;</span> <span class="s1">&#39;{product,title}&#39;</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="n">title_length_bucket</span><span class="p">,</span>
</span><span class='line'>    <span class="n">round</span><span class="p">(</span><span class="k">avg</span><span class="p">(</span><span class="n">review</span> <span class="o">#^&gt;</span> <span class="s1">&#39;{review,rating}&#39;</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span> <span class="k">AS</span> <span class="n">review_average</span><span class="p">,</span>
</span><span class='line'>    <span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span>
</span><span class='line'><span class="k">FROM</span>
</span><span class='line'>    <span class="n">reviews</span>
</span><span class='line'><span class="k">WHERE</span>
</span><span class='line'>    <span class="n">review</span> <span class="o">@&gt;</span> <span class="s1">&#39;{product =&gt; {group =&gt; Book}}&#39;</span>
</span><span class='line'><span class="k">GROUP</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span>
</span><span class='line'><span class="k">ORDER</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span><span class="p">;</span>
</span><span class='line'> <span class="n">title_length_bucket</span> <span class="o">|</span> <span class="n">review_average</span> <span class="o">|</span> <span class="k">count</span>
</span><span class='line'><span class="c1">---------------------+----------------+--------</span>
</span><span class='line'>                   <span class="mi">1</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">42</span> <span class="o">|</span>  <span class="mi">56299</span>
</span><span class='line'>                   <span class="mi">2</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">33</span> <span class="o">|</span> <span class="mi">170774</span>
</span><span class='line'>                   <span class="mi">3</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">45</span> <span class="o">|</span> <span class="mi">104778</span>
</span><span class='line'>                   <span class="mi">4</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">41</span> <span class="o">|</span>  <span class="mi">69719</span>
</span><span class='line'>                   <span class="mi">5</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">36</span> <span class="o">|</span>  <span class="mi">47110</span>
</span><span class='line'>                   <span class="mi">6</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">43</span> <span class="o">|</span>  <span class="mi">43070</span>
</span><span class='line'><span class="p">(</span><span class="mi">6</span> <span class="k">rows</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">Time</span><span class="p">:</span> <span class="mi">849</span><span class="p">.</span><span class="mi">681</span> <span class="n">ms</span>
</span></code></pre></td></tr></table></div></figure>


<p>That time looks better than yesterday&rsquo;s, but in truth I first ran this query
just before building the GIN index and got about the same result. Must be
that Mavericks is finished indexing my disk or something. At any rate, the
index is not buying us much here.</p>

<p>But hey, we&rsquo;re dealing with 1998 Amazon reviews, so querying against books
probably isn&rsquo;t very selective. I don&rsquo;t blame the planner for deciding that a
table scan is cheaper than an index scan. But what if we try a more selective
value, say &ldquo;DVD&rdquo;?</p>

<figure class='code'><figcaption><span>Query DVD Reivews</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="n">reviews</span><span class="o">=#</span> <span class="k">SELECT</span>
</span><span class='line'>    <span class="n">width_bucket</span><span class="p">(</span><span class="k">length</span><span class="p">(</span><span class="n">review</span> <span class="o">#&gt;</span> <span class="s1">&#39;{product,title}&#39;</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="n">title_length_bucket</span><span class="p">,</span>
</span><span class='line'>    <span class="n">round</span><span class="p">(</span><span class="k">avg</span><span class="p">(</span><span class="n">review</span> <span class="o">#^&gt;</span> <span class="s1">&#39;{review,rating}&#39;</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span> <span class="k">AS</span> <span class="n">review_average</span><span class="p">,</span>
</span><span class='line'>    <span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span>
</span><span class='line'><span class="k">FROM</span>
</span><span class='line'>    <span class="n">reviews</span>
</span><span class='line'><span class="k">WHERE</span>
</span><span class='line'>    <span class="n">review</span> <span class="o">@&gt;</span> <span class="s1">&#39;{product =&gt; {group =&gt; DVD}}&#39;</span>
</span><span class='line'><span class="k">GROUP</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span>
</span><span class='line'><span class="k">ORDER</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span><span class="p">;</span>
</span><span class='line'> <span class="n">title_length_bucket</span> <span class="o">|</span> <span class="n">review_average</span> <span class="o">|</span> <span class="k">count</span>
</span><span class='line'><span class="c1">---------------------+----------------+-------</span>
</span><span class='line'>                   <span class="mi">1</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">27</span> <span class="o">|</span>  <span class="mi">2646</span>
</span><span class='line'>                   <span class="mi">2</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">44</span> <span class="o">|</span>  <span class="mi">4180</span>
</span><span class='line'>                   <span class="mi">3</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">53</span> <span class="o">|</span>  <span class="mi">1996</span>
</span><span class='line'>                   <span class="mi">4</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">38</span> <span class="o">|</span>  <span class="mi">2294</span>
</span><span class='line'>                   <span class="mi">5</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">48</span> <span class="o">|</span>   <span class="mi">943</span>
</span><span class='line'>                   <span class="mi">6</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">42</span> <span class="o">|</span>   <span class="mi">738</span>
</span><span class='line'><span class="p">(</span><span class="mi">6</span> <span class="k">rows</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">Time</span><span class="p">:</span> <span class="mi">73</span><span class="p">.</span><span class="mi">913</span> <span class="n">ms</span>
</span></code></pre></td></tr></table></div></figure>


<p>Wow! Under 100ms. That&rsquo;s more like it! <a href="http://en.wikipedia.org/wiki/Inverted_index">Inverted indexing</a> FTW!</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[Testing Nested hstore]]></title>
<link href="http://theory.pm/pg/2013/10/23/testing-nested-hstore/"/>
<updated>2013-10-23T10:26:00+01:00</updated>
<id>http://theory.pm/pg/2013/10/23/testing-nested-hstore</id>

      <content type="html"><![CDATA[<p>I&rsquo;ve been helping Oleg Bartunov and Teodor Sigaev with documentation for the
forthcoming <a href="http://www.sai.msu.su/~megera/postgres/talks/hstore-pgcon-2013.pdf">nested hstore</a> patch for <a href="http://www.posrgresql.org/">PostgreSQL</a>. It adds support for
arrays, numeric and boolean types, and of course arbitrarily nested data
structures. This gives it feature parity with <a href="http://json.org/">JSON</a>, but unlike the
<a href="http://www.postgresql.org/docs/current/static/datatype-json.html">JSON type</a>, its values are stored in a binary representation, which makes it
much more efficient to query. The support for <a href="http://www.postgresql.org/docs/current/static/gist.html">GiST</a> and <a href="http://www.postgresql.org/docs/current/static/gin.html">GIN</a> indexes to
speed up path searches doesn&rsquo;t hurt, either.</p>

<p>As part of the documentation, we wanted to include a short tutorial, something
to show off the schemaless flexibility of the new hstore. The <a href="http://citusdata.com/">CitusDB</a> guys
were kind enough to show off their <a href="https://github.com/citusdata/json_fdw">json_fdw</a> with some Amazon review data in
a <a href="http://citusdata.com/blog/65-run-sql-on-json-files-without-any-data-loads">blog post</a> a few months back; it even includes an interesting query against
the data. Let&rsquo;s see what we can do with it. First, load it:</p>

<figure class='code'><figcaption><span>Load Amazon Reviews as hstore</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; createdb reviews
</span><span class='line'>&gt; psql -d reviews -c <span class="s1">&#39;</span>
</span><span class='line'><span class="s1">    CREATE EXTENSION HSTORE;</span>
</span><span class='line'><span class="s1">    CREATE TABLE reviews(review hstore);</span>
</span><span class='line'><span class="s1">&#39;</span>
</span><span class='line'>CREATE TABLE
</span><span class='line'>&gt; gzcat customer_reviews_nested_1998.json.gz | sed -e <span class="s1">&#39;s/\\/\\\\/g&#39;</span> <span class="se">\</span>
</span><span class='line'> | sed -e <span class="s2">&quot;s/&#39;/&#39;&#39;/g&quot;</span> | sed -e <span class="s1">&#39;s/&quot;:/&quot; =&gt;/g&#39;</span> &gt; /tmp/hstore.copy
</span><span class='line'>&gt; <span class="nb">time </span>psql -d reviews -c <span class="s2">&quot;COPY reviews FROM &#39;/tmp/hstore.copy&#39;&quot;</span>
</span><span class='line'>COPY 589859
</span><span class='line'>       0.00s user 0.00s system 0% cpu 13.059 total
</span></code></pre></td></tr></table></div></figure>


<p>13 seconds to load 589,859 records from a file &ndash; a little over 45k records
per second. Not bad. Let&rsquo;s see what the storage looks like:</p>

<figure class='code'><figcaption><span>How Big is the hstore Data?</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; psql -d reviews -c <span class="s1">&#39;SELECT pg_size_pretty(pg_database_size(current_database()));&#39;</span>
</span><span class='line'> pg_size_pretty
</span><span class='line'>----------------
</span><span class='line'> 277 MB
</span></code></pre></td></tr></table></div></figure>


<p>The original, uncompressed data is 208 MB on disk, so roughly a third bigger
given the overhead of the database. Just for fun, let&rsquo;s compare it to JSON:</p>

<figure class='code'><figcaption><span>Load Amazon Reviews as JSON</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&gt; createdb reviews_js
</span><span class='line'>&gt; psql -d reviews_js -c <span class="s1">&#39;CREATE TABLE reviews(review json);&#39;</span>
</span><span class='line'>CREATE TABLE
</span><span class='line'>&gt; gzcat customer_reviews_nested_1998.json.gz | sed -e <span class="s1">&#39;s/\\/\\\\/g&#39;</span> <span class="se">\</span>
</span><span class='line'> | sed -e <span class="s2">&quot;s/&#39;/&#39;&#39;/g&quot;</span> | &gt; /tmp/json.copy
</span><span class='line'>&gt; <span class="nb">time </span>psql -d reviews_js -c <span class="s2">&quot;COPY reviews FROM &#39;/tmp/json.copy&#39;&quot;</span>
</span><span class='line'>COPY 589859
</span><span class='line'>       0.00s user 0.00s system 0% cpu 7.434 total
</span><span class='line'>&gt; psql -d reviews_js -c <span class="s1">&#39;SELECT pg_size_pretty(pg_database_size(current_database()));&#39;</span>
</span><span class='line'> pg_size_pretty
</span><span class='line'>----------------
</span><span class='line'> 239 MB
</span></code></pre></td></tr></table></div></figure>


<p>Almost 80K records per second, faster, I&rsquo;m guessing, because the JSON type
doesn&rsquo;t convert the data to binary representation its way in. JSON currently
uses less overhead for storage, aw well; I wonder if that&rsquo;s the benefit of
<a href="http://www.postgresql.org/docs/current/static/storage-toast.html">TOAST storage</a>?</p>

<p>Let&rsquo;s try querying these guys. I adapted the query from the CitusDB <a href="http://citusdata.com/blog/65-run-sql-on-json-files-without-any-data-loads">blog
post</a> and ran it on my 2013 MacBook Air (1.7 GHz Intel Core i7) with iTunes
and a bunch of other apps running in the background [yeah, I&rsquo;m lazy]). Check
out those operators, by the way! Given a path, <code>#^&gt;</code> returns a numeric value:</p>

<figure class='code'><figcaption><span>Query the hstore-encoded reviews</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="n">reviews</span><span class="o">=#</span> <span class="k">SELECT</span>
</span><span class='line'>    <span class="n">width_bucket</span><span class="p">(</span><span class="k">length</span><span class="p">(</span><span class="n">review</span> <span class="o">#&gt;</span> <span class="s1">&#39;{product,title}&#39;</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="n">title_length_bucket</span><span class="p">,</span>
</span><span class='line'>    <span class="n">round</span><span class="p">(</span><span class="k">avg</span><span class="p">(</span><span class="n">review</span> <span class="o">#^&gt;</span> <span class="s1">&#39;{review,rating}&#39;</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span> <span class="k">AS</span> <span class="n">review_average</span><span class="p">,</span>
</span><span class='line'>    <span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span>
</span><span class='line'><span class="k">FROM</span>
</span><span class='line'>    <span class="n">reviews</span>
</span><span class='line'><span class="k">WHERE</span>
</span><span class='line'>    <span class="n">review</span> <span class="o">#&gt;</span> <span class="s1">&#39;{product,group}&#39;</span> <span class="o">=</span> <span class="s1">&#39;Book&#39;</span>
</span><span class='line'><span class="k">GROUP</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span>
</span><span class='line'><span class="k">ORDER</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span><span class="p">;</span>
</span><span class='line'> <span class="n">title_length_bucket</span> <span class="o">|</span> <span class="n">review_average</span> <span class="o">|</span> <span class="k">count</span>
</span><span class='line'><span class="c1">---------------------+----------------+--------</span>
</span><span class='line'>                   <span class="mi">1</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">42</span> <span class="o">|</span>  <span class="mi">56299</span>
</span><span class='line'>                   <span class="mi">2</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">33</span> <span class="o">|</span> <span class="mi">170774</span>
</span><span class='line'>                   <span class="mi">3</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">45</span> <span class="o">|</span> <span class="mi">104778</span>
</span><span class='line'>                   <span class="mi">4</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">41</span> <span class="o">|</span>  <span class="mi">69719</span>
</span><span class='line'>                   <span class="mi">5</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">36</span> <span class="o">|</span>  <span class="mi">47110</span>
</span><span class='line'>                   <span class="mi">6</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">43</span> <span class="o">|</span>  <span class="mi">43070</span>
</span><span class='line'><span class="p">(</span><span class="mi">6</span> <span class="k">rows</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">Time</span><span class="p">:</span> <span class="mi">2301</span><span class="p">.</span><span class="mi">620</span> <span class="n">ms</span>
</span></code></pre></td></tr></table></div></figure>


<p>The benefit of the native type is pretty apparent here. I ran this query
several times, and the time was always between 2.3 and 2.4 seconds. The Citus
<a href="https://github.com/citusdata/json_fdw">json_fdw</a> query took &ldquo;about 6 seconds on a 3.1 GHz CPU core.&rdquo; Let&rsquo;s see how
well the JSON type does (pity there is no operator to fetch a value as
numeric; we have to cast from text):</p>

<figure class='code'><figcaption><span>Query the JSON-encoded reviews</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="n">reviews_js</span><span class="o">=#</span> <span class="k">SELECT</span>
</span><span class='line'>    <span class="n">width_bucket</span><span class="p">(</span><span class="k">length</span><span class="p">(</span><span class="n">review</span> <span class="o">#&gt;&gt;</span> <span class="s1">&#39;{product,title}&#39;</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="n">title_length_bucket</span><span class="p">,</span>
</span><span class='line'>    <span class="n">round</span><span class="p">(</span><span class="k">avg</span><span class="p">((</span><span class="n">review</span> <span class="o">#&gt;&gt;</span> <span class="s1">&#39;{review,rating}&#39;</span><span class="p">)::</span><span class="nb">numeric</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span> <span class="k">AS</span> <span class="n">review_average</span><span class="p">,</span>
</span><span class='line'>    <span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span>
</span><span class='line'><span class="k">FROM</span>
</span><span class='line'>    <span class="n">reviews</span>
</span><span class='line'><span class="k">WHERE</span>
</span><span class='line'>    <span class="n">review</span> <span class="o">#&gt;&gt;</span> <span class="s1">&#39;{product,group}&#39;</span> <span class="o">=</span> <span class="s1">&#39;Book&#39;</span>
</span><span class='line'><span class="k">GROUP</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span>
</span><span class='line'><span class="k">ORDER</span> <span class="k">BY</span>
</span><span class='line'>    <span class="n">title_length_bucket</span><span class="p">;</span>
</span><span class='line'> <span class="n">title_length_bucket</span> <span class="o">|</span> <span class="n">review_average</span> <span class="o">|</span> <span class="k">count</span>
</span><span class='line'><span class="c1">---------------------+----------------+--------</span>
</span><span class='line'>                   <span class="mi">1</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">42</span> <span class="o">|</span>  <span class="mi">56299</span>
</span><span class='line'>                   <span class="mi">2</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">33</span> <span class="o">|</span> <span class="mi">170774</span>
</span><span class='line'>                   <span class="mi">3</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">45</span> <span class="o">|</span> <span class="mi">104778</span>
</span><span class='line'>                   <span class="mi">4</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">41</span> <span class="o">|</span>  <span class="mi">69719</span>
</span><span class='line'>                   <span class="mi">5</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">36</span> <span class="o">|</span>  <span class="mi">47110</span>
</span><span class='line'>                   <span class="mi">6</span> <span class="o">|</span>           <span class="mi">4</span><span class="p">.</span><span class="mi">43</span> <span class="o">|</span>  <span class="mi">43070</span>
</span><span class='line'><span class="p">(</span><span class="mi">6</span> <span class="k">rows</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">Time</span><span class="p">:</span> <span class="mi">5530</span><span class="p">.</span><span class="mi">120</span> <span class="n">ms</span>
</span></code></pre></td></tr></table></div></figure>


<p>A little faster than the <a href="https://github.com/citusdata/json_fdw">json_fdw</a> version, but comparable. But takes well
over twice as long as the hstore version, though. For queries, hstore is the
clear winner. Yes, you pay up-front for loading and storage, but the payoff at
query time is substantial. Ideally, of course, we would have the insert and
storage benefits of JSON <em>and</em> the query performance of hstore. There was talk
last spring at PGCon of using the same representation for JSON and hstore;
perhaps that can still come about.</p>

<p>Meanwhile, I expect to play with some other data sets over the next week;
watch this spot for more!</p>
]]></content>
    </entry>
  
    <entry>
      




<title type="html"><![CDATA[The Power of Enums →]]></title>
<link href="http://www.openscg.com/2013/09/the-power-of-enums/"/>
<updated>2013-09-29T14:50:00+01:00</updated>
<id>http://theory.pm/pg/2013/09/29/the-power-of-enums</id>

      <content type="html"><![CDATA[<p>Jim Mlodgenski on using <a href="http://www.postgresql.org/docs/9.3/static/datatype-enum.html">Enums</a> in place of references to small lookup tables:</p>

<blockquote><p>I saw something else I didn’t expect: […] There was a 8% increase
in performance. I was expecting the test with the enums to be close
to the baseline, but I wasn’t expecting it to be faster. Thinking
about it, it makes sense. Enums values are just numbers so we’re
effectively using surrogate keys under the covers, but the users would
still the the enum labels when they are looking at the data. It ended
up being a no brainer to use enums for these static tables. There was
a increase in performance while still maintaining the integrity of the
data.</p></blockquote>

<p>I&rsquo;ve been a big fan of Enums since Andrew and Tom Dunstan released a patch for
them during the PostgreSQL 8.2 era. Today they&rsquo;re a core feature, and as of
9.1, you can even modify their values! You&rsquo;re missing out if you&rsquo;re not using
them yet.</p>
<p><a rel="bookmark" href="http://theory.pm/pg/2013/09/29/the-power-of-enums/">§</a></p>]]></content>
    </entry>
  
</feed>
