-
Allow Timescale to be a non-primary Rails DB
Rails.application.configure do config.after_initialize do Timescaledb::Rails::ApplicationRecord.connects_to database: { writing: :timescale, reading: :timescale } end end
-
Decouple compression policy from the compression settings
Before
add_hypertable_compression :events, 20.days, segment_by: 'event_type, name', order_by: 'occurred_at ASC, recorded_at DESC'
After
enable_hypertable_compression :events, segment_by: 'event_type, name', order_by: 'occurred_at ASC, recorded_at DESC' add_hypertable_compression_policy :events, 20.days
-
Add continuous aggregates refresh support
aggregate = Event.hypertable.continuous_aggregates.first aggregate.refresh!(5.days.ago, 1.day.ago)
-
Add chunk reorder support
chunk = Event.hypertable.chunks.first # If an index is not specified, it will use the one from the reorder policy # In case there is no reorder policy index it will raise an ArgumentError chunk.reorder! # If an index is specified it will use that index chunk.reorder!(index)
-
Add continuous aggregates migration APIs
class CreateTemperatureEventAggregate < ActiveRecord::Migration[7.0] def up create_continuous_aggregate( :temperature_events, Event.time_bucket(1.day).avg(:value).temperature.to_sql ) add_continuous_aggregate_policy(:temperature_events, 1.month, 1.day, 1.hour) end def down drop_continuous_aggregate(:temperature_events) remove_continuous_aggregate_policy(:temperature_events) end end
-
Add finder APIs to Active Record models
# When you know the exact time value Payload.find_at_time(111, Time.new(2022, 01, 01, 10, 15, 30)) # If you know that the record occurred after a given time Payload.find_after(222, 11.days.ago) # Lastly, if you want to scope the search by a time range Payload.find_between(333, 1.week.ago, 1.day.ago)
-
Add time_bucket support to Active Record models
Event.time_bucket(1.day) Event.time_bucket('1 day') Event.time_bucket(1.day, :created_at) Event.time_bucket(1.day, 'occurred_at')
You may add aggregation like so:
Event.time_bucket(1.day).avg(:column) Event.time_bucket(1.day).sum(:column) Event.time_bucket(1.day).min(:column) Event.time_bucket(1.day).max(:column) Event.time_bucket(1.day).count
-
Add time scopes to Active Record models
scope description last_year Returns records from last year last_month Returns records from last month last_week Returns records from last week this_year Returns records from this year this_month Returns records from this month this_week Returns records from this week yesterday Returns records from yesterday today Returns records from today -
Add hypertable reorder policy support
class AddEventReorderPolicy < ActiveRecord::Migration[7.0] def up add_hypertable_reorder_policy :events, :index_events_on_created_at_and_name end def down remove_hypertable_reorder_policy :events end end
-
Add chunk compression support
chunk = Event.hypertable_chunks.first chunk.compress! unless chunk.is_compressed? chunk.decompress! if chunk.is_compressed?
-
Add TimescaleDB support to Active Record models
class Event < ActiveRecord::Base include Timescaledb::Rails::Model end
-
Add hypertable retention policy support
class AddEventRetentionPolicy < ActiveRecord::Migration[7.0] def up add_hypertable_retention_policy :events, 1.year end def down remove_hypertable_retention_policy :events end end
-
Add support to compress by multiple columns
-
Define ActiveRecord::CommandRecorder methods for all timescaledb migration methods and also define their invert command methods.
-
Disable hypertable compression by doing:
class RemoveEventCompression < ActiveRecord::Migration[7.0] def change remove_hypertable_compression :events end end
-
Fix
structure_dump_flags
not working in Rails 6. -
Add
bin/ci
to run tests using all supported rails versions + code linter.
- Exclude
_timescaledb_internal
tables from structure.sql to avoid collision issues when enabling compression on hypertables.
- Fix suggested version dependencies by rubygems.
-
Create a hypertable from a PostgreSQL table by doing:
class CreateEvent < ActiveRecord::Migration[7.0] def change create_table :events, id: false do |t| t.string :name, null: false t.time :occurred_at, null: false t.timestamps end create_hypertable :events, :created_at, chunk_time_interval: '2 days' end end
-
Create a hypertable without a PostgreSQL table by doing:
class CreatePayloadHypertable < ActiveRecord::Migration[7.0] def change create_hypertable :payloads, :created_at, chunk_time_interval: '5 days' do |t| t.string :ip, null: false t.timestamps end end end
-
Enable hypertable compression by doing:
class AddEventCompression < ActiveRecord::Migration[7.0] def change add_hypertable_compression :events, 20.days, segment_by: :name, order_by: 'occurred_at DESC' end end