active_partition 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c038362292769cc7d68786d177c845429a6e15a3bac8c6b1de166e976a27d26
4
- data.tar.gz: 84a4f14e03c642cd562caae6e93fd9aaee1a4bf2bb7e4bb50f284860a809e2fa
3
+ metadata.gz: 942bdda1651b12d3dac2b0137a8d4ec024746bc6a494d7873d6328d1e5166ddc
4
+ data.tar.gz: 1f5770064c39d828a6d7fb4b7d89e4d1f49711bfb8fb304c8ad73430032e29f2
5
5
  SHA512:
6
- metadata.gz: 9d15088d3d42bb257b854e7f04ed265400ae810831692b5fda3f5ef8ae2d7e3582bf09e7703393e2cbdf67cdf388b74a6b1dd77624a74a38706f9face93ac3d1
7
- data.tar.gz: 0a58ffcebd2c11bc4e26faefe7106928ef58fa435c7d8c7e441b11b06487f018298cfcceec3fb8d924ac80a62f1d4a844f8c381a736930f6a583bcc9f334d30d
6
+ metadata.gz: 18119f845f143524abedfe280c09cfdd4cca5591a56aa4428413ee19cf3bb9f035e951ad3d1c7100a12e80fefc81037dc82f613b50b549497a1586ec655192b2
7
+ data.tar.gz: b56f0432c0c6fd7a42c8916547a97cd793be170e759f88b409ff9b2ec773f8a95c330997dfe111b4f37ac66137e52bd7ed3e105fdd547e8d5a92477ae3a78f2e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_partition (0.4.0)
4
+ active_partition (0.5.0)
5
5
  rails
6
6
  range_operators (~> 0.1.1)
7
7
  rspec-rails
data/README.md CHANGED
@@ -38,6 +38,13 @@ class Event < ActiveRecord::Base
38
38
  self.retention_period = 1.month
39
39
  # Keep last n partitions
40
40
  self.retention_partition_count = 3
41
+ # The start time of the partition range, default is Time.current.beginning_of_hour.utc
42
+ # For example, if today is July 31, and you create a new record.
43
+ # if the partition_start_from is 2021-01-01, the new partition should cover [2024-07-01 00:00:00 UTC...2024-08-01 00:00:00 UTC]
44
+ # if the partition_start_from is nil, the coverage can be [2024-07-31 08:00:00 UTC...2024-08-31 08:00:00 UTC]
45
+ # This configuration help us to sync partition ranges of all partitioned tables.
46
+ # Therefore, you can easy to join/drop/manage related partitioned tables.
47
+ self.partition_start_from = DateTime.new(2021, 1, 1)
41
48
  end
42
49
 
43
50
  # auto create a new partition if needed.
data/bin/console CHANGED
@@ -5,7 +5,7 @@ require "bundler/setup"
5
5
  require "active_partition"
6
6
  require "active_record"
7
7
  require "byebug"
8
-
8
+ require "rails"
9
9
  def reload!
10
10
  files = $LOADED_FEATURES.select { |feat| feat =~ /\/active_partition\// }
11
11
  files.each { |file| load file }
@@ -35,6 +35,7 @@ class OutgoingEventsWebhook < ActiveRecord::Base
35
35
  self.primary_key = "id"
36
36
  self.partitioned_by = "created_at"
37
37
  self.partition_range = 1.hour
38
+ self.partition_start_from = DateTime.new(2021, 1, 1)
38
39
 
39
40
  # You can choose 1 of the following 2 options
40
41
  self.retention_period = 1.day
@@ -2,9 +2,10 @@
2
2
 
3
3
  module ActivePartition::PartitionManagers
4
4
  class TimeRange
5
- def initialize(partition_adapter, table_name)
5
+ def initialize(partition_adapter, table_name, partition_start_from = nil)
6
6
  @partition_adapter = partition_adapter
7
7
  @table_name = table_name
8
+ @partition_start_from = partition_start_from
8
9
  end
9
10
 
10
11
  # Retrieves the active ranges from the partition adapter.
@@ -15,6 +16,9 @@ module ActivePartition::PartitionManagers
15
16
  #
16
17
  # @return [Array] The array of active ranges.
17
18
  def active_ranges
19
+ # in test environment, the partition table is not actually created. Therefore, return empty array
20
+ return [] if defined?(Rails) && Rails.env.test?
21
+
18
22
  @active_ranges ||= reload_active_ranges(@partition_adapter.get_all_supported_partition_tables)
19
23
  end
20
24
 
@@ -128,7 +132,7 @@ module ActivePartition::PartitionManagers
128
132
  def latest_partition_coverage_time
129
133
  partition_tables = @partition_adapter.get_all_supported_partition_tables
130
134
  reload_active_ranges(partition_tables)
131
- return Time.current.beginning_of_hour.utc if partition_tables.empty?
135
+ return (@partition_start_from || Time.current.beginning_of_hour.utc) if partition_tables.empty?
132
136
 
133
137
  latest_partition_table = partition_tables.sort_by { |p_name| p_name.split("_").last.to_i }.last
134
138
  @latest_coverage_at = Time.at(latest_partition_table.split("_").last.to_i).utc
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActivePartition
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -40,9 +40,9 @@ module ActivePartition
40
40
  def partition_manager
41
41
  @partition_manager ||= case columns_hash[partitioned_by.to_s].type.to_s
42
42
  when "datetime"
43
- ActivePartition::PartitionManagers::TimeRange.new(partition_adapter, table_name)
43
+ ActivePartition::PartitionManagers::TimeRange.new(partition_adapter, table_name, partition_start_from)
44
44
  else
45
- ActivePartition::PartitionManagers::TimeRange.new(partition_adapter, table_name)
45
+ ActivePartition::PartitionManagers::TimeRange.new(partition_adapter, table_name, partition_start_from)
46
46
  end
47
47
  end
48
48
 
@@ -56,6 +56,8 @@ module ActivePartition
56
56
  attr_accessor :retention_period
57
57
  # Retains the specified number of partitions [Choose one of retention_period or retention_partition_count]
58
58
  attr_accessor :retention_partition_count
59
+ # The start time of the partition range, default is Time.current
60
+ attr_accessor :partition_start_from
59
61
 
60
62
  def delete_expired_partitions
61
63
  if retention_period && retention_period.is_a?(ActiveSupport::Duration)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_partition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thien Tran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-26 00:00:00.000000000 Z
11
+ date: 2024-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug