timescaledb-rails 0.1.1 → 0.1.2

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: 9c8d3c2c06646deb35ad9d586965b9e4147504559734212b3f95168cb9db9e53
4
- data.tar.gz: 6754f263db1dd6dcf3fad150de76bc6eb6cdc72958c68187fab4ee087d2b825f
3
+ metadata.gz: af4399bfd70056ed23cfb0994580a8c61fcd88722a72ce7839761b305c6d2029
4
+ data.tar.gz: dad61e01554eee6ee23fd780a6745196d7fb5b3703398c795bbe3e00dfbe028b
5
5
  SHA512:
6
- metadata.gz: 806178772e4b1138ccce2b8e99ef8196e12b31023c339ba2383919c4f0eb199ce6d2f9af592411dd59c3c07a2dcc1a946a6bff8d926091d296693ba78af442b7
7
- data.tar.gz: e92a57a816dee7069f65c9b68bb578b3bcef4670ae3f87e63f47420b26af2c9cc27cf011e49cece36c33ab9fd3f8deb0ce68794023f4a17f479672572716d4d4
6
+ metadata.gz: 1679b7421eca2f5b557b83a7b338f72444d073009f532b77d75e8e233c07828750d02afc96f4b2ee0a5c61ce6c750f03832742621df1187529cddcec3e46995f
7
+ data.tar.gz: 3628e056c77754629a65c2ef343c59702272bbec882b47f2e70564fbd1008080729e05017f923a0145d78e186bb272a5e13394605ccf96bcb39a34f520c3ad9a
@@ -1,75 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_record/tasks/postgresql_database_tasks'
4
- require 'timescaledb/rails/orderby_compression'
5
-
6
3
  module Timescaledb
7
4
  module Rails
8
5
  module ActiveRecord
9
6
  # :nodoc:
10
7
  module DatabaseTasks
11
- def structure_dump(filename, extra_flags)
12
- super
13
-
14
- return unless timescale_enabled?
15
-
16
- File.open(filename, 'a') do |file|
17
- Timescaledb::Rails::Hypertable.all.each do |hypertable|
18
- drop_ts_insert_trigger_statment(hypertable, file)
19
- create_hypertable_statement(hypertable, file)
20
- add_hypertable_compression_statement(hypertable, file)
21
- end
22
- end
23
- end
24
-
25
- def drop_ts_insert_trigger_statment(hypertable, file)
26
- file << "---\n"
27
- 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
28
- file << "---\n\n"
29
- file << "DROP TRIGGER IF EXISTS ts_insert_blocker ON #{hypertable.hypertable_name};\n"
30
- end
31
-
32
- def create_hypertable_statement(hypertable, file)
33
- options = hypertable_options(hypertable)
34
-
35
- file << "SELECT create_hypertable('#{hypertable.hypertable_name}', '#{hypertable.time_column_name}', #{options});\n\n" # rubocop:disable Layout/LineLength
36
- end
37
-
38
- def add_hypertable_compression_statement(hypertable, file)
39
- return unless hypertable.compression?
40
-
41
- options = hypertable_compression_options(hypertable)
42
-
43
- file << "ALTER TABLE #{hypertable.hypertable_name} SET (#{options});\n\n"
44
- file << "SELECT add_compression_policy('#{hypertable.hypertable_name}', INTERVAL '#{hypertable.compression_policy_interval}');\n\n" # rubocop:disable Layout/LineLength
45
- end
46
-
47
- def hypertable_options(hypertable)
48
- sql_statements = ["if_not_exists => 'TRUE'"]
49
- sql_statements << "chunk_time_interval => INTERVAL '#{hypertable.chunk_time_interval.inspect}'"
50
-
51
- sql_statements.compact.join(', ')
52
- end
53
-
54
- def hypertable_compression_options(hypertable)
55
- segmentby_setting = hypertable.compression_settings.segmentby_setting.first
56
- orderby_setting = hypertable.compression_settings.orderby_setting.first
57
-
58
- sql_statements = ['timescaledb.compress']
59
- sql_statements << "timescaledb.compress_segmentby = '#{segmentby_setting.attname}'" if segmentby_setting
60
-
61
- if orderby_setting
62
- orderby = Timescaledb::Rails::OrderbyCompression.new(orderby_setting.attname,
63
- orderby_setting.orderby_asc).to_s
64
-
65
- sql_statements << "timescaledb.compress_orderby = '#{orderby}'"
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
66
20
  end
67
-
68
- sql_statements.join(', ')
69
21
  end
70
22
 
71
- def timescale_enabled?
72
- Timescaledb::Rails::Hypertable.table_exists?
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'
73
28
  end
74
29
  end
75
30
  end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record/tasks/postgresql_database_tasks'
