diff --git a/CHANGELOG.md b/CHANGELOG.md index f30aafd..bdd29f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/pgslice/helpers.rb b/lib/pgslice/helpers.rb index ee2b1b3..e85feda 100644 --- a/lib/pgslice/helpers.rb +++ b/lib/pgslice/helpers.rb @@ -3,8 +3,11 @@ module Helpers SQL_FORMAT = { day: "YYYYMMDD", month: "YYYYMM", + week: "YYYYWW", year: "YYYY" - } + }.freeze + + DAYS_IN_WEEK = 7 protected @@ -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 @@ -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 @@ -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 diff --git a/test/pgslice_test.rb b/test/pgslice_test.rb index cf8c062..1cd748a 100644 --- a/test/pgslice_test.rb +++ b/test/pgslice_test.rb @@ -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 @@ -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 @@ -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