activerecord 6.1.3.2 → 7.0.0.alpha2

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 (229) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +734 -1058
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/lib/active_record/aggregations.rb +1 -1
  6. data/lib/active_record/association_relation.rb +0 -10
  7. data/lib/active_record/associations/association.rb +35 -7
  8. data/lib/active_record/associations/association_scope.rb +1 -3
  9. data/lib/active_record/associations/belongs_to_association.rb +16 -6
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
  11. data/lib/active_record/associations/builder/association.rb +8 -2
  12. data/lib/active_record/associations/builder/belongs_to.rb +19 -6
  13. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  14. data/lib/active_record/associations/builder/has_many.rb +3 -2
  15. data/lib/active_record/associations/builder/has_one.rb +2 -1
  16. data/lib/active_record/associations/builder/singular_association.rb +2 -2
  17. data/lib/active_record/associations/collection_association.rb +24 -25
  18. data/lib/active_record/associations/collection_proxy.rb +8 -3
  19. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  20. data/lib/active_record/associations/has_many_association.rb +1 -1
  21. data/lib/active_record/associations/has_many_through_association.rb +2 -1
  22. data/lib/active_record/associations/has_one_association.rb +10 -7
  23. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  24. data/lib/active_record/associations/preloader/association.rb +161 -49
  25. data/lib/active_record/associations/preloader/batch.rb +51 -0
  26. data/lib/active_record/associations/preloader/branch.rb +147 -0
  27. data/lib/active_record/associations/preloader/through_association.rb +37 -11
  28. data/lib/active_record/associations/preloader.rb +46 -110
  29. data/lib/active_record/associations/singular_association.rb +8 -2
  30. data/lib/active_record/associations/through_association.rb +1 -1
  31. data/lib/active_record/associations.rb +76 -81
  32. data/lib/active_record/asynchronous_queries_tracker.rb +57 -0
  33. data/lib/active_record/attribute_assignment.rb +1 -1
  34. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -2
  35. data/lib/active_record/attribute_methods/dirty.rb +41 -16
  36. data/lib/active_record/attribute_methods/primary_key.rb +2 -2
  37. data/lib/active_record/attribute_methods/query.rb +2 -2
  38. data/lib/active_record/attribute_methods/read.rb +7 -5
  39. data/lib/active_record/attribute_methods/serialization.rb +66 -12
  40. data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
  41. data/lib/active_record/attribute_methods/write.rb +7 -10
  42. data/lib/active_record/attribute_methods.rb +6 -9
  43. data/lib/active_record/attributes.rb +24 -35
  44. data/lib/active_record/autosave_association.rb +3 -18
  45. data/lib/active_record/base.rb +19 -1
  46. data/lib/active_record/callbacks.rb +2 -2
  47. data/lib/active_record/coders/yaml_column.rb +11 -1
  48. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +312 -0
  49. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +209 -0
  50. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +76 -0
  51. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +31 -558
  52. data/lib/active_record/connection_adapters/abstract/database_statements.rb +45 -21
  53. data/lib/active_record/connection_adapters/abstract/query_cache.rb +24 -12
  54. data/lib/active_record/connection_adapters/abstract/quoting.rb +14 -7
  55. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +5 -18
  56. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +30 -9
  57. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +60 -16
  58. data/lib/active_record/connection_adapters/abstract/transaction.rb +17 -6
  59. data/lib/active_record/connection_adapters/abstract_adapter.rb +115 -69
  60. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +96 -81
  61. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +6 -2
  62. data/lib/active_record/connection_adapters/mysql/database_statements.rb +33 -21
  63. data/lib/active_record/connection_adapters/mysql/quoting.rb +16 -1
  64. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +3 -0
  65. data/lib/active_record/connection_adapters/mysql2_adapter.rb +12 -6
  66. data/lib/active_record/connection_adapters/pool_config.rb +1 -3
  67. data/lib/active_record/connection_adapters/pool_manager.rb +5 -1
  68. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +19 -12
  69. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
  70. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
  71. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
  72. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
  73. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  74. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +28 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
  76. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  77. data/lib/active_record/connection_adapters/postgresql/quoting.rb +6 -6
  78. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +32 -0
  79. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +5 -1
  80. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +12 -12
  81. data/lib/active_record/connection_adapters/postgresql_adapter.rb +157 -100
  82. data/lib/active_record/connection_adapters/schema_cache.rb +35 -4
  83. data/lib/active_record/connection_adapters/sql_type_metadata.rb +0 -2
  84. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +23 -17
  85. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +4 -2
  86. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +61 -30
  87. data/lib/active_record/connection_adapters.rb +8 -5
  88. data/lib/active_record/connection_handling.rb +20 -38
  89. data/lib/active_record/core.rb +129 -117
  90. data/lib/active_record/database_configurations/database_config.rb +12 -0
  91. data/lib/active_record/database_configurations/hash_config.rb +27 -1
  92. data/lib/active_record/database_configurations/url_config.rb +2 -2
  93. data/lib/active_record/database_configurations.rb +18 -9
  94. data/lib/active_record/delegated_type.rb +33 -11
  95. data/lib/active_record/destroy_association_async_job.rb +1 -1
  96. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  97. data/lib/active_record/dynamic_matchers.rb +1 -1
  98. data/lib/active_record/encryption/cipher/aes256_gcm.rb +98 -0
  99. data/lib/active_record/encryption/cipher.rb +53 -0
  100. data/lib/active_record/encryption/config.rb +44 -0
  101. data/lib/active_record/encryption/configurable.rb +61 -0
  102. data/lib/active_record/encryption/context.rb +35 -0
  103. data/lib/active_record/encryption/contexts.rb +72 -0
  104. data/lib/active_record/encryption/derived_secret_key_provider.rb +12 -0
  105. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  106. data/lib/active_record/encryption/encryptable_record.rb +208 -0
  107. data/lib/active_record/encryption/encrypted_attribute_type.rb +140 -0
  108. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  109. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  110. data/lib/active_record/encryption/encryptor.rb +155 -0
  111. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  112. data/lib/active_record/encryption/errors.rb +15 -0
  113. data/lib/active_record/encryption/extended_deterministic_queries.rb +160 -0
  114. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +29 -0
  115. data/lib/active_record/encryption/key.rb +28 -0
  116. data/lib/active_record/encryption/key_generator.rb +42 -0
  117. data/lib/active_record/encryption/key_provider.rb +46 -0
  118. data/lib/active_record/encryption/message.rb +33 -0
  119. data/lib/active_record/encryption/message_serializer.rb +80 -0
  120. data/lib/active_record/encryption/null_encryptor.rb +21 -0
  121. data/lib/active_record/encryption/properties.rb +76 -0
  122. data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
  123. data/lib/active_record/encryption/scheme.rb +99 -0
  124. data/lib/active_record/encryption.rb +55 -0
  125. data/lib/active_record/enum.rb +44 -46
  126. data/lib/active_record/errors.rb +66 -3
  127. data/lib/active_record/fixture_set/file.rb +15 -1
  128. data/lib/active_record/fixture_set/table_row.rb +40 -5
  129. data/lib/active_record/fixture_set/table_rows.rb +4 -4
  130. data/lib/active_record/fixtures.rb +16 -11
  131. data/lib/active_record/future_result.rb +139 -0
  132. data/lib/active_record/gem_version.rb +4 -4
  133. data/lib/active_record/inheritance.rb +55 -17
  134. data/lib/active_record/insert_all.rb +39 -6
  135. data/lib/active_record/integration.rb +1 -1
  136. data/lib/active_record/internal_metadata.rb +3 -5
  137. data/lib/active_record/legacy_yaml_adapter.rb +1 -1
  138. data/lib/active_record/locking/optimistic.rb +10 -9
  139. data/lib/active_record/log_subscriber.rb +6 -2
  140. data/lib/active_record/middleware/database_selector/resolver.rb +6 -10
  141. data/lib/active_record/middleware/database_selector.rb +8 -3
  142. data/lib/active_record/migration/command_recorder.rb +4 -4
  143. data/lib/active_record/migration/compatibility.rb +83 -1
  144. data/lib/active_record/migration/join_table.rb +1 -1
  145. data/lib/active_record/migration.rb +109 -79
  146. data/lib/active_record/model_schema.rb +46 -32
  147. data/lib/active_record/nested_attributes.rb +3 -3
  148. data/lib/active_record/no_touching.rb +2 -2
  149. data/lib/active_record/null_relation.rb +2 -6
  150. data/lib/active_record/persistence.rb +134 -45
  151. data/lib/active_record/query_cache.rb +2 -2
  152. data/lib/active_record/query_logs.rb +203 -0
  153. data/lib/active_record/querying.rb +15 -5
  154. data/lib/active_record/railtie.rb +117 -17
  155. data/lib/active_record/railties/controller_runtime.rb +1 -1
  156. data/lib/active_record/railties/databases.rake +83 -58
  157. data/lib/active_record/readonly_attributes.rb +11 -0
  158. data/lib/active_record/reflection.rb +45 -44
  159. data/lib/active_record/relation/batches/batch_enumerator.rb +19 -5
  160. data/lib/active_record/relation/batches.rb +3 -3
  161. data/lib/active_record/relation/calculations.rb +42 -25
  162. data/lib/active_record/relation/delegation.rb +6 -6
  163. data/lib/active_record/relation/finder_methods.rb +32 -23
  164. data/lib/active_record/relation/merger.rb +20 -13
  165. data/lib/active_record/relation/predicate_builder.rb +1 -6
  166. data/lib/active_record/relation/query_attribute.rb +5 -11
  167. data/lib/active_record/relation/query_methods.rb +233 -50
  168. data/lib/active_record/relation/record_fetch_warning.rb +2 -2
  169. data/lib/active_record/relation/spawn_methods.rb +2 -2
  170. data/lib/active_record/relation/where_clause.rb +22 -15
  171. data/lib/active_record/relation.rb +170 -87
  172. data/lib/active_record/result.rb +17 -2
  173. data/lib/active_record/runtime_registry.rb +2 -4
  174. data/lib/active_record/sanitization.rb +11 -7
  175. data/lib/active_record/schema_dumper.rb +3 -3
  176. data/lib/active_record/schema_migration.rb +0 -4
  177. data/lib/active_record/scoping/default.rb +62 -15
  178. data/lib/active_record/scoping/named.rb +3 -11
  179. data/lib/active_record/scoping.rb +40 -22
  180. data/lib/active_record/serialization.rb +1 -1
  181. data/lib/active_record/signed_id.rb +1 -1
  182. data/lib/active_record/statement_cache.rb +2 -2
  183. data/lib/active_record/tasks/database_tasks.rb +107 -23
  184. data/lib/active_record/tasks/mysql_database_tasks.rb +1 -1
  185. data/lib/active_record/tasks/postgresql_database_tasks.rb +14 -11
  186. data/lib/active_record/test_databases.rb +1 -1
  187. data/lib/active_record/test_fixtures.rb +45 -4
  188. data/lib/active_record/timestamp.rb +3 -4
  189. data/lib/active_record/transactions.rb +9 -14
  190. data/lib/active_record/translation.rb +2 -2
  191. data/lib/active_record/type/adapter_specific_registry.rb +32 -7
  192. data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
  193. data/lib/active_record/type/internal/timezone.rb +2 -2
  194. data/lib/active_record/type/serialized.rb +1 -1
  195. data/lib/active_record/type/type_map.rb +17 -20
  196. data/lib/active_record/type.rb +1 -2
  197. data/lib/active_record/validations/associated.rb +1 -1
  198. data/lib/active_record/validations/numericality.rb +1 -1
  199. data/lib/active_record.rb +170 -2
  200. data/lib/arel/attributes/attribute.rb +0 -8
  201. data/lib/arel/collectors/bind.rb +2 -2
  202. data/lib/arel/collectors/composite.rb +3 -3
  203. data/lib/arel/collectors/sql_string.rb +1 -1
  204. data/lib/arel/collectors/substitute_binds.rb +1 -1
  205. data/lib/arel/crud.rb +18 -22
  206. data/lib/arel/delete_manager.rb +2 -4
  207. data/lib/arel/insert_manager.rb +2 -3
  208. data/lib/arel/nodes/casted.rb +1 -1
  209. data/lib/arel/nodes/delete_statement.rb +8 -13
  210. data/lib/arel/nodes/homogeneous_in.rb +4 -0
  211. data/lib/arel/nodes/insert_statement.rb +2 -2
  212. data/lib/arel/nodes/select_core.rb +2 -2
  213. data/lib/arel/nodes/select_statement.rb +2 -2
  214. data/lib/arel/nodes/update_statement.rb +3 -2
  215. data/lib/arel/predications.rb +3 -3
  216. data/lib/arel/select_manager.rb +10 -4
  217. data/lib/arel/table.rb +0 -1
  218. data/lib/arel/tree_manager.rb +0 -12
  219. data/lib/arel/update_manager.rb +2 -4
  220. data/lib/arel/visitors/dot.rb +80 -90
  221. data/lib/arel/visitors/mysql.rb +6 -1
  222. data/lib/arel/visitors/postgresql.rb +0 -10
  223. data/lib/arel/visitors/to_sql.rb +44 -3
  224. data/lib/arel.rb +1 -1
  225. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  226. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
  227. data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
  228. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  229. metadata +55 -16
