cast story.{upvotes,downvotes} to signed on mysql, integer on postgres

old mysql doesn't support 'cast(1 as integer)', but new/mariadb does

postgres doesn't support 'cast(1 as signed)'

should fix #145
This commit is contained in:
joshua stein 2014-10-03 17:02:02 -05:00
parent 8b3e62d010
commit 9e8b89be1c
4 changed files with 14 additions and 8 deletions

View file

@ -1,11 +1,11 @@
ThinkingSphinx::Index.define :comment, :with => :active_record do ThinkingSphinx::Index.define :comment, :with => :active_record do
indexes comment indexes comment
indexes short_id indexes short_id
indexes user.username, :as => :author indexes user.username, :as => :author
has "(cast(upvotes as integer) - cast(downvotes as integer))", has "(CAST(upvotes as #{Story.votes_cast_type}) - " <<
:as => :score, :type => :bigint, :sortable => true "CAST(downvotes as #{Story.votes_cast_type}))", :as => :score,
:type => :bigint, :sortable => true
has is_deleted has is_deleted
has created_at has created_at

View file

@ -1,4 +1,3 @@
ThinkingSphinx::Index.define :story, :with => :active_record do ThinkingSphinx::Index.define :story, :with => :active_record do
indexes description indexes description
indexes short_id indexes short_id
@ -9,8 +8,9 @@ ThinkingSphinx::Index.define :story, :with => :active_record do
has created_at, :sortable => true has created_at, :sortable => true
has hotness, is_expired has hotness, is_expired
has "(cast(upvotes as integer) - cast(downvotes as integer))", has "(CAST(upvotes as #{Story.votes_cast_type}) - " <<
:as => :score, :type => :bigint, :sortable => true "CAST(downvotes as #{Story.votes_cast_type}))", :as => :score,
:type => :bigint, :sortable => true
set_property :field_weights => { set_property :field_weights => {
:upvotes => 15, :upvotes => 15,

View file

@ -101,6 +101,10 @@ class Story < ActiveRecord::Base
true true
end end
def self.votes_cast_type
Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer"
end
def as_json(options = {}) def as_json(options = {})
h = super(:only => [ h = super(:only => [
:short_id, :short_id,

View file

@ -86,7 +86,8 @@ class StoryRepository
def top(length) def top(length)
top = base_scope.where("created_at >= (NOW() - INTERVAL " << top = base_scope.where("created_at >= (NOW() - INTERVAL " <<
"#{length[:dur]} #{length[:intv].upcase})") "#{length[:dur]} #{length[:intv].upcase})")
top.order("(CAST(upvotes AS integer) - CAST(downvotes AS integer)) DESC") top.order("(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
"CAST(downvotes AS #{Story.votes_cast_type})) DESC")
end end
private private
@ -123,7 +124,8 @@ private
end end
def positive_ranked(scope) def positive_ranked(scope)
scope.where("(CAST(upvotes AS integer) - CAST(downvotes AS integer)) >= -1") scope.where("(CAST(upvotes AS #{Story.votes_cast_type}) - " <<
"CAST(downvotes AS #{Story.votes_cast_type})) >= -1")
end end
def filter_tags(scope, tags) def filter_tags(scope, tags)