Module: Timescaledb::Rails::ActiveRecord::PostgreSQLDatabaseTasks

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_hypertable_compression_statement(hypertable, file) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 39

def add_hypertable_compression_statement(hypertable, file)
  return unless hypertable.compression?

  options = hypertable_compression_options(hypertable)

  file << "ALTER TABLE #{hypertable.hypertable_name} SET (#{options});\n\n"
  file << "SELECT add_compression_policy('#{hypertable.hypertable_name}', INTERVAL '#{hypertable.compression_policy_interval}');\n\n" # rubocop:disable Layout/LineLength
end

#create_hypertable_statement(hypertable, file) ⇒ Object



33
34
35
36
37
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 33

def create_hypertable_statement(hypertable, file)
  options = hypertable_options(hypertable)

  file << "SELECT create_hypertable('#{hypertable.hypertable_name}', '#{hypertable.time_column_name}', #{options});\n\n" # rubocop:disable Layout/LineLength
end

#drop_ts_insert_trigger_statment(hypertable, file) ⇒ Object



26
27
28
29
30
31
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 26

def drop_ts_insert_trigger_statment(hypertable, file)
  file << "---\n"
  file << "--- Drop ts_insert_blocker previously created by pg_dump to avoid pg errors, create_hypertable will re-create it again.\n" # rubocop:disable Layout/LineLength
  file << "---\n\n"
  file << "DROP TRIGGER IF EXISTS ts_insert_blocker ON #{hypertable.hypertable_name};\n"
end

#hypertable_compression_options(hypertable) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 55

def hypertable_compression_options(hypertable)
  segmentby_setting = hypertable.compression_settings.segmentby_setting.first
  orderby_setting = hypertable.compression_settings.orderby_setting.first

  sql_statements = ['timescaledb.compress']
  sql_statements << "timescaledb.compress_segmentby = '#{segmentby_setting.attname}'" if segmentby_setting

  if orderby_setting
    orderby = Timescaledb::Rails::OrderbyCompression.new(orderby_setting.attname,
                                                         orderby_setting.orderby_asc).to_s

    sql_statements << "timescaledb.compress_orderby = '#{orderby}'"
  end

  sql_statements.join(', ')
end

#hypertable_options(hypertable) ⇒ Object



48
49
50
51
52
53
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 48

def hypertable_options(hypertable)
  sql_statements = ["if_not_exists => 'TRUE'"]
  sql_statements << "chunk_time_interval => INTERVAL '#{hypertable.chunk_time_interval.inspect}'"

  sql_statements.compact.join(', ')
end

#structure_dump(filename, extra_flags) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 12

def structure_dump(filename, extra_flags)
  super

  return unless timescale_enabled?

  File.open(filename, 'a') do |file|
    Timescaledb::Rails::Hypertable.all.each do |hypertable|
      drop_ts_insert_trigger_statment(hypertable, file)
      create_hypertable_statement(hypertable, file)
      add_hypertable_compression_statement(hypertable, file)
    end
  end
end

#timescale_enabled?Boolean

Returns:

  • (Boolean)


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

def timescale_enabled?
  Timescaledb::Rails::Hypertable.table_exists?
end