Skip to content

Commit

Permalink
Make scopes alternative on acts_as_hypertable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatas committed Apr 10, 2024
1 parent 315cd66 commit 9aaf83e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/timescaledb/acts_as_hypertable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def acts_as_hypertable?
# acts_as_hypertable time_column: :timestamp
# end
#
# @param [Hash] options The options to initialize your macro with.
# @option options [Boolean] :skip_association_scopes to avoid `.hypertable`, `.chunks` and other scopes related to metadata.
# @option options [Boolean] :skip_default_scopes to avoid the generation of default time related scopes like `last_hour`, `last_week`, `yesterday` and so on...
def acts_as_hypertable(options = {})
return if acts_as_hypertable?

Expand All @@ -53,8 +56,8 @@ def acts_as_hypertable(options = {})
hypertable_options.merge!(options)
normalize_hypertable_options

define_association_scopes
define_default_scopes
define_association_scopes unless options[:skip_association_scopes]
define_default_scopes unless options[:skip_default_scopes]
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,31 @@ class HypertableWithCustomTimeColumn < ActiveRecord::Base
acts_as_hypertable time_column: :timestamp
end

class HypertableLean < ActiveRecord::Base
self.primary_key = "identifier"

acts_as_hypertable skip_association_scopes: true, skip_default_scopes: true
end

class HypertableWithCustomTimeColumnAndLean < ActiveRecord::Base
self.table_name = "hypertable_with_custom_time_column_and_lean"
self.primary_key = "identifier"

acts_as_hypertable time_column: :timestamp, skip_association_scopes: true, skip_default_scopes: true
end

class HypertableWithCustomTimeColumnAndLeanAndNoOptions < ActiveRecord::Base
self.table_name = "hypertable_with_custom_time_column_and_lean_and_no_options"
self.primary_key = "identifier"

acts_as_hypertable skip_association_scopes: true, skip_default_scopes: true
end


class HypertableSkipAllScopes < ActiveRecord::Base
self.table_name = "hypertable_skipping_all_scopes"
acts_as_hypertable time_column: :timestamp, skip_association_scopes: true, skip_default_scopes: true
end

class NonHypertable < ActiveRecord::Base
end
42 changes: 42 additions & 0 deletions spec/timescaledb/acts_as_hypertable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,48 @@
end
end

describe "#define_association_scopes" do
context "when the model is a hypertable" do
it "defines the association scopes" do
expect(Event).to respond_to(:chunks)
expect(Event).to respond_to(:hypertable)
expect(Event).to respond_to(:jobs)
expect(Event).to respond_to(:job_stats)
expect(Event).to respond_to(:compression_settings)
expect(Event).to respond_to(:continuous_aggregates)
end
end
context "when model skips association scopes" do
it "does not define the association scopes" do
expect(HypertableSkipAllScopes).not_to respond_to(:chunks)
expect(HypertableSkipAllScopes).not_to respond_to(:hypertable)
expect(HypertableSkipAllScopes).not_to respond_to(:jobs)
expect(HypertableSkipAllScopes).not_to respond_to(:job_stats)
expect(HypertableSkipAllScopes).not_to respond_to(:compression_settings)
expect(HypertableSkipAllScopes).not_to respond_to(:continuous_aggregates)
end
end
end

describe 'when model skips default scopes' do
context "when the model is a hypertable" do
it "defines the association scopes" do
expect(Event).to respond_to(:previous_month)
expect(Event).to respond_to(:previous_week)
end
end

it 'does not define the default scopes' do
expect(HypertableSkipAllScopes).not_to respond_to(:previous_month)
expect(HypertableSkipAllScopes).not_to respond_to(:previous_week)
expect(HypertableSkipAllScopes).not_to respond_to(:this_month)
expect(HypertableSkipAllScopes).not_to respond_to(:this_week)
expect(HypertableSkipAllScopes).not_to respond_to(:yesterday)
expect(HypertableSkipAllScopes).not_to respond_to(:today)
expect(HypertableSkipAllScopes).not_to respond_to(:last_hour)
end
end

describe ".hypertable_options" do
context "when non-default options are set" do
let(:model) { HypertableWithCustomTimeColumn }
Expand Down

0 comments on commit 9aaf83e

Please sign in to comment.