activerecord 7.0.4.3 → 7.1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1657 -1274
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +18 -18
  5. data/lib/active_record/aggregations.rb +16 -13
  6. data/lib/active_record/association_relation.rb +1 -1
  7. data/lib/active_record/associations/association.rb +20 -4
  8. data/lib/active_record/associations/association_scope.rb +16 -9
  9. data/lib/active_record/associations/belongs_to_association.rb +14 -6
  10. data/lib/active_record/associations/builder/association.rb +3 -3
  11. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
  13. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  14. data/lib/active_record/associations/collection_association.rb +16 -10
  15. data/lib/active_record/associations/collection_proxy.rb +20 -10
  16. data/lib/active_record/associations/foreign_association.rb +10 -3
  17. data/lib/active_record/associations/has_many_association.rb +20 -13
  18. data/lib/active_record/associations/has_many_through_association.rb +10 -6
  19. data/lib/active_record/associations/has_one_association.rb +10 -3
  20. data/lib/active_record/associations/join_dependency.rb +10 -8
  21. data/lib/active_record/associations/preloader/association.rb +31 -7
  22. data/lib/active_record/associations/preloader/through_association.rb +1 -1
  23. data/lib/active_record/associations/preloader.rb +13 -10
  24. data/lib/active_record/associations/singular_association.rb +1 -1
  25. data/lib/active_record/associations/through_association.rb +22 -11
  26. data/lib/active_record/associations.rb +327 -222
  27. data/lib/active_record/attribute_assignment.rb +0 -2
  28. data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
  29. data/lib/active_record/attribute_methods/dirty.rb +52 -34
  30. data/lib/active_record/attribute_methods/primary_key.rb +76 -24
  31. data/lib/active_record/attribute_methods/query.rb +28 -16
  32. data/lib/active_record/attribute_methods/read.rb +18 -5
  33. data/lib/active_record/attribute_methods/serialization.rb +150 -31
  34. data/lib/active_record/attribute_methods/time_zone_conversion.rb +0 -4
  35. data/lib/active_record/attribute_methods/write.rb +3 -3
  36. data/lib/active_record/attribute_methods.rb +108 -26
  37. data/lib/active_record/attributes.rb +3 -3
  38. data/lib/active_record/autosave_association.rb +55 -9
  39. data/lib/active_record/base.rb +7 -2
  40. data/lib/active_record/callbacks.rb +16 -32
  41. data/lib/active_record/coders/column_serializer.rb +61 -0
  42. data/lib/active_record/coders/json.rb +1 -1
  43. data/lib/active_record/coders/yaml_column.rb +70 -42
  44. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +163 -88
  45. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
  46. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
  47. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +74 -51
  48. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
  49. data/lib/active_record/connection_adapters/abstract/database_statements.rb +129 -31
  50. data/lib/active_record/connection_adapters/abstract/query_cache.rb +62 -23
  51. data/lib/active_record/connection_adapters/abstract/quoting.rb +41 -6
  52. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  53. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
  54. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +155 -25
  55. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +290 -124
  56. data/lib/active_record/connection_adapters/abstract/transaction.rb +287 -58
  57. data/lib/active_record/connection_adapters/abstract_adapter.rb +509 -102
  58. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +217 -112
  59. data/lib/active_record/connection_adapters/column.rb +9 -0
  60. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  61. data/lib/active_record/connection_adapters/mysql/database_statements.rb +23 -144
  62. data/lib/active_record/connection_adapters/mysql/quoting.rb +29 -14
  63. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  64. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
  65. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
  66. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +18 -13
  67. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
  68. data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
  69. data/lib/active_record/connection_adapters/pool_config.rb +14 -5
  70. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  71. data/lib/active_record/connection_adapters/postgresql/column.rb +16 -3
  72. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +75 -45
  73. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  74. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  75. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
  76. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +2 -2
  77. data/lib/active_record/connection_adapters/postgresql/quoting.rb +15 -8
  78. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
  79. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
  80. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
  81. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
  82. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +362 -60
  83. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
  84. data/lib/active_record/connection_adapters/postgresql_adapter.rb +354 -193
  85. data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
  86. data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
  87. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +52 -39
  88. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +9 -5
  89. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
  90. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +28 -9
  91. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +211 -83
  92. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  93. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  94. data/lib/active_record/connection_adapters/trilogy_adapter.rb +262 -0
  95. data/lib/active_record/connection_adapters.rb +3 -1
  96. data/lib/active_record/connection_handling.rb +72 -95
  97. data/lib/active_record/core.rb +175 -153
  98. data/lib/active_record/counter_cache.rb +46 -25
  99. data/lib/active_record/database_configurations/database_config.rb +9 -3
  100. data/lib/active_record/database_configurations/hash_config.rb +22 -12
  101. data/lib/active_record/database_configurations/url_config.rb +17 -11
  102. data/lib/active_record/database_configurations.rb +86 -33
  103. data/lib/active_record/delegated_type.rb +9 -4
  104. data/lib/active_record/deprecator.rb +7 -0
  105. data/lib/active_record/destroy_association_async_job.rb +2 -0
  106. data/lib/active_record/disable_joins_association_relation.rb +1 -1
  107. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  108. data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
  109. data/lib/active_record/encryption/config.rb +25 -1
  110. data/lib/active_record/encryption/configurable.rb +12 -19
  111. data/lib/active_record/encryption/context.rb +10 -3
  112. data/lib/active_record/encryption/contexts.rb +5 -1
  113. data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
  114. data/lib/active_record/encryption/encryptable_record.rb +42 -18
  115. data/lib/active_record/encryption/encrypted_attribute_type.rb +21 -6
  116. data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
  117. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
  118. data/lib/active_record/encryption/key_generator.rb +12 -1
  119. data/lib/active_record/encryption/message_serializer.rb +2 -0
  120. data/lib/active_record/encryption/properties.rb +3 -3
  121. data/lib/active_record/encryption/scheme.rb +19 -22
  122. data/lib/active_record/encryption.rb +1 -0
  123. data/lib/active_record/enum.rb +112 -28
  124. data/lib/active_record/errors.rb +112 -18
  125. data/lib/active_record/explain.rb +23 -3
  126. data/lib/active_record/explain_subscriber.rb +1 -1
  127. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  128. data/lib/active_record/fixture_set/render_context.rb +2 -0
  129. data/lib/active_record/fixture_set/table_row.rb +29 -8
  130. data/lib/active_record/fixtures.rb +135 -71
  131. data/lib/active_record/future_result.rb +31 -5
  132. data/lib/active_record/gem_version.rb +4 -4
  133. data/lib/active_record/inheritance.rb +30 -16
  134. data/lib/active_record/insert_all.rb +57 -10
  135. data/lib/active_record/integration.rb +8 -8
  136. data/lib/active_record/internal_metadata.rb +120 -30
  137. data/lib/active_record/locking/optimistic.rb +32 -18
  138. data/lib/active_record/locking/pessimistic.rb +5 -2
  139. data/lib/active_record/log_subscriber.rb +29 -12
  140. data/lib/active_record/marshalling.rb +56 -0
  141. data/lib/active_record/message_pack.rb +124 -0
  142. data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
  143. data/lib/active_record/middleware/database_selector.rb +9 -11
  144. data/lib/active_record/middleware/shard_selector.rb +3 -1
  145. data/lib/active_record/migration/command_recorder.rb +105 -7
  146. data/lib/active_record/migration/compatibility.rb +157 -58
  147. data/lib/active_record/migration/default_strategy.rb +23 -0
  148. data/lib/active_record/migration/execution_strategy.rb +19 -0
  149. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  150. data/lib/active_record/migration.rb +271 -114
  151. data/lib/active_record/model_schema.rb +64 -44
  152. data/lib/active_record/nested_attributes.rb +24 -6
  153. data/lib/active_record/normalization.rb +167 -0
  154. data/lib/active_record/persistence.rb +195 -42
  155. data/lib/active_record/promise.rb +84 -0
  156. data/lib/active_record/query_cache.rb +3 -21
  157. data/lib/active_record/query_logs.rb +77 -52
  158. data/lib/active_record/query_logs_formatter.rb +41 -0
  159. data/lib/active_record/querying.rb +15 -2
  160. data/lib/active_record/railtie.rb +109 -47
  161. data/lib/active_record/railties/controller_runtime.rb +14 -9
  162. data/lib/active_record/railties/databases.rake +142 -148
  163. data/lib/active_record/railties/job_runtime.rb +23 -0
  164. data/lib/active_record/readonly_attributes.rb +32 -5
  165. data/lib/active_record/reflection.rb +182 -44
  166. data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
  167. data/lib/active_record/relation/batches.rb +190 -61
  168. data/lib/active_record/relation/calculations.rb +232 -81
  169. data/lib/active_record/relation/delegation.rb +23 -9
  170. data/lib/active_record/relation/finder_methods.rb +77 -16
  171. data/lib/active_record/relation/merger.rb +2 -0
  172. data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
  173. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
  174. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  175. data/lib/active_record/relation/predicate_builder.rb +26 -14
  176. data/lib/active_record/relation/query_attribute.rb +25 -1
  177. data/lib/active_record/relation/query_methods.rb +387 -71
  178. data/lib/active_record/relation/spawn_methods.rb +18 -1
  179. data/lib/active_record/relation.rb +91 -35
  180. data/lib/active_record/result.rb +25 -9
  181. data/lib/active_record/runtime_registry.rb +24 -1
  182. data/lib/active_record/sanitization.rb +51 -11
  183. data/lib/active_record/schema.rb +2 -3
  184. data/lib/active_record/schema_dumper.rb +50 -7
  185. data/lib/active_record/schema_migration.rb +68 -33
  186. data/lib/active_record/scoping/default.rb +15 -5
  187. data/lib/active_record/scoping/named.rb +2 -2
  188. data/lib/active_record/scoping.rb +2 -1
  189. data/lib/active_record/secure_password.rb +60 -0
  190. data/lib/active_record/secure_token.rb +21 -3
  191. data/lib/active_record/signed_id.rb +7 -5
  192. data/lib/active_record/store.rb +9 -9
  193. data/lib/active_record/suppressor.rb +3 -1
  194. data/lib/active_record/table_metadata.rb +16 -3
  195. data/lib/active_record/tasks/database_tasks.rb +127 -105
  196. data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
  197. data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
  198. data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
  199. data/lib/active_record/test_fixtures.rb +113 -96
  200. data/lib/active_record/timestamp.rb +27 -15
  201. data/lib/active_record/token_for.rb +113 -0
  202. data/lib/active_record/touch_later.rb +11 -6
  203. data/lib/active_record/transactions.rb +39 -13
  204. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  205. data/lib/active_record/type/internal/timezone.rb +7 -2
  206. data/lib/active_record/type/serialized.rb +8 -4
  207. data/lib/active_record/type/time.rb +4 -0
  208. data/lib/active_record/validations/absence.rb +1 -1
  209. data/lib/active_record/validations/numericality.rb +5 -4
  210. data/lib/active_record/validations/presence.rb +5 -28
  211. data/lib/active_record/validations/uniqueness.rb +47 -2
  212. data/lib/active_record/validations.rb +8 -4
  213. data/lib/active_record/version.rb +1 -1
  214. data/lib/active_record.rb +121 -16
  215. data/lib/arel/errors.rb +10 -0
  216. data/lib/arel/factory_methods.rb +4 -0
  217. data/lib/arel/filter_predications.rb +1 -1
  218. data/lib/arel/nodes/and.rb +4 -0
  219. data/lib/arel/nodes/binary.rb +6 -1
  220. data/lib/arel/nodes/bound_sql_literal.rb +61 -0
  221. data/lib/arel/nodes/cte.rb +36 -0
  222. data/lib/arel/nodes/filter.rb +1 -1
  223. data/lib/arel/nodes/fragments.rb +35 -0
  224. data/lib/arel/nodes/homogeneous_in.rb +1 -9
  225. data/lib/arel/nodes/leading_join.rb +8 -0
  226. data/lib/arel/nodes/node.rb +111 -2
  227. data/lib/arel/nodes/sql_literal.rb +6 -0
  228. data/lib/arel/nodes/table_alias.rb +4 -0
  229. data/lib/arel/nodes.rb +4 -0
  230. data/lib/arel/predications.rb +2 -0
  231. data/lib/arel/table.rb +9 -5
  232. data/lib/arel/visitors/mysql.rb +8 -1
  233. data/lib/arel/visitors/to_sql.rb +81 -17
  234. data/lib/arel/visitors/visitor.rb +2 -2
  235. data/lib/arel.rb +16 -2
  236. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  237. data/lib/rails/generators/active_record/migration.rb +3 -1
  238. data/lib/rails/generators/active_record/model/USAGE +113 -0
  239. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  240. metadata +48 -12
  241. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  242. data/lib/active_record/null_relation.rb +0 -63
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model/type/registry"
4
-
5
3
  module ActiveRecord
