core.rb 20.0 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require "active_support/core_ext/hash/indifferent_access"
require "active_support/core_ext/string/filters"
5
require "concurrent/map"
6 7

module ActiveRecord
J
Jon Leighton 已提交
8 9
  module Core
    extend ActiveSupport::Concern
10

11 12
    FILTERED = "[FILTERED]" # :nodoc:

J
Jon Leighton 已提交
13
    included do
J
Jon Leighton 已提交
14 15
      ##
      # :singleton-method:
J
Jon Leighton 已提交
16 17 18 19 20
      #
      # Accepts a logger conforming to the interface of Log4r which is then
      # passed on to any new database connections made and which can be
      # retrieved on both a class and instance level by calling +logger+.
      mattr_accessor :logger, instance_writer: false
21

O
Olivier Lacan 已提交
22 23 24 25 26 27 28
      ##
      # :singleton-method:
      #
      # Specifies if the methods calling database queries should be logged below
      # their relevant queries. Defaults to false.
      mattr_accessor :verbose_query_logs, instance_writer: false, default: false

J
Jon Leighton 已提交
29 30
      ##
      # Contains the database configuration - as is typically stored in config/database.yml -
31
      # as an ActiveRecord::DatabaseConfigurations object.
J
Jon Leighton 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44
      #
      # For example, the following database.yml...
      #
      #   development:
      #     adapter: sqlite3
      #     database: db/development.sqlite3
      #
      #   production:
      #     adapter: sqlite3
      #     database: db/production.sqlite3
      #
      # ...would result in ActiveRecord::Base.configurations to look like this:
      #
45 46 47 48 49 50
      #   #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[
      #     #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
      #       @spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>,
      #     #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="production",
      #       @spec_name="primary", @config={"adapter"=>"mysql2", "database"=>"db/production.sqlite3"}>
      #   ]>
51
      def self.configurations=(config)
52
        @@configurations = ActiveRecord::DatabaseConfigurations.new(config)
53
      end
J
Jon Leighton 已提交
54 55
      self.configurations = {}

56
      # Returns fully resolved ActiveRecord::DatabaseConfigurations object
57 58 59 60
      def self.configurations
        @@configurations
      end

J
Jon Leighton 已提交
61 62 63 64
      ##
      # :singleton-method:
      # 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.
65
      mattr_accessor :default_timezone, instance_writer: false, default: :utc
J
Jon Leighton 已提交
66 67 68 69 70 71 72 73 74

      ##
      # :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.
75
      mattr_accessor :schema_format, instance_writer: false, default: :ruby
J
Jon Leighton 已提交
76

77 78
      ##
      # :singleton-method:
79
      # Specifies if an error should be raised if the query has an order being
80
      # ignored when doing batch queries. Useful in applications where the
81
      # scope being ignored is error-worthy, rather than a warning.
82
      mattr_accessor :error_on_ignored_order, instance_writer: false, default: false
83

84 85 86 87 88
      # :singleton-method:
      # Specify the behavior for unsafe raw query methods. Values are as follows
      #   deprecated - Warnings are logged when unsafe raw SQL is passed to
      #                query methods.
      #   disabled   - Unsafe raw SQL passed to query methods results in
B
Ben Toews 已提交
89 90
      #                UnknownAttributeReference exception.
      mattr_accessor :allow_unsafe_raw_sql, instance_writer: false, default: :deprecated
91

J
Jon Leighton 已提交
92 93 94
      ##
      # :singleton-method:
      # Specify whether or not to use timestamps for migration versions
95
      mattr_accessor :timestamped_migrations, instance_writer: false, default: true
J
Jon Leighton 已提交
96

97 98 99
      ##
      # :singleton-method:
      # Specify whether schema dump should happen at the end of the
100
      # db:migrate rails command. This is true by default, which is useful for the
101 102
      # development environment. This should ideally be false in the production
      # environment where dumping schema is rarely needed.
