activerecord 6.1.7 → 7.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (311) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2030 -1020
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +18 -18
  5. data/lib/active_record/aggregations.rb +17 -14
  6. data/lib/active_record/association_relation.rb +1 -11
  7. data/lib/active_record/associations/association.rb +51 -19
  8. data/lib/active_record/associations/association_scope.rb +17 -12
  9. data/lib/active_record/associations/belongs_to_association.rb +28 -9
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
  11. data/lib/active_record/associations/builder/association.rb +11 -5
  12. data/lib/active_record/associations/builder/belongs_to.rb +40 -14
  13. data/lib/active_record/associations/builder/collection_association.rb +10 -3
  14. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
  15. data/lib/active_record/associations/builder/has_many.rb +3 -2
  16. data/lib/active_record/associations/builder/has_one.rb +2 -1
  17. data/lib/active_record/associations/builder/singular_association.rb +6 -2
  18. data/lib/active_record/associations/collection_association.rb +39 -35
  19. data/lib/active_record/associations/collection_proxy.rb +30 -15
  20. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  21. data/lib/active_record/associations/foreign_association.rb +10 -3
  22. data/lib/active_record/associations/has_many_association.rb +28 -18
  23. data/lib/active_record/associations/has_many_through_association.rb +12 -7
  24. data/lib/active_record/associations/has_one_association.rb +20 -10
  25. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  26. data/lib/active_record/associations/join_dependency/join_association.rb +3 -2
  27. data/lib/active_record/associations/join_dependency.rb +28 -20
  28. data/lib/active_record/associations/preloader/association.rb +210 -52
  29. data/lib/active_record/associations/preloader/batch.rb +48 -0
  30. data/lib/active_record/associations/preloader/branch.rb +147 -0
  31. data/lib/active_record/associations/preloader/through_association.rb +50 -14
  32. data/lib/active_record/associations/preloader.rb +50 -121
  33. data/lib/active_record/associations/singular_association.rb +9 -3
  34. data/lib/active_record/associations/through_association.rb +25 -14
  35. data/lib/active_record/associations.rb +446 -306
  36. data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
  37. data/lib/active_record/attribute_assignment.rb +1 -3
  38. data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
  39. data/lib/active_record/attribute_methods/dirty.rb +73 -22
  40. data/lib/active_record/attribute_methods/primary_key.rb +78 -26
  41. data/lib/active_record/attribute_methods/query.rb +31 -19
  42. data/lib/active_record/attribute_methods/read.rb +27 -12
  43. data/lib/active_record/attribute_methods/serialization.rb +194 -37
  44. data/lib/active_record/attribute_methods/time_zone_conversion.rb +8 -3
  45. data/lib/active_record/attribute_methods/write.rb +12 -15
  46. data/lib/active_record/attribute_methods.rb +161 -40
  47. data/lib/active_record/attributes.rb +27 -38
  48. data/lib/active_record/autosave_association.rb +65 -31
  49. data/lib/active_record/base.rb +25 -2
  50. data/lib/active_record/callbacks.rb +18 -34
  51. data/lib/active_record/coders/column_serializer.rb +61 -0
  52. data/lib/active_record/coders/json.rb +1 -1
  53. data/lib/active_record/coders/yaml_column.rb +70 -46
  54. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
  55. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +113 -597
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -27
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +367 -141
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -150
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +317 -164
  70. data/lib/active_record/connection_adapters/column.rb +13 -0
  71. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
  73. data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
  74. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
  76. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
  77. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +39 -14
  78. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
  79. data/lib/active_record/connection_adapters/mysql2_adapter.rb +112 -55
  80. data/lib/active_record/connection_adapters/pool_config.rb +20 -11
  81. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  85. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
  89. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  90. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
  91. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
  94. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
  96. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
  97. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
  98. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
  99. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
  100. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +397 -75
  101. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
  102. data/lib/active_record/connection_adapters/postgresql_adapter.rb +508 -246
  103. data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
  104. data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
  105. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
  106. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
  107. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
  108. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
  109. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +296 -104
  110. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  111. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  112. data/lib/active_record/connection_adapters/trilogy_adapter.rb +258 -0
  113. data/lib/active_record/connection_adapters.rb +9 -6
  114. data/lib/active_record/connection_handling.rb +108 -137
  115. data/lib/active_record/core.rb +242 -233
  116. data/lib/active_record/counter_cache.rb +52 -27
  117. data/lib/active_record/database_configurations/connection_url_resolver.rb +3 -2
  118. data/lib/active_record/database_configurations/database_config.rb +21 -12
  119. data/lib/active_record/database_configurations/hash_config.rb +88 -16
  120. data/lib/active_record/database_configurations/url_config.rb +18 -12
  121. data/lib/active_record/database_configurations.rb +95 -59
  122. data/lib/active_record/delegated_type.rb +66 -20
  123. data/lib/active_record/deprecator.rb +7 -0
  124. data/lib/active_record/destroy_association_async_job.rb +4 -2
  125. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  126. data/lib/active_record/dynamic_matchers.rb +1 -1
  127. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  128. data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
  129. data/lib/active_record/encryption/cipher.rb +53 -0
  130. data/lib/active_record/encryption/config.rb +68 -0
  131. data/lib/active_record/encryption/configurable.rb +60 -0
  132. data/lib/active_record/encryption/context.rb +42 -0
  133. data/lib/active_record/encryption/contexts.rb +76 -0
  134. data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
  135. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  136. data/lib/active_record/encryption/encryptable_record.rb +230 -0
  137. data/lib/active_record/encryption/encrypted_attribute_type.rb +155 -0
  138. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  139. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  140. data/lib/active_record/encryption/encryptor.rb +155 -0
  141. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  142. data/lib/active_record/encryption/errors.rb +15 -0
  143. data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
  144. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
  145. data/lib/active_record/encryption/key.rb +28 -0
  146. data/lib/active_record/encryption/key_generator.rb +53 -0
  147. data/lib/active_record/encryption/key_provider.rb +46 -0
  148. data/lib/active_record/encryption/message.rb +33 -0
  149. data/lib/active_record/encryption/message_serializer.rb +92 -0
  150. data/lib/active_record/encryption/null_encryptor.rb +21 -0
  151. data/lib/active_record/encryption/properties.rb +76 -0
  152. data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
  153. data/lib/active_record/encryption/scheme.rb +100 -0
  154. data/lib/active_record/encryption.rb +58 -0
  155. data/lib/active_record/enum.rb +154 -63
  156. data/lib/active_record/errors.rb +172 -15
  157. data/lib/active_record/explain.rb +23 -3
  158. data/lib/active_record/explain_registry.rb +11 -6
  159. data/lib/active_record/explain_subscriber.rb +1 -1
  160. data/lib/active_record/fixture_set/file.rb +15 -1
  161. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  162. data/lib/active_record/fixture_set/render_context.rb +2 -0
  163. data/lib/active_record/fixture_set/table_row.rb +70 -14
  164. data/lib/active_record/fixture_set/table_rows.rb +4 -4
  165. data/lib/active_record/fixtures.rb +147 -86
  166. data/lib/active_record/future_result.rb +174 -0
  167. data/lib/active_record/gem_version.rb +3 -3
  168. data/lib/active_record/inheritance.rb +81 -29
  169. data/lib/active_record/insert_all.rb +135 -22
  170. data/lib/active_record/integration.rb +11 -10
  171. data/lib/active_record/internal_metadata.rb +119 -33
  172. data/lib/active_record/legacy_yaml_adapter.rb +2 -39
  173. data/lib/active_record/locking/optimistic.rb +37 -22
  174. data/lib/active_record/locking/pessimistic.rb +15 -6
  175. data/lib/active_record/log_subscriber.rb +52 -19
  176. data/lib/active_record/marshalling.rb +59 -0
  177. data/lib/active_record/message_pack.rb +124 -0
  178. data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
  179. data/lib/active_record/middleware/database_selector.rb +23 -13
  180. data/lib/active_record/middleware/shard_selector.rb +62 -0
  181. data/lib/active_record/migration/command_recorder.rb +112 -14
  182. data/lib/active_record/migration/compatibility.rb +233 -46
  183. data/lib/active_record/migration/default_strategy.rb +23 -0
  184. data/lib/active_record/migration/execution_strategy.rb +19 -0
  185. data/lib/active_record/migration/join_table.rb +1 -1
  186. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  187. data/lib/active_record/migration.rb +361 -173
  188. data/lib/active_record/model_schema.rb +125 -101
  189. data/lib/active_record/nested_attributes.rb +50 -20
  190. data/lib/active_record/no_touching.rb +3 -3
  191. data/lib/active_record/normalization.rb +167 -0
  192. data/lib/active_record/persistence.rb +409 -88
  193. data/lib/active_record/promise.rb +84 -0
  194. data/lib/active_record/query_cache.rb +4 -22
  195. data/lib/active_record/query_logs.rb +174 -0
  196. data/lib/active_record/query_logs_formatter.rb +41 -0
  197. data/lib/active_record/querying.rb +29 -6
  198. data/lib/active_record/railtie.rb +220 -44
  199. data/lib/active_record/railties/controller_runtime.rb +15 -10
  200. data/lib/active_record/railties/databases.rake +188 -252
  201. data/lib/active_record/railties/job_runtime.rb +23 -0
  202. data/lib/active_record/readonly_attributes.rb +41 -3
  203. data/lib/active_record/reflection.rb +248 -81
  204. data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
  205. data/lib/active_record/relation/batches.rb +192 -63
  206. data/lib/active_record/relation/calculations.rb +246 -90
  207. data/lib/active_record/relation/delegation.rb +28 -14
  208. data/lib/active_record/relation/finder_methods.rb +108 -51
  209. data/lib/active_record/relation/merger.rb +22 -13
  210. data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
  211. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
  212. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  213. data/lib/active_record/relation/predicate_builder.rb +27 -20
  214. data/lib/active_record/relation/query_attribute.rb +30 -12
  215. data/lib/active_record/relation/query_methods.rb +670 -129
  216. data/lib/active_record/relation/record_fetch_warning.rb +7 -9
  217. data/lib/active_record/relation/spawn_methods.rb +20 -3
  218. data/lib/active_record/relation/where_clause.rb +10 -19
  219. data/lib/active_record/relation.rb +287 -120
  220. data/lib/active_record/result.rb +37 -11
  221. data/lib/active_record/runtime_registry.rb +32 -13
  222. data/lib/active_record/sanitization.rb +65 -20
  223. data/lib/active_record/schema.rb +36 -22
  224. data/lib/active_record/schema_dumper.rb +73 -24
  225. data/lib/active_record/schema_migration.rb +68 -33
  226. data/lib/active_record/scoping/default.rb +72 -15
  227. data/lib/active_record/scoping/named.rb +5 -13
  228. data/lib/active_record/scoping.rb +65 -34
  229. data/lib/active_record/secure_password.rb +60 -0
  230. data/lib/active_record/secure_token.rb +21 -3
  231. data/lib/active_record/serialization.rb +6 -1
  232. data/lib/active_record/signed_id.rb +10 -8
  233. data/lib/active_record/store.rb +10 -10
  234. data/lib/active_record/suppressor.rb +13 -15
  235. data/lib/active_record/table_metadata.rb +16 -3
  236. data/lib/active_record/tasks/database_tasks.rb +251 -140
  237. data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
  238. data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
  239. data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
  240. data/lib/active_record/test_databases.rb +1 -1
  241. data/lib/active_record/test_fixtures.rb +117 -96
  242. data/lib/active_record/timestamp.rb +32 -19
  243. data/lib/active_record/token_for.rb +113 -0
  244. data/lib/active_record/touch_later.rb +11 -6
  245. data/lib/active_record/transactions.rb +48 -27
  246. data/lib/active_record/translation.rb +3 -3
  247. data/lib/active_record/type/adapter_specific_registry.rb +32 -14
  248. data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
  249. data/lib/active_record/type/internal/timezone.rb +7 -2
  250. data/lib/active_record/type/serialized.rb +9 -5
  251. data/lib/active_record/type/time.rb +4 -0
  252. data/lib/active_record/type/type_map.rb +17 -20
  253. data/lib/active_record/type.rb +1 -2
  254. data/lib/active_record/validations/absence.rb +1 -1
  255. data/lib/active_record/validations/associated.rb +4 -4
  256. data/lib/active_record/validations/numericality.rb +5 -4
  257. data/lib/active_record/validations/presence.rb +5 -28
  258. data/lib/active_record/validations/uniqueness.rb +51 -6
  259. data/lib/active_record/validations.rb +8 -4
  260. data/lib/active_record/version.rb +1 -1
  261. data/lib/active_record.rb +335 -32
  262. data/lib/arel/attributes/attribute.rb +0 -8
  263. data/lib/arel/crud.rb +28 -22
  264. data/lib/arel/delete_manager.rb +18 -4
  265. data/lib/arel/errors.rb +10 -0
  266. data/lib/arel/factory_methods.rb +4 -0
  267. data/lib/arel/filter_predications.rb +9 -0
  268. data/lib/arel/insert_manager.rb +2 -3
  269. data/lib/arel/nodes/and.rb +4 -0
  270. data/lib/arel/nodes/binary.rb +6 -1
  271. data/lib/arel/nodes/bound_sql_literal.rb +61 -0
  272. data/lib/arel/nodes/casted.rb +1 -1
  273. data/lib/arel/nodes/cte.rb +36 -0
  274. data/lib/arel/nodes/delete_statement.rb +12 -13
  275. data/lib/arel/nodes/filter.rb +10 -0
  276. data/lib/arel/nodes/fragments.rb +35 -0
  277. data/lib/arel/nodes/function.rb +1 -0
  278. data/lib/arel/nodes/homogeneous_in.rb +1 -9
  279. data/lib/arel/nodes/insert_statement.rb +2 -2
  280. data/lib/arel/nodes/leading_join.rb +8 -0
  281. data/lib/arel/nodes/node.rb +111 -2
  282. data/lib/arel/nodes/select_core.rb +2 -2
  283. data/lib/arel/nodes/select_statement.rb +2 -2
  284. data/lib/arel/nodes/sql_literal.rb +6 -0
  285. data/lib/arel/nodes/table_alias.rb +4 -0
  286. data/lib/arel/nodes/update_statement.rb +8 -3
  287. data/lib/arel/nodes.rb +5 -0
  288. data/lib/arel/predications.rb +13 -3
  289. data/lib/arel/select_manager.rb +10 -4
  290. data/lib/arel/table.rb +9 -6
  291. data/lib/arel/tree_manager.rb +5 -13
  292. data/lib/arel/update_manager.rb +18 -4
  293. data/lib/arel/visitors/dot.rb +80 -90
  294. data/lib/arel/visitors/mysql.rb +16 -3
  295. data/lib/arel/visitors/postgresql.rb +0 -10
  296. data/lib/arel/visitors/to_sql.rb +141 -20
  297. data/lib/arel/visitors/visitor.rb +2 -2
  298. data/lib/arel.rb +18 -3
  299. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  300. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  301. data/lib/rails/generators/active_record/migration.rb +3 -1
  302. data/lib/rails/generators/active_record/model/USAGE +113 -0
  303. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  304. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
  305. data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
  306. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  307. data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
  308. data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
  309. metadata +96 -16
  310. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  311. data/lib/active_record/null_relation.rb +0 -67
