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 +4 -4
- data/lib/staging_table/bulk_inserter.rb +36 -1
- data/lib/staging_table/version.rb +1 -1
- data/sig/staging_table/bulk_inserter.rbs +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 734447f0a321ace41764ba9f6e8fe784ecf5dd80a315962032c0c7f0d1439c03
|
|
4
|
+
data.tar.gz: 240378228cdc45fe5ece5cb17a41cf9ef0e5cded81ee95a817e7ce336402638b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
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
|
|
@@ -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
|