103
      mattr_accessor :dump_schema_after_migration, instance_writer: false, default: true
104

105 106 107
      ##
      # :singleton-method:
      # Specifies which database schemas to dump when calling db:structure:dump.
108 109 110 111
      # 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.
112
      mattr_accessor :dump_schemas, instance_writer: false, default: :schema_search_path
113

114 115
      ##
      # :singleton-method:
116 117 118 119
      # 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.
120 121
      mattr_accessor :warn_on_records_fetched_greater_than, instance_writer: false

122 123
      mattr_accessor :maintain_test_schema, instance_accessor: false

124 125
      mattr_accessor :belongs_to_required_by_default, instance_accessor: false

126
      class_attribute :default_connection_handler, instance_writer: false
127

128 129 130 131
      ##
      # Specifies columns which don't want to be exposed while calling #inspect
      class_attribute :filter_attributes, instance_writer: false, default: []

132
      def self.connection_handler
133
        ActiveRecord::RuntimeRegistry.connection_handler || default_connection_handler
134 135 136
      end

      def self.connection_handler=(handler)
137
        ActiveRecord::RuntimeRegistry.connection_handler = handler
138 139 140
      end

      self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
141 142
    end

143
    module ClassMethods # :nodoc:
144
      def initialize_find_by_cache # :nodoc:
145
        @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new }
146 147
      end

148
      def inherited(child_class) # :nodoc:
149
        # initialize cache at class definition for thread safety
150 151 152 153
        child_class.initialize_find_by_cache
        super
      end

154
      def find(*ids) # :nodoc:
155 156 157 158
        # We don't have cache keys for this stuff yet
        return super unless ids.length == 1
        return super if block_given? ||
                        primary_key.nil? ||
159
                        scope_attributes? ||
160
                        columns_hash.include?(inheritance_column)
161

162
        id = ids.first
163

164
        return super if StatementCache.unsupported_value?(id)
165 166 167

        key = primary_key

168 169
        statement = cached_find_by_statement(key) { |params|
          where(key => params.bind).limit(1)
170
        }
171

172
        record = statement.execute([id], connection).first
173
        unless record
174 175
          raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
                                   name, primary_key, id)
176 177
        end
        record
178
      rescue ::RangeError
179 180
        raise RecordNotFound.new("Couldn't find #{name} with an out of range value for '#{primary_key}'",
                                 name, primary_key)
181 182
      end

183
      def find_by(*args) # :nodoc:
184
        return super if scope_attributes? || reflect_on_all_aggregations.any?
185 186 187

        hash = args.first

188
        return super if !(Hash === hash) || hash.values.any? { |v|
189
          StatementCache.unsupported_value?(v)
190
        }
191

192 193 194
        # We can't cache Post.find_by(author: david) ...yet
        return super unless hash.keys.all? { |k| columns_hash.has_key?(k.to_s) }

195
        keys = hash.keys
196

197 198
        statement = cached_find_by_statement(keys) { |params|
          wheres = keys.each_with_object({}) { |param, o|
199
            o[param] = params.bind
200
          }
201
          where(wheres).limit(1)
202 203
        }
        begin
204
          statement.execute(hash.values, connection).first
205 206
        rescue TypeError
          raise ActiveRecord::StatementInvalid
207
        rescue ::RangeError
208
          nil
209 210 211
        end
      end

212
      def find_by!(*args) # :nodoc:
213
        find_by(*args) || raise(RecordNotFound.new("Couldn't find #{name}", name))
214 215
      end

216
      def initialize_generated_modules # :nodoc:
217
        generated_association_methods
218 219
      end

220 221 222
      def generated_association_methods
        @generated_association_methods ||= begin
          mod = const_set(:GeneratedAssociationMethods, Module.new)
223
          private_constant :GeneratedAssociationMethods
224
          include mod
225

226 227 228 229 230 231 232 233 234 235
          mod
        end
      end

      # Returns a string like 'Post(id:integer, title:string, body:text)'
      def inspect
        if self == Base
          super
        elsif abstract_class?
          "#{super}(abstract)"
