bulk_importer 0.2.1 → 0.2.2
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/README.md +1 -1
- data/lib/bulk_importer/postgresql_module.rb +9 -0
- data/lib/bulk_importer/version.rb +1 -1
- data/lib/bulk_importer.rb +15 -7
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5b35f73c6f0ad696286c181ec46e0b8cc00334b6
|
|
4
|
+
data.tar.gz: 5666a2358e00501877413c17783d6dc4de2d1246
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c72a504a6946cff11319fb0ca0825ff345d6717c782daf6662a9717f399ca9ed5af9a231ae339713e2072eb6a73ca89197b687322529d119f73d2a42808ba0b2
|
|
7
|
+
data.tar.gz: 9e20be8acc80d4892237b6c57c6b8d373a852300ee42884ae8e9ae2139a66acb0340c15f41572812fdb772b7ee58753042bf3a82d1e4427481e58c4fd91b911f
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# BulkImporter [](https://travis-ci.org/abelosorio/bulk_importer)
|
|
1
|
+
# BulkImporter [](https://travis-ci.org/abelosorio/bulk_importer) [](https://badge.fury.io/rb/bulk_importer)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
This gem allows you to import a big amount of data from files to your database as quick as possible.
|
|
@@ -224,4 +224,13 @@ module PostgresqlModule
|
|
|
224
224
|
|
|
225
225
|
ActiveRecord::Base.connection.execute(query).first['rolsuper'] == 't'
|
|
226
226
|
end
|
|
227
|
+
|
|
228
|
+
# Create index on table (column(s)).
|
|
229
|
+
#
|
|
230
|
+
# * +table+
|
|
231
|
+
# * +columns+
|
|
232
|
+
#
|
|
233
|
+
def self.create_index_on(table, columns)
|
|
234
|
+
ActiveRecord::Base.connection.add_index table, columns
|
|
235
|
+
end
|
|
227
236
|
end
|
data/lib/bulk_importer.rb
CHANGED
|
@@ -8,6 +8,9 @@ module BulkImporter
|
|
|
8
8
|
UPDATE_MODE_UPDATE = 'update'
|
|
9
9
|
UPDATE_MODE_REPLACE = 'replace'
|
|
10
10
|
|
|
11
|
+
# Module name (used for loggin).
|
|
12
|
+
NAME = 'BulkImporter'
|
|
13
|
+
|
|
11
14
|
# Import data from a CSV file to an existing table
|
|
12
15
|
#
|
|
13
16
|
# ==== Parameters
|
|
@@ -43,12 +46,12 @@ module BulkImporter
|
|
|
43
46
|
begin
|
|
44
47
|
# Create temporary table (with all CSV fields)
|
|
45
48
|
Rails.logger.debug \
|
|
46
|
-
"[#{
|
|
49
|
+
"[#{NAME}] Creating temporary table #{temp_name}(#{columns.keys})"
|
|
47
50
|
conn.execute self.make_create_temp_table_sql(temp_name, columns.keys)
|
|
48
51
|
|
|
49
52
|
# Import data
|
|
50
53
|
Rails.logger.debug \
|
|
51
|
-
"[#{
|
|
54
|
+
"[#{NAME}] Importing data from #{file} to #{temp_name}"
|
|
52
55
|
PostgresqlModule.copy_from(
|
|
53
56
|
file,
|
|
54
57
|
temp_name,
|
|
@@ -60,7 +63,7 @@ module BulkImporter
|
|
|
60
63
|
|
|
61
64
|
# Move data from temporary table to target and return total imported rows
|
|
62
65
|
Rails.logger.debug \
|
|
63
|
-
"[#{
|
|
66
|
+
"[#{NAME}] Moving new data to #{target} with mode #{update_mode}"
|
|
64
67
|
self.move_imported_data(temp_name, target, columns, keys, update_mode)
|
|
65
68
|
rescue Exception => e
|
|
66
69
|
Rails.logger.error e.message
|
|
@@ -68,7 +71,7 @@ module BulkImporter
|
|
|
68
71
|
return -1
|
|
69
72
|
ensure
|
|
70
73
|
# Drop temporary table (if exists)
|
|
71
|
-
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS #{temp_name}"
|
|
74
|
+
#ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS #{temp_name}"
|
|
72
75
|
end
|
|
73
76
|
end
|
|
74
77
|
|
|
@@ -88,9 +91,13 @@ module BulkImporter
|
|
|
88
91
|
#
|
|
89
92
|
def self.move_imported_data(origin, destination, columns, keys, update_mode)
|
|
90
93
|
unless self.is_update_mode_valid update_mode
|
|
91
|
-
raise "[#{
|
|
94
|
+
raise "[#{NAME}] Unknown update mode: #{update_mode}"
|
|
92
95
|
end
|
|
93
96
|
|
|
97
|
+
# Create an index to improve move performance.
|
|
98
|
+
Rails.logger.debug "[#{NAME}] Creating index on #{origin}"
|
|
99
|
+
PostgresqlModule.create_index_on origin, keys.keys.map { |i| i.downcase }
|
|
100
|
+
|
|
94
101
|
queries = self.make_move_imported_data_sql(
|
|
95
102
|
origin,
|
|
96
103
|
destination,
|
|
@@ -102,7 +109,7 @@ module BulkImporter
|
|
|
102
109
|
rows = 0
|
|
103
110
|
|
|
104
111
|
queries.each do |query|
|
|
105
|
-
Rails.logger.debug "[#{
|
|
112
|
+
Rails.logger.debug "[#{NAME}] Running query <<#{query}>>"
|
|
106
113
|
rows += ActiveRecord::Base.connection.execute(query).cmd_tuples
|
|
107
114
|
end
|
|
108
115
|
|
|
@@ -243,7 +250,8 @@ module BulkImporter
|
|
|
243
250
|
#
|
|
244
251
|
def self.make_create_temp_table_sql(name, columns)
|
|
245
252
|
columns = columns.map { |i| i + ' text' }
|
|
246
|
-
"CREATE TEMPORARY TABLE #{name} (#{columns.join(',')})"
|
|
253
|
+
#"CREATE TEMPORARY TABLE #{name} (#{columns.join(',')})"
|
|
254
|
+
"CREATE TABLE #{name} (#{columns.join(',')})"
|
|
247
255
|
end
|
|
248
256
|
|
|
249
257
|
# Translate an array of keys in a list with an optional prefix.
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bulk_importer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abel M. Osorio
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-12-
|
|
11
|
+
date: 2016-12-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|