activerecord 5.1.0 → 5.2.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (261) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +596 -450
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -5
  5. data/examples/performance.rb +2 -0
  6. data/examples/simple.rb +2 -0
  7. data/lib/active_record.rb +11 -4
  8. data/lib/active_record/aggregations.rb +6 -5
  9. data/lib/active_record/association_relation.rb +7 -5
  10. data/lib/active_record/associations.rb +77 -85
  11. data/lib/active_record/associations/alias_tracker.rb +23 -32
  12. data/lib/active_record/associations/association.rb +49 -35
  13. data/lib/active_record/associations/association_scope.rb +55 -55
  14. data/lib/active_record/associations/belongs_to_association.rb +30 -11
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
  16. data/lib/active_record/associations/builder/association.rb +4 -7
  17. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  18. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  20. data/lib/active_record/associations/builder/has_many.rb +2 -0
  21. data/lib/active_record/associations/builder/has_one.rb +2 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  23. data/lib/active_record/associations/collection_association.rb +66 -53
  24. data/lib/active_record/associations/collection_proxy.rb +30 -73
  25. data/lib/active_record/associations/foreign_association.rb +2 -0
  26. data/lib/active_record/associations/has_many_association.rb +13 -2
  27. data/lib/active_record/associations/has_many_through_association.rb +37 -19
  28. data/lib/active_record/associations/has_one_association.rb +14 -1
  29. data/lib/active_record/associations/has_one_through_association.rb +13 -8
  30. data/lib/active_record/associations/join_dependency.rb +52 -96
  31. data/lib/active_record/associations/join_dependency/join_association.rb +22 -75
  32. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  33. data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
  34. data/lib/active_record/associations/preloader.rb +17 -37
  35. data/lib/active_record/associations/preloader/association.rb +53 -92
  36. data/lib/active_record/associations/preloader/through_association.rb +72 -73
  37. data/lib/active_record/associations/singular_association.rb +14 -16
  38. data/lib/active_record/associations/through_association.rb +27 -12
  39. data/lib/active_record/attribute_assignment.rb +2 -5
  40. data/lib/active_record/attribute_decorators.rb +3 -2
  41. data/lib/active_record/attribute_methods.rb +65 -24
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +33 -216
  44. data/lib/active_record/attribute_methods/primary_key.rb +10 -13
  45. data/lib/active_record/attribute_methods/query.rb +2 -0
  46. data/lib/active_record/attribute_methods/read.rb +9 -3
  47. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  49. data/lib/active_record/attribute_methods/write.rb +22 -19
  50. data/lib/active_record/attributes.rb +7 -6
  51. data/lib/active_record/autosave_association.rb +15 -13
  52. data/lib/active_record/base.rb +2 -0
  53. data/lib/active_record/callbacks.rb +12 -6
  54. data/lib/active_record/coders/json.rb +2 -0
  55. data/lib/active_record/coders/yaml_column.rb +2 -0
  56. data/lib/active_record/collection_cache_key.rb +15 -11
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +120 -39
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +192 -37
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +13 -2
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -25
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +65 -7
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +158 -87
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -98
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +126 -189
  70. data/lib/active_record/connection_adapters/column.rb +4 -2
  71. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
  73. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -15
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -23
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +258 -129
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +75 -87
  118. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +90 -96
  127. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  128. data/lib/active_record/connection_handling.rb +4 -2
  129. data/lib/active_record/core.rb +41 -61
  130. data/lib/active_record/counter_cache.rb +20 -15
  131. data/lib/active_record/define_callbacks.rb +5 -3
  132. data/lib/active_record/dynamic_matchers.rb +9 -9
  133. data/lib/active_record/enum.rb +18 -13
  134. data/lib/active_record/errors.rb +60 -15
  135. data/lib/active_record/explain.rb +3 -1
  136. data/lib/active_record/explain_registry.rb +2 -0
  137. data/lib/active_record/explain_subscriber.rb +2 -0
  138. data/lib/active_record/fixture_set/file.rb +2 -0
  139. data/lib/active_record/fixtures.rb +67 -60
  140. data/lib/active_record/gem_version.rb +4 -2
  141. data/lib/active_record/inheritance.rb +49 -19
  142. data/lib/active_record/integration.rb +58 -19
  143. data/lib/active_record/internal_metadata.rb +2 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  145. data/lib/active_record/locking/optimistic.rb +30 -42
  146. data/lib/active_record/locking/pessimistic.rb +10 -7
  147. data/lib/active_record/log_subscriber.rb +46 -4
  148. data/lib/active_record/migration.rb +189 -139
  149. data/lib/active_record/migration/command_recorder.rb +11 -9
  150. data/lib/active_record/migration/compatibility.rb +81 -29
  151. data/lib/active_record/migration/join_table.rb +2 -0
  152. data/lib/active_record/model_schema.rb +74 -58
  153. data/lib/active_record/nested_attributes.rb +18 -6
  154. data/lib/active_record/no_touching.rb +3 -1
  155. data/lib/active_record/null_relation.rb +2 -0
  156. data/lib/active_record/persistence.rb +199 -54
  157. data/lib/active_record/query_cache.rb +8 -10
  158. data/lib/active_record/querying.rb +5 -3
  159. data/lib/active_record/railtie.rb +62 -6
  160. data/lib/active_record/railties/console_sandbox.rb +2 -0
  161. data/lib/active_record/railties/controller_runtime.rb +2 -0
  162. data/lib/active_record/railties/databases.rake +48 -38
  163. data/lib/active_record/readonly_attributes.rb +3 -2
  164. data/lib/active_record/reflection.rb +137 -207
  165. data/lib/active_record/relation.rb +132 -207
  166. data/lib/active_record/relation/batches.rb +32 -17
  167. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  168. data/lib/active_record/relation/calculations.rb +66 -25
  169. data/lib/active_record/relation/delegation.rb +45 -29
  170. data/lib/active_record/relation/finder_methods.rb +76 -85
  171. data/lib/active_record/relation/from_clause.rb +2 -8
  172. data/lib/active_record/relation/merger.rb +53 -23
  173. data/lib/active_record/relation/predicate_builder.rb +60 -79
  174. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  175. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  176. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  177. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  178. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  179. data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
  180. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  181. data/lib/active_record/relation/query_attribute.rb +28 -2
  182. data/lib/active_record/relation/query_methods.rb +135 -103
  183. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  184. data/lib/active_record/relation/spawn_methods.rb +4 -2
  185. data/lib/active_record/relation/where_clause.rb +65 -67
  186. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  187. data/lib/active_record/result.rb +2 -0
  188. data/lib/active_record/runtime_registry.rb +2 -0
  189. data/lib/active_record/sanitization.rb +129 -121
  190. data/lib/active_record/schema.rb +4 -2
  191. data/lib/active_record/schema_dumper.rb +36 -26
  192. data/lib/active_record/schema_migration.rb +2 -0
  193. data/lib/active_record/scoping.rb +12 -10
  194. data/lib/active_record/scoping/default.rb +10 -7
  195. data/lib/active_record/scoping/named.rb +40 -12
  196. data/lib/active_record/secure_token.rb +2 -0
  197. data/lib/active_record/serialization.rb +2 -0
  198. data/lib/active_record/statement_cache.rb +22 -12
  199. data/lib/active_record/store.rb +3 -1
  200. data/lib/active_record/suppressor.rb +2 -0
  201. data/lib/active_record/table_metadata.rb +12 -3
  202. data/lib/active_record/tasks/database_tasks.rb +38 -26
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  206. data/lib/active_record/timestamp.rb +13 -6
  207. data/lib/active_record/touch_later.rb +2 -0
  208. data/lib/active_record/transactions.rb +32 -27
  209. data/lib/active_record/translation.rb +2 -0
  210. data/lib/active_record/type.rb +4 -1
  211. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  212. data/lib/active_record/type/date.rb +2 -0
  213. data/lib/active_record/type/date_time.rb +2 -0
  214. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  215. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  216. data/lib/active_record/type/internal/timezone.rb +2 -0
  217. data/lib/active_record/type/json.rb +30 -0
  218. data/lib/active_record/type/serialized.rb +6 -0
  219. data/lib/active_record/type/text.rb +2 -0
  220. data/lib/active_record/type/time.rb +2 -0
  221. data/lib/active_record/type/type_map.rb +2 -0
  222. data/lib/active_record/type/unsigned_integer.rb +2 -0
  223. data/lib/active_record/type_caster.rb +2 -0
  224. data/lib/active_record/type_caster/connection.rb +2 -0
  225. data/lib/active_record/type_caster/map.rb +3 -1
  226. data/lib/active_record/validations.rb +2 -0
  227. data/lib/active_record/validations/absence.rb +2 -0
  228. data/lib/active_record/validations/associated.rb +2 -0
  229. data/lib/active_record/validations/length.rb +2 -0
  230. data/lib/active_record/validations/presence.rb +2 -0
  231. data/lib/active_record/validations/uniqueness.rb +36 -6
  232. data/lib/active_record/version.rb +2 -0
  233. data/lib/rails/generators/active_record.rb +3 -1
  234. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  235. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  236. data/lib/rails/generators/active_record/migration.rb +2 -0
  237. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  238. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  239. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  241. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  242. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  243. metadata +24 -36
  244. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  245. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  246. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  248. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  249. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  250. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  251. data/lib/active_record/attribute.rb +0 -240
  252. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  253. data/lib/active_record/attribute_mutation_tracker.rb +0 -113
  254. data/lib/active_record/attribute_set.rb +0 -113
  255. data/lib/active_record/attribute_set/builder.rb +0 -124
  256. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  257. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  258. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  259. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  260. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  261. data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  class StatementPool # :nodoc:
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionHandling
3
- RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"] || ENV["RACK_ENV"] }
5
+ RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence }
4
6
  DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }
