rails--rails/activerecord/lib/active_record.rb

367 lines
12 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
#--
2022-01-01 06:22:15 +00:00
# Copyright (c) 2004-2022 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
require "active_support"
require "active_support/rails"
require "active_model"
require "arel"
require "yaml"
2009-10-15 02:05:06 +00:00
require "active_record/version"
require "active_model/attribute_set"
Stop autoloading AbstractAdapter prematurely In 7254d23764f7abe8023f3daeb07d99ea1c8e777a, an autoload for `ConnectionAdapters::AbstractAdapter` was added to `active_record.rb`. Later in d6b923adbdfc9a4df20132f741bbfb43db12113c, a manual require for that class was added to `active_record/base.rb` as some constants under `ConnectionAdapters` weren't defined until `AbstractAdapter` was loaded. In 1efd88283ef68d912df215125951a87526768a51, the require was removed and replaced with an autoload in `active_record.rb`, above the previous one. Because the first autoload was for the `ConnectionAdapters` constant and the second one tried to create it, the autoload would fire immediately. Rather than fixing the autoload problem, the require had effectively just been moved from `active_record/base.rb` to `active_record.rb`. Instead of defining autoloads for constants under `ConnectionAdapters` in the `abstract_adapter.rb` file, we can create a separate, autoloaded `connection_adapters.rb` file for this purpose. To avoid a "circular require considered harmful" warning from Ruby, we have to fix the module nesting in `schema_creation.rb`, as a followup to e4108fc619e0f1c28cdec6049d31f2db01d56dfd. `AbstractAdapter` loads many other dependencies, so making it autoload properly has a noticeable impact on the load time of `active_record.rb`. Benchmark: $ cat test.rb require "bundler/setup" before = ObjectSpace.each_object(Module).count start = Process.clock_gettime(Process::CLOCK_MONOTONIC) require "active_record" finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) after = ObjectSpace.each_object(Module).count puts "took #{finish - start} and created #{after - before} modules" Before: $ ruby test.rb took 0.47532399999909103 and created 901 modules After: $ ruby test.rb took 0.3299509999342263 and created 608 modules
2019-09-12 22:02:35 +00:00
require "active_record/errors"
2008-11-24 17:14:24 +00:00
module ActiveRecord
extend ActiveSupport::Autoload
autoload :Base
autoload :Callbacks
autoload :ConnectionHandling
2021-11-18 16:42:35 +00:00
autoload :Core
2013-04-05 06:21:48 +00:00
autoload :CounterCache
autoload :DelegatedType
2021-11-18 16:42:35 +00:00
autoload :DestroyAssociationAsyncJob
autoload :DynamicMatchers
autoload :Encryption
autoload :Enum
autoload :Explain
autoload :Inheritance
autoload :Integration
2021-11-18 16:42:35 +00:00
autoload :InternalMetadata
autoload :Migration
autoload :Migrator, "active_record/migration"
autoload :ModelSchema
autoload :NestedAttributes
autoload :NoTouching
autoload :Persistence
autoload :QueryCache
autoload :Querying
autoload :QueryLogs
autoload :ReadonlyAttributes
autoload :RecordInvalid, "active_record/validations"
autoload :Reflection
autoload :RuntimeRegistry
autoload :Sanitization
autoload :Schema
autoload :SchemaDumper
autoload :SchemaMigration
autoload :Scoping
autoload :SecurePassword
2021-11-18 16:42:35 +00:00
autoload :SecureToken
autoload :Serialization
autoload :SignedId
2021-11-18 16:42:35 +00:00
autoload :Store
2015-02-18 22:55:48 +00:00
autoload :Suppressor
2021-11-18 16:42:35 +00:00
autoload :TestDatabases
2021-11-18 18:39:16 +00:00
autoload :TestFixtures, "active_record/fixtures"
autoload :Timestamp
2021-11-18 16:42:35 +00:00
autoload :TouchLater
autoload :Transactions
autoload :Translation
autoload :Validations
2009-12-22 23:27:37 +00:00
eager_autoload do
autoload :Aggregations
2021-11-18 16:42:35 +00:00
autoload :AssociationRelation
2009-12-22 23:27:37 +00:00
autoload :Associations
2021-11-18 16:42:35 +00:00
autoload :AsynchronousQueriesTracker
autoload :AttributeAssignment
2013-04-05 06:21:48 +00:00
autoload :AttributeMethods
2009-12-22 23:27:37 +00:00
autoload :AutosaveAssociation
2021-11-18 16:42:35 +00:00
autoload :ConnectionAdapters
Add option to skip joins for associations. In a multiple database application, associations can't join across databases. When set, this option tells Rails to make 2 or more queries rather than using joins for associations. Set the option on a has many through association: ```ruby class Dog has_many :treats, through: :humans, disable_joins: true has_many :humans end ``` Then instead of generating join SQL, two queries are used for `@dog.treats`: ``` SELECT "humans"."id" FROM "humans" WHERE "humans"."dog_id" = ? [["dog_id", 1]] SELECT "treats".* FROM "treats" WHERE "treats"."human_id" IN (?, ?, ?) [["human_id", 1], ["human_id", 2], ["human_id", 3]] ``` This code is extracted from a gem we use internally at GitHub which means the implementation here is used in production daily and isn't experimental. I often get the question "why can't Rails do this automatically" so I figured I'd include the answer in the commit. Rails can't do this automatically because associations are lazily loaded. `dog.treats` needs to load `Dog`, then `Human` and then `Treats`. When `dog.treats` is called Rails pre-generates the SQL that will be run and puts that information into a reflection object. Because the SQL parts are pre-generated, as soon as `dog.treats` is loaded it's too late to skip a join. The join is already available on the object and that join is what's run to load `treats` from `dog` through `humans`. I think the only way to avoid setting an option on the association is to rewrite how and when the SQL is generated for associations which is a large undertaking. Basically the way that Active Record associations are designed, it is currently impossible to have Rails figure out to not join (loading the association will cause the join to occur, and that join will raise an error if the models don't live in the same db). The original implementation was written by me and Aaron. Lee helped port over tests, and I refactored the extraction to better match Rails style. Co-authored-by: Lee Quarella <leequarella@gmail.com> Co-authored-by: Aaron Patterson <aaron@rubyonrails.org>
2020-11-03 18:01:41 +00:00
autoload :DisableJoinsAssociationRelation
2021-11-18 16:42:35 +00:00
autoload :FutureResult
autoload :LegacyYamlAdapter
autoload :NullRelation
2021-11-18 16:42:35 +00:00
autoload :Relation
autoload :Result
autoload :StatementCache
autoload :TableMetadata
autoload :Type
autoload_under "relation" do
autoload :QueryMethods
autoload :FinderMethods
autoload :Calculations
autoload :PredicateBuilder
autoload :SpawnMethods
2010-02-12 16:53:51 +00:00
autoload :Batches
autoload :Delegation
end
2009-12-22 23:27:37 +00:00
end
module Coders
autoload :JSON, "active_record/coders/json"
2021-11-18 16:42:35 +00:00
autoload :YAMLColumn, "active_record/coders/yaml_column"
end
2009-07-24 05:25:27 +00:00
module AttributeMethods
extend ActiveSupport::Autoload
2009-12-22 23:27:37 +00:00
eager_autoload do
autoload :BeforeTypeCast
autoload :Dirty
autoload :PrimaryKey
autoload :Query
autoload :Read
2021-11-18 16:42:35 +00:00
autoload :Serialization
2009-12-22 23:27:37 +00:00
autoload :TimeZoneConversion
autoload :Write
end
2009-07-24 05:25:27 +00:00
end
2008-11-24 17:14:24 +00:00
module Locking
extend ActiveSupport::Autoload
2009-12-22 23:27:37 +00:00
eager_autoload do
autoload :Optimistic
autoload :Pessimistic
end
2008-11-24 17:14:24 +00:00
end
module Scoping
extend ActiveSupport::Autoload
eager_autoload do
autoload :Default
2021-11-18 16:42:35 +00:00
autoload :Named
end
end
Adds basic automatic database switching to Rails The following PR adds behavior to Rails to allow an application to automatically switch it's connection from the primary to the replica. A request will be sent to the replica if: * The request is a read request (`GET` or `HEAD`) * AND It's been 2 seconds since the last write to the database (because we don't want to send a user to a replica if the write hasn't made it to the replica yet) A request will be sent to the primary if: * It's not a GET/HEAD request (ie is a POST, PATCH, etc) * Has been less than 2 seconds since the last write to the database The implementation that decides when to switch reads (the 2 seconds) is "safe" to use in production but not recommended without adequate testing with your infrastructure. At GitHub in addition to the a 5 second delay we have a curcuit breaker that checks the replication delay and will send the query to a replica before the 5 seconds has passed. This is specific to our application and therefore not something Rails should be doing for you. You'll need to test and implement more robust handling of when to switch based on your infrastructure. The auto switcher in Rails is meant to be a basic implementation / API that acts as a guide for how to implement autoswitching. The impementation here is meant to be strict enough that you know how to implement your own resolver and operations classes but flexible enough that we're not telling you how to do it. The middleware is not included automatically and can be installed in your application with the classes you want to use for the resolver and operations passed in. If you don't pass any classes into the middleware the Rails default Resolver and Session classes will be used. The Resolver decides what parameters define when to switch, Operations sets timestamps for the Resolver to read from. For example you may want to use cookies instead of a session so you'd implement a Resolver::Cookies class and pass that into the middleware via configuration options. ``` config.active_record.database_selector = { delay: 2.seconds } config.active_record.database_resolver = MyResolver config.active_record.database_operations = MyResolver::MyCookies ``` Your classes can inherit from the existing classes and reimplment the methods (or implement more methods) that you need to do the switching. You only need to implement methods that you want to change. For example if you wanted to set the session token for the last read from a replica you would reimplement the `read_from_replica` method in your resolver class and implement a method that updates a new timestamp in your operations class.
2019-01-17 18:33:48 +00:00
module Middleware
extend ActiveSupport::Autoload
autoload :DatabaseSelector
autoload :ShardSelector
Adds basic automatic database switching to Rails The following PR adds behavior to Rails to allow an application to automatically switch it's connection from the primary to the replica. A request will be sent to the replica if: * The request is a read request (`GET` or `HEAD`) * AND It's been 2 seconds since the last write to the database (because we don't want to send a user to a replica if the write hasn't made it to the replica yet) A request will be sent to the primary if: * It's not a GET/HEAD request (ie is a POST, PATCH, etc) * Has been less than 2 seconds since the last write to the database The implementation that decides when to switch reads (the 2 seconds) is "safe" to use in production but not recommended without adequate testing with your infrastructure. At GitHub in addition to the a 5 second delay we have a curcuit breaker that checks the replication delay and will send the query to a replica before the 5 seconds has passed. This is specific to our application and therefore not something Rails should be doing for you. You'll need to test and implement more robust handling of when to switch based on your infrastructure. The auto switcher in Rails is meant to be a basic implementation / API that acts as a guide for how to implement autoswitching. The impementation here is meant to be strict enough that you know how to implement your own resolver and operations classes but flexible enough that we're not telling you how to do it. The middleware is not included automatically and can be installed in your application with the classes you want to use for the resolver and operations passed in. If you don't pass any classes into the middleware the Rails default Resolver and Session classes will be used. The Resolver decides what parameters define when to switch, Operations sets timestamps for the Resolver to read from. For example you may want to use cookies instead of a session so you'd implement a Resolver::Cookies class and pass that into the middleware via configuration options. ``` config.active_record.database_selector = { delay: 2.seconds } config.active_record.database_resolver = MyResolver config.active_record.database_operations = MyResolver::MyCookies ``` Your classes can inherit from the existing classes and reimplment the methods (or implement more methods) that you need to do the switching. You only need to implement methods that you want to change. For example if you wanted to set the session token for the last read from a replica you would reimplement the `read_from_replica` method in your resolver class and implement a method that updates a new timestamp in your operations class.
2019-01-17 18:33:48 +00:00
end
module Tasks
extend ActiveSupport::Autoload
autoload :DatabaseTasks
autoload :MySQLDatabaseTasks, "active_record/tasks/mysql_database_tasks"
2021-11-18 16:42:35 +00:00
autoload :PostgreSQLDatabaseTasks, "active_record/tasks/postgresql_database_tasks"
autoload :SQLiteDatabaseTasks, "active_record/tasks/sqlite_database_tasks"
end
# Lazily load the schema cache. This option will load the schema cache
# when a connection is established rather than on boot. If set,
# +config.active_record.use_schema_cache_dump+ will be set to false.
singleton_class.attr_accessor :lazily_load_schema_cache
self.lazily_load_schema_cache = false
# A list of tables or regex's to match tables to ignore when
# dumping the schema cache. For example if this is set to +[/^_/]+
# the schema cache will not dump tables named with an underscore.
singleton_class.attr_accessor :schema_cache_ignored_tables
self.schema_cache_ignored_tables = []
singleton_class.attr_accessor :legacy_connection_handling
self.legacy_connection_handling = true
singleton_class.attr_reader :default_timezone
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
# dates and times from the database. This is set to :utc by default.
def self.default_timezone=(default_timezone)
unless %i(local utc).include?(default_timezone)
raise ArgumentError, "default_timezone must be either :utc (default) or :local."
end
@default_timezone = default_timezone
end
self.default_timezone = :utc
singleton_class.attr_accessor :writing_role
self.writing_role = :writing
singleton_class.attr_accessor :reading_role
self.reading_role = :reading
# Sets the async_query_executor for an application. By default the thread pool executor
# set to +nil+ which will not run queries in the background. Applications must configure
# a thread pool executor to use this feature. Options are:
#
# * nil - Does not initialize a thread pool executor. Any async calls will be
# run in the foreground.
# * :global_thread_pool - Initializes a single +Concurrent::ThreadPoolExecutor+
# that uses the +async_query_concurrency+ for the +max_threads+ value.
# * :multi_thread_pool - Initializes a +Concurrent::ThreadPoolExecutor+ for each
# database connection. The initializer values are defined in the configuration hash.
singleton_class.attr_accessor :async_query_executor
self.async_query_executor = nil
def self.global_thread_pool_async_query_executor # :nodoc:
concurrency = global_executor_concurrency || 4
@global_thread_pool_async_query_executor ||= Concurrent::ThreadPoolExecutor.new(
min_threads: 0,
max_threads: concurrency,
max_queue: concurrency * 4,
fallback_policy: :caller_runs
)
end
# Set the +global_executor_concurrency+. This configuration value can only be used
# with the global thread pool async query executor.
def self.global_executor_concurrency=(global_executor_concurrency)
if self.async_query_executor.nil? || self.async_query_executor == :multi_thread_pool
raise ArgumentError, "`global_executor_concurrency` cannot be set when using the executor is nil or set to multi_thead_pool. For multiple thread pools, please set the concurrency in your database configuration."
end
@global_executor_concurrency = global_executor_concurrency
end
def self.global_executor_concurrency # :nodoc:
@global_executor_concurrency ||= nil
end
singleton_class.attr_accessor :index_nested_attribute_errors
self.index_nested_attribute_errors = false
##
# :singleton-method:
#
# Specifies if the methods calling database queries should be logged below
# their relevant queries. Defaults to false.
singleton_class.attr_accessor :verbose_query_logs
self.verbose_query_logs = false
##
# :singleton-method:
#
# Specifies the names of the queues used by background jobs.
singleton_class.attr_accessor :queues
self.queues = {}
singleton_class.attr_accessor :maintain_test_schema
self.maintain_test_schema = nil
##
# :singleton-method:
# Specify a threshold for the size of query result sets. If the number of
# records in the set exceeds the threshold, a warning is logged. This can
# be used to identify queries which load thousands of records and
# potentially cause memory bloat.
singleton_class.attr_accessor :warn_on_records_fetched_greater_than
self.warn_on_records_fetched_greater_than = false
singleton_class.attr_accessor :application_record_class
self.application_record_class = nil
##
# :singleton-method:
# Set the application to log or raise when an association violates strict loading.
# Defaults to :raise.
singleton_class.attr_accessor :action_on_strict_loading_violation
self.action_on_strict_loading_violation = :raise
##
# :singleton-method:
# Specifies the format to use when dumping the database schema with Rails'
# Rakefile. If :sql, the schema is dumped as (potentially database-
# specific) SQL statements. If :ruby, the schema is dumped as an
# ActiveRecord::Schema file which can be loaded into any database that
# supports migrations. Use :ruby if you want to have different database
# adapters for, e.g., your development and test environments.
singleton_class.attr_accessor :schema_format
self.schema_format = :ruby
##
# :singleton-method:
# Specifies if an error should be raised if the query has an order being
# ignored when doing batch queries. Useful in applications where the
# scope being ignored is error-worthy, rather than a warning.
singleton_class.attr_accessor :error_on_ignored_order
self.error_on_ignored_order = false
##
# :singleton-method:
# Specify whether or not to use timestamps for migration versions
singleton_class.attr_accessor :timestamped_migrations
self.timestamped_migrations = true
##
# :singleton-method:
# Specify whether schema dump should happen at the end of the
# bin/rails db:migrate command. This is true by default, which is useful for the
# development environment. This should ideally be false in the production
# environment where dumping schema is rarely needed.
singleton_class.attr_accessor :dump_schema_after_migration
self.dump_schema_after_migration = true
##
# :singleton-method:
# Specifies which database schemas to dump when calling db:schema:dump.
# If the value is :schema_search_path (the default), any schemas listed in
# schema_search_path are dumped. Use :all to dump all schemas regardless
# of schema_search_path, or a string of comma separated schemas for a
# custom list.
singleton_class.attr_accessor :dump_schemas
self.dump_schemas = :schema_search_path
##
# :singleton-method:
# Show a warning when Rails couldn't parse your database.yml
# for multiple databases.
singleton_class.attr_accessor :suppress_multiple_database_warning
self.suppress_multiple_database_warning = false
##
# :singleton-method:
# If true, Rails will verify all foreign keys in the database after loading fixtures.
# An error will be raised if there are any foreign key violations, indicating incorrectly
# written fixtures.
# Supported by PostgreSQL and SQLite.
singleton_class.attr_accessor :verify_foreign_keys_for_fixtures
self.verify_foreign_keys_for_fixtures = false
singleton_class.attr_accessor :query_transformers
self.query_transformers = []
def self.eager_load!
super
ActiveRecord::Locking.eager_load!
ActiveRecord::Scoping.eager_load!
ActiveRecord::Associations.eager_load!
ActiveRecord::AttributeMethods.eager_load!
ActiveRecord::ConnectionAdapters.eager_load!
ActiveRecord::Encryption.eager_load!
end
end
ActiveSupport.on_load(:active_record) do
Arel::Table.engine = self
2008-11-24 17:14:24 +00:00
end
2008-06-19 14:25:27 +00:00
2012-05-10 23:48:23 +00:00
ActiveSupport.on_load(:i18n) do
I18n.load_path << File.expand_path("active_record/locale/en.yml", __dir__)
2012-05-10 23:48:23 +00:00
end
YAML.load_tags["!ruby/object:ActiveRecord::AttributeSet"] = "ActiveModel::AttributeSet"
YAML.load_tags["!ruby/object:ActiveRecord::Attribute::FromDatabase"] = "ActiveModel::Attribute::FromDatabase"
YAML.load_tags["!ruby/object:ActiveRecord::LazyAttributeHash"] = "ActiveModel::LazyAttributeHash"
YAML.load_tags["!ruby/object:ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlString"] = "ActiveRecord::Type::String"