Skip to content
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
- Dropped support for Ruby < 3
- Dropped support for Postgres < 13

## 0.6.2 (2023-11-14)

- Added support for weekly partition

## 0.6.1 (2023-04-26)

- Fixed `uninitialized constant` error
Expand Down
12 changes: 11 additions & 1 deletion lib/pgslice/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ module Helpers
SQL_FORMAT = {
day: "YYYYMMDD",
month: "YYYYMM",
week: "YYYYWW",
year: "YYYY"
}
}.freeze

DAYS_IN_WEEK = 7

protected

Expand Down Expand Up @@ -119,6 +122,8 @@ def name_format(period)
case period.to_sym
when :day
"%Y%m%d"
when :week
"%G%V"
when :month
"%Y%m"
else
Expand All @@ -135,6 +140,9 @@ def round_date(date, period)
case period.to_sym
when :day
date
when :week
now = Date.today
now - (now.wday - 1) % 7
when :month
Date.new(date.year, date.month)
else
Expand All @@ -155,6 +163,8 @@ def advance_date(date, period, count = 1)
case period.to_sym
when :day
date.next_day(count)
when :week
date + (count * DAYS_IN_WEEK)
when :month
date.next_month(count)
else
Expand Down
8 changes: 8 additions & 0 deletions test/pgslice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def test_day
assert_period "day"
end

def test_week
assert_period "week"
end

def test_month
assert_period "month"
end
Expand Down Expand Up @@ -162,6 +166,8 @@ def assert_period(period, column: "createdAt", trigger_based: false, tablespace:
case period
when "day"
"%Y%m%d"
when "week"
"%G%V"
when "month"
"%Y%m"
else
Expand Down Expand Up @@ -213,6 +219,8 @@ def assert_period(period, column: "createdAt", trigger_based: false, tablespace:
case period
when "day"
3
when "week"
3 * PgSlice::Helpers::DAYS_IN_WEEK
when "month"
90
else
Expand Down