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_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 '#{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, **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

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

#hypertable_options_to_sql(options) ⇒ String

Returns:

  • (String)


100
101
102
103
104
105
106
107
108
109
# File 'lib/timescaledb/rails/extensions/active_record/schema_statements.rb', line 100

def hypertable_options_to_sql(options)
  sql_statements = options.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

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