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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a3981e26851ce310caa14a2fc257e9c651eccad
4
- data.tar.gz: 27cbfe3409574d837f8af6fae3509c71def416bb
3
+ metadata.gz: fb82dcf1ba046594e065218c795dca1e874ad398
4
+ data.tar.gz: a44c73dfee9cf88acf5918e6928175c8b0575962
5
5
  SHA512:
6
- metadata.gz: a25b6f749c6f54136edc8697d25dc3a5b270630f9f38a064fbb83a606038b4d45ac3a8b0f212831e2b4e5c85ebc461a6a19433181c7b3b8c1e1178c188347786
7
- data.tar.gz: 7ff8e23b0af3ab71d61c8a8a18b972dc416f6bb9639985c54c8872deb23772326017c21faf7cd2a001f9ca8a8c5a7904140f037daa189f9756eb833b040d0c45
6
+ metadata.gz: 7524de391b276d92d69a1afb595deebb683e353f33f4c28d05cb1e1339c79a7ee07082a8cbbc464279c8e11e2acd799c0d8d4348ddbe77d3da07589265349b56
7
+ data.tar.gz: 078908245a45fb5c72ca07c403369c1a04b3d88ac42a51fcfa78128ee7082e5350e3ffa07fb0a1e695af4c159e7cc5123e8cae5247c6ae8ffb16b62bd27ea2cd
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # BulkImporter
1
+ # BulkImporter [![Build Status](https://travis-ci.org/abelosorio/bulk_importer.svg?branch=master)](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
@@ -7,4 +7,4 @@ Rake::TestTask.new(:test) do |t|
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
8
  end
9
9
 
10
- task :default => :spec
10
+ task :default => :test
@@ -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 "active_support"
25
+ spec.add_development_dependency "activesupport", "~> 4.2"
26
26
  end
data/lib/bulk_importer.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "bulk_importer/version"
2
- require "active_support"
3
- require "active_support/core_ext"
2
+ require "active_support/all"
4
3
  require "bulk_importer/postgresql_module.rb"
5
4
 
6
5
  module BulkImporter
@@ -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 or standard input.
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|stdin.
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 == 'stdin' or from.is_a? File
27
+ return -1 unless from.is_a? File
28
28
 
29
- result = ActiveRecord::Base.connection.execute self.make_import_sql(
30
- from.is_a?(File) ? from.path : from,
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
- result.cmd_tuples
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
@@ -1,3 +1,3 @@
1
1
  module BulkImporter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  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.1.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-01 00:00:00.000000000 Z
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: active_support
56
+ name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
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: '0'
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