Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compression_interval to compress_after #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions lib/timescaledb/acts_as_hypertable/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,41 @@ def define_association_scopes
end

def define_default_scopes
scope :between, ->(start_time, end_time) do
where("#{time_column} BETWEEN ? AND ?", start_time, end_time)
end

scope :previous_month, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.last_month.in_time_zone.beginning_of_month.to_date,
end_time: Date.today.last_month.in_time_zone.end_of_month.to_date
)
ref = 1.month.ago.in_time_zone
between(ref.beginning_of_month, ref.end_of_month)
end

scope :previous_week, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.last_week.in_time_zone.beginning_of_week.to_date,
end_time: Date.today.last_week.in_time_zone.end_of_week.to_date
)
ref = 1.week.ago.in_time_zone
between(ref.beginning_of_week, ref.end_of_week)
end

scope :this_month, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.in_time_zone.beginning_of_month.to_date,
end_time: Date.today.in_time_zone.end_of_month.to_date
)
ref = Time.now.in_time_zone
between(ref.beginning_of_month, ref.end_of_month)
end

scope :this_week, -> do
where(
"DATE(#{time_column}) >= :start_time AND DATE(#{time_column}) <= :end_time",
start_time: Date.today.in_time_zone.beginning_of_week.to_date,
end_time: Date.today.in_time_zone.end_of_week.to_date
)
ref = Time.now.in_time_zone
between(ref.beginning_of_week, ref.end_of_week)
end

scope :yesterday, -> do
ref = 1.day.ago.in_time_zone
between(ref.yesterday, ref.yesterday)
end

scope :today, -> do
ref = Time.now.in_time_zone
between(ref.beginning_of_day, ref.end_of_day)
end

scope :yesterday, -> { where("DATE(#{time_column}) = ?", Date.yesterday.in_time_zone.to_date) }
scope :today, -> { where("DATE(#{time_column}) = ?", Date.today.in_time_zone.to_date) }
scope :last_hour, -> { where("#{time_column} between ? and ?", 1.hour.ago.in_time_zone, Time.now.end_of_hour.in_time_zone) }
scope :last_hour, -> { where("#{time_column} > ?", 1.hour.ago.in_time_zone) }
end

def normalize_hypertable_options
Expand Down
2 changes: 1 addition & 1 deletion lib/timescaledb/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def timescale_compression_settings_for(hypertable)
end

hypertable.jobs.compression.each do |job|
compression_settings[:compression_interval] = job.config["compress_after"]
compression_settings[:compress_after] = job.config["compress_after"]
end

# Pack the compression setting arrays into a comma-separated string instead.
Expand Down
86 changes: 0 additions & 86 deletions spec/timescaledb/acts_as_hypertable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
RSpec.describe Timescaledb::ActsAsHypertable do

before { travel_to Time.utc(2024, 12, 8, 12, 0, 0) }
after { travel_back }

{
'last_month' => 1.month.ago.beginning_of_month,
'this_month' => 1.second.ago.beginning_of_month,
'this_week' => 1.second.ago.beginning_of_week,
'last_week' => 1.week.ago.beginning_of_week,
'one_day_outside_window' => 2.days.ago.beginning_of_month,
'at_edge_of_window' => 1.month.ago.end_of_month.end_of_day,
}.each do |identifier, created_at|
let!("event_#{identifier}") {
Event.create!(
identifier: identifier,
created_at: created_at
)
}
end

describe ".acts_as_hypertable?" do
context "when the model has not been declared as a hypertable" do
Expand Down Expand Up @@ -105,72 +87,4 @@
its(:tablespaces) { is_expected.to be_nil }
its(:hypertable_name) { is_expected.to eq(Event.table_name) }
end

describe ".previous_month" do
context "when there are database records that were created in the previous month" do
it "returns all the records that were created in the previous month" do
last_month = Event.previous_month.pluck(:identifier)
expect(last_month).to include(*%w[last_month last_week])
end
end
end

describe ".previous_week" do
context "when there are database records that were created in the previous week" do
it "returns all the records that were created in the previous week" do
last_week = Event.previous_week.pluck(:identifier)
expect(last_week).to match_array(%w[at_edge_of_window last_week one_day_outside_window this_month])
end
end
end

describe ".this_month" do
context "when there are database records that were created this month" do
it "returns all the records that were created this month" do
this_month = Event.this_month.pluck(:identifier)
expect(this_month).to include(*%w[this_month one_day_outside_window this_week])
end
end
end

describe ".this_week" do
context "when there are database records that were created this week" do
it "returns all the records that were created this week" do
this_week = Event.this_week.pluck(:identifier)
expect(this_week).to match_array(%w[this_week])
end
end
end

describe ".yesterday" do
context "when there are database records that were created yesterday" do
let!(:event_yesterday) {
Event.create!(
identifier: "yesterday",
created_at: 1.day.ago
)
}

it "returns all the records that were created yesterday" do
yesterday = Event.yesterday.pluck(:identifier)
expect(yesterday).to match_array(%w[yesterday])
end
end
end

describe ".today" do
context "when there are database records that were created today" do
it "returns all the records that were created today" do
expect(Event.today).to be_empty
end
end
end

describe ".last_hour" do
context "when there are database records that were created in the last hour" do
it "returns all the records that were created in the last hour" do
expect(Event.last_hour).to be_empty
end
end
end
end
4 changes: 2 additions & 2 deletions spec/timescaledb/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@

context "nulls last" do
it "extracts compress_orderby correctly" do
options = { compress_segmentby: "identifier", compress_orderby: "created_at DESC NULLS LAST" }
options = { compress_segmentby: "identifier", compress_after: "1 month", 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 'create_hypertable "orderby_tests", time_column: "created_at", chunk_time_interval: "7 days", compress_segmentby: "identifier", 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", compress_after: "P1M"'
end
end
end
Expand Down
Loading