@@ -94,7 +94,7 @@ module ActiveRecord
94
94
  sslcapath: "--ssl-capath",
95
95
  sslcipher: "--ssl-cipher",
96
96
  sslkey: "--ssl-key"
97
- }.map { |opt, arg| "#{arg}=#{configuration_hash[opt]}" if configuration_hash[opt] }.compact
97
+ }.filter_map { |opt, arg| "#{arg}=#{configuration_hash[opt]}" if configuration_hash[opt] }
98
98
 
99
99
  args
100
100
  end
@@ -47,16 +47,14 @@ module ActiveRecord
47
47
  end
48
48
 
49
49
  def structure_dump(filename, extra_flags)
50
- set_psql_env
51
-
52
50
  search_path = \
53
- case ActiveRecord::Base.dump_schemas
51
+ case ActiveRecord.dump_schemas
54
52
  when :schema_search_path
55
53
  configuration_hash[:schema_search_path]
56
54
  when :all
57
55
  nil
58
56
  when String
59
- ActiveRecord::Base.dump_schemas
57
+ ActiveRecord.dump_schemas
60
58
  end
61
59
 
62
60
  args = ["--schema-only", "--no-privileges", "--no-owner", "--file", filename]
@@ -79,7 +77,6 @@ module ActiveRecord
79
77
  end