@@ -30,7 +30,7 @@ module ActiveRecord
30
30
  #
31
31
  # ActiveRecord::Base.time_zone_aware_types = [:datetime]
32
32
  #
33
- # You can also add database specific timezone aware types. For example, for PostgreSQL:
33
+ # You can also add database-specific timezone aware types. For example, for PostgreSQL:
34
34
  #
35
35
  # ActiveRecord::Base.time_zone_aware_types += [:tsrange, :tstzrange]
36
36
  #
@@ -54,8 +54,10 @@ module ActiveRecord
54
54
 
55
55
  module ClassMethods # :nodoc:
56
56
  def touch_attributes_with_time(*names, time: nil)
57
+ names = names.map(&:to_s)
58
+ names = names.map { |name| attribute_aliases[name] || name }
57
59
  attribute_names = timestamp_attributes_for_update_in_model
58
- attribute_names |= names.map(&:to_s)
60
+ attribute_names |= names
59
61
  attribute_names.index_with(time || current_time_from_proper_timezone)
60
62
  end
61
63
 
@@ -75,9 +77,17 @@ module ActiveRecord
75
77
  end
76
78
 
77
79
  def current_time_from_proper_timezone
78
- default_timezone == :utc ? Time.now.utc : Time.now
80
+ connection.default_timezone == :utc ? Time.now.utc : Time.now
79
81
  end
