From 9e8b89be1c277caf4b49a4df7c9c8c4188da7d00 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Fri, 3 Oct 2014 17:02:02 -0500 Subject: [PATCH] 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 --- app/indices/comment_index.rb | 6 +++--- app/indices/story_index.rb | 6 +++--- app/models/story.rb | 4 ++++ app/models/story_repository.rb | 6 ++++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/indices/comment_index.rb b/app/indices/comment_index.rb index 7b3d931..d248de9 100644 --- a/app/indices/comment_index.rb +++ b/app/indices/comment_index.rb @@ -1,11 +1,11 @@ - ThinkingSphinx::Index.define :comment, :with => :active_record do indexes comment indexes short_id indexes user.username, :as => :author - has "(cast(upvotes as integer) - cast(downvotes as integer))", - :as => :score, :type => :bigint, :sortable => true + has "(CAST(upvotes as #{Story.votes_cast_type}) - " << + "CAST(downvotes as #{Story.votes_cast_type}))", :as => :score, + :type => :bigint, :sortable => true has is_deleted has created_at diff --git a/app/indices/story_index.rb b/app/indices/story_index.rb index adc09cf..b395f7d 100644 --- a/app/indices/story_index.rb +++ b/app/indices/story_index.rb @@ -1,4 +1,3 @@ - ThinkingSphinx::Index.define :story, :with => :active_record do indexes description indexes short_id @@ -9,8 +8,9 @@ ThinkingSphinx::Index.define :story, :with => :active_record do has created_at, :sortable => true has hotness, is_expired - has "(cast(upvotes as integer) - cast(downvotes as integer))", - :as => :score, :type => :bigint, :sortable => true + has "(CAST(upvotes as #{Story.votes_cast_type}) - " << + "CAST(downvotes as #{Story.votes_cast_type}))", :as => :score, + :type => :bigint, :sortable => true set_property :field_weights => { :upvotes => 15, diff --git a/app/models/story.rb b/app/models/story.rb index 4fb5b6d..a2e2323 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -101,6 +101,10 @@ class Story < ActiveRecord::Base true end + def self.votes_cast_type + Story.connection.adapter_name.match(/mysql/i) ? "signed" : "integer" + end + def as_json(options = {}) h = super(:only => [ :short_id, diff --git a/app/models/story_repository.rb b/app/models/story_repository.rb index b617b34..6393d20 100644 --- a/app/models/story_repository.rb +++ b/app/models/story_repository.rb @@ -86,7 +86,8 @@ class StoryRepository def top(length) top = base_scope.where("created_at >= (NOW() - INTERVAL " << "#{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 private @@ -123,7 +124,8 @@ private end 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 def filter_tags(scope, tags)