postgres-copy 1.6.0 → 1.7.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
  SHA256:
3
- metadata.gz: ff5750f062c23bbbaf42c2c4b54e0d10a2a18c45eda5a66e00abd2fa0d3d9cdc
4
- data.tar.gz: 10920c6056c38c5cc4a83ba0ace5164abf8a9eb6276e5cef7a4c19e39424de11
3
+ metadata.gz: 0c9c2751b59517a10e556c5e4cf9cc6c51cafdf1bded29fc3ea493aee37df8f4
4
+ data.tar.gz: fb386d86afd455b9ebd6b4bbc93f6e2d316b5285fb5e1174ea335284e187c2b5
5
5
  SHA512:
6
- metadata.gz: 25c750eb0da06933151e7707f67fa52b9973e603d2afe3288241acf34dc61f7004179483339e630e245660f4371c9f1ddb086e9e8f3a80b32c8bc5d180a79937
7
- data.tar.gz: 8e537afc3c20801bcac073f208225d8375cc25b14b40ebbe80f904eef79ffded4c00ed672987094a08691d84dbaf1818d71fbf0ec2aeedba8d9d2a611298b68d
6
+ metadata.gz: 4b72e200f779749dbeaf7930835296709ac7c7f8c3b81bab737428c4d221b79032b1fd3996eafe8d2e9280f64f6b811cba1ab456969d6a35b5744cc8d7b6f477
7
+ data.tar.gz: e916531647bc072804b7a72987ac416674d9a81b6fd48d4c93c57c046e13ab99e172be607a66d09ceea0590a6573c7f9591a0d1b8e795a3eec9858c07f7f14cf
data/Gemfile.lock CHANGED
@@ -1,29 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- postgres-copy (1.6.0)
4
+ postgres-copy (1.7.0)
5
5
  activerecord (>= 5.1)
6
6
  pg (>= 0.17)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (7.0.1)
12
- activesupport (= 7.0.1)
13
- activerecord (7.0.1)
14
- activemodel (= 7.0.1)
15
- activesupport (= 7.0.1)
16
- activesupport (7.0.1)
11
+ activemodel (7.0.4)
12
+ activesupport (= 7.0.4)
13
+ activerecord (7.0.4)
14
+ activemodel (= 7.0.4)
15
+ activesupport (= 7.0.4)
16
+ activesupport (7.0.4)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
18
  i18n (>= 1.6, < 2)
19
19
  minitest (>= 5.1)
20
20
  tzinfo (~> 2.0)
21
- concurrent-ruby (1.1.9)
21
+ concurrent-ruby (1.1.10)
22
22
  diff-lcs (1.4.4)
23
- i18n (1.9.1)
23
+ i18n (1.12.0)
24
24
  concurrent-ruby (~> 1.0)
25
- minitest (5.15.0)
26
- pg (1.2.3)
25
+ minitest (5.16.3)
26
+ pg (1.4.5)
27
27
  rake (11.2.2)
28
28
  rdoc (6.2.1)
29
29
  rspec (2.99.0)
@@ -34,7 +34,7 @@ GEM
34
34
  rspec-expectations (2.99.2)
35
35
  diff-lcs (>= 1.1.3, < 2.0)
36
36
  rspec-mocks (2.99.4)
37
- tzinfo (2.0.4)
37
+ tzinfo (2.0.5)
38
38
  concurrent-ruby (~> 1.0)
39
39
 
40
40
  PLATFORMS
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # postgres-copy
1
+ # postgres-copy
2
2
 
