Skip to content

Commit

Permalink
Raise Hanami::Model::MissingPrimaryKeyError if the table doesn't ha…
Browse files Browse the repository at this point in the history
…s a primary key.
  • Loading branch information
jodosha committed Feb 10, 2017
1 parent 88b06cf commit c965327
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A persistence layer for Hanami

### Fixed
- [Thorbjørn Hermansen] Ensure repository to not override given timestamps
- [Luca Guidi] Raise `Hanami::Model::UnknownPrimaryKeyError` if `Repository#find` is ran against a database w/o a primary key
- [Luca Guidi] Raise `Hanami::Model::MissingPrimaryKeyError` if `Repository#find` is ran against a database w/o a primary key
- [Alfonso Uceda] Ensure SQLite databases to be used on JRuby when the database path is in the same directory of the Ruby script (eg. `./test.sqlite`)

### Changed
Expand Down
5 changes: 1 addition & 4 deletions lib/hanami/model/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ class UnknownDatabaseTypeError < Error
# Unknown primary key error
#
# @since 1.0.0.beta1
class UnknownPrimaryKeyError < Error
def initialize(relation)
super("Can't find primary key for `#{relation}' table")
end
class MissingPrimaryKeyError < Error
end
end
end
1 change: 1 addition & 0 deletions lib/hanami/model/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def self.desc(column)
Error.register(ROM::SQL::CheckConstraintError, CheckConstraintViolationError)
Error.register(ROM::SQL::ForeignKeyConstraintError, ForeignKeyConstraintViolationError)
Error.register(ROM::SQL::UnknownDBTypeError, UnknownDatabaseTypeError)
Error.register(ROM::SQL::MissingPrimaryKeyError, MissingPrimaryKeyError)

Error.register(Java::JavaSql::SQLException, DatabaseError) if Utils.jruby?
end
Expand Down
6 changes: 3 additions & 3 deletions lib/hanami/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def initialize
#
# @return [Hanami::Entity,NilClass] the entity, if found
#
# @raise [Hanami::Model::UnknownPrimaryKeyError] if the table doesn't
# @raise [Hanami::Model::MissingPrimaryKeyError] if the table doesn't
# define a primary key
#
# @since 0.7.0
Expand All @@ -394,8 +394,8 @@ def initialize
# user = repository.find(user.id)
def find(id)
root.by_pk(id).as(:entity).one
rescue KeyError
raise Hanami::Model::UnknownPrimaryKeyError.new(self.class.relation)
rescue => e
raise Hanami::Model::Error.for(e)
end

# Return all the records for the relation
Expand Down
4 changes: 2 additions & 2 deletions test/integration/repository/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
it 'raises error' do
repository.create(id: id)

exception = -> { repository.find(id) }.must_raise(Hanami::Model::UnknownPrimaryKeyError)
exception.message.must_equal "Can't find primary key for `labels' table"
exception = -> { repository.find(id) }.must_raise(Hanami::Model::MissingPrimaryKeyError)
exception.message.must_equal "Missing primary key for :labels"
end
end
end
Expand Down

0 comments on commit c965327

Please sign in to comment.