Permalink
Browse files

Make Database#copy_into raise a DatabaseError if the database signals…

… an error in the postgres adapter

Previously, such errors were swallowed by accident.
  • Loading branch information...
1 parent 6197c96 commit 1e8571d7f7e3f7e1373c6393ef9a3c222c93625d @jeremyevans committed Jan 8, 2013
Showing with 13 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +4 −2 lib/sequel/adapters/postgres.rb
  3. +7 −0 spec/adapters/postgres_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Make Database#copy_into raise a DatabaseError if the database signals an error in the postgres adapter (jeremyevans)
+
* Define respond_to_missing? where method_missing is defined and the object supports respond_to? (jeremyevans)
* Allow lambda procs with 0 arity as virtual row blocks on ruby 1.9 (jeremyevans)
@@ -328,10 +328,12 @@ def copy_into(table, opts={})
end
rescue Exception => e
conn.put_copy_end("ruby exception occurred while copying data into PostgreSQL")
- raise
ensure
conn.put_copy_end unless e
- conn.get_result
+ while res = conn.get_result
+ raise e if e
+ check_database_errors{res.check}
+ end
end
end
end
@@ -1314,6 +1314,13 @@ def @ds.check_return
end
end
+ specify "should handle database errors with a rollback of copied data and still have a usable connection" do
+ 2.times do
+ proc{@db.copy_into(:test_copy, :data=>["1\t2\n", "3\ta\n"])}.should raise_error(Sequel::DatabaseError)
+ @ds.select_map([:x, :y]).should == []
+ end
+ end
+
specify "should raise an Error if both :data and a block are provided" do
proc{@db.copy_into(:test_copy, :data=>["1\t2\n", "3\t4\n"]){}}.should raise_error(Sequel::Error)
end

0 comments on commit 1e8571d

Please sign in to comment.