6
4
  # :stopdoc:
7
5
  module Type
@@ -12,7 +10,6 @@ module ActiveRecord
12
10
 
13
11
  def initialize_copy(other)
14
12
  @registrations = @registrations.dup
15
- super
16
13
  end
17
14
 
18
15
  def add_modifier(options, klass, **args)
@@ -56,11 +53,7 @@ module ActiveRecord
56
53
  end
57
54
 
58
55
  def call(_registry, *args, adapter: nil, **kwargs)
59
- if kwargs.any? # https://bugs.ruby-lang.org/issues/10856
60
- block.call(*args, **kwargs)
61
- else
62
- block.call(*args)
63
- end
56
+ block.call(*args, **kwargs)
64
57
  end
65
58
 
66
59
  def matches?(type_name, *args, **kwargs)
@@ -4,12 +4,17 @@ module ActiveRecord
4
4
  module Type
5
5
  module Internal
6
6
  module Timezone
7
+ def initialize(timezone: nil, **kwargs)
8
+ super(**kwargs)
9
+ @timezone = timezone
10
+ end
11
+
7
12
  def is_utc?
8
- ActiveRecord.default_timezone == :utc
13
+ default_timezone == :utc
9
14
  end
10
15
 
11
16
  def default_timezone
12
- ActiveRecord.default_timezone
17
+ @timezone || ActiveRecord.default_timezone
13
18
  end
