Module: Timescaledb::Database::SchemaStatements

Included in:
Timescaledb::Database
Defined in:
lib/timescaledb/database/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_compression_policy_sql(hypertable, compress_after, **options) ⇒ String

Returns The add_compression_policy SQL statement.

Parameters:

  • hypertable (String)

    The name of the hypertable or continuous aggregate to create the policy for

  • compress_after (String)

    The age after which the policy job compresses chunks

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The add_compression_policy SQL statement

See Also:



55
56
57
58
59
60
61
62
# File 'lib/timescaledb/database/schema_statements.rb', line 55

def add_compression_policy_sql(hypertable, compress_after, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(hypertable), interval_to_sql(compress_after)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT add_compression_policy(#{arguments.join(', ')});"
end

#add_continuous_aggregate_policy_sql(continuous_aggregate, start_offset: nil, end_offset: nil, schedule_interval:, **options) ⇒ String

Returns The add_continuous_aggregate_policy SQL statement.

Parameters:

  • continuous_aggregate (String)

    The name of the continuous aggregate to add the policy for

  • start_offset (String) (defaults to: nil)

    The start of the refresh window as an interval relative to the time when the policy is executed

  • end_offset (String) (defaults to: nil)

    The end of the refresh window as an interval relative to the time when the policy is executed

  • schedule_interval (String)

    The interval between refresh executions in wall-clock time

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The add_continuous_aggregate_policy SQL statement

See Also:



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/timescaledb/database/schema_statements.rb', line 175

def add_continuous_aggregate_policy_sql(continuous_aggregate, start_offset: nil, end_offset: nil, schedule_interval:, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(continuous_aggregate)]
  arguments << named_notation_sql(name: :start_offset, value: interval_to_sql(start_offset))
  arguments << named_notation_sql(name: :end_offset, value: interval_to_sql(end_offset))
  arguments << named_notation_sql(name: :schedule_interval, value: interval_to_sql(schedule_interval))
  arguments += continuous_aggregate_policy_options_to_named_notation_sql(options)

  "SELECT add_continuous_aggregate_policy(#{arguments.join(', ')});"
end

#add_reorder_policy_sql(hypertable, index_name, **options) ⇒ String

Returns The add_reorder_policy SQL statement.

Parameters:

  • hypertable (String)

    The name of the hypertable to create the policy for

  • index_name (String)

    The existing index by which to order rows on disk

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The add_reorder_policy SQL statement

See Also:



113
114
115
116
117
118
119
120
# File 'lib/timescaledb/database/schema_statements.rb', line 113

def add_reorder_policy_sql(hypertable, index_name, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(hypertable), quote(index_name)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT add_reorder_policy(#{arguments.join(', ')});"
end

#add_retention_policy_sql(hypertable, drop_after, **options) ⇒ String

Returns The add_retention_policy SQL statement.

Parameters:

  • hypertable (String)

    The name of the hypertable to create the policy for

  • drop_after (String)

    The age after which the policy job drops chunks

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The add_retention_policy SQL statement

See Also:



84
85
86
87
88
89
90
91
# File 'lib/timescaledb/database/schema_statements.rb', line 84

def add_retention_policy_sql(hypertable, drop_after, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(hypertable), interval_to_sql(drop_after)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT add_retention_policy(#{arguments.join(', ')});"
end

#create_continuous_aggregate_sql(continuous_aggregate, sql, **options) ⇒ String

Returns The create materialized view SQL statement.

Parameters:

  • continuous_aggregate (String)

    The name of the continuous aggregate view to be created

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The create materialized view SQL statement

See Also:



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/timescaledb/database/schema_statements.rb', line 141

def create_continuous_aggregate_sql(continuous_aggregate, sql, **options)
  options.transform_keys!(&:to_sym)

  with_data_opts = %w[WITH DATA]
  with_data_opts.insert(1, 'NO') if options.key?(:with_no_data)

  <<~SQL
    CREATE MATERIALIZED VIEW #{continuous_aggregate}
    WITH (timescaledb.continuous) AS
    #{sql.strip}
    #{with_data_opts.join(' ')};
  SQL
end

#create_hypertable_sql(relation, time_column_name, **options) ⇒ String

Returns The create_hypertable SQL statement.

Parameters:

  • relation (String)

    The identifier of the table to convert to hypertable

  • time_column_name (String)

    The name of the column containing time values as well as the primary column to partition by

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The create_hypertable SQL statement

See Also:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/timescaledb/database/schema_statements.rb', line 10

def create_hypertable_sql(relation, time_column_name, **options)
  options.transform_keys!(&:to_sym)

  partitioning_column = options.delete(:partitioning_column)
  number_partitions = options.delete(:number_partitions)

  arguments  = [quote(relation), quote(time_column_name)]
  arguments += [quote(partitioning_column), number_partitions] if partitioning_column && number_partitions
  arguments += create_hypertable_options_to_named_notation_sql(options)

  "SELECT create_hypertable(#{arguments.join(', ')});"
end

#disable_hypertable_compression_sql(hypertable) ⇒ String

Returns The ALTER TABLE SQL to disable compression.

Parameters:

  • hypertable (String)

    The name of the hypertable to disable compression

Returns:

  • (String)

    The ALTER TABLE SQL to disable compression

See Also:



45
46
47
# File 'lib/timescaledb/database/schema_statements.rb', line 45

def disable_hypertable_compression_sql(hypertable)
  "ALTER TABLE #{hypertable} SET (timescaledb.compress = FALSE);"
end

#drop_continuous_aggregate_sql(continuous_aggregate, cascade: false) ⇒ String

Returns The drop materialized view SQL statement.

Parameters:

  • continuous_aggregate (String)

    The name of the continuous aggregate view to be dropped

  • cascade (Boolean) (defaults to: false)

    A boolean to drop objects that depend on the continuous aggregate view

Returns:

  • (String)

    The drop materialized view SQL statement

See Also:



160
161
162
163
164
165
# File 'lib/timescaledb/database/schema_statements.rb', line 160

def drop_continuous_aggregate_sql(continuous_aggregate, cascade: false)
  arguments = [continuous_aggregate]
  arguments << 'CASCADE' if cascade

  "DROP MATERIALIZED VIEW #{arguments.join(' ')};"
end

#enable_hypertable_compression_sql(hypertable, **options) ⇒ String

Returns The ALTER TABLE SQL to enable compression.

Parameters:

  • hypertable (String)

    The name of the hypertable to enable compression

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The ALTER TABLE SQL to enable compression

See Also:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/timescaledb/database/schema_statements.rb', line 28

def enable_hypertable_compression_sql(hypertable, **options)
  options.transform_keys!(&:to_sym)

  compress_orderby = options.delete(:compress_orderby)
  compress_segmentby = options.delete(:compress_segmentby)

  arguments = ['timescaledb.compress']
  arguments << "timescaledb.compress_orderby = #{quote(compress_orderby)}" if compress_orderby
  arguments << "timescaledb.compress_segmentby = #{quote(compress_segmentby)}" if compress_segmentby

  "ALTER TABLE #{hypertable} SET (#{arguments.join(', ')});"
end

#remove_compression_policy_sql(hypertable, **options) ⇒ String

Returns The remove_compression_policy SQL statement.

Parameters:

  • hypertable (String)

    The name of the hypertable to remove the policy from

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The remove_compression_policy SQL statement

See Also:



69
70
71
72
73
74
75
76
# File 'lib/timescaledb/database/schema_statements.rb', line 69

def remove_compression_policy_sql(hypertable, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(hypertable)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT remove_compression_policy(#{arguments.join(', ')});"
end

#remove_continuous_aggregate_policy_sql(continuous_aggregate, **options) ⇒ String

Returns The remove_continuous_aggregate_policy SQL statement.

Parameters:

  • continuous_aggregate (String)

    The name of the continuous aggregate the policy should be removed from

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The remove_continuous_aggregate_policy SQL statement

See Also:



192
193
194
195
196
197
198
199
# File 'lib/timescaledb/database/schema_statements.rb', line 192

def remove_continuous_aggregate_policy_sql(continuous_aggregate, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(continuous_aggregate)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT remove_continuous_aggregate_policy(#{arguments.join(', ')});"
end

#remove_reorder_policy_sql(hypertable, **options) ⇒ String

Returns The remove_retention_policy SQL statement.

Parameters:

  • hypertable (String)

    The name of the hypertable to remove the policy from

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The remove_retention_policy SQL statement

See Also:



127
128
129
130
131
132
133
134
# File 'lib/timescaledb/database/schema_statements.rb', line 127

def remove_reorder_policy_sql(hypertable, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(hypertable)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT remove_reorder_policy(#{arguments.join(', ')});"
end

#remove_retention_policy_sql(hypertable, **options) ⇒ String

Returns The remove_retention_policy SQL statement.

Parameters:

  • hypertable (String)

    The name of the hypertable to remove the policy from

  • options (Hash)

    The optional arguments

Returns:

  • (String)

    The remove_retention_policy SQL statement

See Also:



98
99
100
101
102
103
104
105
# File 'lib/timescaledb/database/schema_statements.rb', line 98

def remove_retention_policy_sql(hypertable, **options)
  options.transform_keys!(&:to_sym)

  arguments = [quote(hypertable)]
  arguments += policy_options_to_named_notation_sql(options)

  "SELECT remove_retention_policy(#{arguments.join(', ')});"
end