umbrellio-utils 1.1.0 → 1.2.1
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 +42 -16
- 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: aaa7935344d41de32a9d61527b1c7b167d64715207422fc23f83d790950e5951
|
4
|
+
data.tar.gz: f9d7707ff893d916462b7e584a08ad8171036825eecacc201eecc09b17fc041f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 319224d118b8c1197b9d93855aa83dcb8dbe80b33fb9395d8782e24cbdd4e303ce7bea798f33b4940febfa268d4f457dae0aac55d14e43e5933fd60d9d038362
|
7
|
+
data.tar.gz: fb536f209a2190fc064db5b0f3a73b01b9286deceab3b7ee83e7f88cbb0023203737b29444baf218b42402dd5e0ad2f2a0bc60edf92bbf0cfbceb1352c5c0f84
|
data/Gemfile.lock
CHANGED
@@ -5,6 +5,7 @@ module UmbrellioUtils
|
|
5
5
|
extend self
|
6
6
|
|
7
7
|
HandledConstaintError = Class.new(StandardError)
|
8
|
+
InvalidPkError = Class.new(StandardError)
|
8
9
|
|
9
10
|
def handle_constraint_error(constraint_name, &block)
|
10
11
|
DB.transaction(savepoint: true, &block)
|
@@ -22,15 +23,16 @@ module UmbrellioUtils
|
|
22
23
|
end
|
23
24
|
|
24
25
|
def each_record(dataset, **options, &block)
|
25
|
-
primary_key = primary_key_from(**options)
|
26
|
+
primary_key = primary_key_from(dataset, **options)
|
26
27
|
|
27
28
|
with_temp_table(dataset, **options) do |ids|
|
28
|
-
|
29
|
+
rows = ids.map { |id| row(id.is_a?(Hash) ? id.values : [id]) }
|
30
|
+
dataset.model.where(row(primary_key) => rows).reverse(row(primary_key)).each(&block)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
34
|
def with_temp_table(dataset, page_size: 1_000, sleep: nil, **options)
|
33
|
-
primary_key = primary_key_from(**options)
|
35
|
+
primary_key = primary_key_from(dataset, **options)
|
34
36
|
sleep_interval = sleep_interval_from(sleep)
|
35
37
|
|
36
38
|
temp_table_name = create_temp_table(dataset, primary_key: primary_key)
|
@@ -39,11 +41,7 @@ module UmbrellioUtils
|
|
39
41
|
|
40
42
|
loop do
|
41
43
|
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
|
-
|
44
|
+
pk_set = pop_next_pk_batch(temp_table_name, primary_key, page_size)
|
47
45
|
yield(pk_set) if pk_set.any?
|
48
46
|
end
|
49
47
|
|
@@ -61,18 +59,22 @@ module UmbrellioUtils
|
|
61
59
|
Lamian.logger.send(:logdevs).each { |x| x.truncate(0) && x.rewind }
|
62
60
|
end
|
63
61
|
|
64
|
-
def create_temp_table(dataset,
|
65
|
-
model = dataset.model
|
62
|
+
def create_temp_table(dataset, **options)
|
66
63
|
time = Time.current
|
64
|
+
model = dataset.model
|
67
65
|
temp_table_name = "temp_#{model.table_name}_#{time.to_i}_#{time.nsec}".to_sym
|
68
|
-
|
66
|
+
primary_key = primary_key_from(dataset, **options)
|
69
67
|
|
70
|
-
DB.drop_table?(temp_table_name)
|
71
68
|
DB.create_table(temp_table_name, unlogged: true) do
|
72
|
-
|
69
|
+
primary_key.each do |field|
|
70
|
+
type = model.db_schema[field][:db_type]
|
71
|
+
column field, type
|
72
|
+
end
|
73
|
+
|
74
|
+
primary_key primary_key
|
73
75
|
end
|
74
76
|
|
75
|
-
insert_ds = dataset.select(
|
77
|
+
insert_ds = dataset.select(*qualified_pk(model.table_name, primary_key))
|
76
78
|
DB[temp_table_name].disable_insert_returning.insert(insert_ds)
|
77
79
|
|
78
80
|
temp_table_name
|
@@ -80,8 +82,22 @@ module UmbrellioUtils
|
|
80
82
|
|
81
83
|
private
|
82
84
|
|
83
|
-
def
|
84
|
-
|
85
|
+
def row(values)
|
86
|
+
Sequel.function(:row, *values)
|
87
|
+
end
|
88
|
+
|
89
|
+
def extract_primary_key(dataset)
|
90
|
+
dataset.db.schema(dataset.first_source).select { |x| x[1][:primary_key] }.map(&:first)
|
91
|
+
end
|
92
|
+
|
93
|
+
def primary_key_from(dataset, **options)
|
94
|
+
Array(options[:primary_key] || extract_primary_key(dataset)).tap do |primary_key|
|
95
|
+
raise InvalidPkError if primary_key.empty?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def qualified_pk(table_name, primary_key)
|
100
|
+
primary_key.map { |f| Sequel[table_name][f] }
|
85
101
|
end
|
86
102
|
|
87
103
|
def sleep_interval_from(sleep)
|
@@ -94,5 +110,15 @@ module UmbrellioUtils
|
|
94
110
|
defined?(Rails) && Rails.env.production? ? 1 : 0
|
95
111
|
end
|
96
112
|
end
|
113
|
+
|
114
|
+
def pop_next_pk_batch(temp_table_name, primary_key, batch_size)
|
115
|
+
row = row(primary_key)
|
116
|
+
pk_expr = DB[temp_table_name].select(*primary_key).reverse(row).limit(batch_size)
|
117
|
+
deleted_items = DB[temp_table_name].where(row => pk_expr).returning.delete
|
118
|
+
deleted_items.map do |item|
|
119
|
+
next item if primary_key.size > 1
|
120
|
+
item[primary_key.first]
|
121
|
+
end
|
122
|
+
end
|
97
123
|
end
|
98
124
|
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.1
|
4
|
+
version: 1.2.1
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: memery
|