80
78
 
81
79
  def structure_load(filename, extra_flags)
82
- set_psql_env
83
80
  args = ["--set", ON_ERROR_STOP_1, "--quiet", "--no-psqlrc", "--file", filename]
84
81
  args.concat(Array(extra_flags)) if extra_flags
85
82
  args << db_config.database
@@ -100,15 +97,21 @@ module ActiveRecord
100
97
  )
101
98
  end
102
99
 
103
- def set_psql_env
104
- ENV["PGHOST"] = db_config.host if db_config.host
105
- ENV["PGPORT"] = configuration_hash[:port].to_s if configuration_hash[:port]
106
- ENV["PGPASSWORD"] = configuration_hash[:password].to_s if configuration_hash[:password]
107
- ENV["PGUSER"] = configuration_hash[:username].to_s if configuration_hash[:username]
100
+ def psql_env
101
+ {}.tap do |env|
102
+ env["PGHOST"] = db_config.host if db_config.host
103
+ env["PGPORT"] = configuration_hash[:port].to_s if configuration_hash[:port]
104
+ env["PGPASSWORD"] = configuration_hash[:password].to_s if configuration_hash[:password]
105
+ env["PGUSER"] = configuration_hash[:username].to_s if configuration_hash[:username]
106
+ env["PGSSLMODE"] = configuration_hash[:sslmode].to_s if configuration_hash[:sslmode]
107
+ env["PGSSLCERT"] = configuration_hash[:sslcert].to_s if configuration_hash[:sslcert]
108
+ env["PGSSLKEY"] = configuration_hash[:sslkey].to_s if configuration_hash[:sslkey]
109
+ env["PGSSLROOTCERT"] = configuration_hash[:sslrootcert].to_s if configuration_hash[:sslrootcert]
110
+ end
108
111
  end
