add support for read only post queries

This commit is contained in:
r888888888
2016-01-25 16:29:53 -08:00
parent 11f816a196
commit 9d3d7abedc
5 changed files with 31 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ class PostsController < ApplicationController
else
limit = params[:limit] || (params[:tags] =~ /(?:^|\s)limit:(\d+)(?:$|\s)/ && $1) || CurrentUser.user.per_page
@random = params[:random] || params[:tags] =~ /(?:^|\s)order:random(?:$|\s)/
@post_set = PostSets::Post.new(tag_query, params[:page], limit, raw: params[:raw], random: @random, format: params[:format])
@post_set = PostSets::Post.new(tag_query, params[:page], limit, raw: params[:raw], random: @random, format: params[:format], read_only: params[:ro])
@posts = @post_set.posts
respond_with(@posts) do |format|
format.atom

View File

@@ -1,6 +1,6 @@
module PostSets
class Post < PostSets::Base
attr_reader :tag_array, :page, :per_page, :raw, :random, :post_count, :format
attr_reader :tag_array, :page, :per_page, :raw, :random, :post_count, :format, :read_only
def initialize(tags, page = 1, per_page = nil, options = {})
@tag_array = Tag.scan_query(tags)
@@ -10,6 +10,7 @@ module PostSets
@raw = options[:raw].present?
@random = options[:random].present?
@format = options[:format] || "html"
@read_only = options[:read_only]
end
def tag_string
@@ -128,7 +129,7 @@ module PostSets
elsif raw
temp = ::Post.raw_tag_match(tag_string).order("posts.id DESC").paginate(page, :count => post_count, :limit => per_page)
else
temp = ::Post.tag_match(tag_string).paginate(page, :count => post_count, :limit => per_page)
temp = ::Post.tag_match(tag_string, read_only).paginate(page, :count => post_count, :limit => per_page)
end
temp.each # hack to force rails to eager load
temp

View File

@@ -1513,8 +1513,12 @@ class Post < ActiveRecord::Base
where("posts.tag_index @@ to_tsquery('danbooru', E?)", tag.to_escaped_for_tsquery)
end
def tag_match(query)
PostQueryBuilder.new(query).build
def tag_match(query, read_only = false)
if read_only
PostQueryBuilder.new(query).build(PostReadOnly.where("true"))
else
PostQueryBuilder.new(query).build
end
end
def positive

View File

@@ -0,0 +1,4 @@
class PostReadOnly < Post
establish_connection "ro_#{Rails.env}".to_sym
attr_readonly *column_names
end

View File

@@ -20,3 +20,20 @@ production:
database: danbooru2
pool: 5
timeout: 5000
# read only databases, just point to local copies if you have no need
ro_development:
adapter: postgresql
database: danbooru2
ro_test:
adapter: postgresql
database: danbooru2_test
ro_production:
adapter: postgresql
database: danbooru2
ro_staging:
adapter: postgresql
database: danbooru2