80
82
 
83
+ protected
84
+ def reload_schema_from_cache(recursive = true)
85
+ @timestamp_attributes_for_create_in_model = nil
86
+ @timestamp_attributes_for_update_in_model = nil
87
+ @all_timestamp_attributes_in_model = nil
88
+ super
89
+ end
90
+
81
91
  private
82
92
  def timestamp_attributes_for_create
83
93
  ["created_at", "created_on"].map! { |name| attribute_aliases[name] || name }
@@ -86,16 +96,14 @@ module ActiveRecord
86
96
  def timestamp_attributes_for_update
87
97
  ["updated_at", "updated_on"].map! { |name| attribute_aliases[name] || name }
88
98
  end
89
-
90
- def reload_schema_from_cache
91
- @timestamp_attributes_for_create_in_model = nil
92
- @timestamp_attributes_for_update_in_model = nil
93
- @all_timestamp_attributes_in_model = nil
94
- super
95
- end
96
99
  end
97
100
 
98
101
  private
102
+ def init_internals
103
+ super
104
+ @_touch_record = nil
105
+ end
106
+
99
107
  def _create_record
100
108
  if record_timestamps
101
109
  current_time = current_time_from_proper_timezone
@@ -109,6 +117,17 @@ module ActiveRecord
109
117
  end
