Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom SQL query option #48

Merged
merged 2 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ pkg

## PROJECT::SPECIFIC
.project

# RVM files
.ruby-version
.ruby-gemset
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ Which will generate the following SQL command:
COPY (SELECT name FROM "users" WHERE "users"."id" IN (1, 2, 3)) TO '/tmp/users.csv' WITH DELIMITER ',' CSV HEADER
```

Alternatively, you can supply customized raw SQL query to copy_to instead of scoped relation:

```ruby
User.copy_to("/tmp/users.csv", query: 'SELECT count(*) as Total FROM users')
```

Which will generate the following SQL command:

```sql
COPY (SELECT count(*) as Total FROM users) TO '/tmp/users.csv' WITH DELIMITER ',' CSV HEADER
```

The COPY command also supports exporting the data in binary format.

```ruby
Expand Down
5 changes: 3 additions & 2 deletions lib/postgres-copy/acts_as_copy_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def copy_to path = nil, options = {}
else
"DELIMITER '#{options[:delimiter]}' CSV #{options[:header] ? 'HEADER' : ''}"
end
options_query = options.delete(:query) || self.all.to_sql

if path
raise "You have to choose between exporting to a file or receiving the lines inside a block" if block_given?
connection.execute "COPY (#{self.all.to_sql}) TO '#{sanitize_sql(path)}' WITH #{options_string}"
connection.execute "COPY (#{options_query}) TO '#{sanitize_sql(path)}' WITH #{options_string}"
else
connection.raw_connection.copy_data "COPY (#{self.all.to_sql}) TO STDOUT WITH #{options_string}" do
connection.raw_connection.copy_data "COPY (#{options_query}) TO STDOUT WITH #{options_string}" do
while line = connection.raw_connection.get_copy_data do
yield(line) if block_given?
end
Expand Down
8 changes: 7 additions & 1 deletion spec/copy_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ActiveRecord::Base.connection.execute %{
TRUNCATE TABLE test_models;
SELECT setval('test_models_id_seq', 1, false);
}
}
TestModel.create :data => 'test data 1'
end

Expand Down Expand Up @@ -79,5 +79,11 @@
end
end.should raise_error
end

it "accepts custom sql query to run instead on the current relation" do
TestModel.copy_to '/tmp/export.csv', query: 'SELECT count(*) as "Total" FROM test_models'
content = File.open('/tmp/export.csv', 'r').read
expect(content).to eq("Total\n1\n")
end
end
end