postgres-copy 1.3.0 → 1.4.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/.gitignore +4 -0
- data/Gemfile.lock +25 -25
- data/README.md +12 -0
- data/lib/postgres-copy/acts_as_copy_target.rb +3 -2
- data/postgres-copy.gemspec +1 -1
- data/spec/copy_to_spec.rb +7 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d64b9718c2f756ad9c55bb61a173ee66b0ed5f7
|
4
|
+
data.tar.gz: b8a084b47fa7136bce031104d11b873482e13071
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8b2b7d20479b9bc5e63a50d2c2ee256f26da73e3baf7d526e7f7976ca61684e022c62e2034acffeeb82ec184e1195c12f08940bb78def6dad9af29c068d558c
|
7
|
+
data.tar.gz: 865261adbe21c5e16d2c928336b99b2e21bc49f5dd0025d49b3edeb89eccaff38f55a5af2ed6ed52e4ff0e01017c975d63ee8ebccebd41b44e4bb2ad2506e9d2
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
postgres-copy (1.
|
4
|
+
postgres-copy (1.4.0)
|
5
5
|
activerecord (>= 5.1)
|
6
6
|
pg (>= 0.17)
|
7
7
|
responders
|
@@ -9,26 +9,26 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actionpack (5.1.
|
13
|
-
actionview (= 5.1.
|
14
|
-
activesupport (= 5.1.
|
12
|
+
actionpack (5.1.4)
|
13
|
+
actionview (= 5.1.4)
|
14
|
+
activesupport (= 5.1.4)
|
15
15
|
rack (~> 2.0)
|
16
|
-
rack-test (
|
16
|
+
rack-test (>= 0.6.3)
|
17
17
|
rails-dom-testing (~> 2.0)
|
18
18
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
19
|
-
actionview (5.1.
|
20
|
-
activesupport (= 5.1.
|
19
|
+
actionview (5.1.4)
|
20
|
+
activesupport (= 5.1.4)
|
21
21
|
builder (~> 3.1)
|
22
22
|
erubi (~> 1.4)
|
23
23
|
rails-dom-testing (~> 2.0)
|
24
24
|
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
25
|
-
activemodel (5.1.
|
26
|
-
activesupport (= 5.1.
|
27
|
-
activerecord (5.1.
|
28
|
-
activemodel (= 5.1.
|
29
|
-
activesupport (= 5.1.
|
25
|
+
activemodel (5.1.4)
|
26
|
+
activesupport (= 5.1.4)
|
27
|
+
activerecord (5.1.4)
|
28
|
+
activemodel (= 5.1.4)
|
29
|
+
activesupport (= 5.1.4)
|
30
30
|
arel (~> 8.0)
|
31
|
-
activesupport (5.1.
|
31
|
+
activesupport (5.1.4)
|
32
32
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
33
33
|
i18n (~> 0.7)
|
34
34
|
minitest (~> 5.1)
|
@@ -37,27 +37,27 @@ GEM
|
|
37
37
|
builder (3.2.3)
|
38
38
|
concurrent-ruby (1.0.5)
|
39
39
|
diff-lcs (1.3)
|
40
|
-
erubi (1.6.
|
41
|
-
i18n (0.8.
|
40
|
+
erubi (1.6.1)
|
41
|
+
i18n (0.8.6)
|
42
42
|
loofah (2.0.3)
|
43
43
|
nokogiri (>= 1.5.9)
|
44
44
|
method_source (0.8.2)
|
45
|
-
mini_portile2 (2.
|
46
|
-
minitest (5.10.
|
47
|
-
nokogiri (1.8.
|
48
|
-
mini_portile2 (~> 2.
|
45
|
+
mini_portile2 (2.3.0)
|
46
|
+
minitest (5.10.3)
|
47
|
+
nokogiri (1.8.1)
|
48
|
+
mini_portile2 (~> 2.3.0)
|
49
49
|
pg (0.21.0)
|
50
50
|
rack (2.0.3)
|
51
|
-
rack-test (0.
|
52
|
-
rack (>= 1.0)
|
51
|
+
rack-test (0.7.0)
|
52
|
+
rack (>= 1.0, < 3)
|
53
53
|
rails-dom-testing (2.0.3)
|
54
54
|
activesupport (>= 4.2.0)
|
55
55
|
nokogiri (>= 1.6)
|
56
56
|
rails-html-sanitizer (1.0.3)
|
57
57
|
loofah (~> 2.0)
|
58
|
-
railties (5.1.
|
59
|
-
actionpack (= 5.1.
|
60
|
-
activesupport (= 5.1.
|
58
|
+
railties (5.1.4)
|
59
|
+
actionpack (= 5.1.4)
|
60
|
+
activesupport (= 5.1.4)
|
61
61
|
method_source
|
62
62
|
rake (>= 0.8.7)
|
63
63
|
thor (>= 0.18.1, < 2.0)
|
@@ -74,7 +74,7 @@ GEM
|
|
74
74
|
rspec-expectations (2.99.2)
|
75
75
|
diff-lcs (>= 1.1.3, < 2.0)
|
76
76
|
rspec-mocks (2.99.4)
|
77
|
-
thor (0.
|
77
|
+
thor (0.20.0)
|
78
78
|
thread_safe (0.3.6)
|
79
79
|
tzinfo (1.2.3)
|
80
80
|
thread_safe (~> 0.1)
|
data/README.md
CHANGED
@@ -97,6 +97,18 @@ Which will generate the following SQL command:
|
|
97
97
|
COPY (SELECT name FROM "users" WHERE "users"."id" IN (1, 2, 3)) TO '/tmp/users.csv' WITH DELIMITER ',' CSV HEADER
|
98
98
|
```
|
99
99
|
|
100
|
+
Alternatively, you can supply customized raw SQL query to copy_to instead of scoped relation:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
User.copy_to("/tmp/users.csv", query: 'SELECT count(*) as Total FROM users')
|
104
|
+
```
|
105
|
+
|
106
|
+
Which will generate the following SQL command:
|
107
|
+
|
108
|
+
```sql
|
109
|
+
COPY (SELECT count(*) as Total FROM users) TO '/tmp/users.csv' WITH DELIMITER ',' CSV HEADER
|
110
|
+
```
|
111
|
+
|
100
112
|
The COPY command also supports exporting the data in binary format.
|
101
113
|
|
102
114
|
```ruby
|
@@ -16,12 +16,13 @@ module PostgresCopy
|
|
16
16
|
else
|
17
17
|
"DELIMITER '#{options[:delimiter]}' CSV #{options[:header] ? 'HEADER' : ''}"
|
18
18
|
end
|
19
|
+
options_query = options.delete(:query) || self.all.to_sql
|
19
20
|
|
20
21
|
if path
|
21
22
|
raise "You have to choose between exporting to a file or receiving the lines inside a block" if block_given?
|
22
|
-
connection.execute "COPY (#{
|
23
|
+
connection.execute "COPY (#{options_query}) TO '#{sanitize_sql(path)}' WITH #{options_string}"
|
23
24
|
else
|
24
|
-
connection.raw_connection.copy_data "COPY (#{
|
25
|
+
connection.raw_connection.copy_data "COPY (#{options_query}) TO STDOUT WITH #{options_string}" do
|
25
26
|
while line = connection.raw_connection.get_copy_data do
|
26
27
|
yield(line) if block_given?
|
27
28
|
end
|
data/postgres-copy.gemspec
CHANGED
data/spec/copy_to_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe "COPY TO" 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
|
TestModel.create :data => 'test data 1'
|
10
10
|
end
|
11
11
|
|
@@ -79,5 +79,11 @@ describe "COPY TO" do
|
|
79
79
|
end
|
80
80
|
end.should raise_error
|
81
81
|
end
|
82
|
+
|
83
|
+
it "accepts custom sql query to run instead on the current relation" do
|
84
|
+
TestModel.copy_to '/tmp/export.csv', query: 'SELECT count(*) as "Total" FROM test_models'
|
85
|
+
content = File.open('/tmp/export.csv', 'r').read
|
86
|
+
expect(content).to eq("Total\n1\n")
|
87
|
+
end
|
82
88
|
end
|
83
89
|
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.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diogo Biazus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|