5
7
 
6
8
  # Establishes the connection to the database. Accepts a hash as input where
@@ -138,6 +140,6 @@ module ActiveRecord
138
140
  end
139
141
 
140
142
  delegate :clear_active_connections!, :clear_reloadable_connections!,
141
- :clear_all_connections!, to: :connection_handler
143
+ :clear_all_connections!, :flush_idle_connections!, to: :connection_handler
142
144
  end
143
145
  end
@@ -1,7 +1,8 @@
1
- require "thread"
1
+ # frozen_string_literal: true
2
+
2
3
  require "active_support/core_ext/hash/indifferent_access"
3
- require "active_support/core_ext/object/duplicable"
4
4
  require "active_support/core_ext/string/filters"
5
+ require "concurrent/map"
5
6
 
6
7
  module ActiveRecord
7
8
  module Core
@@ -16,6 +17,13 @@ module ActiveRecord
16
17
  # retrieved on both a class and instance level by calling +logger+.
17
18
  mattr_accessor :logger, instance_writer: false
18
19
 
20
+ ##
21
+ # :singleton-method:
22
+ #
23
+ # Specifies if the methods calling database queries should be logged below
24
+ # their relevant queries. Defaults to false.
25
+ mattr_accessor :verbose_query_logs, instance_writer: false, default: false
26
+
19
27
  ##
