postgres-copy 0.5.8 → 0.6.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.
- data/Gemfile.lock +7 -8
- data/lib/postgres-copy/active_record.rb +12 -4
- data/postgres-copy.gemspec +1 -1
- data/spec/fixtures/comma_without_header.csv +1 -0
- data/spec/pg_copy_from_spec.rb +11 -1
- metadata +3 -2
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
postgres-copy (0.
|
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.
|
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.
|
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.
|
53
|
-
multi_json (1.7.
|
54
|
-
pg (0.15.
|
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.
|
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
|
-
|
46
|
+
|
47
47
|
if options[:format] == :binary
|
48
48
|
columns_list = options[:columns] || []
|
49
|
-
|
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 #{
|
64
|
+
connection.execute %{COPY #{table} #{columns_string} FROM STDIN #{options_string}}
|
57
65
|
if options[:format] == :binary
|
58
66
|
bytes = 0
|
59
67
|
begin
|
data/postgres-copy.gemspec
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
1,test data 1
|
data/spec/pg_copy_from_spec.rb
CHANGED
@@ -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.
|
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:
|
188
|
+
hash: -1552817411289584753
|
188
189
|
requirements: []
|
189
190
|
rubyforge_project:
|
190
191
|
rubygems_version: 1.8.25
|