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



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

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

#add_hypertable_reorder_policy_statement(hypertable, file) ⇒ Object



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

def add_hypertable_reorder_policy_statement(hypertable, file)
  return unless hypertable.reorder?

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

#add_hypertable_retention_policy_statement(hypertable, file) ⇒ Object



59
60
61
62
63
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 59

def add_hypertable_retention_policy_statement(hypertable, file)
  return unless hypertable.retention?

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

#compression_order_settings(hypertable) ⇒ Object



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

def compression_order_settings(hypertable)
  hypertable.compression_order_settings.map do |os|
    Timescaledb::Rails::OrderbyCompression.new(os.attname, os.orderby_asc).to_s
  end
end

#compression_segment_settings(hypertable) ⇒ Object



92
93
94
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 92

def compression_segment_settings(hypertable)
  hypertable.compression_segment_settings.map(&:attname)
end

#create_hypertable_statement(hypertable, file) ⇒ Object



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

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



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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 72

def hypertable_compression_options(hypertable)
  sql_statements = ['timescaledb.compress']

  if (segments = compression_segment_settings(hypertable)).present?
    sql_statements << "timescaledb.compress_segmentby = '#{segments.join(', ')}'"
  end

  if (orders = compression_order_settings(hypertable)).present?
    sql_statements << "timescaledb.compress_orderby = '#{orders.join(', ')}'"
  end

  sql_statements.join(', ')
end

#hypertable_options(hypertable) ⇒ Object



65
66
67
68
69
70
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 65

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
28
29
# 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)
      add_hypertable_reorder_policy_statement(hypertable, file)
      add_hypertable_retention_policy_statement(hypertable, file)
    end
  end
end

#timescale_enabled?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 104

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)


99
100
101
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 99

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