20
28
  # Contains the database configuration - as is typically stored in config/database.yml -
21
29
  # as a Hash.
@@ -56,8 +64,7 @@ module ActiveRecord
56
64
  # :singleton-method:
57
65
  # Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
58
66
  # dates and times from the database. This is set to :utc by default.
59
- mattr_accessor :default_timezone, instance_writer: false
60
- self.default_timezone = :utc
67
+ mattr_accessor :default_timezone, instance_writer: false, default: :utc
61
68
 
62
69
  ##
63
70
  # :singleton-method:
@@ -67,42 +74,27 @@ module ActiveRecord
67
74
  # ActiveRecord::Schema file which can be loaded into any database that
68
75
  # supports migrations. Use :ruby if you want to have different database
69
76
  # adapters for, e.g., your development and test environments.
70
- mattr_accessor :schema_format, instance_writer: false
71
- self.schema_format = :ruby
77
+ mattr_accessor :schema_format, instance_writer: false, default: :ruby
72
78
 
73
79
  ##
74
80
  # :singleton-method:
75
81
  # Specifies if an error should be raised if the query has an order being
76
82
  # ignored when doing batch queries. Useful in applications where the
77
83
  # scope being ignored is error-worthy, rather than a warning.
78
- mattr_accessor :error_on_ignored_order, instance_writer: false
79
- self.error_on_ignored_order = false
80
-
81
- def self.error_on_ignored_order_or_limit
82
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
83
- The flag error_on_ignored_order_or_limit is deprecated. Limits are
84
- now supported. Please use error_on_ignored_order instead.
85
- MSG
86
- error_on_ignored_order
87
- end
88
-
89
- def error_on_ignored_order_or_limit
90
- self.class.error_on_ignored_order_or_limit
91
- end
84
+ mattr_accessor :error_on_ignored_order, instance_writer: false, default: false
92
85
 
