timescaledb 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5f8ebd4460e965fbf9a35600630c05d476b1a4b674192cd925a3d5f948a64a1
4
- data.tar.gz: d697ab124689a8c4f1ffc5b809a7cecd2ac07bbce84c7b9ca539dc5ae67068c9
3
+ metadata.gz: f5cb8809c9bfe578eca1fc5fd3e5d6f93a3ff7eaa9a82f6f343d6258aa591817
4
+ data.tar.gz: 790728e08291b9df8539e08c37639bf81e8669df4e9321bc494cb27b25a21e81
5
5
  SHA512:
6
- metadata.gz: 1070bf2f732137006d81790ac1c4b467f733edd3bb724e8773d3c9f6ecb3b5c1dc32c3da638f351589fa944a48c1b8f7a037da6a896beed1557e4fb34e2a8442
7
- data.tar.gz: f5bc47e8c0022d079189e7ad68e2214da6760c4f420bca3c83950137dd7026985fb9239924c241b28f261546a860c744a32db154548bfc0f8871302d35109ff7
6
+ metadata.gz: 61deae5c4a9884e56595771fbbd5de843bc4b06b395bd119495193c5de5a9eb66087453d749d940223e79bfccd5703def5c829f9737385ebd414460782ef5340
7
+ data.tar.gz: fbd19b66f8d35af8995c25335bd001b92218acf30032533bd5972dff1bfed76105053c99c680ae705d67b6c87c8149c7426daa49de4913a58cc8d2b62327e442
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- timescaledb (0.2.4)
4
+ timescaledb (0.2.5)
5
5
  activerecord
6
6
  activesupport
7
7
  pg (~> 1.2)
@@ -4,7 +4,12 @@ require 'active_record/connection_adapters/postgresql_adapter'
4
4
  module Timescaledb
5
5
  # Migration helpers can help you to setup hypertables by default.
6
6
  module MigrationHelpers
7
- # create_table can receive `hypertable` argument
7
+ # `create_table` accepts a `hypertable` argument with options for creating
8
+ # a TimescaleDB hypertable.
9
+ #
10
+ # See https://docs.timescale.com/api/latest/hypertable/create_hypertable/#optional-arguments
11
+ # for additional options supported by the plugin.
12
+ #
8
13
  # @example
9
14
  # options = {
10
15
  # time_column: 'created_at',
@@ -27,15 +32,29 @@ module Timescaledb
27
32
  # Setup hypertable from options
28
33
  # @see create_table with the hypertable options.
29
34
  def create_hypertable(table_name,
30
- time_column: 'created_at',
31
- chunk_time_interval: '1 week',
32
- compress_segmentby: nil,
33
- compress_orderby: 'created_at',
34
- compression_interval: nil
35
- )
35
+ time_column: 'created_at',
36
+ chunk_time_interval: '1 week',
37
+ compress_segmentby: nil,
38
+ compress_orderby: 'created_at',
39
+ compression_interval: nil,
40
+ partition_column: nil,
41
+ number_partitions: nil,
42
+ **hypertable_options)
36
43
 
37
44
  ActiveRecord::Base.logger = Logger.new(STDOUT)
38
- execute "SELECT create_hypertable('#{table_name}', '#{time_column}', chunk_time_interval => INTERVAL '#{chunk_time_interval}')"
45
+
46
+ options = ["chunk_time_interval => INTERVAL '#{chunk_time_interval}'"]
47
+ options += hypertable_options.map { |k, v| "#{k} => #{quote(v)}" }
48
+
49
+ arguments = [
50
+ quote(table_name),
51
+ quote(time_column),
52
+ (quote(partition_column) if partition_column),
53
+ (number_partitions if partition_column),
54
+ *options
55
+ ]
56
+
57
+ execute "SELECT create_hypertable(#{arguments.compact.join(', ')})"
39
58
 
40
59
  if compress_segmentby
41
60
  execute <<~SQL
@@ -21,15 +21,15 @@ module Timescaledb
21
21
  end
22
22
 
23
23
  def timescale_hypertables(stream)
24
- stream.puts # Insert a blank line above the hypertable definitions, for readability
25
-
26
24
  sorted_hypertables.each do |hypertable|
27
- timescale_hypertable(hypertable, stream)
25
+ timescale_hypertable(hypertable, stream)
28
26
  end
29
27
  end
30
28
 
31
29
  def timescale_retention_policies(stream)
32
- stream.puts # Insert a blank line above the retention policies, for readability
30
+ if sorted_hypertables.any? { |hypertable| hypertable.jobs.exists?(proc_name: "policy_retention") }
31
+ stream.puts # Insert a blank line above the retention policies, for readability
32
+ end
33
33
 
34
34
  sorted_hypertables.each do |hypertable|
35
35
  timescale_retention_policy(hypertable, stream)
@@ -39,13 +39,18 @@ module Timescaledb
39
39
  private
40
40
 
41
41
  def timescale_hypertable(hypertable, stream)
42
- dim = hypertable.main_dimension
43
- extra_settings = {
44
- time_column: "#{dim.column_name}",
45
- chunk_time_interval: "#{dim.time_interval.inspect}"
46
- }.merge(timescale_compression_settings_for(hypertable)).map {|k, v| %Q[#{k}: "#{v}"]}.join(", ")
47
-
48
- stream.puts %Q[ create_hypertable "#{hypertable.hypertable_name}", #{extra_settings}]
42
+ time = hypertable.main_dimension
43
+
44
+ options = {
45
+ time_column: time.column_name,
46
+ chunk_time_interval: time.time_interval.inspect,
47
+ **timescale_compression_settings_for(hypertable),
48
+ **timescale_space_partition_for(hypertable),
49
+ **timescale_index_options_for(hypertable)
50
+ }
51
+
52
+ options = options.map { |k, v| "#{k}: #{v.to_json}" }.join(", ")
53
+ stream.puts %Q[ create_hypertable "#{hypertable.hypertable_name}", #{options}]
49
54
  end
50
55
 
51
56
  def timescale_retention_policy(hypertable, stream)
@@ -70,6 +75,22 @@ module Timescaledb
70
75
  compression_settings
71
76
  end
72
77
 
78
+ def timescale_space_partition_for(hypertable)
79
+ return {} unless hypertable.dimensions.length > 1
80
+
81
+ space = hypertable.dimensions.last
82
+ {partition_column: space.column_name, number_partitions: space.num_partitions}
83
+ end
84
+
85
+ def timescale_index_options_for(hypertable)
86
+ time = hypertable.main_dimension
87
+ if @connection.indexes(hypertable.hypertable_name).any? { |i| i.columns == [time.column_name] }
88
+ {}
89
+ else
90
+ {create_default_indexes: false}
91
+ end
92
+ end
93
+
73
94
  def timescale_continuous_aggregates(stream)
74
95
  Timescaledb::ContinuousAggregates.all.each do |aggregate|
75
96
  opts = if (refresh_policy = aggregate.jobs.refresh_continuous_aggregate.first)
@@ -95,6 +116,7 @@ module Timescaledb
95
116
 
96
117
  "INTERVAL '#{value}'"
97
118
  end
119
+
98
120
  def sorted_hypertables
99
121
  @sorted_hypertables ||= Timescaledb::Hypertable.order(:hypertable_name).to_a
100
122
  end
@@ -1,3 +1,3 @@
1
1
  module Timescaledb
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timescaledb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jônatas Davi Paganini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-16 00:00:00.000000000 Z
11
+ date: 2022-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg