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



42
43
44
45
46
47
48
49
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 42

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



36
37
38
39
40
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 36

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



29
30
31
32
33
34
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 29

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



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

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



51
52
53
54
55
56
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 51

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

  sql_statements.compact.join(', ')
end

#structure_dump(filename, extra_flags) ⇒ Object



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

def structure_dump(filename, extra_flags) # rubocop:disable Metrics/MethodLength
  extra_flags = Array(extra_flags)
  extra_flags << timescale_structure_dump_default_flags if timescale_enabled?

  super(filename, extra_flags)

  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)


83
84
85
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 83

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

#timescale_structure_dump_default_flagsString

Returns ‘pg_dump` flag to exclude `_timescaledb_internal` schema tables.

Returns:

  • (String)


78
79
80
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 78

def timescale_structure_dump_default_flags
  '--exclude-schema=_timescaledb_internal'
end