14
19
  end
15
20
  end
@@ -55,6 +55,10 @@ module ActiveRecord
55
55
  coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
56
56
  end
57
57
 
58
+ def serialized? # :nodoc:
59
+ true
60
+ end
61
+
58
62
  private
59
63
  def default_value?(value)
60
64
  value == coder.load(nil)
@@ -63,11 +67,11 @@ module ActiveRecord
63
67
  def encoded(value)
64
68
  return if default_value?(value)
65
69
  payload = coder.dump(value)
66
- if payload && binary? && payload.encoding != Encoding::BINARY
67
- payload = payload.dup if payload.frozen?
68
- payload.force_encoding(Encoding::BINARY)
70
+ if payload && @subtype.binary?
71
+ ActiveModel::Type::Binary::Data.new(payload)
72
+ else
73
+ payload
69
74
  end
70
- payload
71
75
  end
72
76
  end
73
77
  end
@@ -17,6 +17,10 @@ module ActiveRecord
17
17
  end
18
18
  end
19
19
 
20
+ def serialize_cast_value(value) # :nodoc:
21
+ Value.new(super) if value
22
+ end
23
+
20
24
  private
21
25
  def cast_value(value)
22
26
  case value = super
@@ -14,7 +14,7 @@ module ActiveRecord
14
14
  module ClassMethods
