timescaledb-rails 0.1.2 → 0.1.3

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: af4399bfd70056ed23cfb0994580a8c61fcd88722a72ce7839761b305c6d2029
4
- data.tar.gz: dad61e01554eee6ee23fd780a6745196d7fb5b3703398c795bbe3e00dfbe028b
3
+ metadata.gz: 2b682514201eb2e88a829297e9582cfe9513a127432de8acf7a060b769f38ca2
4
+ data.tar.gz: c30e268a3b9be19d95537230126a1c12c8aa97746afb3227f0e544293527d399
5
5
  SHA512:
6
- metadata.gz: 1679b7421eca2f5b557b83a7b338f72444d073009f532b77d75e8e233c07828750d02afc96f4b2ee0a5c61ce6c750f03832742621df1187529cddcec3e46995f
7
- data.tar.gz: 3628e056c77754629a65c2ef343c59702272bbec882b47f2e70564fbd1008080729e05017f923a0145d78e186bb272a5e13394605ccf96bcb39a34f520c3ad9a
6
+ metadata.gz: 1011081beb9b4a23ff4ceafe7b47278ad7418ace4438e3a67097e8db933802afd15875ba0a28f4141154ba12170bff7e4db28c4d8c5040d716c49f53b6c8d328
7
+ data.tar.gz: cd429d48862ce9dd82002fe35d3953814e893bd066a6e2143a9d5245036015d21b2b23174165389888225649e963a696a2328e0b5a228bf88efc268c39a5919e
data/README.md CHANGED
@@ -60,6 +60,16 @@ class AddEventCompression < ActiveRecord::Migration[7.0]
60
60
  end