93
- def self.error_on_ignored_order_or_limit=(value)
94
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
95
- The flag error_on_ignored_order_or_limit is deprecated. Limits are
96
- now supported. Please use error_on_ignored_order= instead.
97
- MSG
98
- self.error_on_ignored_order = value
99
- end
86
+ # :singleton-method:
87
+ # Specify the behavior for unsafe raw query methods. Values are as follows
88
+ # deprecated - Warnings are logged when unsafe raw SQL is passed to
89
+ # query methods.
90
+ # disabled - Unsafe raw SQL passed to query methods results in
91
+ # UnknownAttributeReference exception.
92
+ mattr_accessor :allow_unsafe_raw_sql, instance_writer: false, default: :deprecated
100
93
 
101
94
  ##
102
95
  # :singleton-method:
103
96
  # Specify whether or not to use timestamps for migration versions
104
- mattr_accessor :timestamped_migrations, instance_writer: false
105
- self.timestamped_migrations = true
97
+ mattr_accessor :timestamped_migrations, instance_writer: false, default: true
106
98
 
107
99
  ##
108
100
  # :singleton-method:
@@ -110,8 +102,7 @@ module ActiveRecord
110
102
  # db:migrate rake task. This is true by default, which is useful for the
111
103
  # development environment. This should ideally be false in the production
112
104
  # environment where dumping schema is rarely needed.
113
- mattr_accessor :dump_schema_after_migration, instance_writer: false
114
- self.dump_schema_after_migration = true
105
+ mattr_accessor :dump_schema_after_migration, instance_writer: false, default: true
115
106
 
116
107
  ##
117
108
  # :singleton-method:
@@ -120,8 +111,7 @@ module ActiveRecord
120
111
  # schema_search_path are dumped. Use :all to dump all schemas regardless
121
112
  # of schema_search_path, or a string of comma separated schemas for a
122
113
  # custom list.
123
- mattr_accessor :dump_schemas, instance_writer: false
124
- self.dump_schemas = :schema_search_path
114
+ mattr_accessor :dump_schemas, instance_writer: false, default: :schema_search_path
125
115
 
126
116
  ##
127
117
  # :singleton-method:
@@ -130,7 +120,6 @@ module ActiveRecord
130
120
  # be used to identify queries which load thousands of records and
131
121
  # potentially cause memory bloat.
132
122
  mattr_accessor :warn_on_records_fetched_greater_than, instance_writer: false
133
- self.warn_on_records_fetched_greater_than = nil
134
123
 
135
124
  mattr_accessor :maintain_test_schema, instance_accessor: false
136
125
 
@@ -149,14 +138,14 @@ module ActiveRecord
149
138
  self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
150
139
  end
151
140
 
152
- module ClassMethods
141
+ module ClassMethods # :nodoc:
153
142
  def allocate
154
143
  define_attribute_methods
155
144
  super
156
145
  end
157
146
 
158
147
  def initialize_find_by_cache # :nodoc:
159
- @find_by_statement_cache = { true => {}.extend(Mutex_m), false => {}.extend(Mutex_m) }
148
+ @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new }
160
149
  end
