I have a feature where I am attempting to copy a very large CSV file into my database. I am using the pg gem to do so very quickly as explained in this article here POSTGRESQL COPY.
In my schema.rb, I have unique constraints on a model so there are times during the upload process, I'll encounter a PG::UniqueViolation constraint error when attempting to import a file.
What I need to do is I need to be able to capture this error and once captured, write some code that will log the error and the message along with some other details. The problem I am experiencing is that I am unable to currently capture the exception of writing the data into the file. Below is the following pseudo code:
db_conn.copy_data(CODE_COPY_STATEMENT, enco) do
iterator.each do |line|
.......# CODE #.......
begin
db_conn.put_copy_data([information_to_copy])
rescue StandardError => e
I need to do some stuff here for processing the error, etc.
end
.......# CODE #......
end
end
So far I'm unable to capture the error and I've tried to capture PG::UniqueValidation, StandardError etc. but to no success. Ultimately, what I need to do is to skip over this error and continue processing the file. Does anyone have anything I can try? Help would be greatly appreciated.
COPYis al or none. The best practice is toCOPYinto a staging table that has no constraints and then move the data from there via SQL commands to the final table.