110
118
 
111
119
  def _update_record
120
+ record_update_timestamps
121
+
122
+ super
123
+ end
124
+
125
+ def create_or_update(touch: true, **)
126
+ @_touch_record = touch
127
+ super
128
+ end
129
+
130
+ def record_update_timestamps
112
131
  if @_touch_record && should_record_timestamps?
113
132
  current_time = current_time_from_proper_timezone
114
133
 
@@ -118,16 +137,11 @@ module ActiveRecord
118
137
  end
119
138
  end
120
139
 
121
- super
122
- end
123
-
124
- def create_or_update(touch: true, **)
125
- @_touch_record = touch
126
- super
140
+ yield if block_given?
127
141
  end
128
142
 
129
143
  def should_record_timestamps?
130
- record_timestamps && (!partial_writes? || has_changes_to_save?)
144
+ record_timestamps && (!partial_updates? || has_changes_to_save?)
131
145
  end
132
146
 
133
147
  def timestamp_attributes_for_create_in_model
@@ -148,8 +162,7 @@ module ActiveRecord
148
162
 
149
163
  def max_updated_column_timestamp
150
164
  timestamp_attributes_for_update_in_model
151
- .map { |attr| self[attr]&.to_time }
152
- .compact
165
+ .filter_map { |attr| self[attr]&.to_time }
153
166
  .max
154
167
  end