161
150
 
162
151
  def inherited(child_class) # :nodoc:
@@ -175,8 +164,7 @@ module ActiveRecord
175
164
 
176
165
  id = ids.first
177
166
 
178
- return super if id.kind_of?(Array) ||
179
- id.is_a?(ActiveRecord::Base)
167
+ return super if StatementCache.unsupported_value?(id)
180
168
 
181
169
  key = primary_key
182
170
 
@@ -184,7 +172,7 @@ module ActiveRecord
184
172
  where(key => params.bind).limit(1)
185
173
  }
186
174
 
187
- record = statement.execute([id], self, connection).first
175
+ record = statement.execute([id], connection).first
188
176
  unless record
189
177
  raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
190
178
  name, primary_key, id)
@@ -196,12 +184,13 @@ module ActiveRecord
196
184
  end
197
185
 
198
186
  def find_by(*args) # :nodoc:
199
- return super if scope_attributes? || reflect_on_all_aggregations.any?
187
+ return super if scope_attributes? || reflect_on_all_aggregations.any? ||
188
+ columns_hash.key?(inheritance_column) && base_class != self
200
189
 
201
190
  hash = args.first
202
191
 
203
192
  return super if !(Hash === hash) || hash.values.any? { |v|
204
- v.nil? || Array === v || Hash === v || Relation === v || Base === v
193
+ StatementCache.unsupported_value?(v)
205
194
  }
206
195
 
207
196
  # We can't cache Post.find_by(author: david) ...yet
@@ -216,7 +205,7 @@ module ActiveRecord
216
205
  where(wheres).limit(1)
217
206
  }
218
207
  begin
219
- statement.execute(hash.values, self, connection).first
208
+ statement.execute(hash.values, connection).first
220
209
  rescue TypeError
221
210
  raise ActiveRecord::StatementInvalid
222
211
  rescue ::RangeError
@@ -258,7 +247,7 @@ module ActiveRecord
258
247
  end
259
248
  end
260
249
 
261
- # Overwrite the default class equality method to provide support for association proxies.
250
+ # Overwrite the default class equality method to provide support for decorated models.
262
251
  def ===(object)
263
252
  object.is_a?(self)
264
253
  end
@@ -272,16 +261,6 @@ module ActiveRecord
272
261
  @arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
273
262
  end
274
263
 
275
- # Returns the Arel engine.
276
- def arel_engine # :nodoc:
277
- @arel_engine ||=
278
- if Base == self || connection_handler.retrieve_connection_pool(connection_specification_name)
279
- self
280
- else
281
- superclass.arel_engine
282
- end
283
- end
284
-
285
264
  def arel_attribute(name, table = arel_table) # :nodoc:
286
265
  name = attribute_alias(name) if attribute_alias?(name)
287
266
  table[name]
@@ -299,16 +278,15 @@ module ActiveRecord
299
278
 
300
279
  def cached_find_by_statement(key, &block)
301
280
  cache = @find_by_statement_cache[connection.prepared_statements]
302
- cache[key] || cache.synchronize {
303
- cache[key] ||= StatementCache.create(connection, &block)
304
- }
281
+ cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
305
282
  end
306
283
 
307
284
  def relation
308
- relation = Relation.create(self, arel_table, predicate_builder)
285
+ relation = Relation.create(self)
309
286
 
310
287
  if finder_needs_type_condition? && !ignore_default_scope?
311
- relation.where(type_condition).create_with(inheritance_column.to_s => sti_name)
288
+ relation.where!(type_condition)
289
+ relation.create_with!(inheritance_column.to_s => sti_name)
312
290
  else
313
291
  relation
314
292
  end
@@ -405,8 +383,10 @@ module ActiveRecord
405
383
 
406
384
  _run_initialize_callbacks
407
385
 
408
- @new_record = true
409
- @destroyed = false
386
+ @new_record = true
387
+ @destroyed = false
388
+ @_start_transaction_state = {}
389
+ @transaction_state = nil
410
390
 
