This gem adds support for using the PostgreSQL partitioning mechanism describe here.
Partitionable assumes the model you want to partition has a date attribute which will be used for checking the partitions constraints and triggers.
Let's say you have a model named ArticleStat
and its respective table named article_stats
.
Suppose this model also has a logdate
attribute of type date. We want to partition
the data by year and month using this attribute.
Add the acts_as_partitionable
module and method to the model. The index_fields
and logdate_attr
are mandatory
options. The first one adds an index for those attributes when creating the partitions and the latter
is the date attribute used for routing the records to the correct partitions:
class ArticleStat < ApplicationRecord
include Partitionable::ActsAsPartitionable
acts_as_partitionable index_fields: ['id', 'site'], logdate_attr: 'logdate'
end
And that's it. Now every time you create a new record, the gem will create the correspondent partition table if doesn't exists. It also will update the trigger function so every other new record that should go into this partition gets correctly routed.
Add this line to your application's Gemfile:
gem "partitionable", '~> 0.2.0'
And then execute:
$ bundle
First, create the database for the dummy embedded application:
cd test/dummy
bin/rails db:setup
cd ../..
Then you can run the tests with:
bin/test
- Fork it ( https://github.com/pacuna/partitionable/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The gem is available as open source under the terms of the MIT License.