2021-03-30 06:07:28 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-28 03:51:01 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2011 - present Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-03-10 05:07:30 +08:00
|
|
|
class LoadAccount
|
2021-04-16 01:59:00 +08:00
|
|
|
class << self
|
|
|
|
def check_schema_cache
|
|
|
|
return if @schema_cache_loaded
|
|
|
|
|
|
|
|
@schema_cache_loaded = true
|
|
|
|
MultiCache.fetch("schema_cache", expires_in: 1.week) do
|
|
|
|
conn = ActiveRecord::Base.connection
|
2024-02-02 05:00:46 +08:00
|
|
|
if $canvas_rails == "7.1"
|
|
|
|
reflection = ActiveRecord::Base.connection_pool.schema_reflection
|
|
|
|
cache = reflection.send(:empty_cache)
|
|
|
|
cache.add_all(conn)
|
|
|
|
reflection.set_schema_cache(cache)
|
|
|
|
cache
|
|
|
|
else
|
|
|
|
conn.schema_cache.clear!
|
|
|
|
conn.data_sources.each { |table| conn.schema_cache.add(table) }
|
|
|
|
conn.schema_cache
|
|
|
|
end
|
2021-04-16 01:59:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def schema_cache_loaded!
|
|
|
|
@schema_cache_loaded = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-10 05:07:30 +08:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2021-04-30 06:35:02 +08:00
|
|
|
self.class.check_schema_cache
|
2013-03-21 04:30:20 +08:00
|
|
|
domain_root_account = ::LoadAccount.default_domain_root_account
|
2011-03-10 05:07:30 +08:00
|
|
|
configure_for_root_account(domain_root_account)
|
|
|
|
|
|
|
|
env["canvas.domain_root_account"] = domain_root_account
|
|
|
|
@app.call(env)
|
2021-04-14 03:10:17 +08:00
|
|
|
ensure
|
|
|
|
clear_caches
|
2011-03-10 05:07:30 +08:00
|
|
|
end
|
|
|
|
|
2011-12-13 01:17:35 +08:00
|
|
|
def self.default_domain_root_account
|
|
|
|
Account.default
|
|
|
|
end
|
|
|
|
|
2014-08-26 22:53:26 +08:00
|
|
|
def clear_caches
|
2017-09-14 03:28:05 +08:00
|
|
|
Canvas::Reloader.reload! if Canvas::Reloader.pending_reload
|
2015-09-22 05:26:53 +08:00
|
|
|
::Account.clear_special_account_cache!(::LoadAccount.force_special_account_reload)
|
2014-09-29 17:04:16 +08:00
|
|
|
::LoadAccount.clear_shard_cache
|
2020-08-25 00:19:02 +08:00
|
|
|
Account.current_domain_root_account = nil
|
2014-08-26 22:53:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_shard_cache
|
2023-12-08 03:55:41 +08:00
|
|
|
@timed_cache ||= TimedCache.new(-> { 60.seconds.ago }) do
|
2014-08-26 22:53:26 +08:00
|
|
|
Shard.clear_cache
|
|
|
|
end
|
|
|
|
@timed_cache.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
# this should really only be set to true in spec runs
|
|
|
|
cattr_accessor :force_special_account_reload
|
|
|
|
self.force_special_account_reload = false
|
|
|
|
|
2011-03-10 05:07:30 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def configure_for_root_account(domain_root_account)
|
2018-03-03 05:37:50 +08:00
|
|
|
Attachment.current_root_account = domain_root_account
|
2020-08-25 00:19:02 +08:00
|
|
|
Account.current_domain_root_account = domain_root_account
|
2011-03-10 05:07:30 +08:00
|
|
|
end
|
|
|
|
end
|