109
112
 
110
113
  def run_cmd(cmd, args, action)
111
- fail run_cmd_error(cmd, args, action) unless Kernel.system(cmd, *args)
114
+ fail run_cmd_error(cmd, args, action) unless Kernel.system(psql_env, cmd, *args)
112
115
  end
113
116
 
114
117
  def run_cmd_error(cmd, args, action)
@@ -14,7 +14,7 @@ module ActiveRecord
14
14
  ActiveRecord::Base.configurations.configs_for(env_name: env_name).each do |db_config|
15
15
  db_config._database = "#{db_config.database}-#{i}"
16
16
 
17
- ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config, ActiveRecord::Base.schema_format, nil)
17
+ ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config, ActiveRecord.schema_format, nil)
18
18
  end
19
19
  ensure
20
20
  ActiveRecord::Base.establish_connection
@@ -111,6 +111,8 @@ module ActiveRecord
111
111
  @fixture_connections = []
112
112
  @@already_loaded_fixtures ||= {}
113
113
  @connection_subscriber = nil
114
+ @legacy_saved_pool_configs = Hash.new { |hash, key| hash[key] = {} }
115
+ @saved_pool_configs = Hash.new { |hash, key| hash[key] = {} }
114
116
 
115
117
  # Load fixtures once and begin transaction.