61
61
  ```
62
62
 
63
+ Disable hypertable compression by doing:
64
+
65
+ ```ruby
66
+ class RemoveEventCompression < ActiveRecord::Migration[7.0]
67
+ def change
68
+ remove_hypertable_compression :events
69
+ end
70
+ end
71
+ ```
72
+
63
73
  ## Supported Ruby/Rails versions
64
74
 
65
75
  Supported Ruby/Rails versions are listed in [`.github/workflows/ci.yaml`](https://github.com/crunchloop/timescaledb-rails/blob/main/.github/workflows/ci.yaml)
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Timescaledb
4
+ module Rails
5
+ module ActiveRecord
6
+ # :nodoc:
7
+ module CommandRecorder
8
+ def create_hypertable(*args, &block)
9
+ record(:create_hypertable, args, &block)
10
+ end
11
+
12
+ def add_hypertable_compression(*args, &block)
13
+ record(:add_hypertable_compression, args, &block)
14
+ end
15
+
16
+ def remove_hypertable_compression(*args, &block)
17
+ record(:remove_hypertable_compression, args, &block)
18
+ end
19
+
20
+ def invert_create_hypertable(args, &block)
21
+ if block.nil?
22
+ raise ::ActiveRecord::IrreversibleMigration, 'create_hypertable is only reversible if given a block (can be empty).' # rubocop:disable Layout/LineLength
23
+ end
24
+
25
+ [:drop_table, args.first, block]
26
+ end
27
+
28
+ def invert_add_hypertable_compression(args, &block)
29
+ [:remove_hypertable_compression, args, block]
30
+ end
31
+
32
+ def invert_remove_hypertable_compression(args, &block)
33
+ if args.size < 2
34
+ raise ::ActiveRecord::IrreversibleMigration, 'remove_hypertable_compression is only reversible if given table name and compress period.' # rubocop:disable Layout/LineLength
35
+ end
36
+
37
+ [:add_hypertable_compression, args, block]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -9,8 +9,11 @@ module Timescaledb
9
9
  # :nodoc:
10
10
  module PostgreSQLDatabaseTasks
11
11
  # @override
12
- def structure_dump(filename, extra_flags)
13
- super
12
+ def structure_dump(filename, extra_flags) # rubocop:disable Metrics/MethodLength
13
+ extra_flags = Array(extra_flags)
14
+ extra_flags << timescale_structure_dump_default_flags if timescale_enabled?
15
+
16
+ super(filename, extra_flags)
14
17
 
15
18
  return unless timescale_enabled?
16
19
 
@@ -47,7 +50,7 @@ module Timescaledb
47
50
 
48
51
  def hypertable_options(hypertable)
49
52
  sql_statements = ["if_not_exists => 'TRUE'"]
50
- sql_statements << "chunk_time_interval => INTERVAL '#{hypertable.chunk_time_interval.inspect}'"
53
+ sql_statements << "chunk_time_interval => INTERVAL '#{hypertable.chunk_time_interval}'"
51
54
 
52
55
  sql_statements.compact.join(', ')
53
56
  end
@@ -69,6 +72,14 @@ module Timescaledb
69
72
  sql_statements.join(', ')
70
73
  end
71
74
 
75
+ # Returns `pg_dump` flag to exclude `_timescaledb_internal` schema tables.
76
+ #
77
+ # @return [String]
78
+ def timescale_structure_dump_default_flags
79
+ '--exclude-schema=_timescaledb_internal'
80
+ end
81
+
82
+ # @return [Boolean]
72
83
  def timescale_enabled?
73
84
  Timescaledb::Rails::Hypertable.table_exists?
74
85
  end
@@ -41,6 +41,15 @@ module Timescaledb
41
41
  execute "SELECT add_compression_policy('#{table_name}', INTERVAL '#{compress_after}')"
42
42
  end
43
43
 
44
+ # Disables compression from given table.
45
+ #
46
+ # remove_hypertable_compression('events')
47
+ #
48
+ def remove_hypertable_compression(table_name, compress_after = nil, segment_by: nil, order_by: nil) # rubocop:disable Lint/UnusedMethodArgument
49
+ execute "SELECT remove_compression_policy('#{table_name}');"
50
+ end
51
+
52
+ # @return [String]
44
53
  def hypertable_options_to_sql(options)
45
54
  sql_statements = options.map do |option, value|
46
55
  case option
@@ -19,7 +19,9 @@ module Timescaledb
19
19
 
20
20
  # @return [String]
21
21
  def chunk_time_interval
22
- time_dimension.time_interval
22
+ interval = time_dimension.time_interval
23
+
24
+ interval.is_a?(String) ? interval : interval.inspect
23
25
  end
24
26
 
25
27
  # @return [String]
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rails'
4
4
 
5
- require_relative 'extensions/active_record/database_tasks'
5
+ require_relative 'extensions/active_record/command_recorder'
6
6
  require_relative 'extensions/active_record/postgresql_database_tasks'
7
7
  require_relative 'extensions/active_record/schema_dumper'
8
8
  require_relative 'extensions/active_record/schema_statements'
@@ -19,8 +19,8 @@ module Timescaledb
19
19
 
20
20
  initializer 'timescaledb-rails.add_timescale_support_to_active_record' do
21
21
  ActiveSupport.on_load(:active_record) do
22
- ::ActiveRecord::Tasks::DatabaseTasks.extend(
23
- Timescaledb::Rails::ActiveRecord::DatabaseTasks
22
+ ::ActiveRecord::Migration::CommandRecorder.prepend(
23
+ Timescaledb::Rails::ActiveRecord::CommandRecorder
24
24
  )
25
25
 
26
26
  ::ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(
@@ -3,6 +3,6 @@
3
3
  module Timescaledb
4
4
  # :nodoc:
5
5
  module Rails
6
- VERSION = '0.1.2'
6
+ VERSION = '0.1.3'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timescaledb-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iván Etchart
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-11-24 00:00:00.000000000 Z
12
+ date: 2022-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -119,7 +119,7 @@ files:
119
119
  - README.md
120
120
  - lib/timescaledb-rails.rb
121
121
  - lib/timescaledb/rails.rb
122
- - lib/timescaledb/rails/extensions/active_record/database_tasks.rb
122
+ - lib/timescaledb/rails/extensions/active_record/command_recorder.rb
123
123
  - lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb
124
124
  - lib/timescaledb/rails/extensions/active_record/schema_dumper.rb
125
125
  - lib/timescaledb/rails/extensions/active_record/schema_statements.rb
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Timescaledb
4
- module Rails
5
- module ActiveRecord
6
- # :nodoc:
7
- module DatabaseTasks
8
- # @override
9
- def structure_dump_flags_for(adapter)
10
- return (flags = super) if adapter != 'postgresql'
11
-
12
- if flags.nil?
13
- timescaledb_structure_dump_default_flags
14
- elsif flags.is_a?(Array)
15
- flags << timescaledb_structure_dump_default_flags
16
- elsif flags.is_a?(String)
17
- "#{flags} #{timescaledb_structure_dump_default_flags}"
18
- else
19
- flags
20
- end
21
- end
22
-
23
- # Returns `pg_dump` flag to exclude `_timescaledb_internal` schema tables.
24
- #
25
- # @return [String]
26
- def timescaledb_structure_dump_default_flags
27
- '--exclude-schema=_timescaledb_internal'
28
- end
29
- end
30
- end
31
- end
32
- end