activerecord_worm_table

activerecord_worm_table manages multiple backing tables for an ActiveRecord model. there is a single active table, a different working table, and one or more history tables [default 1]

inserts are done to the working table. the model’s class method advance_version then makes the the active table history, the working table active, and creates a new working table with identical schema to the base table

the model’s table_name method is overwritten to reflect the active table, so all ActiveRecord model methods, like finders, work transparently

to use, just include ActiveRecord::WormTable into your model :

class Foo < ActiveRecord::Base
  include ActiveRecord::WormTable
end

there are class methods to get the different table names, and manipulate the version :

  • Foo.table_name : current active table name

  • Foo.advance_version(&block) : run block to create a new version of the table. the working table will appear active to the Thread running block , and if block completes then the working table will become permanently globally active

  • Foo.working_table_name : current working table name

  • Foo.advance_version : make active table historical, working table active and create a new working table

the active table name is maintained as a row in an automatically created auxilliary table, [foos_switch in the case of Foo], and this is updated in a transaction

the presence of the historical tables [which are recycled] means that even though a transaction may advance_version, other transactions already in progress will continue to see the old active table_name, and selects in progress will continue to completion without rollback [ provided they don’t take longer to complete than it takes to recycle all history tables ]

Dependencies

only works with MySQL at the moment : ddl statements are too database specific, and rails’ schema operations too limited, to easily make it generic. wouldn’t be too hard to extend for another db tho

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 mccraig mccraig of the clan mccraig. See LICENSE for details.