116
118
  if run_in_transaction?
@@ -169,6 +171,7 @@ module ActiveRecord
169
171
  connection.pool.lock_thread = false
170
172
  end
171
173
  @fixture_connections.clear
174
+ teardown_shared_connection_pool
172
175
  else
173
176
  ActiveRecord::FixtureSet.reset_cache
174
177
  end
@@ -190,8 +193,8 @@ module ActiveRecord
190
193
  # need to share a connection pool so that the reading connection
191
194
  # can see data in the open transaction on the writing connection.
192
195
  def setup_shared_connection_pool
193
- if ActiveRecord::Base.legacy_connection_handling
194
- writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
196
+ if ActiveRecord.legacy_connection_handling
197
+ writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord.writing_role]
195
198
 
196
199
  ActiveRecord::Base.connection_handlers.values.each do |handler|
197
200
  if handler != writing_handler
@@ -200,8 +203,13 @@ module ActiveRecord
200
203
  return unless writing_pool_manager
201
204
 
202
205
  pool_manager = handler.send(:owner_to_pool_manager)[name]
206
+ @legacy_saved_pool_configs[handler][name] ||= {}
203
207
  pool_manager.shard_names.each do |shard_name|
204
208
  writing_pool_config = writing_pool_manager.get_pool_config(nil, shard_name)
