postgres-copy 0.5.8 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- postgres-copy (0.5.8)
4
+ postgres-copy (0.6.0)
5
5
  activerecord (>= 3.0.0)
6
6
  pg
7
7
  rails (>= 3.0.0)
@@ -41,17 +41,16 @@ GEM
41
41
  builder (3.0.4)
42
42
  diff-lcs (1.1.3)
43
43
  erubis (2.7.0)
44
- hike (1.2.1)
44
+ hike (1.2.2)
45
45
  i18n (0.6.1)
46
46
  journey (1.0.4)
47
47
  json (1.7.6)
48
- mail (2.5.3)
49
- i18n (>= 0.4.0)
48
+ mail (2.5.4)
50
49
  mime-types (~> 1.16)
51
50
  treetop (~> 1.4.8)
52
- mime-types (1.22)
53
- multi_json (1.7.2)
54
- pg (0.15.0)
51
+ mime-types (1.23)
52
+ multi_json (1.7.3)
53
+ pg (0.15.1)
55
54
  polyglot (0.3.3)
56
55
  rack (1.4.5)
57
56
  rack-cache (1.2)
@@ -94,7 +93,7 @@ GEM
94
93
  rack (~> 1.0)
95
94
  tilt (~> 1.1, != 1.3.0)
96
95
  thor (0.18.1)
97
- tilt (1.3.6)
96
+ tilt (1.4.1)
98
97
  treetop (1.4.12)
99
98
  polyglot
100
99
  polyglot (>= 0.3.1)
@@ -36,24 +36,32 @@ module ActiveRecord
36
36
  # * You can map fields from the file to different fields in the table using a map in the options hash
37
37
  # * For further details on usage take a look at the README.md
38
38
  def self.pg_copy_from path_or_io, options = {}
39
- options = {:delimiter => ",", :format => :csv}.merge(options)
39
+ options = {:delimiter => ",", :format => :csv, :header => true}.merge(options)
40
40
  options_string = if options[:format] == :binary
41
41
  "BINARY"
42
42
  else
43
43
  "DELIMITER '#{options[:delimiter]}' CSV"
44
44
  end
45
45
  io = path_or_io.instance_of?(String) ? File.open(path_or_io, 'r') : path_or_io
46
- # The first line should be always the HEADER.
46
+
47
47
  if options[:format] == :binary
48
48
  columns_list = options[:columns] || []
49
- else
49
+ elsif options[:header]
50
50
  line = io.gets
51
51
  columns_list = options[:columns] || line.strip.split(options[:delimiter])
52
+ else
53
+ columns_list = options[:columns]
54
+ end
55
+
56
+ table = if options[:table]
57
+ connection.quote_table_name(options[:table])
58
+ else
59
+ quoted_table_name
52
60
  end
53
61
 
54
62
  columns_list = columns_list.map{|c| options[:map][c.to_s] } if options[:map]
55
63
  columns_string = columns_list.size > 0 ? "(\"#{columns_list.join('","')}\")" : ""
56
- connection.execute %{COPY #{quoted_table_name} #{columns_string} FROM STDIN WITH #{options_string}}
64
+ connection.execute %{COPY #{table} #{columns_string} FROM STDIN #{options_string}}
57
65
  if options[:format] == :binary
58
66
  bytes = 0
59
67
  begin
@@ -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 = "0.5.8"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.required_ruby_version = ">= 1.8.7"
@@ -0,0 +1 @@
1
+ 1,test data 1
@@ -5,7 +5,7 @@ describe "COPY FROM" do
5
5
  ActiveRecord::Base.connection.execute %{
6
6
  TRUNCATE TABLE test_models;
7
7
  SELECT setval('test_models_id_seq', 1, false);
8
- }
8
+ }
9
9
  end
10
10
 
11
11
  it "should import from file if path is passed without field_map" do
@@ -53,6 +53,16 @@ describe "COPY FROM" do
53
53
  ExtraField.order(:id).all.map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1', 'created_at' => nil, 'updated_at' => nil}]
54
54
  end
55
55
 
56
+ it "should not expect a header when :header is false" do
57
+ TestModel.pg_copy_from(File.open(File.expand_path('spec/fixtures/comma_without_header.csv'), 'r'), :header => false, :columns => [:id,:data])
58
+ TestModel.order(:id).all.map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1'}]
59
+ end
60
+
61
+ it "should use the table name given by :table" do
62
+ ActiveRecord::Base.pg_copy_from(File.open(File.expand_path('spec/fixtures/comma_without_header.csv'), 'r'), :header => false, :columns => [:id,:data], :table => "test_models")
63
+ TestModel.order(:id).all.map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1'}]
64
+ end
65
+
56
66
  it "should be able to map the header in the file to diferent column names" do
57
67
  TestModel.pg_copy_from(File.open(File.expand_path('spec/fixtures/tab_with_different_header.csv'), 'r'), :delimiter => "\t", :map => {'cod' => 'id', 'info' => 'data'})
58
68
  TestModel.order(:id).all.map{|r| r.attributes}.should == [{'id' => 1, 'data' => 'test data 1'}]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgres-copy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -146,6 +146,7 @@ files:
146
146
  - postgres-copy.gemspec
147
147
  - spec/fixtures/2_col_binary_data.dat
148
148
  - spec/fixtures/comma_with_header.csv
149
+ - spec/fixtures/comma_without_header.csv
149
150
  - spec/fixtures/extra_field.rb
150
151
  - spec/fixtures/reserved_word_model.rb
151
152
  - spec/fixtures/reserved_words.csv
@@ -184,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
185
  version: '0'
185
186
  segments:
186
187
  - 0
187
- hash: 489106335970440028
188
+ hash: -1552817411289584753
188
189
  requirements: []
189
190
  rubyforge_project:
190
191
  rubygems_version: 1.8.25