15
15
  # Validates that the specified attributes are not present (as defined by
16
16
  # Object#present?). If the attribute is an association, the associated object
17
- # is considered absent if it was marked for destruction.
17
+ # is also considered not present if it is marked for destruction.
18
18
  #
19
19
  # See ActiveModel::Validations::HelperMethods.validates_absence_of for more information.
20
20
  def validates_absence_of(*attr_names)
@@ -21,10 +21,11 @@ module ActiveRecord
21
21
 
22
22
  module ClassMethods
23
23
  # Validates whether the value of the specified attribute is numeric by
24
- # trying to convert it to a float with Kernel.Float (if <tt>only_integer</tt>
25
- # is +false+) or applying it to the regular expression <tt>/\A[\+\-]?\d+\z/</tt>
26
- # (if <tt>only_integer</tt> is set to +true+). Kernel.Float precision
27
- # defaults to the column's precision value or 15.
24
+ # trying to convert it to a float with +Kernel.Float+ (if
25
+ # <tt>only_integer</tt> is +false+) or applying it to the regular
26
+ # expression <tt>/\A[\+\-]?\d+\z/</tt> (if <tt>only_integer</tt> is set to
27
+ # +true+). +Kernel.Float+ precision defaults to the column's precision
28
+ # value or 15.
28
29
  #
29
30
  # See ActiveModel::Validations::HelperMethods.validates_numericality_of for more information.
30
31
  def validates_numericality_of(*attr_names)
@@ -13,9 +13,8 @@ module ActiveRecord
13
13
 
14
14
  module ClassMethods
15
15
  # Validates that the specified attributes are not blank (as defined by
16
- # Object#blank?), and, if the attribute is an association, that the
17
- # associated object is not marked for destruction. Happens by default
18
- # on save.
16
+ # Object#blank?). If the attribute is an association, the associated object
17
+ # is also considered blank if it is marked for destruction.
19
18
  #
20
19
  # class Person < ActiveRecord::Base
21
20
  # has_one :face
@@ -25,41 +24,19 @@ module ActiveRecord
25
24
  # The face attribute must be in the object and it cannot be blank or marked
26
25
  # for destruction.
27
26
  #
28
- # If you want to validate the presence of a boolean field (where the real values
29
- # are true and false), you will want to use
30
- # <tt>validates_inclusion_of :field_name, in: [true, false]</tt>.
31
- #
32
- # This is due to the way Object#blank? handles boolean values:
33
- # <tt>false.blank? # => true</tt>.
34
- #
35
27
  # This validator defers to the Active Model validation for presence, adding the
36
28
  # check to see that an associated object is not marked for destruction. This
37
29
  # prevents the parent object from validating successfully and saving, which then
38
30
  # deletes the associated object, thus putting the parent object into an invalid
39
31
  # state.
40
32
  #
33
+ # See ActiveModel::Validations::HelperMethods.validates_presence_of for
34
+ # more information.
35
+ #
41
36
  # NOTE: This validation will not fail while using it with an association
42
37
  # if the latter was assigned but not valid. If you want to ensure that
43
38
  # it is both present and valid, you also need to use
44
39
  # {validates_associated}[rdoc-ref:Validations::ClassMethods#validates_associated].
45
- #
46
- # Configuration options:
47
- # * <tt>:message</tt> - A custom error message (default is: "can't be blank").
48
- # * <tt>:on</tt> - Specifies the contexts where this validation is active.
49
- # Runs in all validation contexts by default +nil+. You can pass a symbol
50
- # or an array of symbols. (e.g. <tt>on: :create</tt> or
51
- # <tt>on: :custom_validation_context</tt> or
52
- # <tt>on: [:create, :custom_validation_context]</tt>)
53
- # * <tt>:if</tt> - Specifies a method, proc, or string to call to determine if
54
- # the validation should occur (e.g. <tt>if: :allow_validation</tt>, or
55
- # <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
56
- # or string should return or evaluate to a +true+ or +false+ value.
57
- # * <tt>:unless</tt> - Specifies a method, proc, or string to call to determine
58
- # if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
59
- # or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
60
- # proc or string should return or evaluate to a +true+ or +false+ value.
61
- # * <tt>:strict</tt> - Specifies whether validation should be strict.
62
- # See ActiveModel::Validations#validates! for more information.
63
40
  def validates_presence_of(*attr_names)