209
+ pool_config = pool_manager.get_pool_config(nil, shard_name)
210
+ next if pool_config == writing_pool_config
211
+
212
+ @legacy_saved_pool_configs[handler][name][shard_name] = pool_config
205
213
  pool_manager.set_pool_config(nil, shard_name, writing_pool_config)
206
214
  end
207
215
  end
@@ -213,9 +221,13 @@ module ActiveRecord
213
221
  handler.connection_pool_names.each do |name|
214
222
  pool_manager = handler.send(:owner_to_pool_manager)[name]
215
223
  pool_manager.shard_names.each do |shard_name|
216
- writing_pool_config = pool_manager.get_pool_config(ActiveRecord::Base.writing_role, shard_name)
224
+ writing_pool_config = pool_manager.get_pool_config(ActiveRecord.writing_role, shard_name)
225
+ @saved_pool_configs[name][shard_name] ||= {}
217
226
  pool_manager.role_names.each do |role|
218
- next unless pool_manager.get_pool_config(role, shard_name)
227
+ next unless pool_config = pool_manager.get_pool_config(role, shard_name)
228
+ next if pool_config == writing_pool_config
229
+
230
+ @saved_pool_configs[name][shard_name][role] = pool_config
219
231
  pool_manager.set_pool_config(role, shard_name, writing_pool_config)
220
232
  end
221
233
  end
@@ -223,6 +235,35 @@ module ActiveRecord
223
235
  end
224
236
  end
225
237
 
238
+ def teardown_shared_connection_pool
239
+ if ActiveRecord.legacy_connection_handling
240
+ @legacy_saved_pool_configs.each_pair do |handler, names|
241
+ names.each_pair do |name, shards|
242
+ shards.each_pair do |shard_name, pool_config|
243
+ pool_manager = handler.send(:owner_to_pool_manager)[name]
244
+ pool_manager.set_pool_config(nil, shard_name, pool_config)
245
+ end
246
+ end
247
+ end
248
+ else
249
+ handler = ActiveRecord::Base.connection_handler
250
+
251
+ @saved_pool_configs.each_pair do |name, shards|
252
+ pool_manager = handler.send(:owner_to_pool_manager)[name]
253
+ shards.each_pair do |shard_name, roles|
254
+ roles.each_pair do |role, pool_config|
255
+ next unless pool_manager.get_pool_config(role, shard_name)
256
+
257
+ pool_manager.set_pool_config(role, shard_name, pool_config)
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ @legacy_saved_pool_configs.clear
264
+ @saved_pool_configs.clear
265
+ end
266
+
226
267
  def load_fixtures(config)
227
268
  ActiveRecord::FixtureSet.create_fixtures(fixture_path, fixture_table_names, fixture_class_names, config).index_by(&:name)
228
269
  end
@@ -75,7 +75,7 @@ module ActiveRecord
75
75
  end
76
76
 
77
77
  def current_time_from_proper_timezone
78
- default_timezone == :utc ? Time.now.utc : Time.now
78
+ ActiveRecord.default_timezone == :utc ? Time.now.utc : Time.now
79
79
  end
80
80
 
81
81
  private
@@ -127,7 +127,7 @@ module ActiveRecord
127
127
  end
128
128
 
129
129
  def should_record_timestamps?
130
- record_timestamps && (!partial_writes? || has_changes_to_save?)
130
+ record_timestamps && (!partial_updates? || has_changes_to_save?)
131
131
  end
132
132
 
133
133
  def timestamp_attributes_for_create_in_model
@@ -148,8 +148,7 @@ module ActiveRecord
148
148
 
149
149
  def max_updated_column_timestamp
150
150
  timestamp_attributes_for_update_in_model
151
- .map { |attr| self[attr]&.to_time }
152
- .compact
151
+ .filter_map { |attr| self[attr]&.to_time }
153
152
  .max
154
153
  end
155
154
 
@@ -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
@@ -290,19 +290,19 @@ module ActiveRecord
290
290
  self.class.transaction(**options, &block)
291
291
  end
292
292
 
