bulk_importer 0.1.0 → 0.2.0
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 +2 -1
- data/Rakefile +1 -1
- data/bulk_importer.gemspec +1 -1
- data/lib/bulk_importer.rb +1 -2
- data/lib/bulk_importer/postgresql_module.rb +93 -6
- data/lib/bulk_importer/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fb82dcf1ba046594e065218c795dca1e874ad398
|
|
4
|
+
data.tar.gz: a44c73dfee9cf88acf5918e6928175c8b0575962
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7524de391b276d92d69a1afb595deebb683e353f33f4c28d05cb1e1339c79a7ee07082a8cbbc464279c8e11e2acd799c0d8d4348ddbe77d3da07589265349b56
|
|
7
|
+
data.tar.gz: 078908245a45fb5c72ca07c403369c1a04b3d88ac42a51fcfa78128ee7082e5350e3ffa07fb0a1e695af4c159e7cc5123e8cae5247c6ae8ffb16b62bd27ea2cd
|
data/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
# BulkImporter
|
|
1
|
+
# BulkImporter [](https://travis-ci.org/abelosorio/bulk_importer)
|
|
2
|
+
|
|
2
3
|
|
|
3
4
|
This gem allows you to import a big amount of data from files to your database as quick as possible.
|
|
4
5
|
|
data/Rakefile
CHANGED
data/bulk_importer.gemspec
CHANGED
|
@@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
|
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.11"
|
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
24
|
spec.add_development_dependency "minitest", "~> 5.0"
|
|
25
|
-
spec.add_development_dependency "
|
|
25
|
+
spec.add_development_dependency "activesupport", "~> 4.2"
|
|
26
26
|
end
|
data/lib/bulk_importer.rb
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
# +see+ https://www.postgresql.org/docs/9.4/static/index.html
|
|
4
4
|
#
|
|
5
5
|
module PostgresqlModule
|
|
6
|
-
# Copy from file
|
|
6
|
+
# Copy from file.
|
|
7
7
|
#
|
|
8
8
|
# +see+ https://www.postgresql.org/docs/9.4/static/sql-copy.html
|
|
9
9
|
#
|
|
10
10
|
# ==== Parameters
|
|
11
11
|
#
|
|
12
|
-
# * +from+ path_to_some_file
|
|
12
|
+
# * +from+ path_to_some_file.
|
|
13
13
|
# * +target+ Destination table name.
|
|
14
14
|
#
|
|
15
15
|
# ==== Options
|
|
@@ -24,10 +24,63 @@ module PostgresqlModule
|
|
|
24
24
|
# +integer+ Number of imported rows.
|
|
25
25
|
#
|
|
26
26
|
def self.copy_from(from, target, format: 'csv', delimiter: ',', null: '', header: true)
|
|
27
|
-
return -1 unless from
|
|
27
|
+
return -1 unless from.is_a? File
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if self.can_execute_copy
|
|
30
|
+
# Copy file directly.
|
|
31
|
+
result = ActiveRecord::Base.connection.execute self.make_import_sql(
|
|
32
|
+
from.path,
|
|
33
|
+
target,
|
|
34
|
+
format: format,
|
|
35
|
+
delimiter: delimiter,
|
|
36
|
+
null: null,
|
|
37
|
+
header: header
|
|
38
|
+
)
|
|
39
|
+
else
|
|
40
|
+
# Copy file by sending it trough stdin.
|
|
41
|
+
result = self.copy_trough_stdin(
|
|
42
|
+
from.path,
|
|
43
|
+
target,
|
|
44
|
+
format: format,
|
|
45
|
+
delimiter: delimiter,
|
|
46
|
+
null: null,
|
|
47
|
+
header: header
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
result.cmd_tuples
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Copy from file by sendind it trough stdin.
|
|
55
|
+
#
|
|
56
|
+
# You should not run this method directly, instead use copy_from.
|
|
57
|
+
#
|
|
58
|
+
# ==== Parameters
|
|
59
|
+
#
|
|
60
|
+
# * +from+ path_to_some_file.
|
|
61
|
+
# * +target+ Destination table name.
|
|
62
|
+
#
|
|
63
|
+
# ==== Options
|
|
64
|
+
#
|
|
65
|
+
# * +format+ File format. Defaults to 'csv'.
|
|
66
|
+
# * +delimiter+ Column separator character. Defaults ','.
|
|
67
|
+
# * +null+ String that represent null values. Defaults '' (empty).
|
|
68
|
+
# * +header+ File includes header? Defaults to True.
|
|
69
|
+
#
|
|
70
|
+
# ==== Return
|
|
71
|
+
#
|
|
72
|
+
# +integer+ Number of imported rows.
|
|
73
|
+
#
|
|
74
|
+
def self.copy_trough_stdin(from, target, format: 'csv', delimiter: ',', null: '', header: true)
|
|
75
|
+
|
|
76
|
+
# This solution was taken from:
|
|
77
|
+
# http://www.kadrmasconcepts.com/blog/2013/12/15/copy-millions-of-rows-to-postgresql-with-rails
|
|
78
|
+
|
|
79
|
+
conn = ActiveRecord::Base.connection
|
|
80
|
+
raw = conn.raw_connection
|
|
81
|
+
|
|
82
|
+
raw.exec self.make_import_sql(
|
|
83
|
+
'stdin',
|
|
31
84
|
target,
|
|
32
85
|
format: format,
|
|
33
86
|
delimiter: delimiter,
|
|
@@ -35,7 +88,16 @@ module PostgresqlModule
|
|
|
35
88
|
header: header
|
|
36
89
|
)
|
|
37
90
|
|
|
38
|
-
|
|
91
|
+
file = File.open from, 'r'
|
|
92
|
+
|
|
93
|
+
until file.eof?
|
|
94
|
+
raw.put_copy_data file.readline
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
raw.put_copy_end
|
|
98
|
+
file.close
|
|
99
|
+
|
|
100
|
+
raw.get_result
|
|
39
101
|
end
|
|
40
102
|
|
|
41
103
|
# Get column types.
|
|
@@ -134,4 +196,29 @@ module PostgresqlModule
|
|
|
134
196
|
WHERE table_name = '#{table}'
|
|
135
197
|
eof
|
|
136
198
|
end
|
|
199
|
+
|
|
200
|
+
# Checks if the current db user can execute COPY.
|
|
201
|
+
#
|
|
202
|
+
# ==== Return
|
|
203
|
+
#
|
|
204
|
+
# +bool+
|
|
205
|
+
#
|
|
206
|
+
def self.can_execute_copy()
|
|
207
|
+
# Only superusers can execute COPY.
|
|
208
|
+
query = <<-eof
|
|
209
|
+
SELECT *
|
|
210
|
+
FROM pg_roles
|
|
211
|
+
WHERE rolsuper AND
|
|
212
|
+
rolname = CURRENT_USER
|
|
213
|
+
eof
|
|
214
|
+
|
|
215
|
+
begin
|
|
216
|
+
ActiveRecord::Base.connection.execute(query).cmd_tuples > 0
|
|
217
|
+
rescue Exception => e
|
|
218
|
+
Rails.logger.error e.message
|
|
219
|
+
Rails.logger.error e.backtrace
|
|
220
|
+
|
|
221
|
+
false
|
|
222
|
+
end
|
|
223
|
+
end
|
|
137
224
|
end
|
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.
|
|
4
|
+
version: 0.2.0
|
|
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-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -53,19 +53,19 @@ dependencies:
|
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: '5.0'
|
|
55
55
|
- !ruby/object:Gem::Dependency
|
|
56
|
-
name:
|
|
56
|
+
name: activesupport
|
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
|
59
|
-
- - "
|
|
59
|
+
- - "~>"
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '
|
|
61
|
+
version: '4.2'
|
|
62
62
|
type: :development
|
|
63
63
|
prerelease: false
|
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
65
|
requirements:
|
|
66
|
-
- - "
|
|
66
|
+
- - "~>"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '
|
|
68
|
+
version: '4.2'
|
|
69
69
|
description: Import big amount of data into any table of your project.
|
|
70
70
|
email:
|
|
71
71
|
- abel.m.osorio@gmail.com
|