64
41
  validates_with PresenceValidator, _merge_attributes(attr_names)
65
42
  end
@@ -20,10 +20,12 @@ module ActiveRecord
20
20
  finder_class = find_finder_class_for(record)
21
21
  value = map_enum_attribute(finder_class, attribute, value)
22
22
 
23
+ return if record.persisted? && !validation_needed?(finder_class, record, attribute)
24
+
23
25
  relation = build_relation(finder_class, attribute, value)
24
26
  if record.persisted?
25
27
  if finder_class.primary_key
26
- relation = relation.where.not(finder_class.primary_key => record.id_in_database)
28
+ relation = relation.where.not(finder_class.primary_key => [record.id_in_database])
27
29
  else
28
30
  raise UnknownPrimaryKey.new(finder_class, "Cannot validate uniqueness for persisted record without primary key.")
29
31
  end
@@ -64,6 +66,48 @@ module ActiveRecord
64
66
  class_hierarchy.detect { |klass| !klass.abstract_class? }
65
67
  end
66
68
 
69
+ def validation_needed?(klass, record, attribute)
70
+ return true if options[:conditions] || options.key?(:case_sensitive)
71
+
72
+ scope = Array(options[:scope])
73
+ attributes = scope + [attribute]
74
+ attributes = resolve_attributes(record, attributes)
75
+
76
+ return true if attributes.any? { |attr| record.attribute_changed?(attr) ||
77
+ record.read_attribute(attr).nil? }
78
+
79
+ !covered_by_unique_index?(klass, record, attribute, scope)
80
+ end
81
+
82
+ def covered_by_unique_index?(klass, record, attribute, scope)
83
+ @covered ||= self.attributes.map(&:to_s).select do |attr|
84
+ attributes = scope + [attr]
85
+ attributes = resolve_attributes(record, attributes)
86
+
87
+ klass.connection.schema_cache.indexes(klass.table_name).any? do |index|
88
+ index.unique &&
89
+ index.where.nil? &&
90
+ (Array(index.columns) - attributes).empty?
91
+ end
92
+ end
93
+
94
+ @covered.include?(attribute.to_s)
95
+ end
96
+
97
+ def resolve_attributes(record, attributes)
98
+ attributes.flat_map do |attribute|
99
+ reflection = record.class._reflect_on_association(attribute)
100
+
101
+ if reflection.nil?
102
+ attribute.to_s
103
+ elsif reflection.polymorphic?
104
+ [reflection.foreign_key, reflection.foreign_type]
105
+ else
106
+ reflection.foreign_key
107
+ end
108
+ end
109
+ end
110
+
67
111
  def build_relation(klass, attribute, value)
68
112
  relation = klass.unscoped
69
113
  comparison = relation.bind_attribute(attribute, value) do |attr, bind|
@@ -221,7 +265,7 @@ module ActiveRecord
221
265
  # When the database catches such a duplicate insertion,
222
266
  # {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] will raise an ActiveRecord::StatementInvalid
223
267
  # exception. You can either choose to let this error propagate (which
224
- # will result in the default Rails exception page being shown), or you
268
+ # will result in the default \Rails exception page being shown), or you
225
269
  # can catch it and restart the transaction (e.g. by telling the user
226
270
  # that the title already exists, and asking them to re-enter the title).
227
271
  # This technique is also known as
@@ -236,6 +280,7 @@ module ActiveRecord
236
280
  # The following bundled adapters throw the ActiveRecord::RecordNotUnique exception:
237
281
  #
238
282
  # * ActiveRecord::ConnectionAdapters::Mysql2Adapter.
283
+ # * ActiveRecord::ConnectionAdapters::TrilogyAdapter.
239
284
  # * ActiveRecord::ConnectionAdapters::SQLite3Adapter.
240
285
  # * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.
241
286
  def validates_uniqueness_of(*attr_names)
@@ -30,10 +30,12 @@ module ActiveRecord
30
30
 
31
31
  # = Active Record \Validations
32
32
  #
33
- # Active Record includes the majority of its validations from ActiveModel::Validations
34
- # all of which accept the <tt>:on</tt> argument to define the context where the
35
- # validations are active. Active Record will always supply either the context of
36
- # <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a
33
+ # Active Record includes the majority of its validations from ActiveModel::Validations.
34
+ #
35
+ # In Active Record, all validations are performed on save by default.
36
+ # Validations accept the <tt>:on</tt> argument to define the context where
37
+ # the validations are active. Active Record will pass either the context of
38
+ # <tt>:create</tt> or <tt>:update</tt> depending on whether the model is a
37
39
  # {new_record?}[rdoc-ref:Persistence#new_record?].