293
- def destroy #:nodoc:
293
+ def destroy # :nodoc:
294
294
  with_transaction_returning_status { super }
295
295
  end
296
296
 
297
- def save(**) #:nodoc:
297
+ def save(**) # :nodoc:
298
298
  with_transaction_returning_status { super }
299
299
  end
300
300
 
301
- def save!(**) #:nodoc:
301
+ def save!(**) # :nodoc:
302
302
  with_transaction_returning_status { super }
303
303
  end
304
304
 
305
- def touch(*, **) #:nodoc:
305
+ def touch(*, **) # :nodoc:
306
306
  with_transaction_returning_status { super }
307
307
  end
308
308
 
@@ -314,8 +314,8 @@ module ActiveRecord
314
314
  #
315
315
  # Ensure that it is not called if the object was never persisted (failed create),
316
316
  # but call it after the commit of a destroyed object.
317
- def committed!(should_run_callbacks: true) #:nodoc:
318
- force_clear_transaction_record_state
317
+ def committed!(should_run_callbacks: true) # :nodoc:
318
+ @_start_transaction_state = nil
319
319
  if should_run_callbacks
320
320
  @_committed_already_called = true
321
321
  _run_commit_callbacks
@@ -326,7 +326,7 @@ module ActiveRecord
326
326
 
327
327
  # Call the #after_rollback callbacks. The +force_restore_state+ argument indicates if the record
328
328
  # 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:
329
+ def rolledback!(force_restore_state: false, should_run_callbacks: true) # :nodoc:
330
330
  if should_run_callbacks
331
331
  _run_rollback_callbacks
332
332
  end
@@ -389,12 +389,7 @@ module ActiveRecord
389
389
  def clear_transaction_record_state
390
390
  return unless @_start_transaction_state
391
391
  @_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
392
+ @_start_transaction_state = nil if @_start_transaction_state[:level] < 1
398
393
  end
399
394
 
400
395
  # Restore the new record state and id of a record that was previously saved by a call to save_record_state.
@@ -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
@@ -17,7 +17,7 @@ module ActiveRecord
17
17
  end
18
18
 
19
19
  # Set the i18n scope to overwrite ActiveModel.
20
- def i18n_scope #:nodoc:
20
+ def i18n_scope # :nodoc:
21
21
  :activerecord
22
22
  end
23
23
  end
@@ -5,15 +5,40 @@ require "active_model/type/registry"
5
5
  module ActiveRecord
6
6
  # :stopdoc:
7
7
  module Type
8
- class AdapterSpecificRegistry < ActiveModel::Type::Registry
8
+ class AdapterSpecificRegistry # :nodoc:
9
+ def initialize
10
+ @registrations = []
11
+ end
12
+
13
+ def initialize_copy(other)
14
+ @registrations = @registrations.dup
15
+ super
16
+ end
17
+
9
18
  def add_modifier(options, klass, **args)
10
19
  registrations << DecorationRegistration.new(options, klass, **args)
11
20
  end
12
21
 
13
- private
14
- def registration_klass
15
- Registration
22
+ def register(type_name, klass = nil, **options, &block)
23
+ unless block_given?
24
+ block = proc { |_, *args| klass.new(*args) }
25
+ block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
16
26
  end
27
+ registrations << Registration.new(type_name, block, **options)
28
+ end
29
+
30
+ def lookup(symbol, *args, **kwargs)
31
+ registration = find_registration(symbol, *args, **kwargs)
32
+
33
+ if registration
34
+ registration.call(self, symbol, *args, **kwargs)
35
+ else
36
+ raise ArgumentError, "Unknown type #{symbol.inspect}"
37
+ end
38
+ end
39
+
40
+ private
41
+ attr_reader :registrations
17
42
 
18
43
  def find_registration(symbol, *args, **kwargs)
19
44
  registrations
@@ -22,7 +47,7 @@ module ActiveRecord
22
47
  end