236
        elsif !connected?
A
Arun Agrawal 已提交
237
          "#{super} (call '#{super}.connection' to establish a connection)"
238
        elsif table_exists?
239
          attr_list = attribute_types.map { |name, type| "#{name}: #{type.type}" } * ", "
240 241 242 243 244 245
          "#{super}(#{attr_list})"
        else
          "#{super}(Table doesn't exist)"
        end
      end

246
      # Overwrite the default class equality method to provide support for decorated models.
247 248 249 250
      def ===(object)
        object.is_a?(self)
      end

P
Peter Suschlik 已提交
251
      # Returns an instance of <tt>Arel::Table</tt> loaded with the current table name.
O
Oscar Del Ben 已提交
252 253
      #
      #   class Post < ActiveRecord::Base
254
      #     scope :published_and_commented, -> { published.and(arel_table[:comments_count].gt(0)) }
O
Oscar Del Ben 已提交
255
      #   end
256
      def arel_table # :nodoc:
257
        @arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
258 259
      end

260
      def arel_attribute(name, table = arel_table) # :nodoc:
261 262 263 264
        name = attribute_alias(name) if attribute_alias?(name)
        table[name]
      end

265
      def predicate_builder # :nodoc:
266
        @predicate_builder ||= PredicateBuilder.new(table_metadata)
267 268
      end

269 270 271 272
      def type_caster # :nodoc:
        TypeCaster::Map.new(self)
      end

273 274
      private

A
Akira Matsuda 已提交
275
        def cached_find_by_statement(key, &block)
276
          cache = @find_by_statement_cache[connection.prepared_statements]
277
          cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
278
        end
279

A
Akira Matsuda 已提交
280
        def relation
281
          relation = Relation.create(self)
282

283
          if finder_needs_type_condition? && !ignore_default_scope?
284 285
            relation.where!(type_condition)
            relation.create_with!(inheritance_column.to_s => sti_name)
286 287 288
          else
            relation
          end
289
        end
290

A
Akira Matsuda 已提交
291
        def table_metadata
292 293
          TableMetadata.new(self, arel_table)
        end
294 295 296 297 298 299 300
    end

    # New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
    # attributes but not yet saved (pass a hash with key names matching the associated table column names).
    # In both instances, valid attribute keys are determined by the column names of the associated table --
    # hence you can't have attributes that aren't part of the table columns.
    #
301
    # ==== Example:
302
    #   # Instantiates a single new object
A
AvnerCohen 已提交
303
    #   User.new(first_name: 'Jamie')
304
    def initialize(attributes = nil)
305
      self.class.define_attribute_methods
306
      @attributes = self.class._default_attributes.deep_dup
307 308

      init_internals
309
      initialize_internals_callback
310

311
      assign_attributes(attributes) if attributes
312 313

      yield self if block_given?
314
      _run_initialize_callbacks
315 316
    end

317 318
    # Initialize an empty model object from +coder+. +coder+ should be
    # the result of previously encoding an Active Record model, using
319
    # #encode_with.
320 321 322 323
    #
    #   class Post < ActiveRecord::Base
    #   end
    #
324 325 326 327
    #   old_post = Post.new(title: "hello world")
    #   coder = {}
    #   old_post.encode_with(coder)
    #
328
    #   post = Post.allocate
329
    #   post.init_with(coder)
330 331
    #   post.title # => 'hello world'
    def init_with(coder)
332
      coder = LegacyYamlAdapter.convert(self.class, coder)
333
      @attributes = self.class.yaml_encoder.decode(coder)
334

335 336
      init_internals

337
      @new_record = coder["new_record"]
338

339 340
      self.class.define_attribute_methods

341 342
      yield self if block_given?

343 344
      _run_find_callbacks
      _run_initialize_callbacks
345 346 347

      self
    end
348