411
391
  super
412
392
  end
@@ -548,7 +528,7 @@ module ActiveRecord
548
528
  #
549
529
  # So we can avoid the +method_missing+ hit by explicitly defining +#to_ary+ as +nil+ here.
550
530
  #
551
- # See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
531
+ # See also https://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
552
532
  def to_ary
553
533
  nil
554
534
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # = Active Record Counter Cache
3
5
  module CounterCache
@@ -45,13 +47,17 @@ module ActiveRecord
45
47
  reflection = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
46
48
  counter_name = reflection.counter_cache_column
47
49
 
48
- updates = { counter_name.to_sym => object.send(counter_association).count(:all) }
49
- updates.merge!(touch_updates(touch)) if touch
50
+ updates = { counter_name => object.send(counter_association).count(:all) }
51
+
52
+ if touch
53
+ names = touch if touch != true
54
+ updates.merge!(touch_attributes_with_time(*names))
55
+ end
50
56
 
51
57
  unscoped.where(primary_key => object.id).update_all(updates)
52
58
  end
53
59
 
54
- return true
60
+ true
55
61
  end
56
62
 
57
63
  # A generic "counter updater" implementation, intended primarily to be
@@ -66,8 +72,8 @@ module ActiveRecord
66
72
  # * +counters+ - A Hash containing the names of the fields
67
73
  # to update as keys and the amount to update the field by as values.
68
74
  # * <tt>:touch</tt> option - Touch timestamp columns when updating.
69
- # Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
70
- # touch that column or an array of symbols to touch just those ones.
75
+ # If attribute names are passed, they are updated along with updated_at/on
76
+ # attributes.
71
77
  #
72
78
  # ==== Examples
73
79
  #
@@ -105,11 +111,18 @@ module ActiveRecord
105
111
  end
106
112
 
107
113
  if touch
108
- touch_updates = touch_updates(touch)
114
+ names = touch if touch != true
115
+ touch_updates = touch_attributes_with_time(*names)
109
116
  updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
110
117
  end
111
118
 
112
- unscoped.where(primary_key => id).update_all updates.join(", ")
119
+ if id.is_a?(Relation) && self == id.klass
120
+ relation = id
121
+ else
122
+ relation = unscoped.where!(primary_key => id)
123
+ end
124
+
125
+ relation.update_all updates.join(", ")
113
126
  end
114
127
 
115
128
  # Increment a numeric field by one, via a direct SQL update.
@@ -163,13 +176,6 @@ module ActiveRecord
163
176
  def decrement_counter(counter_name, id, touch: nil)
164
177
  update_counters(id, counter_name => -1, touch: touch)
165
178
  end
166
-
167
- private
168
- def touch_updates(touch)
169
- touch = timestamp_attributes_for_update_in_model if touch == true
170
- touch_time = current_time_from_proper_timezone
171
- Array(touch).map { |column| [ column, touch_time ] }.to_h
172
- end
173
179
  end
174
180
 
175
181
  private
@@ -180,7 +186,6 @@ module ActiveRecord
180
186
  each_counter_cached_associations do |association|
181
187
  if send(association.reflection.name)
182
188
  association.increment_counters
183
- @_after_create_counter_called = true
184
189
  end
185
190
  end
186
191
 
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
- # This module exists because `ActiveRecord::AttributeMethods::Dirty` needs to
3
- # define callbacks, but continue to have its version of `save` be the super
4
- # method of `ActiveRecord::Callbacks`. This will be removed when the removal
4
+ # This module exists because ActiveRecord::AttributeMethods::Dirty needs to
5
+ # define callbacks, but continue to have its version of +save+ be the super
6
+ # method of ActiveRecord::Callbacks. This will be removed when the removal
5
7
  # of deprecated code removes this need.
6
8
  module DefineCallbacks
7
9
  extend ActiveSupport::Concern
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecord
3
4
  module DynamicMatchers #:nodoc:
4
- def respond_to_missing?(name, include_private = false)
5
- if self == Base
6
- super
7
- else
8
- match = Method.match(self, name)
9
- match && match.valid? || super
10
- end
11
- end
12
-
13
5
  private