23
48
  end
24
49
 
25
- class Registration
50
+ class Registration # :nodoc:
26
51
  def initialize(name, block, adapter: nil, override: nil)
27
52
  @name = name
28
53
  @block = block
@@ -89,7 +114,7 @@ module ActiveRecord
89
114
  end
90
115
  end
91
116
 
92
- class DecorationRegistration < Registration
117
+ class DecorationRegistration < Registration # :nodoc:
93
118
  def initialize(options, klass, adapter: nil)
94
119
  @options = options
95
120
  @klass = klass
@@ -120,7 +145,7 @@ module ActiveRecord
120
145
  end
121
146
  end
122
147
 
123
- class TypeConflictError < StandardError
148
+ class TypeConflictError < StandardError # :nodoc:
124
149
  end
125
150
  # :startdoc:
126
151
  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
@@ -5,11 +5,11 @@ module ActiveRecord
5
5
  module Internal
6
6
  module Timezone
7
7
  def is_utc?
8
- ActiveRecord::Base.default_timezone == :utc
8
+ ActiveRecord.default_timezone == :utc
9
9
  end
10
10
 
11
11
  def default_timezone
12
- ActiveRecord::Base.default_timezone
12
+ ActiveRecord.default_timezone
13
13
  end
14
14
  end
15
15
  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)
@@ -5,55 +5,52 @@ require "concurrent/map"
5
5
  module ActiveRecord
6
6
  module Type
7
7
  class TypeMap # :nodoc:
8
- def initialize
8
+ def initialize(parent = nil)
9
9
  @mapping = {}
10
- @cache = Concurrent::Map.new do |h, key|
11
- h.fetch_or_store(key, Concurrent::Map.new)
12
- end
10
+ @parent = parent
11
+ @cache = Concurrent::Map.new
13
12
  end
14
13
 
15
- def lookup(lookup_key, *args)
16
- fetch(lookup_key, *args) { Type.default_value }
14
+ def lookup(lookup_key)
15
+ fetch(lookup_key) { Type.default_value }
17
16
  end
18
17
 
19
- def fetch(lookup_key, *args, &block)
20
- @cache[lookup_key].fetch_or_store(args) do
21
- perform_fetch(lookup_key, *args, &block)
18
+ def fetch(lookup_key, &block)
19
+ @cache.fetch_or_store(lookup_key) do
20
+ perform_fetch(lookup_key, &block)
22
21
  end
23
22
  end
24
23
 
25
24
  def register_type(key, value = nil, &block)
26
25
  raise ::ArgumentError unless value || block
27
- @cache.clear
28
26
 
29
27
  if block
30
28
  @mapping[key] = block
31
29
  else
32
30
  @mapping[key] = proc { value }
33
31
  end
32
+ @cache.clear
34
33
  end
35
34
 
36
35
  def alias_type(key, target_key)
37
- register_type(key) do |sql_type, *args|
36
+ register_type(key) do |sql_type|
38
37
  metadata = sql_type[/\(.*\)/, 0]
39
- lookup("#{target_key}#{metadata}", *args)
38
+ lookup("#{target_key}#{metadata}")
40
39
  end
41
40
  end
42
41
 
43
- def clear
44
- @mapping.clear
45
- end
46
-
47
- private
48
- def perform_fetch(lookup_key, *args)
42
+ protected
43
+ def perform_fetch(lookup_key, &block)
49
44
  matching_pair = @mapping.reverse_each.detect do |key, _|
50
45
  key === lookup_key
51
46
  end
52
47
 
53
48
  if matching_pair
54
- matching_pair.last.call(lookup_key, *args)
49
+ matching_pair.last.call(lookup_key)
50
+ elsif @parent
51
+ @parent.perform_fetch(lookup_key, &block)
55
52
  else
56
- yield lookup_key, *args
53
+ yield lookup_key
57
54
  end
58
55
  end
59
56
  end