diogob / postgres-copy

Simple PostgreSQL's COPY command support in ActiveRecord models
MIT License
429 stars 64 forks source link

Ensure valid row values when fields have commas and block is given #41

Closed boricic closed 7 years ago

boricic commented 7 years ago

Hey @diogob,

I've noticed and issue while using postgres-copy.

When there are commas inside fields and block is given there is a problem with row values. Line is split by delimiter, regardless of CSV format, and when delimiter is a comma, row has more values than there are columns.

I've added a test that demonstrates the issue.

To fix it, I've simply added CSV lib to parse the line and avoid splitting just by delimiter. Line is split with regards to CSV format.

When yield finishes, line is again generated by CSV lib generate_line to ensure the line is a valid CSV line.

Please let me know what you think!

diogob commented 7 years ago

@boricic thanks for the PR 👍 I like your solution, I will release a new version of the gem later tonight.