Skip to content

Commit

Permalink
Use new interface for create_hypertable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatas committed Dec 3, 2024
1 parent 7a21256 commit c9ecb1e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.2
3.3.3
13 changes: 9 additions & 4 deletions lib/timescaledb/migration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,30 @@ def valid_table_definition_options # :nodoc:
# @see create_table with the hypertable options.
def create_hypertable(table_name,
time_column: 'created_at',
by_range: :created_at,
chunk_time_interval: '1 week',
compress_segmentby: nil,
compress_orderby: 'created_at',
compress_after: nil,
drop_after: nil,
partition_column: nil,
number_partitions: nil,
**hypertable_options)

original_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = Logger.new(STDOUT)

arguments = [
quote(table_name),
"by_range(#{quote(time_column)}, #{parse_interval(chunk_time_interval)})",
dimension = "by_range(#{quote(time_column)}, #{parse_interval(chunk_time_interval)})"

arguments = [ quote(table_name), dimension,
*hypertable_options.map { |k, v| "#{k} => #{quote(v)}" }
]

execute "SELECT create_hypertable(#{arguments.compact.join(', ')})"

if partition_column && number_partitions
execute "SELECT add_dimension('#{table_name}', by_hash(#{quote(partition_column)}, #{number_partitions}))"
end

if compress_segmentby || compress_after
add_compression_policy(table_name, orderby: compress_orderby, segmentby: compress_segmentby, compress_after: compress_after)
end
Expand Down
10 changes: 4 additions & 6 deletions lib/timescaledb/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,12 @@ def timescale_compression_settings_for(hypertable)
if setting.orderby_column_index
if setting.orderby_asc
direction = "ASC"
if setting.orderby_nullsfirst
direction += " NULLS FIRST"
end
# For ASC, default is NULLS LAST, so only add if explicitly set to FIRST
direction += " NULLS FIRST" if setting.orderby_nullsfirst == true
else
direction = "DESC"
if !setting.orderby_nullsfirst
direction += " NULLS LAST"
end
# For DESC, default is NULLS FIRST, so only add if explicitly set to LAST
direction += " NULLS LAST" if setting.orderby_nullsfirst == false
end

compression_settings[:compress_orderby] << "#{setting.attname} #{direction}"
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require_relative "support/active_record/models"
require_relative "support/active_record/schema"

Dotenv.load! if File.exists?(".env")
Dotenv.load! if File.exist?(".env")

# Establish a connection for testing

Expand Down
37 changes: 20 additions & 17 deletions spec/timescaledb/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

context "with retention policies" do
before do
con.create_retention_policy("events", interval: "1 week")
con.create_retention_policy("events", drop_after: "1 week")
end
after do
con.remove_retention_policy("events")
Expand Down Expand Up @@ -108,42 +108,45 @@

describe "dumping hypertable options" do
before(:each) do
con.drop_table :schema_tests, force: :cascade if con.table_exists?(:schema_tests)
con.drop_table :partition_by_hash_tests, force: :cascade, if_exists: true
con.drop_table :partition_by_range_tests, force: :cascade, if_exists: true
con.drop_table :partition_by_integer_tests, force: :cascade, if_exists: true
end

it "extracts spatial partition options" do
options = { partition_column: "category", number_partitions: 3 }
con.create_table :schema_tests, hypertable: options, id: false do |t|
it "extracts by_hash options" do
options = { partition_column: "category", number_partitions: 3, create_default_indexes: false }

con.create_table :partition_by_hash_tests, id: false, hypertable: options do |t|
t.string :category
t.timestamps
t.timestamptz :created_at, default: -> { "now()" }
t.index [:category, :created_at], unique: true, name: "index_partition_by_hash_tests_on_category_and_created_at"
end

dump = dump_output

expect(dump).to include 'partition_column: "category"'
expect(dump).to include "number_partitions: 3"
expect(dump).to include 'create_hypertable "partition_by_hash_tests", time_column: "created_at", chunk_time_interval: "7 days", partition_column: "category", number_partitions: 3, create_default_indexes: false'
end

it "extracts index options" do
options = { create_default_indexes: false }
con.create_table :schema_tests, hypertable: options, id: false do |t|
con.create_table :partition_by_range_tests, id: false, hypertable: options do |t|
t.timestamps
end

dump = dump_output

expect(dump).to include "create_default_indexes: false"
expect(dump).to include 'create_hypertable "partition_by_range_tests", time_column: "created_at", chunk_time_interval: "7 days"'
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|
con.create_table :partition_by_integer_tests, hypertable: options do |t|
t.timestamps
end

dump = dump_output

expect(dump).to include "chunk_time_interval: 10000"
expect(dump).to include 'create_hypertable "partition_by_integer_tests", time_column: "id", chunk_time_interval: 10000'
end

context "compress_segmentby" do
Expand All @@ -161,7 +164,7 @@

dump = dump_output

expect(dump).to include 'compress_segmentby: "identifier, second_identifier"'
expect(dump).to include 'create_hypertable "segmentby_tests", time_column: "created_at", chunk_time_interval: "7 days", compress_segmentby: "identifier, second_identifier", compress_orderby: "created_at ASC"'
end
end

Expand All @@ -181,21 +184,21 @@

dump = dump_output

expect(dump).to include 'compress_orderby: "created_at ASC NULLS FIRST"'
expect(dump).to include 'create_hypertable "orderby_tests", time_column: "created_at", chunk_time_interval: "7 days", compress_segmentby: "identifier", compress_orderby: "created_at ASC NULLS FIRST"'
end
end

context "nulls last" do
it "extracts compress_orderby correctly" do
options = { compress_segmentby: "identifier", compress_orderby: "created_at ASC NULLS LAST" }
options = { compress_segmentby: "identifier", compress_orderby: "created_at DESC NULLS LAST" }
con.create_table :orderby_tests, hypertable: options, id: false do |t|
t.string :identifier
t.timestamps
end

dump = dump_output

expect(dump).to include 'compress_orderby: "created_at ASC"'
expect(dump).to include 'create_hypertable "orderby_tests", time_column: "created_at", chunk_time_interval: "7 days", compress_segmentby: "identifier", compress_orderby: "created_at DESC NULLS LAST"'
end
end
end
Expand Down Expand Up @@ -225,7 +228,7 @@

dump = dump_output

expect(dump).to include 'compress_orderby: "created_at DESC NULLS LAST"'
expect(dump).to include 'create_hypertable "orderby_tests", time_column: "created_at", chunk_time_interval: "7 days", compress_segmentby: "identifier", compress_orderby: "created_at DESC NULLS LAST"'
end
end
end
Expand Down

0 comments on commit c9ecb1e

Please sign in to comment.