umbrellio-utils 1.2.0 → 1.2.1

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: 72645a52796579230696a94c8da844e4466cd44c50de201e0c878569f9f524d4
4
- data.tar.gz: 8d1133ddd2dd1405aa015886316d9096208aa6f155a26dee7e6a537adc26af67
3
+ metadata.gz: aaa7935344d41de32a9d61527b1c7b167d64715207422fc23f83d790950e5951
4
+ data.tar.gz: f9d7707ff893d916462b7e584a08ad8171036825eecacc201eecc09b17fc041f
5
5
  SHA512:
6
- metadata.gz: a70e82c15b575921e97ed5f82f23e66cb27753ccc67ebc76128a53c90f46d63b89399f0e55214eea9da7c97653575b888ee70535ab44cd835e374998664991f6
7
- data.tar.gz: d6887244a85571acdeee74a0f7c914081f27c751608f41330dbad594a68c69473670fc65c877842f1489cc019b75e6b523c1f34b1bcb236d5d666729e357ee2c
6
+ metadata.gz: 319224d118b8c1197b9d93855aa83dcb8dbe80b33fb9395d8782e24cbdd4e303ce7bea798f33b4940febfa268d4f457dae0aac55d14e43e5933fd60d9d038362
7
+ data.tar.gz: fb536f209a2190fc064db5b0f3a73b01b9286deceab3b7ee83e7f88cbb0023203737b29444baf218b42402dd5e0ad2f2a0bc60edf92bbf0cfbceb1352c5c0f84
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- umbrellio-utils (1.2.0)
4
+ umbrellio-utils (1.2.1)
5
5
  memery (~> 1)
6
6
 
7
7
  GEM
@@ -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,7 +23,7 @@ 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]) }
@@ -31,7 +32,7 @@ module UmbrellioUtils
31
32
  end
32
33
 
33
34
  def with_temp_table(dataset, page_size: 1_000, sleep: nil, **options)
34
- primary_key = primary_key_from(**options)
35
+ primary_key = primary_key_from(dataset, **options)
35
36
  sleep_interval = sleep_interval_from(sleep)
36
37
 
37
38
  temp_table_name = create_temp_table(dataset, primary_key: primary_key)
@@ -62,7 +63,7 @@ module UmbrellioUtils
62
63
  time = Time.current
63
64
  model = dataset.model
64
65
  temp_table_name = "temp_#{model.table_name}_#{time.to_i}_#{time.nsec}".to_sym
65
- primary_key = primary_key_from(**options)
66
+ primary_key = primary_key_from(dataset, **options)
66
67
 
67
68
  DB.create_table(temp_table_name, unlogged: true) do
68
69
  primary_key.each do |field|
@@ -85,8 +86,14 @@ module UmbrellioUtils
85
86
  Sequel.function(:row, *values)
86
87
  end
87
88
 
88
- def primary_key_from(**options)
89
- Array(options.fetch(:primary_key, :id))
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
90
97
  end
91
98
 
92
99
  def qualified_pk(table_name, primary_key)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UmbrellioUtils
4
- VERSION = "1.2.0"
4
+ VERSION = "1.2.1"
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.2.0
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-17 00:00:00.000000000 Z
11
+ date: 2024-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memery