38
40
  module Validations
39
41
  extend ActiveSupport::Concern
@@ -60,6 +62,8 @@ module ActiveRecord
60
62
  #
61
63
  # If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
62
64
  # {new_record?}[rdoc-ref:Persistence#new_record?] is +true+, and to <tt>:update</tt> if it is not.
65
+ # If the argument is an array of contexts, <tt>post.valid?([:create, :update])</tt>, the validations are
66
+ # run within multiple contexts.
63
67
  #
64
68
  # \Validations with no <tt>:on</tt> option will run no matter the context. \Validations with
65
69
  # some <tt>:on</tt> option will only run in the specified context.
@@ -3,7 +3,7 @@
3
3
  require_relative "gem_version"
4
4
 
5
5
  module ActiveRecord
6
- # Returns the currently loaded version of Active Record as a <tt>Gem::Version</tt>.
6
+ # Returns the currently loaded version of Active Record as a +Gem::Version+.
7
7
  def self.version
8
8
  gem_version
9
9
  end
data/lib/active_record.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  #--
4
- # Copyright (c) 2004-2022 David Heinemeier Hansson
4
+ # Copyright (c) David Heinemeier Hansson
5
5
  #
6
6
  # Permission is hereby granted, free of charge, to any person obtaining
7
7
  # a copy of this software and associated documentation files (the
@@ -30,9 +30,11 @@ require "arel"
30
30
  require "yaml"
31
31
 
32
32
  require "active_record/version"
33
+ require "active_record/deprecator"
33
34
  require "active_model/attribute_set"
34
35
  require "active_record/errors"
35
36
 
37
+ # :include: activerecord/README.rdoc
36
38
  module ActiveRecord
37
39
  extend ActiveSupport::Autoload
38
40
 
@@ -47,18 +49,22 @@ module ActiveRecord
47
49
  autoload :Encryption
48
50
  autoload :Enum
49
51
  autoload :Explain
52
+ autoload :FixtureSet, "active_record/fixtures"
50
53
  autoload :Inheritance
51
54
  autoload :Integration
52
55
  autoload :InternalMetadata
56
+ autoload :LogSubscriber
57
+ autoload :Marshalling
53
58
  autoload :Migration
54
59
  autoload :Migrator, "active_record/migration"
55
60
  autoload :ModelSchema
56
61
  autoload :NestedAttributes
57
62
  autoload :NoTouching
63
+ autoload :Normalization
58
64
  autoload :Persistence
59
65
  autoload :QueryCache
60
- autoload :Querying
61
66
  autoload :QueryLogs
67
+ autoload :Querying
62
68
  autoload :ReadonlyAttributes
63
69
  autoload :RecordInvalid, "active_record/validations"
64
70
  autoload :Reflection
@@ -68,6 +74,7 @@ module ActiveRecord
68
74
  autoload :SchemaDumper
69
75
  autoload :SchemaMigration
70
76
  autoload :Scoping
77
+ autoload :SecurePassword
71
78
  autoload :SecureToken
72
79
  autoload :Serialization
73
80
  autoload :SignedId
@@ -76,6 +83,7 @@ module ActiveRecord
76
83
  autoload :TestDatabases
77
84
  autoload :TestFixtures, "active_record/fixtures"
78
85
  autoload :Timestamp
86
+ autoload :TokenFor
79
87
  autoload :TouchLater
80
88
  autoload :Transactions
81
89
  autoload :Translation
@@ -93,7 +101,7 @@ module ActiveRecord
93
101
  autoload :DisableJoinsAssociationRelation
94
102
  autoload :FutureResult
95
103
  autoload :LegacyYamlAdapter
96
- autoload :NullRelation
104
+ autoload :Promise
97
105
  autoload :Relation
98
106
  autoload :Result
99
107
  autoload :StatementCache
@@ -101,17 +109,18 @@ module ActiveRecord
101
109
  autoload :Type
102
110
 
103
111
  autoload_under "relation" do
104
- autoload :QueryMethods
105
- autoload :FinderMethods
112
+ autoload :Batches
106
113
  autoload :Calculations
114
+ autoload :Delegation
115
+ autoload :FinderMethods
107
116
  autoload :PredicateBuilder
117
+ autoload :QueryMethods
108
118
  autoload :SpawnMethods
109
- autoload :Batches
110
- autoload :Delegation
111
119
  end
112
120
  end
113
121
 
114
122
  module Coders
123
+ autoload :ColumnSerializer, "active_record/coders/column_serializer"
115
124
  autoload :JSON, "active_record/coders/json"
116
125
  autoload :YAMLColumn, "active_record/coders/yaml_column"
117
126
  end
@@ -165,6 +174,9 @@ module ActiveRecord
165
174
  autoload :SQLiteDatabaseTasks, "active_record/tasks/sqlite_database_tasks"