155
168
 
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/object/json"
4
+
5
+ module ActiveRecord
6
+ module TokenFor
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ class_attribute :token_definitions, instance_accessor: false, instance_predicate: false, default: {}
11
+ class_attribute :generated_token_verifier, instance_accessor: false, instance_predicate: false
12
+ end
13
+
14
+ TokenDefinition = Struct.new(:defining_class, :purpose, :expires_in, :block) do # :nodoc:
15
+ def full_purpose
16
+ @full_purpose ||= [defining_class.name, purpose, expires_in].join("\n")
17
+ end
18
+
19
+ def message_verifier
20
+ defining_class.generated_token_verifier
21
+ end
22
+
23
+ def payload_for(model)
24
+ block ? [model.id, model.instance_eval(&block).as_json] : [model.id]
25
+ end
26
+
27
+ def generate_token(model)
28
+ message_verifier.generate(payload_for(model), expires_in: expires_in, purpose: full_purpose)
29
+ end
30
+
31
+ def resolve_token(token)
32
+ payload = message_verifier.verified(token, purpose: full_purpose)
33
+ model = yield(payload[0]) if payload
34
+ model if model && payload_for(model) == payload
35
+ end
36
+ end
37
+
38
+ module ClassMethods
39
+ # Defines the behavior of tokens generated for a specific +purpose+.
40
+ # A token can be generated by calling TokenFor#generate_token_for on a
41
+ # record. Later, that record can be fetched by calling #find_by_token_for
42
+ # (or #find_by_token_for!) with the same purpose and token.
43
+ #
44
+ # Tokens are signed so that they are tamper-proof. Thus they can be
45
+ # exposed to outside world as, for example, password reset tokens.
46
+ #
47
+ # By default, tokens do not expire. They can be configured to expire by
48
+ # specifying a duration via the +expires_in+ option. The duration becomes
49
+ # part of the token's signature, so changing the value of +expires_in+
50
+ # will automatically invalidate previously generated tokens.
51
+ #
52
+ # A block may also be specified. When generating a token with
53
+ # TokenFor#generate_token_for, the block will be evaluated in the context
54
+ # of the record, and its return value will be embedded in the token as
55
+ # JSON. Later, when fetching the record with #find_by_token_for, the block
56
+ # will be evaluated again in the context of the fetched record. If the two
57
+ # JSON values do not match, the token will be treated as invalid. Note
58
+ # that the value returned by the block <b>should not contain sensitive
59
+ # information</b> because it will be embedded in the token as
60
+ # <b>human-readable plaintext JSON</b>.
61
+ #
62
+ # ==== Examples
63
+ #
64
+ # class User < ActiveRecord::Base
65
+ # has_secure_password
66
+ #
67
+ # generates_token_for :password_reset, expires_in: 15.minutes do
68
+ # # Last 10 characters of password salt, which changes when password is updated:
69
+ # password_salt&.last(10)
70
+ # end
71
+ # end
72
+ #
73
+ # user = User.first
74
+ #
75
+ # token = user.generate_token_for(:password_reset)
76
+ # User.find_by_token_for(:password_reset, token) # => user
77
+ # # 16 minutes later...
78
+ # User.find_by_token_for(:password_reset, token) # => nil
79
+ #
80
+ # token = user.generate_token_for(:password_reset)
81
+ # User.find_by_token_for(:password_reset, token) # => user
82
+ # user.update!(password: "new password")
83
+ # User.find_by_token_for(:password_reset, token) # => nil
84
+ def generates_token_for(purpose, expires_in: nil, &block)
85
+ self.token_definitions = token_definitions.merge(purpose => TokenDefinition.new(self, purpose, expires_in, block))
86
+ end
87
+
88
+ # Finds a record using a given +token+ for a predefined +purpose+. Returns
89
+ # +nil+ if the token is invalid or the record was not found.
90
+ def find_by_token_for(purpose, token)
91
+ raise UnknownPrimaryKey.new(self) unless primary_key
92
+ token_definitions.fetch(purpose).resolve_token(token) { |id| find_by(primary_key => id) }
93
+ end
94
+
95
+ # Finds a record using a given +token+ for a predefined +purpose+. Raises
96
+ # ActiveSupport::MessageVerifier::InvalidSignature if the token is invalid
97
+ # (e.g. expired, bad format, etc). Raises ActiveRecord::RecordNotFound if
98
+ # the token is valid but the record was not found.
99
+ def find_by_token_for!(purpose, token)
100
+ token_definitions.fetch(purpose).resolve_token(token) { |id| find(id) } ||
101
+ (raise ActiveSupport::MessageVerifier::InvalidSignature)
102
+ end
103
+ end
104
+
105
+ # Generates a token for a predefined +purpose+.
106
+ #
107
+ # Use ClassMethods#generates_token_for to define a token purpose and
108
+ # behavior.
109
+ def generate_token_for(purpose)
110
+ self.class.token_definitions.fetch(purpose).generate_token(self)
111
+ end
112
+ end
113
+ end
@@ -24,9 +24,13 @@ module ActiveRecord
24
24
  @_new_record_before_last_commit ||= false
25
25
 
26
26
  # touch the parents as we are not calling the after_save callbacks
27
- self.class.reflect_on_all_associations(:belongs_to).each do |r|
27
+ self.class.reflect_on_all_associations.each do |r|
28
28
  if touch = r.options[:touch]
