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

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

Overview

:nodoc: rubocop:disable Layout/LineLength

Instance Method Summary collapse

Instance Method Details

#add_continuous_aggregate_policy_statement(continuous_aggregate, file) ⇒ Object



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

def add_continuous_aggregate_policy_statement(continuous_aggregate, file)
  return unless continuous_aggregate.refresh?

  start_offset = continuous_aggregate.refresh_start_offset
  end_offset = continuous_aggregate.refresh_end_offset
  schedule_interval = continuous_aggregate.refresh_schedule_interval

  file << "SELECT add_continuous_aggregate_policy('#{continuous_aggregate.view_schema}.#{continuous_aggregate.view_name}', start_offset => INTERVAL '#{start_offset}', end_offset => INTERVAL '#{end_offset}', schedule_interval => INTERVAL '#{schedule_interval}');\n\n"
end

#add_hypertable_compression_statement(hypertable, file) ⇒ Object



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

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

  options = hypertable_compression_options(hypertable)

  file << "ALTER TABLE #{hypertable.hypertable_schema}.#{hypertable.hypertable_name} SET (#{options});\n\n"
  file << "SELECT add_compression_policy('#{hypertable.hypertable_schema}.#{hypertable.hypertable_name}', INTERVAL '#{hypertable.compression_policy_interval}');\n\n"
end

#add_hypertable_reorder_policy_statement(hypertable, file) ⇒ Object



68
69
70
71
72
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 68

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

  file << "SELECT add_reorder_policy('#{hypertable.hypertable_schema}.#{hypertable.hypertable_name}', '#{hypertable.reorder_policy_index_name}');\n\n"
end

#add_hypertable_retention_policy_statement(hypertable, file) ⇒ Object



74
75
76
77
78
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 74

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

  file << "SELECT add_retention_policy('#{hypertable.hypertable_schema}.#{hypertable.hypertable_name}', INTERVAL '#{hypertable.retention_policy_interval}');\n\n"
end

#compression_order_settings(hypertable) ⇒ Object



116
117
118
119
120
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 116

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



122
123
124
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 122

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

#continuous_aggregates(filename) ⇒ Object



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

def continuous_aggregates(filename)
  File.open(filename, 'a') do |file|
    Timescaledb::Rails::ContinuousAggregate.all.each do |continuous_aggregate|
      create_continuous_aggregate_statement(continuous_aggregate, file)
      add_continuous_aggregate_policy_statement(continuous_aggregate, file)
    end
  end
end

#create_continuous_aggregate_statement(continuous_aggregate, file) ⇒ Object



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

def create_continuous_aggregate_statement(continuous_aggregate, file)
  file << "CREATE MATERIALIZED VIEW #{continuous_aggregate.view_schema}.#{continuous_aggregate.view_name} WITH (timescaledb.continuous) AS\n"
  file << "#{continuous_aggregate.view_definition.strip.indent(2)}\n\n"
end

#create_hypertable_statement(hypertable, file) ⇒ Object



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

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

  file << "SELECT create_hypertable('#{hypertable.hypertable_schema}.#{hypertable.hypertable_name}', '#{hypertable.time_column_name}', #{options});\n\n"
end

#drop_ts_insert_trigger_statment(hypertable, file) ⇒ Object



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

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"
  file << "---\n\n"
  file << "DROP TRIGGER IF EXISTS ts_insert_blocker ON #{hypertable.hypertable_schema}.#{hypertable.hypertable_name};\n"
end

#hypertable_compression_options(hypertable) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 102

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



95
96
97
98
99
100
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 95

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

#hypertables(filename) ⇒ Object



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

def hypertables(filename)
  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

#structure_dump(filename, extra_flags) ⇒ Object



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

def structure_dump(filename, extra_flags)
  extra_flags = Array(extra_flags)
  extra_flags |= timescale_structure_dump_default_flags if timescale_enabled?

  super(filename, extra_flags)

  return unless timescale_enabled?

  hypertables(filename)
  continuous_aggregates(filename)
end

#timescale_enabled?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 141

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

#timescale_structure_dump_default_flagsArray<String>

Returns ‘pg_dump` flags to exclude `_timescaledb_internal` schema tables and exclude the corresponding continuous aggregate views.

Returns:

  • (Array<String>)


130
131
132
133
134
135
136
137
138
# File 'lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb', line 130

def timescale_structure_dump_default_flags
  flags = ['--exclude-schema=_timescaledb_internal']

  Timescaledb::Rails::ContinuousAggregate.pluck(:view_schema, :view_name).each do |view_schema, view_name|
    flags << "--exclude-table=#{view_schema}.#{view_name}"
  end

  flags
end