6
+ def respond_to_missing?(name, _)
7
+ if self == Base
8
+ super
9
+ else
10
+ match = Method.match(self, name)
11
+ match && match.valid? || super
12
+ end
13
+ end
14
14
 
15
15
  def method_missing(name, *arguments, &block)
16
16
  match = Method.match(self, name)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/deep_dup"
2
4
 
3
5
  module ActiveRecord
@@ -95,8 +97,7 @@ module ActiveRecord
95
97
 
96
98
  module Enum
97
99
  def self.extended(base) # :nodoc:
98
- base.class_attribute(:defined_enums, instance_writer: false)
99
- base.defined_enums = {}
100
+ base.class_attribute(:defined_enums, instance_writer: false, default: {})
100
101
  end
101
102
 
102
103
  def inherited(base) # :nodoc:
@@ -154,11 +155,12 @@ module ActiveRecord
154
155
  definitions.each do |name, values|
155
156
  # statuses = { }
156
157
  enum_values = ActiveSupport::HashWithIndifferentAccess.new
157
- name = name.to_sym
158
+ name = name.to_s
158
159
 
159
160
  # def self.statuses() statuses end
160
- detect_enum_conflict!(name, name.to_s.pluralize, true)
161
- klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values }
161
+ detect_enum_conflict!(name, name.pluralize, true)
162
+ singleton_class.send(:define_method, name.pluralize) { enum_values }
163
+ defined_enums[name] = enum_values
162
164
 
163
165
  detect_enum_conflict!(name, name)
164
166
  detect_enum_conflict!(name, "#{name}=")
@@ -170,7 +172,7 @@ module ActiveRecord
170
172
 
171
173
  _enum_methods_module.module_eval do
172
174
  pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
173
- pairs.each do |value, i|
175
+ pairs.each do |label, value|
174
176
  if enum_prefix == true
175
177
  prefix = "#{name}_"
176
178
  elsif enum_prefix
@@ -182,23 +184,24 @@ module ActiveRecord
182
184
  suffix = "_#{enum_suffix}"
183
185
  end
184
186
 
185
- value_method_name = "#{prefix}#{value}#{suffix}"
186
- enum_values[value] = i
187
+ value_method_name = "#{prefix}#{label}#{suffix}"
188
+ enum_values[label] = value
189
+ label = label.to_s
187
190
 
188
- # def active?() status == 0 end
191
+ # def active?() status == "active" end
189
192
  klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
190
- define_method("#{value_method_name}?") { self[attr] == value.to_s }
193
+ define_method("#{value_method_name}?") { self[attr] == label }
191
194
 
192
- # def active!() update! status: :active end
195
+ # def active!() update!(status: 0) end
193
196
  klass.send(:detect_enum_conflict!, name, "#{value_method_name}!")
194
197
  define_method("#{value_method_name}!") { update!(attr => value) }
195
198
 
196
- # scope :active, -> { where status: 0 }
199
+ # scope :active, -> { where(status: 0) }
197
200
  klass.send(:detect_enum_conflict!, name, value_method_name, true)
198
201
  klass.scope value_method_name, -> { where(attr => value) }
199
202
  end
200
203
  end
201
- defined_enums[name.to_s] = enum_values
204
+ enum_values.freeze
202
205
  end
203
206
  end
204
207
 
@@ -219,6 +222,8 @@ module ActiveRecord
219
222
  def detect_enum_conflict!(enum_name, method_name, klass_method = false)
220
223
  if klass_method && dangerous_class_method?(method_name)
221
224
  raise_conflict_error(enum_name, method_name, type: "class")
225
+ elsif klass_method && method_defined_within?(method_name, Relation)
226
+ raise_conflict_error(enum_name, method_name, type: "class", source: Relation.name)
222
227
  elsif !klass_method && dangerous_attribute_method?(method_name)
223
228
  raise_conflict_error(enum_name, method_name)
224
229
  elsif !klass_method && method_defined_within?(method_name, _enum_methods_module, Module)