29
- ActiveRecord::Associations::Builder::BelongsTo.touch_record(self, changes_to_save, r.foreign_key, r.name, touch, :touch_later)
29
+ if r.macro == :belongs_to
30
+ ActiveRecord::Associations::Builder::BelongsTo.touch_record(self, changes_to_save, r.foreign_key, r.name, touch)
31
+ elsif r.macro == :has_one
32
+ ActiveRecord::Associations::Builder::HasOne.touch_record(self, r.name, touch)
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -42,6 +46,11 @@ module ActiveRecord
42
46
  end
43
47
 
44
48
  private
49
+ def init_internals
50
+ super
51
+ @_defer_touch_attrs = nil
52
+ end
53
+
45
54
  def surreptitiously_touch(attr_names)
46
55
  attr_names.each do |attr_name|
47
56
  _write_attribute(attr_name, @_touch_time)
@@ -57,9 +66,5 @@ module ActiveRecord
57
66
  def has_defer_touch_attrs?
58
67
  defined?(@_defer_touch_attrs) && @_defer_touch_attrs.present?
59
68
  end
60
-
61
- def belongs_to_touch_method
62
- :touch_later
63
- end
64
69
  end
65
70
  end
@@ -4,7 +4,7 @@ module ActiveRecord
4
4
  # See ActiveRecord::Transactions::ClassMethods for documentation.
5
5
  module Transactions
6
6
  extend ActiveSupport::Concern
7
- #:nodoc:
7
+ # :nodoc:
8
8
  ACTIONS = [:create, :destroy, :update]
9
9
 
10
10
  included do
@@ -13,7 +13,9 @@ module ActiveRecord
13
13
  scope: [:kind, :name]
14
14
  end
15
15
 
16
- # = Active Record Transactions
16
+ attr_accessor :_new_record_before_last_commit # :nodoc:
17
+
18
+ # = Active Record \Transactions
17
19
  #
18
20
  # \Transactions are protective blocks where SQL statements are only permanent
19
21
  # if they can all succeed as one atomic action. The classic example is a
@@ -98,7 +100,8 @@ module ActiveRecord
98
100
  # catch those in your application code.
99
101
  #
100
102
  # One exception is the ActiveRecord::Rollback exception, which will trigger
101
- # a ROLLBACK when raised, but not be re-raised by the transaction block.
103
+ # a ROLLBACK when raised, but not be re-raised by the transaction block. Any
104
+ # other exception will be re-raised.
102
105
  #
103
106
  # *Warning*: one should not catch ActiveRecord::StatementInvalid exceptions
104
107
  # inside a transaction block. ActiveRecord::StatementInvalid exceptions indicate that an
@@ -227,31 +230,31 @@ module ActiveRecord
227
230
  # after_commit :do_bar_baz, on: [:update, :destroy]
228
231
  #
229
232
  def after_commit(*args, &block)
230
- set_options_for_callbacks!(args)
233
+ set_options_for_callbacks!(args, prepend_option)
231
234
  set_callback(:commit, :after, *args, &block)
232
235
  end
233
236
 
234
237
  # Shortcut for <tt>after_commit :hook, on: [ :create, :update ]</tt>.
235
238
  def after_save_commit(*args, &block)
236
- set_options_for_callbacks!(args, on: [ :create, :update ])
239
+ set_options_for_callbacks!(args, on: [ :create, :update ], **prepend_option)
237
240
  set_callback(:commit, :after, *args, &block)
238
241
  end
239
242
 
240
243
  # Shortcut for <tt>after_commit :hook, on: :create</tt>.
241
244
  def after_create_commit(*args, &block)
242
- set_options_for_callbacks!(args, on: :create)
245
+ set_options_for_callbacks!(args, on: :create, **prepend_option)
243
246
  set_callback(:commit, :after, *args, &block)
244
247
  end
245
248
 
246
249
  # Shortcut for <tt>after_commit :hook, on: :update</tt>.
247
250
  def after_update_commit(*args, &block)
248
- set_options_for_callbacks!(args, on: :update)
251
+ set_options_for_callbacks!(args, on: :update, **prepend_option)
249
252
  set_callback(:commit, :after, *args, &block)
250
253
  end
251
254
 
252
255
  # Shortcut for <tt>after_commit :hook, on: :destroy</tt>.
253
256
  def after_destroy_commit(*args, &block)
254
- set_options_for_callbacks!(args, on: :destroy)
257
+ set_options_for_callbacks!(args, on: :destroy, **prepend_option)
255
258
  set_callback(:commit, :after, *args, &block)
256
259
  end
257
260
 
@@ -259,11 +262,19 @@ module ActiveRecord
259
262
  #
260
263
  # Please check the documentation of #after_commit for options.
261
264
  def after_rollback(*args, &block)
262
- set_options_for_callbacks!(args)
265
+ set_options_for_callbacks!(args, prepend_option)
263
266
  set_callback(:rollback, :after, *args, &block)
264
267
  end
265
268
 
266
269
  private
270
+ def prepend_option
271
+ if ActiveRecord.run_after_transaction_callbacks_in_order_defined
272
+ { prepend: true }
273
+ else
274
+ {}
275
+ end
276
+ end
277
+
267
278
  def set_options_for_callbacks!(args, enforced_options = {})
268
279
  options = args.extract_options!.merge!(enforced_options)