3
3
  ![Ruby](https://github.com/diogob/postgres-copy/workflows/Ruby/badge.svg)
4
4
 
@@ -207,6 +207,19 @@ This is useful for removing byte order marks when matching column headers.
207
207
  User.copy_from "/tmp/users_with_byte_order_mark.csv", :encoding => 'bom|utf-8'
208
208
  ```
209
209
 
210
+ ### Using PostgresCopy::WithTempTable.generate
211
+
212
+ Based on [nfedyashev](https://github.com/nfedyashev)'s [comment](https://github.com/diogob/postgres-copy/issues/51):
213
+
214
+ ```ruby
215
+ PostgresCopy::WithTempTable.generate do |t|
216
+ columns.each do |column_name|
217
+ t.string column_name.to_sym
218
+ end
219
+ end
220
+ ```
221
+
222
+ This auto-generates an id column, but the temp table creation is configurable.
210
223
 
211
224
  ### Using the CSV Responder
212
225
  If you want to make the result of a COPY command available to download this gem provides a CSV responder that, in conjunction with [inherited_resources](https://github.com/josevalim/inherited_resources), is a very powerfull tool. BTW, do not try to use the responder without inherited_resources.
@@ -108,7 +108,7 @@ module PostgresCopy
108
108
  quoted_table_name
109
109
  end
110
110
 
111
- columns_list = columns_list.map{|c| options[:map][c.to_s] } if options[:map]
111
+ columns_list = columns_list.map{|c| options[:map][c.to_s] || c.to_s } if options[:map]
112
112
  columns_string = columns_list.size > 0 ? "(\"#{columns_list.join('","')}\")" : ""
113
113
  connection.raw_connection.copy_data %{COPY #{table} #{columns_string} FROM STDIN #{options_string}} do
114
114
  if options[:format] == :binary
@@ -0,0 +1,18 @@
1
+ module PostgresCopy
2
+ module WithTempTable
3
+ def self.generate(connection = ActiveRecord::Base.connection, base_klass:
4
+ ActiveRecord::Base, temp_table_name: nil, create_table_opts: {id: :bigint})
5
+ raise "You have to pass a table schema definition block!" unless block_given?
6
+ table_name = temp_table_name || "temp_table_#{SecureRandom.hex}"
7
+
8
+ connection.create_table table_name, temporary: true, **create_table_opts do |t|
9
+ yield t
10
+ end
11
+
12
+ klass = Class.new(base_klass) do
13
+ acts_as_copy_target
14
+ self.table_name = table_name
15
+ end
16
+ end
17
+ end
18
+ end
@@ -5,7 +5,7 @@ $:.unshift lib unless $:.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "postgres-copy"
8
- s.version = "1.6.0"
8
+ s.version = "1.7.0"
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.required_ruby_version = ">= 1.9.3"
11
11
  s.authors = ["Diogo Biazus"]
@@ -14,6 +14,12 @@ describe "COPY FROM" do
14
14
  TestModel.order(:id).map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1'}]
15
15
  end
16
16
 
17
+ it "should import from file if path is with a field_map" do
18
+ TestModel.copy_from File.expand_path('spec/fixtures/comma_with_header_to_map.csv'), map: {'ref' => 'id'}
19
+ TestModel.order(:id).map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1'}]
20
+ end
21
+
22
+
17
23
  it "should import from IO without field_map" do
18
24
  TestModel.copy_from File.open(File.expand_path('spec/fixtures/comma_with_header.csv'), 'r')
19
25
  TestModel.order(:id).map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1'}]
@@ -0,0 +1,2 @@
1
+ ref,data
2
+ 1,test data 1
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'postgres-copy/with_temp_table'
3
+
4
+ describe '.generate' do
5
+ subject(:generate) {
6
+ PostgresCopy::WithTempTable.generate do |t|
7
+ t.string :data
8
+ end
9
+ }
10
+
11
+ it {
12
+ generate.copy_from 'spec/fixtures/comma_with_header.csv'
13
+ data = generate.all.first
14
+ expect(data.id).to eq(1)
15
+ expect(data.data).to eq('test data 1')
16
+ }
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgres-copy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diogo Biazus
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-07 00:00:00.000000000 Z
11
+ date: 2022-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -113,6 +113,7 @@ files:
113
113
  - lib/postgres-copy.rb
114
114
  - lib/postgres-copy/acts_as_copy_target.rb
115
115
  - lib/postgres-copy/csv_responder.rb
116
+ - lib/postgres-copy/with_temp_table.rb
116
117
  - postgres-copy.gemspec
117
118
  - spec/copy_from_binary_spec.rb
118
119
  - spec/copy_from_spec.rb
@@ -127,6 +128,7 @@ files:
127
128
  - spec/fixtures/comma_with_header_and_scope.csv
128
129
  - spec/fixtures/comma_with_header_empty_values_at_the_end.csv
129
130
  - spec/fixtures/comma_with_header_multi.csv
131
+ - spec/fixtures/comma_with_header_to_map.csv
130
132
  - spec/fixtures/comma_without_header.csv
131
133
  - spec/fixtures/extra_field.rb
132
134
  - spec/fixtures/reserved_word_model.rb
@@ -146,12 +148,13 @@ files:
146
148
  - spec/fixtures/tab_with_two_lines.tsv
147
149
  - spec/fixtures/test_extended_model.rb
148
150
  - spec/fixtures/test_model.rb
151
+ - spec/postgres-copy/with_temp_table_spec.rb
149
152
  - spec/spec.opts
150
153
  - spec/spec_helper.rb
151
154
  homepage: http://github.com/diogob/postgres-copy
152
155
  licenses: []
153
156
  metadata: {}
154
- post_install_message:
157
+ post_install_message:
155
158
  rdoc_options: []
156
159
  require_paths:
157
160
  - lib
@@ -166,8 +169,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
169
  - !ruby/object:Gem::Version
167
170
  version: '0'
168
171
  requirements: []
169
- rubygems_version: 3.2.3
170
- signing_key:
172
+ rubygems_version: 3.1.6
173
+ signing_key:
171
174
  specification_version: 4
172
175
  summary: Put COPY command functionality in ActiveRecord's model class
173
176
  test_files: []