{"id":37,"date":"2007-08-02T20:16:40","date_gmt":"2007-08-03T02:16:40","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/2007\/08\/02\/rails-and-the-notion-of-stupid-databases-being-a-good-idea\/"},"modified":"2007-08-04T12:18:24","modified_gmt":"2007-08-04T18:18:24","slug":"rails-and-the-notion-of-stupid-databases-being-a-good-idea","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2007\/08\/02\/rails-and-the-notion-of-stupid-databases-being-a-good-idea\/","title":{"rendered":"Rails and the notion of Stupid Databases Being a Good Idea"},"content":{"rendered":"<p>For the last few days I&#8217;ve been struggling to bend Rails to my will regarding the proper way to assure data consistency. Today I made some progress. This builds upon some research I did a few months ago, and hopefully this is a more or less complete solution to the problem of making Rails work the way I want it to regarding test databases.<br \/>\n<!--more--><br \/>\n<a href=\"http:\/\/en.wikipedia.org\/wiki\/David_Heinemeier_Hansson\">DHH<\/a> has clearly stated that <a href=\"http:\/\/web.archive.org\/web\/20060418215514\/http:\/\/www.loudthinking.com\/arc\/000516.html\">he does not like a smart database<\/a>. This is common among application developers, particularly in the agile methods camp, in that they generally appear not to understand relational set theory, or if they do, they believe that it is inherently inferior to object oriented methods (which lack a theoretical basis, as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fabian_Pascal\">Fabian Pascal<\/a> will happily shout at anyone who will listen). I gather from DHH&#8217;s statements that he merely is trying to practice <a href=\"http:\/\/c2.com\/cgi-bin\/wiki?DontRepeatYourself\">Don&#8217;t Repeat Yourself<\/a> (a.k.a. DRY, one of the most important values of Rails). I gather from Rails itself that he either respects the need of some folks to disagree with him enough to provide hooks to bypass ActiveRecord, or that he at least agreed with someone else&#8217;s patch. By this I mean that there are ways around the ORM features of ActiveRecord, to do raw SQL and to execute raw DDL at database creation time, which implies that he isn&#8217;t trying to force his opinions on others, but rather to make it easier to do things his way than to do them a different way.<\/p>\n<p>Fair enough. Rails is opinionated software, as DHH often says, and I have found several cases where letting go of my particular way of doing things has been fine, given that Rails has a different but equally valid way of doing things that is made super easy by the framework. <\/p>\n<p>However, I disagree with his decision to keep the DB stupid, for two reasons.<\/p>\n<p>First, I prefer to put logic where it belongs, rather than gathering it all in one place. AJAX, and in particular Google Maps, is a good example of presentation logic going where it belongs, making the whole application work better. SQL RDBMSs have features that can be abused, and in some cases these features are there because a wrong-thinking but wealthy client demanded them, but most of the advanced features that a &#8220;Real Database&#8221; has are there so that you can protect yourself against data loss or data corruption. The database is in a unique position to let you declare rules for things that must always be true, and then to trust that the database will never violate those rules. Older versions of MySQL were notably lacking in these features and their absence was justified by MySQL staff who basically said &#8220;you don&#8217;t need that, and if you want it, you&#8217;re confused.&#8221; Rails has inherited some of these damaged assumptions from MySQL, leaving basic relational features like foreign keys out of the framework(!). Fortunately Rails allows plugins, and there is a set of <a href=\"http:\/\/www.redhillonrails.org\/\">foreign key plugins<\/a> that overturn this decision. But in general, if the database belongs to your application, that&#8217;s not an excuse to move database functionality into application code. By calling it your application&#8217;s database (as opposed to an <a href=\"http:\/\/martinfowler.com\/bliki\/DatabaseStyles.html\">Integration Database<\/a>) you imply that it is part of your application, and therefore any rules or procedural code in it is necessarily also part of your application. You can&#8217;t monopolize the database and say that no one else has any business using it, while at the same time holding it at arm&#8217;s length and saying it&#8217;s not a valid part of the application. It is. No, business rules probably don&#8217;t belong in the database, but basic data consistency maintenance (in rule or procedural form) does.<\/p>\n<p>I&#8217;m being charitable here, but my experience with individual practitioners of the Stupid Database Method invariably ends with me finding out that they don&#8217;t really understand databases at all (hence the desire to abstract the database away entirely with a driver plugin architecture topped by an ORM layer, lest they have to understand how a specific database product works), and would rather remain ignorant and reinvent the same functionality in the application layer or in the ORM layer. (It&#8217;s a case of &#8220;when all you have is a hammer, everything looks like a nail&#8221;, where the hammer is a general-purpose programming language, and you&#8217;re looking at a problem of high performance concurrent transactional programming.)<\/p>\n<p>Not surprisingly, the database of choice for these folks is the least featureful, lowest cost, easiest to install one available. Because naturally it&#8217;s much more agile to write and debug new multithreaded transactional code in a high level dynamic language. than it is to get the same functionality for free in a thoroughly tested product that&#8217;s written in C. Right? Perhaps DHH is not one of these people. I assume he is not, again based on what he has said and coded. But nevertheless, the folks I&#8217;ve talked to personally who agree with his point of view are all coming from a point of view of willful ignorance.<\/p>\n<p>Secondly, I prefer to employ defense in depth against data errors. Transient errors can have workarounds, but data errors are permanent, and that means that if your data is valuable, the damage done can be irreversible. Just because it&#8217;s possible for correct application code to avoid race conditions, improper escaping, etc. doesn&#8217;t mean that you should put all your eggs in that basket. When the price of data corruption is high (i.e. if you value the data in your database) then it&#8217;s worth the duplication of effort: test the application code, but also put a constraint in the database that will catch things the application code missed.<\/p>\n<p>This is the same sort of thinking that leads to using automated unit tests, then functional tests, then integration tests, and then some manual QA, all overlapping. Duplication of effort? Yes. Worth it? Yes. Database bugs are arguably the worst kind of bugs to find in production, so they merit extra code that maybe isn&#8217;t absolutely necessary for the application to work, but is nice to have since you&#8217;d like to sleep at night.<\/p>\n<p>So, I feel justified in wanting to put CHECK constraints and triggers in my database.<\/p>\n<p>The implementation details are discussed in <a href=\"\/blog\/2007\/08\/02\/rails-fixtures-the-test-db-and-testunit\/\">part 2<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the last few days I&#8217;ve been struggling to bend Rails to my will regarding the proper way to assure data consistency. Today I made some progress. This builds upon some research I did a few months ago, and hopefully this is a more or less complete solution to the problem of making Rails work &hellip; <a href=\"http:\/\/www.pervasivecode.com\/blog\/2007\/08\/02\/rails-and-the-notion-of-stupid-databases-being-a-good-idea\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rails and the notion of Stupid Databases Being a Good Idea&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,17,19,26,20,18],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-architecture","category-databases","category-postgresql","category-ruby","category-ruby-on-rails","category-sql"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/37"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":0,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}