Skip to content

Commit

Permalink
remove (Query): #first and #last shortcuts
Browse files Browse the repository at this point in the history
BREAKING CHANGE: It is done because primary key can be
an unordable type, e.g. UUID, therefore it would make
no sense to call `#first`
  • Loading branch information
vladfaust committed Mar 9, 2019
1 parent 34d2a0e commit f0d60b2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
2 changes: 1 addition & 1 deletion spec/model/class_query_shortcuts_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe Onyx::SQL::Model::ClassQueryShortcuts do
end
end

{% for m in %w(update delete all one first last) %}
{% for m in %w(update delete all one) %}
describe {{m}} do
it do
User.{{m.id}}.should eq Query(User).new.{{m.id}}
Expand Down
31 changes: 0 additions & 31 deletions src/onyx-sql/query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module Onyx::SQL
# * Shortcuts:
# * `#one`
# * `#all`
# * `#first`
# * `#last`
# * `#and`
# * `#or`
# * `#and_not`
Expand Down Expand Up @@ -217,18 +215,6 @@ module Onyx::SQL
limit(1)
end

# Alias of `#order_by(primary_key, :asc).one`. The primary key is determined by the
# `Model::Options` `:primary_key` option.
def first
order_by(primary_key, :asc).one
end

# Alias of `#order_by(primary_key, :desc).one`. The primary key is determined by the
# `Model::Options` `:primary_key` option.
def last
order_by(primary_key, :desc).one
end

# Return the SQL representation of this query.
# Pass `true` to replace `"?"` query arguments with `"$n"`, which would work for PostgreSQL.
def to_s(index_params = false)
Expand Down Expand Up @@ -330,22 +316,5 @@ module Onyx::SQL
private class ParamIndex
property value = 0
end

protected def primary_key
{% begin %}
{%
options = T.annotation(Model::Options)
raise "Onyx::SQL::Model::Options annotation must be defined for #{T}" unless options

pk = options[:primary_key]
raise "Onyx::SQL::Model::Options annotation is missing :primary_key option for #{T}" unless pk

pk_ivar = T.instance_vars.find { |riv| "@#{riv.name}".id == pk.id }
raise "Cannot find primary key field #{pk} for #{T}" unless pk_ivar
%}

{{pk_ivar.name.symbolize}}
{% end %}
end
end
end

0 comments on commit f0d60b2

Please sign in to comment.