Replace Makara with an initial Rails 6 role based splitting method.
This commit is contained in:
committed by
admin
parent
31922ea58b
commit
473c48b2aa
@@ -65,7 +65,11 @@ class Api::BaseController < ApplicationController
|
||||
def current_resource_owner
|
||||
if doorkeeper_token
|
||||
@current_user ||= Rails.cache.fetch("dk:user:#{doorkeeper_token.resource_owner_id}", expires_in: 25.hours) do
|
||||
User.find(doorkeeper_token.resource_owner_id)
|
||||
u = nil
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
u = User.find(doorkeeper_token.resource_owner_id)
|
||||
end
|
||||
u
|
||||
end
|
||||
end
|
||||
return @current_user
|
||||
|
||||
@@ -22,11 +22,17 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
|
||||
private
|
||||
|
||||
def set_account
|
||||
@account = Account.find(params[:account_id])
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
@account = Account.find(params[:account_id])
|
||||
end
|
||||
end
|
||||
|
||||
def load_statuses
|
||||
cached_account_statuses
|
||||
cas = nil
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
cas = cached_account_statuses
|
||||
end
|
||||
cas
|
||||
end
|
||||
|
||||
def cached_account_statuses
|
||||
|
||||
@@ -17,7 +17,11 @@ class Api::V1::BookmarkCollections::BookmarksController < Api::BaseController
|
||||
private
|
||||
|
||||
def load_statuses
|
||||
cached_bookmarks
|
||||
cb = nil
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
cb = cached_bookmarks
|
||||
end
|
||||
cb
|
||||
end
|
||||
|
||||
def cached_bookmarks
|
||||
@@ -25,7 +29,7 @@ class Api::V1::BookmarkCollections::BookmarksController < Api::BaseController
|
||||
Status.reorder(nil).joins(:status_bookmarks).merge(results),
|
||||
Status
|
||||
)
|
||||
end
|
||||
`` end
|
||||
|
||||
def results
|
||||
@_results ||= account_bookmarks.paginate_by_id(
|
||||
@@ -65,4 +69,4 @@ class Api::V1::BookmarkCollections::BookmarksController < Api::BaseController
|
||||
def pagination_params(core_params)
|
||||
params.slice(:limit).permit(:limit).merge(core_params)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,7 +27,6 @@ class Api::V1::NotificationsController < Api::BaseController
|
||||
def mark_read
|
||||
if !params[:id].nil? and !current_account.user.nil?
|
||||
conn = ActiveRecord::Base.connection
|
||||
conn.stick_to_master!
|
||||
conn.exec_query "update users set last_read_notification = #{params[:id].to_i} where id = #{current_account.user.id}"
|
||||
end
|
||||
# current_account.notifications.find(params[:id]).mark_read!
|
||||
|
||||
@@ -34,14 +34,6 @@ class Api::V1::Statuses::FavouritesController < Api::BaseController
|
||||
end
|
||||
|
||||
def requested_status
|
||||
rs = nil
|
||||
begin
|
||||
rs = Status.find(params[:status_id])
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
Status.connection.stick_to_master!
|
||||
rs = Status.find(params[:status_id])
|
||||
end
|
||||
return rs unless rs.nil?
|
||||
raise ActiveRecord::RecordNotFound
|
||||
Status.find(params[:status_id])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -31,15 +31,7 @@ class Api::V1::Statuses::ReblogsController < Api::BaseController
|
||||
private
|
||||
|
||||
def status_for_reblog
|
||||
rs = nil
|
||||
begin
|
||||
rs = Status.find(params[:status_id])
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
Status.connection.stick_to_master!
|
||||
rs = Status.find(params[:status_id])
|
||||
end
|
||||
return rs unless rs.nil?
|
||||
raise ActiveRecord::RecordNotFound
|
||||
Status.find(params[:status_id])
|
||||
end
|
||||
|
||||
def status_for_destroy
|
||||
|
||||
@@ -39,7 +39,11 @@ class Api::V1::Timelines::ExploreController < Api::BaseController
|
||||
end
|
||||
|
||||
def cached_explore_statuses
|
||||
cache_collection explore_statuses, Status
|
||||
es = nil
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
es = cache_collection explore_statuses, Status
|
||||
end
|
||||
es
|
||||
end
|
||||
|
||||
def explore_statuses
|
||||
|
||||
@@ -56,7 +56,11 @@ class Api::V1::Timelines::GroupCollectionController < Api::BaseController
|
||||
end
|
||||
|
||||
def cached_group_collection_statuses
|
||||
cache_collection group_collection_statuses, Status
|
||||
gcs = nil
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
gcs = cache_collection group_collection_statuses, Status
|
||||
end
|
||||
gcs
|
||||
end
|
||||
|
||||
def group_collection_statuses
|
||||
|
||||
@@ -46,7 +46,11 @@ class Api::V1::Timelines::GroupController < Api::BaseController
|
||||
end
|
||||
|
||||
def cached_group_statuses
|
||||
cache_collection group_statuses, Status
|
||||
gs = nil
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
gs = cache_collection group_statuses, Status
|
||||
end
|
||||
gs
|
||||
end
|
||||
|
||||
def group_statuses
|
||||
|
||||
@@ -16,7 +16,11 @@ class Api::V1::Timelines::ProController < Api::BaseController
|
||||
end
|
||||
|
||||
def cached_pro_statuses
|
||||
cache_collection pro_statuses, Status
|
||||
ps = nil
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
ps = cache_collection pro_statuses, Status
|
||||
end
|
||||
ps
|
||||
end
|
||||
|
||||
def pro_statuses
|
||||
|
||||
@@ -89,8 +89,12 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
def current_session
|
||||
return nil if cookies.signed['_session_id'].nil?
|
||||
@current_session ||= Rails.cache.fetch("dk:sess:#{cookies.signed['_session_id']}", expires_in: 25.hours) do
|
||||
SessionActivation.find_by(session_id: cookies.signed['_session_id'])
|
||||
@current_session ||= Rails.cache.fetch("dk:sess:#{cookies.signed['_session_id']}", expires_in: 25.hours) do
|
||||
sa = nil
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
sa = SessionActivation.find_by(session_id: cookies.signed['_session_id'])
|
||||
end
|
||||
sa
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ class Auth::SessionsController < Devise::SessionsController
|
||||
prepend_before_action :authenticate_with_two_factor, if: :two_factor_enabled?, only: [:create]
|
||||
before_action :set_instance_presenter, only: [:new]
|
||||
before_action :set_body_classes
|
||||
before_action :use_master
|
||||
|
||||
def new
|
||||
Devise.omniauth_configs.each do |provider, config|
|
||||
@@ -36,10 +35,6 @@ class Auth::SessionsController < Devise::SessionsController
|
||||
|
||||
protected
|
||||
|
||||
def use_master
|
||||
User.connection.stick_to_master!
|
||||
end
|
||||
|
||||
def find_user
|
||||
if session[:otp_user_id]
|
||||
User.find(session[:otp_user_id])
|
||||
|
||||
@@ -13,9 +13,10 @@ module SessionTrackingConcern
|
||||
|
||||
def set_session_activity
|
||||
return unless session_needs_update?
|
||||
conn = ActiveRecord::Base.connection
|
||||
conn.stick_to_master!
|
||||
conn.exec_query "update session_activations set updated_at = NOW() where id = #{current_session.id}"
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
conn = ActiveRecord::Base.connection
|
||||
conn.exec_query "update session_activations set updated_at = NOW() where id = #{current_session.id}"
|
||||
end
|
||||
end
|
||||
|
||||
def session_needs_update?
|
||||
|
||||
@@ -12,7 +12,9 @@ module UserTrackingConcern
|
||||
private
|
||||
|
||||
def set_user_activity
|
||||
current_user.update_sign_in!(request) if user_needs_sign_in_update?
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
current_user.update_sign_in!(request) if user_needs_sign_in_update?
|
||||
end
|
||||
end
|
||||
|
||||
def user_needs_sign_in_update?
|
||||
|
||||
@@ -9,6 +9,9 @@ class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController
|
||||
|
||||
include Localized
|
||||
|
||||
include ForceDbWriterRole
|
||||
around_action :force_writer_db_role, only: [:store_current_location, :render_success]
|
||||
|
||||
private
|
||||
|
||||
def store_current_location
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Oauth::TokensController < Doorkeeper::TokensController
|
||||
include ForceDbWriterRole
|
||||
around_action :force_writer_db_role, only: :revoke
|
||||
|
||||
def revoke
|
||||
unsubscribe_for_token if authorized? && token.accessible?
|
||||
super
|
||||
|
||||
@@ -17,16 +17,16 @@ class ReactController < ApplicationController
|
||||
before_action :set_instance_presenter
|
||||
|
||||
def react
|
||||
#
|
||||
#
|
||||
end
|
||||
|
||||
|
||||
def groupBySlug
|
||||
@group = Group.where(slug: params[:groupSlug], is_archived: false).first
|
||||
unless @group.nil?
|
||||
return redirect_to "/groups/#{@group.id}"
|
||||
end
|
||||
|
||||
return not_found
|
||||
return not_found
|
||||
end
|
||||
|
||||
def status_show
|
||||
@@ -62,7 +62,7 @@ class ReactController < ApplicationController
|
||||
|
||||
def redirect_to_original
|
||||
if @status.reblog?
|
||||
redirect_to ::TagManager.instance.url_for(@status.reblog)
|
||||
redirect_to ::TagManager.instance.url_for(@status.reblog)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,7 +75,7 @@ class ReactController < ApplicationController
|
||||
elsif find_public_route_matches
|
||||
return
|
||||
elsif request.path.count("/") == 1 && request.path.length === 1
|
||||
#
|
||||
#
|
||||
elsif request.path.count("/") == 1 && !request.path.include?("@")
|
||||
acctFromPath = request.path.sub("/", "")
|
||||
@account = Account.find_local!(acctFromPath)
|
||||
@@ -108,14 +108,14 @@ class ReactController < ApplicationController
|
||||
end
|
||||
|
||||
def initial_state_params
|
||||
if !current_user.nil?
|
||||
if !current_user.nil? && !current_session.nil?
|
||||
{
|
||||
settings: Web::Setting.find_by(user: current_user)&.data || {},
|
||||
push_subscription: current_account.user.web_push_subscription(current_session),
|
||||
current_account: current_account,
|
||||
token: current_session.token,
|
||||
}
|
||||
else
|
||||
else
|
||||
return {}
|
||||
end
|
||||
end
|
||||
|
||||
10
app/lib/force_db_writer_role.rb
Normal file
10
app/lib/force_db_writer_role.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
module ForceDbWriterRole
|
||||
extend ActiveSupport::Concern
|
||||
# This is intended to be used as an around_action hook for GET
|
||||
# endpoints that need to perform writes to ActiveRecord
|
||||
def force_writer_db_role
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,6 +3,7 @@
|
||||
#
|
||||
# Table name: accounts
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# username :string default(""), not null
|
||||
# domain :string
|
||||
# secret :string default(""), not null
|
||||
@@ -19,11 +20,11 @@
|
||||
# url :string
|
||||
# avatar_file_name :string
|
||||
# avatar_content_type :string
|
||||
# avatar_file_size :bigint(8)
|
||||
# avatar_file_size :integer
|
||||
# avatar_updated_at :datetime
|
||||
# header_file_name :string
|
||||
# header_content_type :string
|
||||
# header_file_size :bigint(8)
|
||||
# header_file_size :integer
|
||||
# header_updated_at :datetime
|
||||
# avatar_remote_url :string
|
||||
# subscription_expires_at :datetime
|
||||
@@ -35,7 +36,6 @@
|
||||
# shared_inbox_url :string default(""), not null
|
||||
# followers_url :string default(""), not null
|
||||
# protocol :integer default(0), not null
|
||||
# id :bigint(8) not null, primary key
|
||||
# memorial :boolean default(FALSE), not null
|
||||
# moved_to_account_id :bigint(8)
|
||||
# featured_collection_url :string
|
||||
@@ -43,10 +43,10 @@
|
||||
# actor_type :string
|
||||
# discoverable :boolean
|
||||
# also_known_as :string is an Array
|
||||
# is_pro :boolean default(FALSE), not null
|
||||
# pro_expires_at :datetime
|
||||
# silenced_at :datetime
|
||||
# suspended_at :datetime
|
||||
# is_pro :boolean default(FALSE), not null
|
||||
# pro_expires_at :datetime
|
||||
# is_verified :boolean default(FALSE), not null
|
||||
# is_donor :boolean default(FALSE), not null
|
||||
# is_investor :boolean default(FALSE), not null
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
connects_to database: {
|
||||
writing: :master,
|
||||
reading: :slave1
|
||||
}
|
||||
self.abstract_class = true
|
||||
include Remotable
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
# user_id :bigint(8)
|
||||
# dump_file_name :string
|
||||
# dump_content_type :string
|
||||
# dump_file_size :bigint(8)
|
||||
# dump_file_size :integer
|
||||
# dump_updated_at :datetime
|
||||
# processed :boolean default(FALSE), not null
|
||||
# created_at :datetime not null
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
# domain :string
|
||||
# image_file_name :string
|
||||
# image_content_type :string
|
||||
# image_file_size :bigint(8)
|
||||
# image_file_size :integer
|
||||
# image_updated_at :datetime
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
# description :string not null
|
||||
# cover_image_file_name :string
|
||||
# cover_image_content_type :string
|
||||
# cover_image_file_size :bigint(8)
|
||||
# cover_image_file_size :integer
|
||||
# cover_image_updated_at :datetime
|
||||
# is_nsfw :boolean default(FALSE), not null
|
||||
# is_featured :boolean default(FALSE), not null
|
||||
@@ -18,7 +18,7 @@
|
||||
# member_count :integer default(0)
|
||||
# slug :text
|
||||
# is_private :boolean default(FALSE)
|
||||
# is_visible :boolean default(TRUE)
|
||||
# is_visible :boolean default(FALSE)
|
||||
# tags :string default([]), is an Array
|
||||
# password :string
|
||||
# group_category_id :integer
|
||||
|
||||
@@ -8,7 +8,9 @@ class HomeFeed < Feed
|
||||
end
|
||||
|
||||
def get(limit = 20, max_id = nil, since_id = nil, min_id = nil)
|
||||
from_database(limit, max_id, since_id, min_id)
|
||||
ActiveRecord::Base.connected_to(role: :reading) do
|
||||
from_database(limit, max_id, since_id, min_id)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
# id :bigint(8) not null, primary key
|
||||
# list_id :bigint(8) not null
|
||||
# account_id :bigint(8) not null
|
||||
# follow_id :bigint(8)
|
||||
# follow_id :bigint(8) default(1)
|
||||
#
|
||||
|
||||
class ListAccount < ApplicationRecord
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
#
|
||||
# Table name: media_attachments
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# status_id :bigint(8)
|
||||
# file_file_name :string
|
||||
# file_content_type :string
|
||||
# file_file_size :bigint(8)
|
||||
# file_file_size :integer
|
||||
# file_updated_at :datetime
|
||||
# remote_url :string default(""), not null
|
||||
# created_at :datetime not null
|
||||
@@ -15,7 +16,6 @@
|
||||
# type :integer default("image"), not null
|
||||
# file_meta :json
|
||||
# account_id :bigint(8)
|
||||
# id :bigint(8) not null, primary key
|
||||
# description :text
|
||||
# scheduled_status_id :bigint(8)
|
||||
# blurhash :string
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
# description :string default(""), not null
|
||||
# image_file_name :string
|
||||
# image_content_type :string
|
||||
# image_file_size :bigint(8)
|
||||
# image_file_size :integer
|
||||
# image_updated_at :datetime
|
||||
# type :integer default("link"), not null
|
||||
# html :text default(""), not null
|
||||
|
||||
@@ -44,10 +44,13 @@ class SessionActivation < ApplicationRecord
|
||||
end
|
||||
|
||||
def activate(**options)
|
||||
SessionActivation.record_timestamps = true
|
||||
activation = create!(options)
|
||||
purge_old
|
||||
SessionActivation.record_timestamps = false
|
||||
activation = nil
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
SessionActivation.record_timestamps = true
|
||||
activation = create!(options)
|
||||
purge_old
|
||||
SessionActivation.record_timestamps = false
|
||||
end
|
||||
activation
|
||||
end
|
||||
|
||||
@@ -72,12 +75,16 @@ class SessionActivation < ApplicationRecord
|
||||
end
|
||||
|
||||
def assign_access_token
|
||||
superapp = Doorkeeper::Application.find_by(superapp: true)
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
|
||||
self.access_token = Doorkeeper::AccessToken.create!(application_id: superapp&.id,
|
||||
resource_owner_id: user_id,
|
||||
scopes: 'read write follow',
|
||||
expires_in: Doorkeeper.configuration.access_token_expires_in,
|
||||
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?)
|
||||
superapp = Doorkeeper::Application.find_by(superapp: true)
|
||||
|
||||
self.access_token = Doorkeeper::AccessToken.create!(application_id: superapp&.id,
|
||||
resource_owner_id: user_id,
|
||||
scopes: 'read write follow',
|
||||
expires_in: Doorkeeper.configuration.access_token_expires_in,
|
||||
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?)
|
||||
end
|
||||
self.access_token
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
# var :string default(""), not null
|
||||
# file_file_name :string
|
||||
# file_content_type :string
|
||||
# file_file_size :bigint(8)
|
||||
# file_file_size :integer
|
||||
# file_updated_at :datetime
|
||||
# meta :json
|
||||
# created_at :datetime not null
|
||||
|
||||
@@ -155,26 +155,34 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def update_tracked_fields!(request)
|
||||
super
|
||||
prepare_returning_user!
|
||||
ru = nil
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
super
|
||||
ru = prepare_returning_user!
|
||||
end
|
||||
ru
|
||||
end
|
||||
|
||||
def update_sign_in!(request, new_sign_in: false)
|
||||
old_current, new_current = current_sign_in_at, Time.now.utc
|
||||
self.last_sign_in_at = old_current || new_current
|
||||
self.current_sign_in_at = new_current
|
||||
ru = nil
|
||||
ActiveRecord::Base.connected_to(role: :writing) do
|
||||
old_current, new_current = current_sign_in_at, Time.now.utc
|
||||
self.last_sign_in_at = old_current || new_current
|
||||
self.current_sign_in_at = new_current
|
||||
|
||||
old_current, new_current = current_sign_in_ip, request.remote_ip
|
||||
self.last_sign_in_ip = old_current || new_current
|
||||
self.current_sign_in_ip = new_current
|
||||
old_current, new_current = current_sign_in_ip, request.remote_ip
|
||||
self.last_sign_in_ip = old_current || new_current
|
||||
self.current_sign_in_ip = new_current
|
||||
|
||||
if new_sign_in
|
||||
self.sign_in_count ||= 0
|
||||
self.sign_in_count += 1
|
||||
if new_sign_in
|
||||
self.sign_in_count ||= 0
|
||||
self.sign_in_count += 1
|
||||
end
|
||||
|
||||
save(validate: false) unless new_record?
|
||||
ru = prepare_returning_user!
|
||||
end
|
||||
|
||||
save(validate: false) unless new_record?
|
||||
prepare_returning_user!
|
||||
ru
|
||||
end
|
||||
|
||||
def disable_two_factor!
|
||||
@@ -233,6 +241,7 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def web_push_subscription(session)
|
||||
return nil if session.nil?
|
||||
session.web_push_subscription.nil? ? nil : session.web_push_subscription
|
||||
end
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ class FavouriteService < BaseService
|
||||
begin
|
||||
favourite = Favourite.find_by(account: account, status: status)
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
Favourite.connection.stick_to_master!
|
||||
favourite = Favourite.find_by(account: account, status: status)
|
||||
end
|
||||
|
||||
|
||||
@@ -153,7 +153,6 @@ class PostStatusService < BaseService
|
||||
|
||||
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
|
||||
|
||||
@account.media_attachments.connection.stick_to_master!
|
||||
@media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
|
||||
|
||||
hasVideoOrGif = @media.find(&:video?) || @media.find(&:gifv?)
|
||||
|
||||
@@ -14,12 +14,7 @@ class ReblogService < BaseService
|
||||
authorize_with account, reblogged_status, :reblog?
|
||||
|
||||
reblog = nil
|
||||
begin
|
||||
reblog = account.statuses.find_by(reblog: reblogged_status)
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
account.statuses.connection.stick_to_master!
|
||||
reblog = account.statuses.find_by(reblog: reblogged_status)
|
||||
end
|
||||
reblog = account.statuses.find_by(reblog: reblogged_status)
|
||||
|
||||
return reblog unless reblog.nil?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user