349 350 351 352 353 354 355 356 357 358 359 360
    ##
    # Initializer used for instantiating objects that have been read from the
    # database.  +attributes+ should be an attributes object, and unlike the
    # `initialize` method, no assignment calls are made per attribute.
    #
    # :nodoc:
    def init_from_db(attributes)
      init_internals

      @new_record = false
      @attributes = attributes

361 362
      self.class.define_attribute_methods

363 364 365 366 367 368 369 370
      yield self if block_given?

      _run_find_callbacks
      _run_initialize_callbacks

      self
    end

371 372 373 374
    ##
    # :method: clone
    # Identical to Ruby's clone method.  This is a "shallow" copy.  Be warned that your attributes are not copied.
    # That means that modifying attributes of the clone will modify the original, since they will both point to the
V
Vijay Dev 已提交
375
    # same attributes hash. If you need a copy of your attributes hash, please use the #dup method.
376 377 378 379 380 381 382 383 384
    #
    #   user = User.first
    #   new_user = user.clone
    #   user.name               # => "Bob"
    #   new_user.name = "Joe"
    #   user.name               # => "Joe"
    #
    #   user.object_id == new_user.object_id            # => false
    #   user.name.object_id == new_user.name.object_id  # => true
385
    #
386
    #   user.name.object_id == user.dup.name.object_id  # => false
387

388 389
    ##
    # :method: dup
390 391 392 393 394 395
    # Duped objects have no id assigned and are treated as new records. Note
    # that this is a "shallow" copy as it copies the object's attributes
    # only, not its associations. The extent of a "deep" copy is application
    # specific and is therefore left to the application to implement according
    # to its need.
    # The dup method does not preserve the timestamps (created|updated)_(at|on).
396

397
    ##
398
    def initialize_dup(other) # :nodoc:
399
      @attributes = @attributes.deep_dup
400
      @attributes.reset(self.class.primary_key)
401

402
      _run_initialize_callbacks
403

404 405 406 407
      @new_record               = true
      @destroyed                = false
      @_start_transaction_state = {}
      @transaction_state        = nil
408 409 410 411 412 413

      super
    end

    # Populate +coder+ with attributes about this record that should be
    # serialized. The structure of +coder+ defined in this method is
414
    # guaranteed to match the structure of +coder+ passed to the #init_with
415 416 417 418 419 420 421 422
    # method.
    #
    # Example:
    #
    #   class Post < ActiveRecord::Base
    #   end
    #   coder = {}
    #   Post.new.encode_with(coder)
423
    #   coder # => {"attributes" => {"id" => nil, ... }}
424
    def encode_with(coder)
425
      self.class.yaml_encoder.encode(@attributes, coder)
426 427
      coder["new_record"] = new_record?
      coder["active_record_yaml_version"] = 2
428 429 430 431 432 433 434 435 436 437 438 439 440 441
    end

    # Returns true if +comparison_object+ is the same exact object, or +comparison_object+
    # is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
    #
    # Note that new records are different from any other record by definition, unless the
    # other record is the receiver itself. Besides, if you fetch existing records with
    # +select+ and leave the ID out, you're on your own, this predicate will return false.
    #
    # Note also that destroying a record preserves its ID in the model instance, so deleted
    # models are still comparable.
    def ==(comparison_object)
      super ||
        comparison_object.instance_of?(self.class) &&
442
        !id.nil? &&
443 444 445 446 447 448 449
        comparison_object.id == id
    end
    alias :eql? :==

    # Delegates to id in order to allow two records of the same type and id to work with something like:
    #   [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
    def hash
450
      if id
451
        self.class.hash ^ id.hash
452 453 454
      else
        super
      end
455 456
    end

457 458 459
    # Clone and freeze the attributes hash such that associations are still
    # accessible, even on destroyed records, but cloned models will not be
    # frozen.
460
    def freeze
461
      @attributes = @attributes.clone.freeze
462
      self
463 464 465 466
    end

    # Returns +true+ if the attributes hash has been frozen.
    def frozen?
