activerecord_snapshot_view provides simple management of snapshot materialized views for ActiveRecord
a snapshot view is created by includeing the ActiveRecord::SnapshotView module into an ActiveRecord class. new versions of the snapshot are created using the new_version class method
multiple versions of the view are maintained : there is a single active version, a working version, and one or more history tables [by default 1]
the model's table_name method is overwritten to reflect the active table, so all ActiveRecord model methods, like finders, work transparently. the caveat is that you must use the table_name method to determine the current snapshot's table name when constructing SQL
to use, include ActiveRecord::SnapshotView into your model :
class Foo < ActiveRecord::Base include ActiveRecord::SnapshotView end
there are class methods to get the different table names, and manipulate the version :
Foo.table_name : current active or working table name
Foo.working_table_name : current working table name
Foo.active_table_name : current active table name
Foo.new_version(&block) : run block to create a new version of the table
Foo.updated_version(&block) : copy all data from the active table to the working table, then run block to update the working version
during the execution of block in new_version or updated_version Foo.table_name returns the working table name. if block completes without raising an exception, or if the exception is an ActiveRecord::SnapshotView::SaveWork, then the working table becomes permanently globally active
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 ]
MySQL only at the moment
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.