Permalink
Browse files

Add Sequel::Postgres::ExclusionConstraintViolation

PostgreSQL supports exclusion constraints, which are similar to
unique constraints but instead of disallowing equal entries, they
disallow overlapping entries.  This gives a unique exception
class for such errors.
  • Loading branch information...
1 parent 3b74892 commit f38dad9d4c494f82b560757269ad7b119f0b8156 @jeremyevans committed Jan 24, 2013
Showing with 6 additions and 2 deletions.
  1. +4 −0 lib/sequel/adapters/shared/postgres.rb
  2. +2 −2 spec/adapters/postgres_spec.rb
@@ -89,6 +89,9 @@ def validate_constraint(name)
end
end
+ # Error raised when Sequel determines a PostgreSQL exclusion constraint has been violated.
+ class ExclusionConstraintViolation < Sequel::ConstraintViolation; end
+
# Methods shared by Database instances that connect to PostgreSQL.
module DatabaseMethods
EXCLUDE_SCHEMAS = /pg_*|information_schema/i
@@ -632,6 +635,7 @@ def constraint_definition_sql(constraint)
/violates foreign key constraint/ => ForeignKeyConstraintViolation,
/violates check constraint/ => CheckConstraintViolation,
/violates not-null constraint/ => NotNullConstraintViolation,
+ /conflicting key value violates exclusion constraint/ => ExclusionConstraintViolation,
}.freeze
def database_error_regexps
DATABASE_ERROR_REGEXPS
@@ -157,13 +157,13 @@ def logger.method_missing(m, msg)
@db.create_table!(:atest){Integer :t; exclude [[Sequel.desc(:t, :nulls=>:last), '=']], :using=>:btree, :where=>proc{t > 0}}
@db[:atest].insert(1)
@db[:atest].insert(2)
- proc{@db[:atest].insert(2)}.should raise_error(Sequel::DatabaseError)
+ proc{@db[:atest].insert(2)}.should raise_error(Sequel::Postgres::ExclusionConstraintViolation)
@db.create_table!(:atest){Integer :t}
@db.alter_table(:atest){add_exclusion_constraint [[:t, '=']], :using=>:btree, :name=>'atest_ex'}
@db[:atest].insert(1)
@db[:atest].insert(2)
- proc{@db[:atest].insert(2)}.should raise_error(Sequel::DatabaseError)
+ proc{@db[:atest].insert(2)}.should raise_error(Sequel::Postgres::ExclusionConstraintViolation)
@db.alter_table(:atest){drop_constraint 'atest_ex'}
end if POSTGRES_DB.server_version >= 90000

0 comments on commit f38dad9

Please sign in to comment.