Module: Timescaledb::Rails::ActiveRecord::SchemaStatements

Defined in:
lib/timescaledb/rails/extensions/active_record/schema_statements.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_continuous_aggregate_policy(view_name, start_offset, end_offset, schedule_interval) ⇒ Object

Adds refresh continuous aggregate policy

add_continuous_aggregate_policy('temperature_events', 1.month, 1.day, 1.hour)


122
123
124
125
126
127
128
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 122

def add_continuous_aggregate_policy(view_name, start_offset, end_offset, schedule_interval)
  start_offset = start_offset.nil? ? 'NULL' : "INTERVAL '#{stringify_interval(start_offset)}'"
  end_offset = end_offset.nil? ? 'NULL' : "INTERVAL '#{stringify_interval(end_offset)}'"
  schedule_interval = schedule_interval.nil? ? 'NULL' : "INTERVAL '#{stringify_interval(schedule_interval)}'"

  execute "SELECT add_continuous_aggregate_policy('#{view_name}', start_offset => #{start_offset}, end_offset => #{end_offset}, schedule_interval => #{schedule_interval});" # rubocop:disable Layout/LineLength
end

#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)

  options = ['timescaledb.compress']
  options << "timescaledb.compress_orderby = '#{order_by}'" unless order_by.nil?
  options << "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 '#{stringify_interval(compress_after)}');"
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')


88
89
90
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 88

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)


72
73
74
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 72

def add_hypertable_retention_policy(table_name, drop_after)
  execute "SELECT add_retention_policy('#{table_name}', INTERVAL '#{stringify_interval(drop_after)}');"
end

#create_continuous_aggregate(view_name, view_query) ⇒ Object

Creates a continuous aggregate

create_continuous_aggregate(
  'temperature_events', "SELECT * FROM events where event_type = 'temperature'"
)


106
107
108
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 106

def create_continuous_aggregate(view_name, view_query)
  execute "CREATE MATERIALIZED VIEW #{view_name} WITH (timescaledb.continuous) AS #{view_query};"
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, **options, &block)
  options = options.symbolize_keys
  options_as_sql = hypertable_options_to_sql(options)

  if block
    primary_key = options[:primary_key]
    force = options[:force]

    create_table(table_name, id: false, primary_key: primary_key, force: force, **options, &block)
  end

  execute "SELECT create_hypertable('#{table_name}', '#{time_column_name}', #{options_as_sql});"
end

#drop_continuous_aggregate(view_name, _view_query = nil) ⇒ Object

Drops a continuous aggregate

drop_continuous_aggregate('temperature_events')


114
115
116
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 114

def drop_continuous_aggregate(view_name, _view_query = nil)
  execute "DROP MATERIALIZED VIEW #{view_name};"
end

#hypertable_exists?(hypertable) ⇒ Boolean

Checks to see if the hypertable exists on the database.

hypertable_exists?(:developers)

Returns:

  • (Boolean)


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(
    "      SELECT COUNT(*) FROM timescaledb_information.hypertables WHERE hypertable_name = \#{quote(hypertable)}\n    SQL\n  ).to_i.positive?\nend\n".squish

#hypertablesObject

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_continuous_aggregate_policy(view_name, _start_offset = nil, _end_offset = nil, _schedule_interval = nil) ⇒ Object

Removes refresh continuous aggregate policy

remove_continuous_aggregate_policy('temperature_events')


134
135
136
137
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 134

def remove_continuous_aggregate_policy(view_name, _start_offset = nil,
                                       _end_offset = nil, _schedule_interval = nil)
  execute "SELECT remove_continuous_aggregate_policy('#{view_name}');"
end

#remove_hypertable_compression(table_name, compress_after = nil, segment_by: nil, order_by: nil) ⇒ Object

Removes compression policy and disables compression from given hypertable.

remove_hypertable_compression('events')


63
64
65
66
# 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}');"
  execute "ALTER TABLE #{table_name.inspect} SET (timescaledb.compress = false);"
end

#remove_hypertable_reorder_policy(table_name, _index_name = nil) ⇒ Object

Removes a policy to reorder a particular hypertable.

remove_hypertable_reorder_policy('events')


96
97
98
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 96

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')


80
81
82
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 80

def remove_hypertable_retention_policy(table_name, _drop_after = nil)
  execute "SELECT remove_retention_policy('#{table_name}');"
end