diff --git a/lib/timescaledb/migration_helpers.rb b/lib/timescaledb/migration_helpers.rb index c1c5249..8c5f09e 100644 --- a/lib/timescaledb/migration_helpers.rb +++ b/lib/timescaledb/migration_helpers.rb @@ -49,7 +49,7 @@ def create_hypertable(table_name, original_logger = ActiveRecord::Base.logger ActiveRecord::Base.logger = Logger.new(STDOUT) - options = ["chunk_time_interval => INTERVAL '#{chunk_time_interval}'"] + options = ["chunk_time_interval => #{chunk_time_interval_clause(chunk_time_interval)}"] options += hypertable_options.map { |k, v| "#{k} => #{quote(v)}" } arguments = [ @@ -165,6 +165,14 @@ def build_with_clause_option_string(option_key, options) value = options[option_key] ? 'true' : 'false' ",timescaledb.#{option_key}=#{value}" end + + def chunk_time_interval_clause(chunk_time_interval) + if chunk_time_interval.is_a?(Numeric) + chunk_time_interval + else + "INTERVAL '#{chunk_time_interval}'" + end + end end end diff --git a/lib/timescaledb/schema_dumper.rb b/lib/timescaledb/schema_dumper.rb index b0ab811..1cde7d1 100644 --- a/lib/timescaledb/schema_dumper.rb +++ b/lib/timescaledb/schema_dumper.rb @@ -78,7 +78,7 @@ def timescale_hypertable(hypertable, stream) options = { time_column: time.column_name, - chunk_time_interval: time.time_interval.inspect, + chunk_time_interval: time.time_interval ? time.time_interval.inspect : time.integer_interval, **timescale_compression_settings_for(hypertable), **timescale_space_partition_for(hypertable), **timescale_index_options_for(hypertable) diff --git a/spec/support/active_record/schema.rb b/spec/support/active_record/schema.rb index 97977f9..67eea6b 100644 --- a/spec/support/active_record/schema.rb +++ b/spec/support/active_record/schema.rb @@ -39,6 +39,11 @@ def setup_tables options: { time_column: 'timestamp' } ) + create_table(:hypertable_with_id_partitioning, hypertable: { + time_column: 'id', + chunk_time_interval: 1_000_000 + }) + create_table(:non_hypertables) do |t| t.string :name end diff --git a/spec/timescaledb/schema_dumper_spec.rb b/spec/timescaledb/schema_dumper_spec.rb index 3fb7e25..7d75840 100644 --- a/spec/timescaledb/schema_dumper_spec.rb +++ b/spec/timescaledb/schema_dumper_spec.rb @@ -123,6 +123,17 @@ expect(dump).to include "create_default_indexes: false" end + it "extracts integer chunk_time_interval" do + options = { time_column: :id, chunk_time_interval: 10000 } + con.create_table :schema_tests, hypertable: options do |t| + t.timestamps + end + + dump = dump_output + + expect(dump).to include "chunk_time_interval: 10000" + end + context "compress_segmentby" do before(:each) do con.drop_table :segmentby_tests, force: :cascade if con.table_exists?(:segmentby_tests)