269
280
  args << options
@@ -290,19 +301,19 @@ module ActiveRecord
290
301
  self.class.transaction(**options, &block)
291
302
  end
292
303
 
293
- def destroy #:nodoc:
304
+ def destroy # :nodoc:
294
305
  with_transaction_returning_status { super }
295
306
  end
296
307
 
297
- def save(**) #:nodoc:
308
+ def save(**) # :nodoc:
298
309
  with_transaction_returning_status { super }
299
310
  end
300
311
 
301
- def save!(**) #:nodoc:
312
+ def save!(**) # :nodoc:
302
313
  with_transaction_returning_status { super }
303
314
  end
304
315
 
305
- def touch(*, **) #:nodoc:
316
+ def touch(*, **) # :nodoc:
306
317
  with_transaction_returning_status { super }
307
318
  end
308
319
 
@@ -314,8 +325,8 @@ module ActiveRecord
314
325
  #
315
326
  # Ensure that it is not called if the object was never persisted (failed create),
316
327
  # but call it after the commit of a destroyed object.
317
- def committed!(should_run_callbacks: true) #:nodoc:
318
- force_clear_transaction_record_state
328
+ def committed!(should_run_callbacks: true) # :nodoc:
329
+ @_start_transaction_state = nil
319
330
  if should_run_callbacks
320
331
  @_committed_already_called = true
321
332
  _run_commit_callbacks
@@ -326,7 +337,7 @@ module ActiveRecord
326
337
 
327
338
  # Call the #after_rollback callbacks. The +force_restore_state+ argument indicates if the record
328
339
  # state should be rolled back to the beginning or just to the last savepoint.
329
- def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
340
+ def rolledback!(force_restore_state: false, should_run_callbacks: true) # :nodoc:
330
341
  if should_run_callbacks
331
342
  _run_rollback_callbacks
332
343
  end
@@ -336,9 +347,9 @@ module ActiveRecord
336
347
  @_trigger_update_callback = @_trigger_destroy_callback = false if force_restore_state
337
348
  end
338
349
 
339
- # Executes +method+ within a transaction and captures its return value as a
340
- # status flag. If the status is true the transaction is committed, otherwise
341
- # a ROLLBACK is issued. In any case the status flag is returned.
350
+ # Executes a block within a transaction and captures its return value as a
351
+ # status flag. If the status is true, the transaction is committed,
352
+ # otherwise a ROLLBACK is issued. In any case, the status flag is returned.
342
353
  #
343
354
  # This method is available within the context of an ActiveRecord::Base
344
355
  # instance.
@@ -365,6 +376,13 @@ module ActiveRecord
365
376
  private
366
377
  attr_reader :_committed_already_called, :_trigger_update_callback, :_trigger_destroy_callback
367
378
 
379
+ def init_internals
380
+ super
381
+ @_start_transaction_state = nil
382
+ @_committed_already_called = nil
383
+ @_new_record_before_last_commit = nil
384
+ end
385
+
368
386
  # Save the new record state and id of a record so it can be restored later if a transaction fails.
369
387
  def remember_transaction_record_state
