tartarus-rb 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffb5cafa941ec43d2c07a6745ef9b75b91c31784649bbf04187e90f6960812e0
4
- data.tar.gz: 9615766c2064a4e127c836ca4cf6ad9b0f2925d08246e4486b9f9fd382884e4f
3
+ metadata.gz: f1336e4444881135833d1d1784514a523a9abce37fe911683398b3f2b2bac0c6
4
+ data.tar.gz: 82ec19e8f7ea7bde9df2d8d9c75c218f152832bfb977963489e07e3ffbff6cb3
5
5
  SHA512:
6
- metadata.gz: a97481429cc2c5faa977d75baae80c78e17f56cfa0323b76e613a91a57586bbfb6890442e340b7d9aff88eb7a155f694174109a92317d77e7b8c4c367224ae50
7
- data.tar.gz: 143dc43864c453873d6154f52d1504eff11f0f88e406e69893fd62937ea8b59899e4c1fdd344adc65fab4e4d6f15c8dcee4cd5c09c14e86a19d47be2e17d403d
6
+ metadata.gz: 13139b7f8b5d5397cd0bd5d7104ed676c26345197875824eab50b7eebd3a50a4a983e44c73788da33490c2f5ba610422b643104c55e23b004817a016c4d56f44
7
+ data.tar.gz: 4622b5a42e74f7802e27f8bcb48959f9d6957a334ec7647f184b85bc09736718fc1e653da2a416d5c8d08417372eebeb89f869b099bb732085fffaa86fbb9506
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ - Add `delete_all_using_limit_in_batches` strategy
6
+
3
7
  ## 0.2.0
4
8
  - Add support for deleting and destroying in batches
5
9
  - Add integration tests with a real database and ActiveRecord
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tartarus-rb (0.2.0)
4
+ tartarus-rb (0.3.0)
5
5
  sidekiq (>= 5)
6
6
  sidekiq-cron (~> 1)
7
7
 
@@ -24,7 +24,7 @@ GEM
24
24
  diff-lcs (1.4.4)
25
25
  et-orbi (1.2.4)
26
26
  tzinfo
27
- fugit (1.4.0)
27
+ fugit (1.4.1)
28
28
  et-orbi (~> 1.1, >= 1.1.8)
29
29
  raabro (~> 1.4)
30
30
  i18n (1.8.5)
data/README.md CHANGED
@@ -78,7 +78,8 @@ You can use the following config params:
78
78
  - `tenant_id_field` - required when using tenant_value_source/tenant_value_source. It's a DB column that will be used for scoping records by a tenant. For example, here it would be: `ModelThatYouWantToArchive.where(account_uuid: value_of_uuid_from_some_active_account)`
79
79
  - `archive_items_older_than` - required, for defining retention policy
80
80
  - `timestamp_field` - required, used for performing a query using the value from `archive_items_older_than`
81
- - `archive_with` - optional (defaults to `delete_all`). Could be `delete_all`, `destroy_all`, `delete_all_without_batches`, `destroy_all_without_batches`
81
+ - `archive_with` - optional (defaults to `delete_all`). Could be `delete_all`, `destroy_all`, `delete_all_without_batches`, `destroy_all_without_batches`, `delete_all_using_limit_in_batches`
82
+ - `batch_size` - optional (defaults to `10_000`, used with `delete_all_using_limit_in_batches` strategy)
82
83
 
83
84
  ### Testing before actually using it
84
85
 
@@ -4,6 +4,7 @@ require "tartarus/archivable_item/sidekiq_cron_job_serializer"
4
4
  require "tartarus/archive_strategy"
5
5
  require "tartarus/archive_strategy/delete_all"
6
6
  require "tartarus/archive_strategy/delete_all_without_batches"
7
+ require "tartarus/archive_strategy/delete_all_using_limit_in_batches"
7
8
  require "tartarus/archive_strategy/destroy_all"
8
9
  require "tartarus/archive_strategy/destroy_all_without_batches"
9
10
  require "tartarus/archive_strategy/extract_batch"
