umbrellio-utils 1.0.0 → 1.2.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: e5415ce42c2ba64403bb051b4d1772724226f8f0eb321d7eacae22af3db9961e
4
- data.tar.gz: 11f4a31ba535995db43fd3d5f8d180061d494488baf2a7548fa13844ee5b5b53
3
+ metadata.gz: 72645a52796579230696a94c8da844e4466cd44c50de201e0c878569f9f524d4
4
+ data.tar.gz: 8d1133ddd2dd1405aa015886316d9096208aa6f155a26dee7e6a537adc26af67
5
5
  SHA512:
6
- metadata.gz: 715ccd8c42effe71b0be1e8482d8188b8dace23acd7e579b35d3491d4ba0c8d8d77b3e33396c22b034ea6f6199a0983b274828e05ab62ea684bc6ff57d74594b
7
- data.tar.gz: d312202f931ee6d070f660f39148aaece7d71f5cca2a68ee31b61f4c5da525950b6702e0cd0f616e126047627aab9d6326641ed6310279c6ffa2e07cae536364
6
+ metadata.gz: a70e82c15b575921e97ed5f82f23e66cb27753ccc67ebc76128a53c90f46d63b89399f0e55214eea9da7c97653575b888ee70535ab44cd835e374998664991f6
7
+ data.tar.gz: d6887244a85571acdeee74a0f7c914081f27c751608f41330dbad594a68c69473670fc65c877842f1489cc019b75e6b523c1f34b1bcb236d5d666729e357ee2c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- umbrellio-utils (1.0.0)
4
+ umbrellio-utils (1.2.0)
5
5
  memery (~> 1)
6
6
 
7
7
  GEM
@@ -25,7 +25,8 @@ module UmbrellioUtils
25
25
  primary_key = primary_key_from(**options)
26
26
 
27
27
  with_temp_table(dataset, **options) do |ids|
28
- dataset.model.where(primary_key => ids).reverse(primary_key).each(&block)
28
+ rows = ids.map { |id| row(id.is_a?(Hash) ? id.values : [id]) }
29
+ dataset.model.where(row(primary_key) => rows).reverse(row(primary_key)).each(&block)
29
30
  end
30
31
  end
31
32
 
@@ -39,11 +40,7 @@ module UmbrellioUtils
39
40
 
40
41
  loop do
41
42
  DB.transaction do
42
- pk_expr = DB[temp_table_name].select(primary_key).reverse(primary_key).limit(page_size)
43
-
44
- deleted_items = DB[temp_table_name].where(primary_key => pk_expr).returning.delete
45
- pk_set = deleted_items.map { |item| item[primary_key] }
46
-
43
+ pk_set = pop_next_pk_batch(temp_table_name, primary_key, page_size)
47
44
  yield(pk_set) if pk_set.any?
48
45
  end
49
46
 
@@ -61,18 +58,22 @@ module UmbrellioUtils
61
58
  Lamian.logger.send(:logdevs).each { |x| x.truncate(0) && x.rewind }
62
59
  end
63
60
 
64
- def create_temp_table(dataset, primary_key:)
65
- model = dataset.model
61
+ def create_temp_table(dataset, **options)
66
62
  time = Time.current
63
+ model = dataset.model
67
64
  temp_table_name = "temp_#{model.table_name}_#{time.to_i}_#{time.nsec}".to_sym
68
- type = model.db_schema[primary_key][:db_type]
65
+ primary_key = primary_key_from(**options)
69
66
 
70
- DB.drop_table?(temp_table_name)
71
67
  DB.create_table(temp_table_name, unlogged: true) do
72
- column primary_key, type, primary_key: true
68
+ primary_key.each do |field|
69
+ type = model.db_schema[field][:db_type]
70
+ column field, type
71
+ end
72
+
73
+ primary_key primary_key
73
74
  end
74
75
 
75
- insert_ds = dataset.select(Sequel[model.table_name][primary_key])
76
+ insert_ds = dataset.select(*qualified_pk(model.table_name, primary_key))
76
77
  DB[temp_table_name].disable_insert_returning.insert(insert_ds)
77
78
 
78
79
  temp_table_name
@@ -80,8 +81,16 @@ module UmbrellioUtils
80
81
 
81
82
  private
82
83
 
84
+ def row(values)
85
+ Sequel.function(:row, *values)
86
+ end
87
+
83
88
  def primary_key_from(**options)
84
- options.fetch(:primary_key, :id)
89
+ Array(options.fetch(:primary_key, :id))
90
+ end
91
+
92
+ def qualified_pk(table_name, primary_key)
93
+ primary_key.map { |f| Sequel[table_name][f] }
85
94
  end
86
95
 
87
96
  def sleep_interval_from(sleep)
@@ -94,5 +103,15 @@ module UmbrellioUtils
94
103
  defined?(Rails) && Rails.env.production? ? 1 : 0
95
104
  end
96
105
  end
106
+
107
+ def pop_next_pk_batch(temp_table_name, primary_key, batch_size)
108
+ row = row(primary_key)
109
+ pk_expr = DB[temp_table_name].select(*primary_key).reverse(row).limit(batch_size)
110
+ deleted_items = DB[temp_table_name].where(row => pk_expr).returning.delete
111
+ deleted_items.map do |item|
112
+ next item if primary_key.size > 1
113
+ item[primary_key.first]
114
+ end
115
+ end
97
116
  end
98
117
  end
@@ -14,6 +14,7 @@ module UmbrellioUtils
14
14
  end
15
15
 
16
16
  memoize def body
17
+ request.body.rewind
17
18
  request.body.read.dup.force_encoding("utf-8")
18
19
  end
19
20
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UmbrellioUtils
4
- VERSION = "1.0.0"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: umbrellio-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Team Umbrellio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-18 00:00:00.000000000 Z
11
+ date: 2024-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memery
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  requirements: []
89
- rubygems_version: 3.5.7
89
+ rubygems_version: 3.5.9
90
90
  signing_key:
91
91
  specification_version: 4
92
92
  summary: A set of utilities that speed up development