active_partition 0.5.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/bin/console +4 -3
- data/lib/active_partition/partition_managers/time_range.rb +17 -6
- data/lib/active_partition/version.rb +1 -1
- data/lib/active_partition.rb +4 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da2366e713d210800cf673216c0fef5f0789ca8defcc7400d4fcd45551441342
|
4
|
+
data.tar.gz: 1b6412d55ab7a691073fb4bc225db6991b1d46398f3cb7ed4a53bb489a49739b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c21a20dc712152a6b0279f842933bcc6629dda6bcb23b45d8a1cfcae11a1e4f2e1fe27a88e2767d0aee45baabe6a7a2c50783d3ba950ec67621b33e64d376a8c
|
7
|
+
data.tar.gz: 26dc7eb4a4867a0a9129bd68e3cd66b549548e4befdb0a17b51975f37b49c6ed3279fff631d7af3aa97842c2c6363420ad8cc9aad0fcd33f21df9c986148b4cc
|
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 }
|
@@ -34,10 +34,11 @@ class OutgoingEventsWebhook < ActiveRecord::Base
|
|
34
34
|
include ActivePartition::Partitionable
|
35
35
|
self.primary_key = "id"
|
36
36
|
self.partitioned_by = "created_at"
|
37
|
-
self.partition_range = 1.
|
37
|
+
self.partition_range = 1.month
|
38
|
+
self.partition_start_from = DateTime.new(2021, 1, 1)
|
38
39
|
|
39
40
|
# You can choose 1 of the following 2 options
|
40
|
-
self.retention_period = 1.
|
41
|
+
self.retention_period = 1.month
|
41
42
|
self.retention_partition_count = 3
|
42
43
|
end
|
43
44
|
|
@@ -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
|
|
@@ -58,11 +62,18 @@ module ActivePartition::PartitionManagers
|
|
58
62
|
def prepare_partition(partitioned_value, period)
|
59
63
|
return if active_partitions_cover?(partitioned_value)
|
60
64
|
|
61
|
-
diff
|
62
|
-
|
63
|
-
|
65
|
+
# when the latest_coverage_at is too far, the diff can be wrong (because leap years add up to the diff)
|
66
|
+
# therefore, we need to calculate the diff based on the period
|
67
|
+
# diff = (partitioned_value.utc - latest_coverage_at) / period
|
68
|
+
# from_time = latest_coverage_at + (diff.floor * period)
|
69
|
+
# to_time = from_time + period
|
70
|
+
|
71
|
+
from_time = latest_coverage_at
|
72
|
+
while !(from_time..(from_time + period)).cover?(partitioned_value) do
|
73
|
+
from_time += period * (partitioned_value > from_time ? 1 : -1)
|
74
|
+
end
|
64
75
|
|
65
|
-
create_partition(from_time,
|
76
|
+
create_partition(from_time, from_time + period)
|
66
77
|
end
|
67
78
|
|
68
79
|
# Builds a partition name based on the given time range.
|
@@ -128,7 +139,7 @@ module ActivePartition::PartitionManagers
|
|
128
139
|
def latest_partition_coverage_time
|
129
140
|
partition_tables = @partition_adapter.get_all_supported_partition_tables
|
130
141
|
reload_active_ranges(partition_tables)
|
131
|
-
return Time.current.beginning_of_hour.utc if partition_tables.empty?
|
142
|
+
return (@partition_start_from || Time.current.beginning_of_hour).utc if partition_tables.empty?
|
132
143
|
|
133
144
|
latest_partition_table = partition_tables.sort_by { |p_name| p_name.split("_").last.to_i }.last
|
134
145
|
@latest_coverage_at = Time.at(latest_partition_table.split("_").last.to_i).utc
|
data/lib/active_partition.rb
CHANGED
@@ -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.
|
4
|
+
version: 0.7.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-
|
11
|
+
date: 2024-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|