166
175
  end
167
176
 
177
+ singleton_class.attr_accessor :disable_prepared_statements
178
+ self.disable_prepared_statements = false
179
+
168
180
  # Lazily load the schema cache. This option will load the schema cache
169
181
  # when a connection is established rather than on boot. If set,
170
182
  # +config.active_record.use_schema_cache_dump+ will be set to false.
@@ -177,9 +189,6 @@ module ActiveRecord
177
189
  singleton_class.attr_accessor :schema_cache_ignored_tables
178
190
  self.schema_cache_ignored_tables = []
179
191
 
180
- singleton_class.attr_accessor :legacy_connection_handling
181
- self.legacy_connection_handling = true
182
-
183
192
  singleton_class.attr_reader :default_timezone
184
193
 
185
194
  # Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
@@ -194,12 +203,53 @@ module ActiveRecord
194
203
 
195
204
  self.default_timezone = :utc
196
205
 
206
+ # The action to take when database query produces warning.
207
+ # Must be one of :ignore, :log, :raise, :report, or a custom proc.
208
+ # The default is :ignore.
209
+ singleton_class.attr_reader :db_warnings_action
210
+
211
+ def self.db_warnings_action=(action)
212
+ @db_warnings_action =
213
+ case action
214
+ when :ignore
215
+ nil
216
+ when :log
217
+ ->(warning) do
218
+ warning_message = "[#{warning.class}] #{warning.message}"
219
+ warning_message += " (#{warning.code})" if warning.code
220
+ ActiveRecord::Base.logger.warn(warning_message)
221
+ end
222
+ when :raise
223
+ ->(warning) { raise warning }
224
+ when :report
225
+ ->(warning) { Rails.error.report(warning, handled: true) }
226
+ when Proc
227
+ action
228
+ else
229
+ raise ArgumentError, "db_warnings_action must be one of :ignore, :log, :raise, :report, or a custom proc."
230
+ end
231
+ end
232
+
233
+ self.db_warnings_action = :ignore
234
+
235
+ # Specify allowlist of database warnings.
236
+ singleton_class.attr_accessor :db_warnings_ignore
237
+ self.db_warnings_ignore = []
238
+
197
239
  singleton_class.attr_accessor :writing_role
198
240
  self.writing_role = :writing
199
241
 
200
242
  singleton_class.attr_accessor :reading_role
201
243
  self.reading_role = :reading
202
244
 
245
+ def self.legacy_connection_handling=(_)
246
+ raise ArgumentError, <<~MSG.squish
247
+ The `legacy_connection_handling` setter was deprecated in 7.0 and removed in 7.1,
248
+ but is still defined in your configuration. Please remove this call as it no longer
249
+ has any effect."
250
+ MSG
251
+ end
252
+
203
253
  # Sets the async_query_executor for an application. By default the thread pool executor
204
254
  # set to +nil+ which will not run queries in the background. Applications must configure
205
255
  # a thread pool executor to use this feature. Options are:
@@ -258,6 +308,21 @@ module ActiveRecord
258
308
  singleton_class.attr_accessor :maintain_test_schema
259
309
  self.maintain_test_schema = nil
260
310
 
311
+ singleton_class.attr_accessor :raise_on_assign_to_attr_readonly
312
+ self.raise_on_assign_to_attr_readonly = false
313
+
314
+ singleton_class.attr_accessor :belongs_to_required_validates_foreign_key
315
+ self.belongs_to_required_validates_foreign_key = true
316
+
317
+ singleton_class.attr_accessor :before_committed_on_all_records
318
+ self.before_committed_on_all_records = false
319
+
320
+ singleton_class.attr_accessor :run_after_transaction_callbacks_in_order_defined
321
+ self.run_after_transaction_callbacks_in_order_defined = false
322
+
323
+ singleton_class.attr_accessor :commit_transaction_on_non_local_return
324
+ self.commit_transaction_on_non_local_return = false
325
+
261
326
  ##
262
327
  # :singleton-method:
263
328
  # Specify a threshold for the size of query result sets. If the number of
@@ -302,6 +367,12 @@ module ActiveRecord
302
367
  singleton_class.attr_accessor :timestamped_migrations
303
368
  self.timestamped_migrations = true
304
369
 
370
+ ##
371
+ # :singleton-method:
372
+ # Specify strategy to use for executing migrations.
373
+ singleton_class.attr_accessor :migration_strategy
374
+ self.migration_strategy = Migration::DefaultStrategy
375
+
305
376
  ##
306
377
  # :singleton-method:
307
378
  # Specify whether schema dump should happen at the end of the
@@ -321,12 +392,19 @@ module ActiveRecord
321
392
  singleton_class.attr_accessor :dump_schemas
322
393
  self.dump_schemas = :schema_search_path
323
394
 
