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.
-
#add_hypertable_reorder_policy(table_name, index_name) ⇒ Object
Adds a policy to reorder chunks on a given hypertable index in the background.
-
#add_hypertable_retention_policy(table_name, drop_after) ⇒ Object
Add a data retention policy to given hypertable.
-
#create_hypertable(table_name, time_column_name, **options, &block) ⇒ Object
Converts given standard PG table into a hypertable.
-
#hypertable_exists?(hypertable) ⇒ Boolean
Checks to see if the hypertable exists on the database.
- #hypertable_options_to_sql(options) ⇒ String
-
#hypertables ⇒ Object
Returns an array of hypertable names defined in the database.
-
#remove_hypertable_compression(table_name, compress_after = nil, segment_by: nil, order_by: nil) ⇒ Object
Disables compression from given table.
-
#remove_hypertable_reorder_policy(table_name, _index_name = nil) ⇒ Object
Removes a policy to reorder a particular hypertable.
-
#remove_hypertable_retention_policy(table_name, _drop_after = nil) ⇒ Object
Removes data retention policy from given hypertable.
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)
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 47 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 (#{.join(', ')})" execute "SELECT add_compression_policy('#{table_name}', INTERVAL '#{compress_after.inspect}')" end |
#add_hypertable_reorder_policy(table_name, index_name) ⇒ Object
Adds a policy to reorder chunks on a given hypertable index in the background.
add_hypertable_reorder_policy('events', 'index_events_on_created_at_and_name')
87 88 89 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 87 def add_hypertable_reorder_policy(table_name, index_name) execute "SELECT add_reorder_policy('#{table_name}', '#{index_name}')" end |
#add_hypertable_retention_policy(table_name, drop_after) ⇒ Object
Add a data retention policy to given hypertable.
add_hypertable_retention_policy('events', 7.days)
71 72 73 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 71 def add_hypertable_retention_policy(table_name, drop_after) execute "SELECT add_retention_policy('#{table_name}', INTERVAL '#{drop_after.inspect}')" 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')
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 29 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}', #{})" end |
#hypertable_exists?(hypertable) ⇒ Boolean
Checks to see if the hypertable exists on the database.
hypertable_exists?(:developers)
17 18 19 20 21 22 23 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 17 def hypertable_exists?(hypertable) query_value( <<-SQL.squish SELECT COUNT(*) FROM timescaledb_information.hypertables WHERE hypertable_name = #{quote(hypertable)} SQL ).to_i.positive? end |
#hypertable_options_to_sql(options) ⇒ String
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 100 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 |
#hypertables ⇒ Object
Returns an array of hypertable names defined in the database.
9 10 11 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 9 def hypertables query_values('SELECT hypertable_name FROM timescaledb_information.hypertables') 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')
63 64 65 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 63 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.inspect}');" end |
#remove_hypertable_reorder_policy(table_name, _index_name = nil) ⇒ Object
Removes a policy to reorder a particular hypertable.
remove_hypertable_reorder_policy('events')
95 96 97 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 95 def remove_hypertable_reorder_policy(table_name, _index_name = nil) execute "SELECT remove_reorder_policy('#{table_name}')" end |
#remove_hypertable_retention_policy(table_name, _drop_after = nil) ⇒ Object
Removes data retention policy from given hypertable.
remove_hypertable_retention_policy('events')
79 80 81 |
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 79 def remove_hypertable_retention_policy(table_name, _drop_after = nil) execute "SELECT remove_retention_policy('#{table_name}')" end |