staging_table 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c89644e006af09c46ea8ea69f9a5687b20c88e400fbb1436615c10982ce63eb6
4
- data.tar.gz: 90a9f0c62c6a63b60fb512411c2a74a26f927cb66a8407b082a8bb1e6421ca6c
3
+ metadata.gz: 734447f0a321ace41764ba9f6e8fe784ecf5dd80a315962032c0c7f0d1439c03
4
+ data.tar.gz: 240378228cdc45fe5ece5cb17a41cf9ef0e5cded81ee95a817e7ce336402638b
5
5
  SHA512:
6
- metadata.gz: fc32f9c5d72b5971002d503bf3969c8941e6675d1764d59976a366a14a95134c2ca22259b70c1a84b29c04f085df9c217c4b9451f506094d7c32150b902cc309
7
- data.tar.gz: bb3e29593966192d22fc8168656100ba7880d5f0cfa14f520667dec0ad0822c0efd0212c4d4d53e4e0c1bb49fd06f3b0ad63431a78111ee2ddd535efc0f7cf31
6
+ metadata.gz: b1e52c345bdfdcf5bef6432c3515a1f0c48c0f40320d5744e19f1b8e856157f687a408f675e0c20ad1d4b62067efe73dd98bdd29663f43488721150bfc1ec418
7
+ data.tar.gz: f15115f9a93df8092fd5d245bd560e470761a9f705c043c5e2b2ff8f99b38f04dec90e688aa24efb7eb7e0c2738338bc16fa6cdf704215278bfe55dba88b8b57
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
4
+
3
5
  module StagingTable
4
6
  class BulkInserter
5
7
  attr_reader :model, :batch_size
@@ -16,6 +18,8 @@ module StagingTable
16
18
  raise RecordError, "All records must be hashes. If passing ActiveRecord objects, use Session#insert which normalizes them automatically."
17
19
  end
18
20
 
21
+ records = apply_timestamps(records)
22
+
19
23
  columns = records.first.keys.map(&:to_s)
20
24
  quoted_columns = columns.map { |c| connection.quote_column_name(c) }.join(", ")
21
25
  quoted_table = connection.quote_table_name(model.table_name)
@@ -32,12 +36,43 @@ module StagingTable
32
36
 
33
37
  private
34
38
 
39
+ TIMESTAMP_COLUMNS = %w[created_at updated_at].freeze
40
+
41
+ def apply_timestamps(records)
42
+ return records if records.empty?
43
+
44
+ sample_record = records.first
45
+ model_columns = model.column_names
46
+
47
+ missing_timestamps = TIMESTAMP_COLUMNS.select do |col|
48
+ model_columns.include?(col) && !record_has_key?(sample_record, col)
49
+ end
50
+
51
+ return records if missing_timestamps.empty?
52
+
53
+ now = Time.current
54
+ records.map do |record|
55
+ record = record.dup
56
+ missing_timestamps.each { |col| record[col.to_sym] = now }
57
+ record
58
+ end
59
+ end
60
+
61
+ def record_has_key?(record, key)
62
+ record.key?(key.to_sym) || record.key?(key.to_s)
63
+ end
64
+
35
65
  def connection
36
66
  model.connection
37
67
  end
38
68
 
39
69
  def quote(value)
40
- connection.quote(value)
70
+ case value
71
+ when Array, Hash
72
+ connection.quote(value.to_json)
73
+ else
74
+ connection.quote(value)
75
+ end
41
76
  end
42
77
  end
43
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StagingTable
4
- VERSION = "0.1.0"
4
+ VERSION = '0.1.1'
5
5
  end
@@ -10,6 +10,10 @@ module StagingTable
10
10
 
11
11
  private
12
12
 
13
+ TIMESTAMP_COLUMNS: Array[String]
14
+
15
+ def apply_timestamps: (Array[Hash[String | Symbol, untyped]] records) -> Array[Hash[String | Symbol, untyped]]
16
+ def record_has_key?: (Hash[String | Symbol, untyped] record, String key) -> bool
13
17
  def connection: () -> ActiveRecord::ConnectionAdapters::AbstractAdapter
14
18
  def quote: (untyped value) -> String
15
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: staging_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - eagerworks