370
388
  @_start_transaction_state ||= {
@@ -389,12 +407,7 @@ module ActiveRecord
389
407
  def clear_transaction_record_state
390
408
  return unless @_start_transaction_state
391
409
  @_start_transaction_state[:level] -= 1
392
- force_clear_transaction_record_state if @_start_transaction_state[:level] < 1
393
- end
394
-
395
- # Force to clear the transaction record state.
396
- def force_clear_transaction_record_state
397
- @_start_transaction_state = nil
410
+ @_start_transaction_state = nil if @_start_transaction_state[:level] < 1
398
411
  end
399
412
 
400
413
  # Restore the new record state and id of a record that was previously saved by a call to save_record_state.
@@ -411,8 +424,16 @@ module ActiveRecord
411
424
  end
412
425
  @mutations_from_database = nil
413
426
  @mutations_before_last_save = nil
414
- if @attributes.fetch_value(@primary_key) != restore_state[:id]
415
- @attributes.write_from_user(@primary_key, restore_state[:id])
427
+ if self.class.composite_primary_key?
428
+ if restore_state[:id] != @primary_key.map { |col| @attributes.fetch_value(col) }
429
+ @primary_key.zip(restore_state[:id]).each do |col, val|
430
+ @attributes.write_from_user(col, val)
431
+ end
432
+ end
433
+ else
434
+ if @attributes.fetch_value(@primary_key) != restore_state[:id]
435
+ @attributes.write_from_user(@primary_key, restore_state[:id])
436
+ end
416
437
  end
417
438
  freeze if restore_state[:frozen?]
418
439
  end
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  include ActiveModel::Translation
6
6
 
7
7
  # Set the lookup ancestors for ActiveModel.
8
- def lookup_ancestors #:nodoc:
8
+ def lookup_ancestors # :nodoc:
9
9
  klass = self
10
10
  classes = [klass]
11
11
  return classes if klass == ActiveRecord::Base
@@ -16,8 +16,8 @@ module ActiveRecord
16
16
  classes
17
17
  end
18
18
 
19
- # Set the i18n scope to overwrite ActiveModel.
20
- def i18n_scope #:nodoc:
19
+ # Set the i18n scope to override ActiveModel.
20
+ def i18n_scope # :nodoc:
21
21
  :activerecord
22
22
  end
23
23
  end
@@ -1,19 +1,41 @@
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
8
- class AdapterSpecificRegistry < ActiveModel::Type::Registry
6
+ class AdapterSpecificRegistry # :nodoc:
7
+ def initialize
8
+ @registrations = []
9
+ end
10
+
11
+ def initialize_copy(other)
12
+ @registrations = @registrations.dup
13
+ end
14
+
9
15
  def add_modifier(options, klass, **args)
10
16
  registrations << DecorationRegistration.new(options, klass, **args)
11
17
  end
12
18
 
13
- private
14
- def registration_klass
15
- Registration
19
+ def register(type_name, klass = nil, **options, &block)
20
+ unless block_given?
21
+ block = proc { |_, *args| klass.new(*args) }
22
+ block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
23
+ end
24
+ registrations << Registration.new(type_name, block, **options)
25
+ end
26
+
27
+ def lookup(symbol, *args, **kwargs)
28
+ registration = find_registration(symbol, *args, **kwargs)
29
+
30
+ if registration
31
+ registration.call(self, symbol, *args, **kwargs)
32
+ else
33
+ raise ArgumentError, "Unknown type #{symbol.inspect}"
16
34
  end
35
+ end
36
+
37
+ private
38
+ attr_reader :registrations
17
39
 
18
40
  def find_registration(symbol, *args, **kwargs)
19
41
  registrations
@@ -22,7 +44,7 @@ module ActiveRecord
22
44
  end
23
45
  end
24
46
 
25
- class Registration
47
+ class Registration # :nodoc:
26
48
  def initialize(name, block, adapter: nil, override: nil)
27
49
  @name = name
28
50
  @block = block
@@ -31,11 +53,7 @@ module ActiveRecord
31
53
  end
32
54
 
33
55
  def call(_registry, *args, adapter: nil, **kwargs)
34
- if kwargs.any? # https://bugs.ruby-lang.org/issues/10856
35
- block.call(*args, **kwargs)
36
- else
37
- block.call(*args)
38
- end
56
+ block.call(*args, **kwargs)
39
57
  end
40
58
 
41
59
  def matches?(type_name, *args, **kwargs)
@@ -89,7 +107,7 @@ module ActiveRecord
89
107
  end
90
108
  end
91
109
 
92
- class DecorationRegistration < Registration
110
+ class DecorationRegistration < Registration # :nodoc:
93
111
  def initialize(options, klass, adapter: nil)
94
112
  @options = options
95
113
  @klass = klass
@@ -120,7 +138,7 @@ module ActiveRecord
120
138
  end
121
139
  end
122
140
 
123
- class TypeConflictError < StandardError
141
+ class TypeConflictError < StandardError # :nodoc:
124
142
  end
125
143
  # :startdoc:
126
144
  end
@@ -2,7 +2,40 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Type
5
- class HashLookupTypeMap < TypeMap # :nodoc:
5
+ class HashLookupTypeMap # :nodoc:
6
+ def initialize(parent = nil)
7
+ @mapping = {}
8
+ @cache = Concurrent::Map.new do |h, key|
9
+ h.fetch_or_store(key, Concurrent::Map.new)
10
+ end
11
+ end
12
+
13
+ def lookup(lookup_key, *args)
14
+ fetch(lookup_key, *args) { Type.default_value }
15
+ end
16
+
17
+ def fetch(lookup_key, *args, &block)
18
+ @cache[lookup_key].fetch_or_store(args) do
19
+ perform_fetch(lookup_key, *args, &block)
20
+ end
21
+ end
22
+
23
+ def register_type(key, value = nil, &block)
24
+ raise ::ArgumentError unless value || block
25
+
26
+ if block
27
+ @mapping[key] = block
28
+ else
29
+ @mapping[key] = proc { value }
30
+ end
31
+ @cache.clear
32
+ end
33
+
34
+ def clear
35
+ @mapping.clear
36
+ @cache.clear
37
+ end
38
+
6
39
  def alias_type(type, alias_type)
7
40
  register_type(type) { |_, *args| lookup(alias_type, *args) }
8
41
  end
@@ -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::Base.default_timezone == :utc
13
+ default_timezone == :utc
9
14
  end
10
15
 
11
16
  def default_timezone
12
- ActiveRecord::Base.default_timezone
17
+ @timezone || ActiveRecord.default_timezone
13
18
  end
14
19
  end
15
20
  end
@@ -31,7 +31,7 @@ module ActiveRecord
31
31
  end
32
32
 
33
33
  def inspect
34
- Kernel.instance_method(:inspect).bind(self).call
34
+ Kernel.instance_method(:inspect).bind_call(self)
35
35
  end
36
36
 
37
37
  def changed_in_place?(raw_old_value, value)
@@ -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