@@ -13,6 +13,7 @@ class Tartarus
13
13
  ensure_column_exists(collection, model_name, tenant_id_field)
14
14
 
15
15
  collection.where("#{timestamp_field} < ?", timestamp).where(tenant_id_field => tenant_id)
16
+ .order(tenant_id_field, timestamp_field)
16
17
  end
17
18
 
18
19
  def items_older_than(model_name, timestamp_field, timestamp)
@@ -1,7 +1,7 @@
1
1
  class Tartarus::ArchivableItem
2
2
  REQUIRED_ATTRIBUTES_NAMES = %i(model cron queue archive_items_older_than timestamp_field active_job
3
3
  archive_with tenant_value_source).freeze
4
- OPTIONAL_ATTRIBUTES_NAMES = %i(tenants_range tenant_id_field).freeze
4
+ OPTIONAL_ATTRIBUTES_NAMES = %i(tenants_range tenant_id_field batch_size).freeze
5
5
 
6
6
  attr_accessor *(REQUIRED_ATTRIBUTES_NAMES + OPTIONAL_ATTRIBUTES_NAMES)
7
7
 
@@ -45,6 +45,12 @@ class Tartarus::ArchivableItem
45
45
  @archive_with ||= :delete_all
46
46
  end
47
47
 
48
+ def batch_size
49
+ return @batch_size if defined?(@batch_size)
50
+
51
+ @batch_size ||= 10_000
52
+ end
53
+
48
54
  def validate!
49
55
  validate_presence
50
56
  end
@@ -54,7 +60,7 @@ class Tartarus::ArchivableItem
54
60
  end
55
61
 
56
62
  def archive_strategy(factory: Tartarus::ArchiveStrategy.new)
57
- factory.for(archive_with)
63
+ factory.for(archive_with, batch_size: batch_size)
58
64
  end
59
65
 
60
66
  def for_model?(provided_model_name)
@@ -1,5 +1,5 @@
1
1
  class Tartarus::ArchiveStrategy
2
- def for(strategy_name)
2
+ def for(strategy_name, batch_size: 0)
3
3
  case strategy_name.to_sym
4
4
  when :delete_all
5
5
  Tartarus::ArchiveStrategy::DeleteAll.new
@@ -9,6 +9,8 @@ class Tartarus::ArchiveStrategy
9
9
  Tartarus::ArchiveStrategy::DeleteAllWithoutBatches.new
10
10
  when :destroy_all_without_batches
11
11
  Tartarus::ArchiveStrategy::DestroyAllWithoutBatches.new
12
+ when :delete_all_using_limit_in_batches
13
+ Tartarus::ArchiveStrategy::DeleteAllUsingLimitInBatches.new(batch_size: batch_size)
12
14
  else
13
15
  raise "unknown strategy: #{strategy_name}"
14
16
  end
@@ -0,0 +1,20 @@
1
+ class Tartarus
2
+ class ArchiveStrategy
3
+ class DeleteAllUsingLimitInBatches
4
+ attr_reader :batch_size
5
+ private :batch_size
6
+
7
+ def initialize(batch_size:)
8
+ @batch_size = batch_size
9
+ end
10
+
11
+ def call(collection)
12
+ num = 1
13
+
14
+ while num > 0
15
+ num = collection.limit(batch_size).delete_all
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  class Tartarus
2
2
  module Rb
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tartarus-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Galanciak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-04 00:00:00.000000000 Z
11
+ date: 2021-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -136,6 +136,7 @@ files:
136
136
  - lib/tartarus/archive_model_without_tenant.rb
137
137
  - lib/tartarus/archive_strategy.rb
138
138
  - lib/tartarus/archive_strategy/delete_all.rb
139
+ - lib/tartarus/archive_strategy/delete_all_using_limit_in_batches.rb
139
140
  - lib/tartarus/archive_strategy/delete_all_without_batches.rb
140
141
  - lib/tartarus/archive_strategy/destroy_all.rb
141
142
  - lib/tartarus/archive_strategy/destroy_all_without_batches.rb