467
      @attributes.frozen?
468 469
    end

470 471 472
    # Allows sort on objects
    def <=>(other_object)
      if other_object.is_a?(self.class)
473
        to_key <=> other_object.to_key
474 475 476 477 478
      else
        super
      end
    end

479 480 481 482 483 484 485 486 487 488 489
    # Returns +true+ if the record is read only. Records loaded through joins with piggy-back
    # attributes will be marked as read only since they cannot be saved.
    def readonly?
      @readonly
    end

    # Marks this record as read only.
    def readonly!
      @readonly = true
    end

490 491 492 493
    def connection_handler
      self.class.connection_handler
    end

494 495
    # Returns the contents of the record as a nicely formatted string.
    def inspect
496
      filter_attributes = self.filter_attributes.map(&:to_s).to_set
497
      # We check defined?(@attributes) not to issue warnings if the object is
498
      # allocated but not initialized.
499
      inspection = if defined?(@attributes) && @attributes
500
        self.class.attribute_names.collect do |name|
501
          if has_attribute?(name)
502 503 504 505 506
            if filter_attributes.include?(name) && !read_attribute(name).nil?
              "#{name}: #{ActiveRecord::Core::FILTERED}"
            else
              "#{name}: #{attribute_for_inspect(name)}"
            end
507
          end
508
        end.compact.join(", ")
509 510 511
      else
        "not initialized"
      end
512

513 514 515
      "#<#{self.class} #{inspection}>"
    end

516
    # Takes a PP and prettily prints this record to it, allowing you to get a nice result from <tt>pp record</tt>
517 518
    # when pp is required.
    def pretty_print(pp)
519
      return super if custom_inspect_method_defined?
520
      filter_attributes = self.filter_attributes.map(&:to_s).to_set
521 522 523
      pp.object_address_group(self) do
        if defined?(@attributes) && @attributes
          column_names = self.class.column_names.select { |name| has_attribute?(name) || new_record? }
524
          pp.seplist(column_names, proc { pp.text "," }) do |column_name|
525
            column_value = read_attribute(column_name)
526
            pp.breakable " "
527 528
            pp.group(1) do
              pp.text column_name
529
              pp.text ":"
530
              pp.breakable
531 532 533 534 535
              if filter_attributes.include?(column_name) && !column_value.nil?
                pp.text ActiveRecord::Core::FILTERED
              else
                pp.pp column_value
              end
536 537 538
            end
          end
        else
539 540
          pp.breakable " "
          pp.text "not initialized"
541 542 543 544
        end
      end
    end

545 546
    # Returns a hash of the given methods with their names as keys and returned values as values.
    def slice(*methods)
547
      Hash[methods.flatten.map! { |method| [method, public_send(method)] }].with_indifferent_access
548 549
    end

550 551
    private

552 553 554 555 556 557 558
      # +Array#flatten+ will call +#to_ary+ (recursively) on each of the elements of
      # the array, and then rescues from the possible +NoMethodError+. If those elements are
      # +ActiveRecord::Base+'s, then this triggers the various +method_missing+'s that we have,
      # which significantly impacts upon performance.
      #
      # So we can avoid the +method_missing+ hit by explicitly defining +#to_ary+ as +nil+ here.
      #
559
      # See also https://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
560
      def to_ary
561 562
        nil
      end
563

564 565 566 567 568 569 570 571 572
      def init_internals
        @readonly                 = false
        @destroyed                = false
        @marked_for_destruction   = false
        @destroyed_by_association = nil
        @new_record               = true
        @_start_transaction_state = {}
        @transaction_state        = nil
      end
573

574 575
      def initialize_internals_callback
      end
576

577 578 579 580
      def thaw
        if frozen?
          @attributes = @attributes.dup
        end
581
      end
582

583 584 585
      def custom_inspect_method_defined?
        self.class.instance_method(:inspect).owner != ActiveRecord::Base.instance_method(:inspect).owner
      end
586 587
  end
end