Now, it is a widely known fact, among possibly half of my six subscribers, that I'm working on an ORM and database migration package for Rubyists that will kick ActiveRecord into the floor. Not that I don't have some respect for the library - it has, after all, seriously raised the bar in terms of user (ie webapp developer) simplicity. But it suffers some serious architectural faults, and has begun to outgrow the spirit of the ActiveRecord pattern (obligatory Fowler link). I am not the only person who thinks so either, some people such as Matthew at PlayLouder are far more frustrated than me.

I have been using the new RSpec plaintext story runner for this. I've been notoriously bad at doing integration testing before, mainly because of working with Rails and never having the time to get SafariWatir working. Put working on a pure library, I had no excuse. Here is the story that drove this first iteration (which has been a week of full-time evenings since last Saturday):

    Story: user executes simple query
      As a database application developer
      I want to connect to a database
      So that I can run a simple query via a connection

      Scenario: SQLite
        Given an engine for dialect: sqlite, pool: static,
          strategy: default and database: :memory:
        When user gets a connection object
        And executes the query "SELECT 'lucky' as guess;"
        And closes the connection
        Then user should get the result
          '[{0=>"lucky", "guess"=>"lucky"}]'

(I've added some artificial line breaks for the sake of the blog formatting.)

Now, while that may be short, it actually took several hundred lines of code (including blanks and comments, I will get exact stats when I've got some stats reporting) to drive this basic functionality. I've got a lot of framework in place for connection pooling, database dialects etc. It's been pretty slow so far, I hope development will speed up enormously now I've got some structure to play with. The hardest thing is always getting the pen to paper, writing is often the easy bit.

Thanks are due to David Chelimsky and others on the RSpec team for their work on RSpec stories and tireless support in the rspec-users list. To me, RSpec is the single most important library available in Ruby today, and I doubt that will change any time soon.

Other tools that have proved invaluable

While writing this, I've gone out of my way to use the best tools I can find. I've already mentioned the RSpec story runner, but I'm also now relying on these:

autotest

I can't tell you how much time it saves having something sit there running your specs for you. At first I missed the TextMate HTML output, now all I care about is that my specs have been re-run before I've had chance to switch to the implementation code.

darcs

I've got a lot of respect for Subversion, but I recently took a look at some distributed source control systems. After seeing this video of Linus Torvalds being a patronising arsehole, during which he takes the odd breath in-between insults to explain his motivations for building an SCM, I began to think that maybe the client-server model for source control isn't ideal. I'm totally sold on darcs - it just feels right. If you aren't as taken by it as me, you could always consider Mercurial, or another distributed SCM

There's some other stuff I want to get going, like heckle, but that will have to wait until I can find a few minutes to pull a Rakefile together.

4 Responses to “Behaviour-driven database ORM with RSpec stories”

  1. Pawel Kaminski Says:

    Wow Ash..i am looking forward to start using a new ORM.. when we should expect it to be ready?? and maybe there is any chance to see svn trunk? ..:) Rspec rocks...:)) One more thing is there any RSS feed somewhere on the blog or is it just me heaving problems finidng it...??? (usability issue :))

  2. Ash Says:

    It will be a good while before it will do anything useful... expect a basic version in a month or two (more like two) as I'm doing it in the evenings and weekends. I'm hoping to have it useable for a webapp around next spring, but who knows what will come up then.

    Right now it is stored in a darcs repository on my iMac. I might put this up on my server (the one running this blog) at some point, but not until it does something more advanced than pass queries straight through to SQLite :)

    There is an RSS feed, any decent news reader should pick it up if you tell it to open http://aviewfromafar.net/ . You DO use NetNewsWire don't you???

  3. swombat Says:

    Hey,

    Looks interesting, but from my own experience, ORM is really a big pain to write. There are a lot of really complicated issues in implementing an ORM layer. Have you looked at DataMapper and the other ORM layers that you can plug into Merb? That might save you some (a lot?) of time...?

    Daniel

  4. Ash Says:

    Hi Daniel

    Yes, ORM is a BIG pain to write! But it's so fundamental, and so often badly done, I wanted to have a go at something done right. Something that enables me to write database code in a clean, testable way, that is flexible but also transparent. I've seen DataMapper - I may well use it on a project. For me this is a combination of my frustration with current ORM tools and my perfectionist everything-must-be-done-BDD attitude to development. I think it's a manifestation of my OCD :)

Leave a Reply