umbrellio-utils 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/umbrellio_utils/database.rb +32 -13
- data/lib/umbrellio_utils/version.rb +1 -1
- 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: 72645a52796579230696a94c8da844e4466cd44c50de201e0c878569f9f524d4
|
4
|
+
data.tar.gz: 8d1133ddd2dd1405aa015886316d9096208aa6f155a26dee7e6a537adc26af67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a70e82c15b575921e97ed5f82f23e66cb27753ccc67ebc76128a53c90f46d63b89399f0e55214eea9da7c97653575b888ee70535ab44cd835e374998664991f6
|
7
|
+
data.tar.gz: d6887244a85571acdeee74a0f7c914081f27c751608f41330dbad594a68c69473670fc65c877842f1489cc019b75e6b523c1f34b1bcb236d5d666729e357ee2c
|
data/Gemfile.lock
CHANGED
@@ -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
|
-
|
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
|
-
|
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,
|
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
|
-
|
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
|
-
|
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(
|
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
|
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.
|
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-05-
|
11
|
+
date: 2024-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: memery
|