4
+ require 'timescaledb/rails/orderby_compression'
5
+
6
+ module Timescaledb
7
+ module Rails
8
+ module ActiveRecord
9
+ # :nodoc:
10
+ module PostgreSQLDatabaseTasks
11
+ # @override
12
+ def structure_dump(filename, extra_flags)
13
+ super
14
+
15
+ return unless timescale_enabled?
16
+
17
+ File.open(filename, 'a') do |file|
18
+ Timescaledb::Rails::Hypertable.all.each do |hypertable|
19
+ drop_ts_insert_trigger_statment(hypertable, file)
20
+ create_hypertable_statement(hypertable, file)
21
+ add_hypertable_compression_statement(hypertable, file)
22
+ end
23
+ end
24
+ end
25
+
26
+ def drop_ts_insert_trigger_statment(hypertable, file)
27
+ file << "---\n"
28
+ 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
29
+ file << "---\n\n"
30
+ file << "DROP TRIGGER IF EXISTS ts_insert_blocker ON #{hypertable.hypertable_name};\n"
31
+ end
32
+
33
+ def create_hypertable_statement(hypertable, file)
34
+ options = hypertable_options(hypertable)
35
+
36
+ file << "SELECT create_hypertable('#{hypertable.hypertable_name}', '#{hypertable.time_column_name}', #{options});\n\n" # rubocop:disable Layout/LineLength
37
+ end
38
+
39
+ def add_hypertable_compression_statement(hypertable, file)
40
+ return unless hypertable.compression?
41
+
42
+ options = hypertable_compression_options(hypertable)
43
+
44
+ file << "ALTER TABLE #{hypertable.hypertable_name} SET (#{options});\n\n"
45
+ file << "SELECT add_compression_policy('#{hypertable.hypertable_name}', INTERVAL '#{hypertable.compression_policy_interval}');\n\n" # rubocop:disable Layout/LineLength
46
+ end
47
+
48
+ def hypertable_options(hypertable)
49
+ sql_statements = ["if_not_exists => 'TRUE'"]
50
+ sql_statements << "chunk_time_interval => INTERVAL '#{hypertable.chunk_time_interval.inspect}'"
51
+
52
+ sql_statements.compact.join(', ')
53
+ end
54
+
55
+ def hypertable_compression_options(hypertable)
56
+ segmentby_setting = hypertable.compression_settings.segmentby_setting.first
57
+ orderby_setting = hypertable.compression_settings.orderby_setting.first
58
+
59
+ sql_statements = ['timescaledb.compress']
60
+ sql_statements << "timescaledb.compress_segmentby = '#{segmentby_setting.attname}'" if segmentby_setting
61
+
62
+ if orderby_setting
63
+ orderby = Timescaledb::Rails::OrderbyCompression.new(orderby_setting.attname,
64
+ orderby_setting.orderby_asc).to_s
65
+
66
+ sql_statements << "timescaledb.compress_orderby = '#{orderby}'"
67
+ end
68
+
69
+ sql_statements.join(', ')
70
+ end
71
+
72
+ def timescale_enabled?
73
+ Timescaledb::Rails::Hypertable.table_exists?
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -3,6 +3,7 @@
3
3
  require 'rails'
4
4
 
5
5
  require_relative 'extensions/active_record/database_tasks'
6
+ require_relative 'extensions/active_record/postgresql_database_tasks'
6
7
  require_relative 'extensions/active_record/schema_dumper'
7
8
  require_relative 'extensions/active_record/schema_statements'
8
9
 
@@ -18,10 +19,14 @@ module Timescaledb
18
19
 
19
20
  initializer 'timescaledb-rails.add_timescale_support_to_active_record' do
20
21
  ActiveSupport.on_load(:active_record) do
21
- ::ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(
22
+ ::ActiveRecord::Tasks::DatabaseTasks.extend(
22
23
  Timescaledb::Rails::ActiveRecord::DatabaseTasks
23
24
  )
24
25
 
26
+ ::ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(
27
+ Timescaledb::Rails::ActiveRecord::PostgreSQLDatabaseTasks
28
+ )
29
+
25
30
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(
26
31
  Timescaledb::Rails::ActiveRecord::SchemaStatements
27
32
  )
@@ -3,6 +3,6 @@
3
3
  module Timescaledb
4
4
  # :nodoc:
5
5
  module Rails
6
- VERSION = '0.1.1'
6
+ VERSION = '0.1.2'
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.1
4
+ version: 0.1.2
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-22 00:00:00.000000000 Z
12
+ date: 2022-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -120,6 +120,7 @@ files:
120
120
  - lib/timescaledb-rails.rb
121
121
  - lib/timescaledb/rails.rb
122
122
  - lib/timescaledb/rails/extensions/active_record/database_tasks.rb
123
+ - lib/timescaledb/rails/extensions/active_record/postgresql_database_tasks.rb
123
124
  - lib/timescaledb/rails/extensions/active_record/schema_dumper.rb
124
125
  - lib/timescaledb/rails/extensions/active_record/schema_statements.rb
125
126
  - lib/timescaledb/rails/models.rb