Module: Timescaledb::Rails::ActiveRecord::SchemaStatements
- Defined in:
- lib/timescaledb/rails/extensions/active_record/schema_statements.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#add_hypertable_compression(table_name, compress_after, segment_by: nil, order_by: nil) ⇒ Object
Enables compression and sets compression options.
-
#create_hypertable(table_name, time_column_name, **options, &block) ⇒ Object
Converts given standard PG table into a hypertable.
- #hypertable_options_to_sql(options) ⇒ String
-
#remove_hypertable_compression(table_name, compress_after = nil, segment_by: nil, order_by: nil) ⇒ Object
Disables compression from given table.
Instance Method Details
#add_hypertable_compression(table_name, compress_after, segment_by: nil, order_by: nil) ⇒ Object
Enables compression and sets compression options.
add_hypertable_compression('events', 7.days, segment_by: :created_at, order_by: :name)
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 32 def add_hypertable_compression(table_name, compress_after, segment_by: nil, order_by: nil) compress_after = compress_after.inspect if compress_after.is_a?(ActiveSupport::Duration) = ['timescaledb.compress'] << "timescaledb.compress_orderby = '#{order_by}'" unless order_by.nil? << "timescaledb.compress_segmentby = '#{segment_by}'" unless segment_by.nil? execute "ALTER TABLE #{table_name} SET (#{options.join(', ')})" execute "SELECT add_compression_policy('#{table_name}', INTERVAL '#{compress_after}')" end |
#create_hypertable(table_name, time_column_name, **options, &block) ⇒ Object
Converts given standard PG table into a hypertable.
create_hypertable('readings', 'created_at', chunk_time_interval: '7 days')
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 14 def create_hypertable(table_name, time_column_name, **, &block) = .symbolize_keys = () if block primary_key = [:primary_key] force = [:force] create_table(table_name, id: false, primary_key: primary_key, force: force, **, &block) end execute "SELECT create_hypertable('#{table_name}', '#{time_column_name}', #{options_as_sql})" end |
#hypertable_options_to_sql(options) ⇒ String
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 53 def () sql_statements = .map do |option, value| case option when :chunk_time_interval then "chunk_time_interval => INTERVAL '#{value}'" when :if_not_exists then "if_not_exists => #{value ? 'TRUE' : 'FALSE'}" end end sql_statements.compact.join(', ') end |
#remove_hypertable_compression(table_name, compress_after = nil, segment_by: nil, order_by: nil) ⇒ Object
Disables compression from given table.
remove_hypertable_compression('events')
48 49 50 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 48 def remove_hypertable_compression(table_name, compress_after = nil, segment_by: nil, order_by: nil) # rubocop:disable Lint/UnusedMethodArgument execute "SELECT remove_compression_policy('#{table_name}');" end |