<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: The bitter-sweet taste of agnostic database schemas</title>
    <link>http://www.robbyonrails.com/articles/2005/09/27/the-bitter-sweet-taste-of-agnostic-database-schemas</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>The bitter-sweet taste of agnostic database schemas</title>
      <description>&lt;p&gt;If you know me at all by now&amp;#8230; you might know that I am a &lt;strong&gt;huge&lt;/strong&gt; fan of &lt;a href="http://www.postgresql.org"&gt;PostgreSQL&lt;/a&gt;. Coming to the &lt;a href="http://www.rubyonrails.org"&gt;Rails&lt;/a&gt; camp  was a bit of an eye-opener./  Working with PostgreSQL used to save me quite a bit of work and let me sleep at night. I have dealt with the problems that &lt;a href="http://sql-info.de/mysql/gotchas.html"&gt;many developers face&lt;/a&gt; while working with &lt;a href="http://www.mysql.com"&gt;MySQL&lt;/a&gt;. Like many of us in the Open Source world, I was exposed to MySQL more often than PostgreSQL. For several years, I convinced myself that MySQL was awesome because it was fast&amp;#8230; but &lt;a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/LanguageChoice.rdoc"&gt;speed isn&amp;#8217;t always&lt;/a&gt; the most important factor in the technology world. Otherwise, we&amp;#8217;d all be coding in C. What PostgreSQL provided to me as a developer was better relational integrity. Foo cannot exist unless Bar does, or vice versa. &lt;code&gt;ON DELETE CASCADE&lt;/code&gt; was a godsend and saved me quite a bit of work when building an application. Triggers&amp;#8230; Procedural Languages&amp;#8230; &lt;a href="http://www.robbyonrails.com/articles/tag/plruby"&gt;oh so beautiful&lt;/a&gt;.&lt;/p&gt;


	&lt;h3&gt;PostgreSQL was (and is) the Open Source database server that both the &lt;strong&gt;Enterprise&lt;/strong&gt; DBA and Developer could agree on.&lt;/h3&gt;


	&lt;h3&gt;Enter Ruby on Rails&lt;/h3&gt;


	&lt;p&gt;At first, I just ignored my instincts and jumped in head first. &lt;strong&gt;Pluralization&lt;/strong&gt;?  Primary keys named &lt;code&gt;id&lt;/code&gt;? &lt;strong&gt;Blasphemy&lt;/strong&gt;! Yet, I moved forward. After a few days of consistently reminding myself of these new-fangled conventions (I even printed out a little cheat sheet and taped it to my monitor)... I was hooked. Rails worked with PostgreSQL. Let me say that again.&lt;/p&gt;


	&lt;h3&gt;&lt;span class="caps"&gt;RAILS WORKS WITH POSTGRESQL&lt;/span&gt;!&lt;/h3&gt;


	&lt;p&gt;Don&amp;#8217;t freak out. Yes, the person who started the Rails project is a &lt;a href="http://www.loudthinking.com/arc/000516.html"&gt;MySQL fan&lt;/a&gt;... but Rails is &lt;a href="http://weblog.rubyonrails.com/archives/2005/09/27/database-agnostic-schemas-with-migrations"&gt;database agnostic&lt;/a&gt;. It&amp;#8217;s true.&lt;/p&gt;


	&lt;p&gt;I wanted to write an entry to clear up a &lt;a href="http://people.planetpostgresql.org/xzilla/index.php?/archives/62-OSCon-Day-1-Evening-or-Ruby-on-Rails.html"&gt;few&lt;/a&gt; &lt;a href="http://blog.amber.org/2005/09/27/least-common-denominator/"&gt;myths&lt;/a&gt; about Rails.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Table names are &lt;strong&gt;not forced&lt;/strong&gt;&lt;/li&gt;
		&lt;li&gt;Primary Key field names are &lt;strong&gt;not forced&lt;/strong&gt;&lt;/li&gt;
		&lt;li&gt;Foriegn Key field names are &lt;strong&gt;not forced&lt;/strong&gt;&lt;/li&gt;
		&lt;li&gt;Legacy databases &lt;strong&gt;can&lt;/strong&gt; &lt;a href="http://www.robbyonrails.com/articles/2005/07/25/the-legacy-of-databases-with-rails"&gt;work with Rails&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;Active Record (like much of Rails) &lt;strong&gt;can&lt;/strong&gt; be customized for your particular application&amp;#8217;s needs&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I was, and still am, concerned about trusting Active Record with handling my relational data&amp;#8230; because in some cases it &lt;a href="http://www.robbyonrails.com/articles/2005/08/18/active-record-i-3-u-but-i-still-trust-my-database-server-a-tiny-bit-more"&gt;wasn&amp;#8217;t doing what it should&lt;/a&gt;.   But, that is being worked on&amp;#8230; and because Rails is Open Source, the issue is transparent for us developers to see and help fix.&lt;/p&gt;


	&lt;p&gt;In the past, you might have found me advocating the addition of &lt;strong&gt;business logic&lt;/strong&gt; in your database server, whenever possible. &lt;em&gt;It is the gate keeper&lt;/em&gt;. The key master. It is not meant to be &lt;strong&gt;flexible&lt;/strong&gt; with your data. If business rules are to change, you change them there so that if you have one or more &lt;span class="caps"&gt;INPUT&lt;/span&gt; streams into your database, the data will not get into your server unless your business rules are met. PostgreSQL even provides you with a nice &lt;span class="caps"&gt;ERROR&lt;/span&gt; message, which some other servers do not. If you can control everything through one &lt;span class="caps"&gt;INPUT&lt;/span&gt; stream (&lt;a href="http://martinfowler.com/bliki/ApplicationDatabase.html"&gt;Application Database&lt;/a&gt;),  then putting these constraints solely in your database abstraction layer is a-ok. However, if you are interfacing with your server through a variety of avenues (&lt;a href="http://martinfowler.com/bliki/IntegrationDatabase.html"&gt;Integration Database&lt;/a&gt;),  you probably already know that your system is going to be a pain to maintain in the long run. So, what are you to do?&lt;/p&gt;


	&lt;p&gt;Handling Legacy systems is going to be a headache, regardless of what you do&amp;#8230; but you can&amp;#8217;t always rebuild the whole thing and migrate your data. Add that to &lt;a href="http://www.43folders.com/2005/09/building_a_smar.html"&gt;your Someday-Maybe list&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;When you&amp;#8217;re adding new pieces to your application, start by using &lt;a href="http://subversion.tigris.org/"&gt;good tools&lt;/a&gt;, &lt;a href="http://www.rubyonrails.org/"&gt;frameworks&lt;/a&gt;, and &lt;a href="http://www.pragmaticprogrammer.com/ppbook/index.shtml"&gt;practices&lt;/a&gt;. For example, try your best to follow the &lt;a href="http://martinfowler.com/bliki/ApplicationDatabase.html"&gt;Application Database&lt;/a&gt; path to &lt;em&gt;pragmatic enlightenment&lt;/em&gt;.  Begin working on a new layer for your legacy database. Add on web services that use this layer. Refactor your existing applications. Can they use these new services? &lt;strong&gt;ReThink everything&lt;/strong&gt;. That is&amp;#8230; if you have the time and resources to do so.&lt;/p&gt;


	&lt;p&gt;If you are questioning whether or not to put your business logic in your database abstraction layer, stop. Just do it&amp;#8482;. But, do it with caution. Test it. Test it well. I&amp;#8217;ll admit that I &lt;strong&gt;still&lt;/strong&gt; add some constraints into my database schemas-&lt;del&gt;I am not ready to give up that extra layer of data security. But, that&amp;#8217;s just me. :&lt;/del&gt;)&lt;/p&gt;


	&lt;h3&gt;PostgreSQL works with Rails!&lt;/h3&gt;


	&lt;p&gt;...and I promise to show you how to do some fun legacy stuff with Rails in my book. :-)&lt;/p&gt;
</description>
      <pubDate>Tue, 27 Sep 2005 08:41:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:7af17a431c8a1a0b1f1b471e9cb07e79</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/09/27/the-bitter-sweet-taste-of-agnostic-database-schemas</link>
      <category>Ruby on Rails</category>
      <category>Programming</category>
      <category>PostgreSQL</category>
      <category>rails</category>
      <category>dhh</category>
      <category>postgresql</category>
    </item>
  </channel>
</rss>