324
- ##
325
- # :singleton-method:
326
- # Show a warning when Rails couldn't parse your database.yml
327
- # for multiple databases.
328
- singleton_class.attr_accessor :suppress_multiple_database_warning
329
- self.suppress_multiple_database_warning = false
395
+ def self.suppress_multiple_database_warning
396
+ ActiveRecord.deprecator.warn(<<-MSG.squish)
397
+ config.active_record.suppress_multiple_database_warning is deprecated and will be removed in Rails 7.2.
398
+ It no longer has any effect and should be removed from the configuration file.
399
+ MSG
400
+ end
401
+
402
+ def self.suppress_multiple_database_warning=(value)
403
+ ActiveRecord.deprecator.warn(<<-MSG.squish)
404
+ config.active_record.suppress_multiple_database_warning= is deprecated and will be removed in Rails 7.2.
405
+ It no longer has any effect and should be removed from the configuration file.
406
+ MSG
407
+ end
330
408
 
331
409
  ##
332
410
  # :singleton-method:
@@ -337,6 +415,13 @@ module ActiveRecord
337
415
  singleton_class.attr_accessor :verify_foreign_keys_for_fixtures
338
416
  self.verify_foreign_keys_for_fixtures = false
339
417
 
418
+ ##
419
+ # :singleton-method:
420
+ # If true, Rails will continue allowing plural association names in where clauses on singular associations
421
+ # This behavior will be removed in Rails 7.2.
422
+ singleton_class.attr_accessor :allow_deprecated_singular_associations_name
423
+ self.allow_deprecated_singular_associations_name = true
424
+
340
425
  singleton_class.attr_accessor :query_transformers
341
426
  self.query_transformers = []
342
427
 
@@ -362,6 +447,21 @@ module ActiveRecord
362
447
  singleton_class.attr_accessor :yaml_column_permitted_classes
363
448
  self.yaml_column_permitted_classes = [Symbol]
364
449
 
450
+ ##
451
+ # :singleton-method:
452
+ # Controls when to generate a value for <tt>has_secure_token</tt>
453
+ # declarations. Defaults to <tt>:create</tt>.
454
+ singleton_class.attr_accessor :generate_secure_token_on
455
+ self.generate_secure_token_on = :create
456
+
457
+ def self.marshalling_format_version
458
+ Marshalling.format_version
459
+ end
460
+
461
+ def self.marshalling_format_version=(value)
462
+ Marshalling.format_version = value
463
+ end
464
+
365
465
  def self.eager_load!
366
466
  super
367
467
  ActiveRecord::Locking.eager_load!
@@ -371,6 +471,11 @@ module ActiveRecord
371
471
  ActiveRecord::ConnectionAdapters.eager_load!
372
472
  ActiveRecord::Encryption.eager_load!
373
473
  end
474
+
475
+ # Explicitly closes all database connections in all pools.
476
+ def self.disconnect_all!
477
+ ConnectionAdapters::PoolConfig.disconnect_all!
478
+ end
374
479
  end
375
480
 
376
481
  ActiveSupport.on_load(:active_record) do
data/lib/arel/errors.rb CHANGED
@@ -6,4 +6,14 @@ module Arel # :nodoc: all
6
6
 
7
7
  class EmptyJoinError < ArelError
8
8
  end
9
+
10
+ class BindError < ArelError
11
+ def initialize(message, sql = nil)
12
+ if sql
13
+ super("#{message} in: #{sql.inspect}")
14
+ else
15
+ super(message)
16
+ end
17
+ end
18
+ end
9
19
  end
@@ -45,5 +45,9 @@ module Arel # :nodoc: all
45
45
  def coalesce(*exprs)
46
46
  Nodes::NamedFunction.new "COALESCE", exprs
47
47
  end
48
+
49
+ def cast(name, type)
50
+ Nodes::NamedFunction.new "CAST", [name.as(type)]
51
+ end
48
52
  end
49
53
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Arel
3
+ module Arel # :nodoc: all
4
4
  module FilterPredications
5
5
  def filter(expr)
6
6
  Nodes::Filter.new(self, expr)
@@ -18,6 +18,10 @@ module Arel # :nodoc: all
18
18
  children[1]
19
19
  end
20
20
 
21
+ def fetch_attribute(&block)
22
+ children.any? && children.all? { |child| child.fetch_attribute(&block) }
23
+ end
24
+
21
25
  def hash
22
26
  children.hash
23
27
  end
@@ -39,6 +39,12 @@ module Arel # :nodoc: all
39
39
  end
40
40
  end
41
41
 
42
+ class As < Binary
43
+ def to_cte
44
+ Arel::Nodes::Cte.new(left.name, right)
45
+ end
46
+ end
47
+
42
48
  class Between < Binary; include FetchAttribute; end
43
49
 
44
50
  class GreaterThan < Binary
@@ -112,7 +118,6 @@ module Arel # :nodoc: all
112
118
  end
113
119
 
114
120
  %w{
115
- As
116
121
  Assignment
117
122
  Join
118
123
  Union