activerecord 7.0.8.1 → 7.2.2.1

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 (279) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +642 -1925
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +29 -29
  5. data/examples/performance.rb +2 -2
  6. data/lib/active_record/aggregations.rb +16 -13
  7. data/lib/active_record/association_relation.rb +2 -2
  8. data/lib/active_record/associations/alias_tracker.rb +25 -19
  9. data/lib/active_record/associations/association.rb +35 -12
  10. data/lib/active_record/associations/association_scope.rb +16 -9
  11. data/lib/active_record/associations/belongs_to_association.rb +23 -8
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -2
  13. data/lib/active_record/associations/builder/association.rb +3 -3
  14. data/lib/active_record/associations/builder/belongs_to.rb +22 -8
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -7
  16. data/lib/active_record/associations/builder/has_many.rb +3 -4
  17. data/lib/active_record/associations/builder/has_one.rb +3 -4
  18. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  19. data/lib/active_record/associations/collection_association.rb +26 -14
  20. data/lib/active_record/associations/collection_proxy.rb +29 -11
  21. data/lib/active_record/associations/errors.rb +265 -0
  22. data/lib/active_record/associations/foreign_association.rb +10 -3
  23. data/lib/active_record/associations/has_many_association.rb +21 -14
  24. data/lib/active_record/associations/has_many_through_association.rb +17 -7
  25. data/lib/active_record/associations/has_one_association.rb +10 -3
  26. data/lib/active_record/associations/join_dependency/join_association.rb +30 -27
  27. data/lib/active_record/associations/join_dependency.rb +10 -10
  28. data/lib/active_record/associations/nested_error.rb +47 -0
  29. data/lib/active_record/associations/preloader/association.rb +33 -8
  30. data/lib/active_record/associations/preloader/branch.rb +7 -1
  31. data/lib/active_record/associations/preloader/through_association.rb +1 -3
  32. data/lib/active_record/associations/preloader.rb +13 -10
  33. data/lib/active_record/associations/singular_association.rb +7 -1
  34. data/lib/active_record/associations/through_association.rb +22 -11
  35. data/lib/active_record/associations.rb +354 -485
  36. data/lib/active_record/attribute_assignment.rb +0 -4
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
  38. data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
  39. data/lib/active_record/attribute_methods/dirty.rb +53 -35
  40. data/lib/active_record/attribute_methods/primary_key.rb +45 -25
  41. data/lib/active_record/attribute_methods/query.rb +28 -16
  42. data/lib/active_record/attribute_methods/read.rb +8 -7
  43. data/lib/active_record/attribute_methods/serialization.rb +131 -32
  44. data/lib/active_record/attribute_methods/time_zone_conversion.rb +11 -6
  45. data/lib/active_record/attribute_methods/write.rb +6 -6
  46. data/lib/active_record/attribute_methods.rb +148 -33
  47. data/lib/active_record/attributes.rb +64 -50
  48. data/lib/active_record/autosave_association.rb +69 -37
  49. data/lib/active_record/base.rb +9 -5
  50. data/lib/active_record/callbacks.rb +11 -25
  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 -42
  54. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +123 -131
  55. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +4 -1
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +323 -88
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +160 -45
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +217 -63
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -63
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +307 -129
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +367 -75
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +510 -111
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +278 -125
  69. data/lib/active_record/connection_adapters/column.rb +9 -0
  70. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  71. data/lib/active_record/connection_adapters/mysql/database_statements.rb +26 -139
  72. data/lib/active_record/connection_adapters/mysql/quoting.rb +53 -54
  73. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  74. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
  76. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +25 -13
  77. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
  78. data/lib/active_record/connection_adapters/mysql2_adapter.rb +101 -68
  79. data/lib/active_record/connection_adapters/pool_config.rb +20 -10
  80. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +100 -43
  83. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +1 -1
  85. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  86. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
  87. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
  88. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +14 -4
  89. data/lib/active_record/connection_adapters/postgresql/quoting.rb +65 -61
  90. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
  91. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
  92. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +151 -2
  93. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
  94. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +370 -63
  95. data/lib/active_record/connection_adapters/postgresql_adapter.rb +367 -201
  96. data/lib/active_record/connection_adapters/schema_cache.rb +302 -79
  97. data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
  98. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +60 -43
  99. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +45 -46
  100. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
  101. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +14 -0
  102. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
  103. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +50 -8
  104. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +290 -110
  105. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  106. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  107. data/lib/active_record/connection_adapters/trilogy_adapter.rb +229 -0
  108. data/lib/active_record/connection_adapters.rb +124 -1
  109. data/lib/active_record/connection_handling.rb +96 -104
  110. data/lib/active_record/core.rb +251 -176
  111. data/lib/active_record/counter_cache.rb +68 -34
  112. data/lib/active_record/database_configurations/connection_url_resolver.rb +8 -3
  113. data/lib/active_record/database_configurations/database_config.rb +26 -5
  114. data/lib/active_record/database_configurations/hash_config.rb +52 -34
  115. data/lib/active_record/database_configurations/url_config.rb +37 -12
  116. data/lib/active_record/database_configurations.rb +87 -34
  117. data/lib/active_record/delegated_type.rb +39 -10
  118. data/lib/active_record/deprecator.rb +7 -0
  119. data/lib/active_record/destroy_association_async_job.rb +3 -1
  120. data/lib/active_record/dynamic_matchers.rb +2 -2
  121. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  122. data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
  123. data/lib/active_record/encryption/config.rb +25 -1
  124. data/lib/active_record/encryption/configurable.rb +12 -19
  125. data/lib/active_record/encryption/context.rb +10 -3
  126. data/lib/active_record/encryption/contexts.rb +5 -1
  127. data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
  128. data/lib/active_record/encryption/encryptable_record.rb +45 -21
  129. data/lib/active_record/encryption/encrypted_attribute_type.rb +47 -12
  130. data/lib/active_record/encryption/encryptor.rb +18 -3
  131. data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
  132. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
  133. data/lib/active_record/encryption/key_generator.rb +12 -1
  134. data/lib/active_record/encryption/key_provider.rb +1 -1
  135. data/lib/active_record/encryption/message_pack_message_serializer.rb +76 -0
  136. data/lib/active_record/encryption/message_serializer.rb +6 -0
  137. data/lib/active_record/encryption/null_encryptor.rb +4 -0
  138. data/lib/active_record/encryption/properties.rb +3 -3
  139. data/lib/active_record/encryption/read_only_null_encryptor.rb +4 -0
  140. data/lib/active_record/encryption/scheme.rb +22 -21
  141. data/lib/active_record/encryption.rb +3 -0
  142. data/lib/active_record/enum.rb +129 -28
  143. data/lib/active_record/errors.rb +151 -31
  144. data/lib/active_record/explain.rb +21 -12
  145. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  146. data/lib/active_record/fixture_set/render_context.rb +2 -0
  147. data/lib/active_record/fixture_set/table_row.rb +29 -8
  148. data/lib/active_record/fixtures.rb +167 -97
  149. data/lib/active_record/future_result.rb +47 -8
  150. data/lib/active_record/gem_version.rb +3 -3
  151. data/lib/active_record/inheritance.rb +34 -18
  152. data/lib/active_record/insert_all.rb +72 -22
  153. data/lib/active_record/integration.rb +11 -8
  154. data/lib/active_record/internal_metadata.rb +124 -20
  155. data/lib/active_record/locking/optimistic.rb +8 -7
  156. data/lib/active_record/locking/pessimistic.rb +5 -2
  157. data/lib/active_record/log_subscriber.rb +18 -22
  158. data/lib/active_record/marshalling.rb +59 -0
  159. data/lib/active_record/message_pack.rb +124 -0
  160. data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
  161. data/lib/active_record/middleware/database_selector.rb +6 -8
  162. data/lib/active_record/middleware/shard_selector.rb +3 -1
  163. data/lib/active_record/migration/command_recorder.rb +106 -8
  164. data/lib/active_record/migration/compatibility.rb +147 -5
  165. data/lib/active_record/migration/default_strategy.rb +22 -0
  166. data/lib/active_record/migration/execution_strategy.rb +19 -0
  167. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  168. data/lib/active_record/migration.rb +234 -117
  169. data/lib/active_record/model_schema.rb +90 -102
  170. data/lib/active_record/nested_attributes.rb +48 -11
  171. data/lib/active_record/normalization.rb +163 -0
  172. data/lib/active_record/persistence.rb +168 -339
  173. data/lib/active_record/promise.rb +84 -0
  174. data/lib/active_record/query_cache.rb +18 -25
  175. data/lib/active_record/query_logs.rb +92 -52
  176. data/lib/active_record/query_logs_formatter.rb +41 -0
  177. data/lib/active_record/querying.rb +33 -8
  178. data/lib/active_record/railtie.rb +129 -85
  179. data/lib/active_record/railties/controller_runtime.rb +22 -7
  180. data/lib/active_record/railties/databases.rake +145 -154
  181. data/lib/active_record/railties/job_runtime.rb +23 -0
  182. data/lib/active_record/readonly_attributes.rb +32 -5
  183. data/lib/active_record/reflection.rb +267 -69
  184. data/lib/active_record/relation/batches/batch_enumerator.rb +20 -5
  185. data/lib/active_record/relation/batches.rb +198 -63
  186. data/lib/active_record/relation/calculations.rb +250 -93
  187. data/lib/active_record/relation/delegation.rb +30 -19
  188. data/lib/active_record/relation/finder_methods.rb +93 -18
  189. data/lib/active_record/relation/merger.rb +6 -6
  190. data/lib/active_record/relation/predicate_builder/array_handler.rb +2 -2
  191. data/lib/active_record/relation/predicate_builder/association_query_value.rb +18 -3
  192. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
  193. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  194. data/lib/active_record/relation/predicate_builder.rb +28 -16
  195. data/lib/active_record/relation/query_attribute.rb +2 -1
  196. data/lib/active_record/relation/query_methods.rb +576 -107
  197. data/lib/active_record/relation/record_fetch_warning.rb +3 -0
  198. data/lib/active_record/relation/spawn_methods.rb +5 -4
  199. data/lib/active_record/relation/where_clause.rb +7 -19
  200. data/lib/active_record/relation.rb +580 -90
  201. data/lib/active_record/result.rb +49 -48
  202. data/lib/active_record/runtime_registry.rb +63 -1
  203. data/lib/active_record/sanitization.rb +70 -25
  204. data/lib/active_record/schema.rb +8 -7
  205. data/lib/active_record/schema_dumper.rb +63 -14
  206. data/lib/active_record/schema_migration.rb +75 -24
  207. data/lib/active_record/scoping/default.rb +15 -5
  208. data/lib/active_record/scoping/named.rb +3 -2
  209. data/lib/active_record/scoping.rb +2 -1
  210. data/lib/active_record/secure_password.rb +60 -0
  211. data/lib/active_record/secure_token.rb +21 -3
  212. data/lib/active_record/signed_id.rb +27 -6
  213. data/lib/active_record/statement_cache.rb +7 -7
  214. data/lib/active_record/store.rb +8 -8
  215. data/lib/active_record/suppressor.rb +3 -1
  216. data/lib/active_record/table_metadata.rb +1 -1
  217. data/lib/active_record/tasks/database_tasks.rb +190 -118
  218. data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
  219. data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
  220. data/lib/active_record/tasks/sqlite_database_tasks.rb +16 -7
  221. data/lib/active_record/test_fixtures.rb +170 -155
  222. data/lib/active_record/testing/query_assertions.rb +121 -0
  223. data/lib/active_record/timestamp.rb +31 -17
  224. data/lib/active_record/token_for.rb +123 -0
  225. data/lib/active_record/touch_later.rb +12 -7
  226. data/lib/active_record/transaction.rb +132 -0
  227. data/lib/active_record/transactions.rb +106 -24
  228. data/lib/active_record/translation.rb +0 -2
  229. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  230. data/lib/active_record/type/internal/timezone.rb +7 -2
  231. data/lib/active_record/type/serialized.rb +1 -3
  232. data/lib/active_record/type/time.rb +4 -0
  233. data/lib/active_record/type_caster/connection.rb +4 -4
  234. data/lib/active_record/validations/absence.rb +1 -1
  235. data/lib/active_record/validations/associated.rb +9 -3
  236. data/lib/active_record/validations/numericality.rb +5 -4
  237. data/lib/active_record/validations/presence.rb +5 -28
  238. data/lib/active_record/validations/uniqueness.rb +61 -11
  239. data/lib/active_record/validations.rb +12 -5
  240. data/lib/active_record/version.rb +1 -1
  241. data/lib/active_record.rb +247 -33
  242. data/lib/arel/alias_predication.rb +1 -1
  243. data/lib/arel/collectors/bind.rb +2 -0
  244. data/lib/arel/collectors/composite.rb +7 -0
  245. data/lib/arel/collectors/sql_string.rb +1 -1
  246. data/lib/arel/collectors/substitute_binds.rb +1 -1
  247. data/lib/arel/errors.rb +10 -0
  248. data/lib/arel/factory_methods.rb +4 -0
  249. data/lib/arel/nodes/binary.rb +6 -7
  250. data/lib/arel/nodes/bound_sql_literal.rb +65 -0
  251. data/lib/arel/nodes/cte.rb +36 -0
  252. data/lib/arel/nodes/fragments.rb +35 -0
  253. data/lib/arel/nodes/homogeneous_in.rb +1 -9
  254. data/lib/arel/nodes/leading_join.rb +8 -0
  255. data/lib/arel/nodes/{and.rb → nary.rb} +5 -2
  256. data/lib/arel/nodes/node.rb +115 -5
  257. data/lib/arel/nodes/sql_literal.rb +13 -0
  258. data/lib/arel/nodes/table_alias.rb +4 -0
  259. data/lib/arel/nodes.rb +6 -2
  260. data/lib/arel/predications.rb +3 -1
  261. data/lib/arel/select_manager.rb +1 -1
  262. data/lib/arel/table.rb +9 -5
  263. data/lib/arel/tree_manager.rb +8 -3
  264. data/lib/arel/update_manager.rb +2 -1
  265. data/lib/arel/visitors/dot.rb +1 -0
  266. data/lib/arel/visitors/mysql.rb +17 -5
  267. data/lib/arel/visitors/postgresql.rb +1 -12
  268. data/lib/arel/visitors/sqlite.rb +25 -0
  269. data/lib/arel/visitors/to_sql.rb +112 -34
  270. data/lib/arel/visitors/visitor.rb +2 -2
  271. data/lib/arel.rb +21 -3
  272. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  273. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +4 -1
  274. data/lib/rails/generators/active_record/migration.rb +3 -1
  275. data/lib/rails/generators/active_record/model/USAGE +113 -0
  276. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  277. metadata +59 -17
  278. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  279. data/lib/active_record/null_relation.rb +0 -63
@@ -6,20 +6,23 @@ require "zlib"
6
6
  require "set"
7
7
  require "active_support/dependencies"
8
8
  require "active_support/core_ext/digest/uuid"
9
- require "active_record/fixture_set/file"
10
- require "active_record/fixture_set/render_context"
11
- require "active_record/fixture_set/table_rows"
12
9
  require "active_record/test_fixtures"
13
10
 
14
11
  module ActiveRecord
15
12
  class FixtureClassNotFound < ActiveRecord::ActiveRecordError # :nodoc:
16
13
  end
17
14
 
15
+ # = Active Record \Fixtures
16
+ #
18
17
  # \Fixtures are a way of organizing data that you want to test against; in short, sample data.
19
18
  #
20
- # They are stored in YAML files, one file per model, which are placed in the directory
21
- # appointed by <tt>ActiveSupport::TestCase.fixture_path=(path)</tt> (this is automatically
22
- # configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>).
19
+ # They are stored in YAML files, one file per model, which are by default placed in either
20
+ # <tt><your-rails-app>/test/fixtures/</tt> or in the <tt>test/fixtures</tt>
21
+ # folder under any of your application's engines.
22
+ #
23
+ # The location can also be changed with ActiveSupport::TestCase.fixture_paths=,
24
+ # once you have <tt>require "rails/test_help"</tt> in your +test_helper.rb+.
25
+ #
23
26
  # The fixture file ends with the +.yml+ file extension, for example:
24
27
  # <tt><your-rails-app>/test/fixtures/web_sites.yml</tt>).
25
28
  #
@@ -39,10 +42,17 @@ module ActiveRecord
39
42
  # is followed by an indented list of key/value pairs in the "key: value" format. Records are
40
43
  # separated by a blank line for your viewing pleasure.
41
44
  #
42
- # Note: Fixtures are unordered. If you want ordered fixtures, use the omap YAML type.
43
- # See https://yaml.org/type/omap.html
44
- # for the specification. You will need ordered fixtures when you have foreign key constraints
45
- # on keys in the same table. This is commonly needed for tree structures. Example:
45
+ # == Ordering
46
+ #
47
+ # Fixtures by default are unordered. This is because the maps in YAML are unordered.
48
+ #
49
+ # If you want ordered fixtures, use the omap YAML type.
50
+ # See https://yaml.org/type/omap.html for the specification.
51
+ #
52
+ # You will need ordered fixtures when you have foreign key constraints
53
+ # on keys in the same table. This is commonly needed for tree structures.
54
+ #
55
+ # For example:
46
56
  #
47
57
  # --- !omap
48
58
  # - parent:
@@ -54,7 +64,7 @@ module ActiveRecord
54
64
  # parent_id: 1
55
65
  # title: Child
56
66
  #
57
- # = Using Fixtures in Test Cases
67
+ # == Using Fixtures in Test Cases
58
68
  #
59
69
  # Since fixtures are a testing construct, we use them in our unit and functional tests. There
60
70
  # are two ways to use the fixtures, but first let's take a look at a sample unit test:
@@ -100,6 +110,12 @@ module ActiveRecord
100
110
  # assert_raise(StandardError) { web_sites(:reddit) }
101
111
  # end
102
112
  #
113
+ # If the model names conflicts with a +TestCase+ methods, you can use the generic +fixture+ accessor
114
+ #
115
+ # test "generic find" do
116
+ # assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
117
+ # end
118
+ #
103
119
  # Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the
104
120
  # following tests:
105
121
  #
@@ -124,7 +140,7 @@ module ActiveRecord
124
140
  # traversed in the database to create the fixture hash and/or instance variables. This is expensive for
125
141
  # large sets of fixtured data.
126
142
  #
127
- # = Dynamic fixtures with ERB
143
+ # == Dynamic fixtures with \ERB
128
144
  #
129
145
  # Sometimes you don't care about the content of the fixtures as much as you care about the volume.
130
146
  # In these cases, you can mix ERB in with your YAML fixtures to create a bunch of fixtures for load
@@ -162,7 +178,7 @@ module ActiveRecord
162
178
  # name: kitten.png
163
179
  # sha: <%= file_sha 'files/kitten.png' %>
164
180
  #
165
- # = Transactional Tests
181
+ # == Transactional Tests
166
182
  #
167
183
  # Test cases can use begin+rollback to isolate their changes to the database instead of having to
168
184
  # delete+insert for every test case.
@@ -198,7 +214,7 @@ module ActiveRecord
198
214
  # 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
199
215
  # Use InnoDB, MaxDB, or NDB instead.
200
216
  #
201
- # = Advanced Fixtures
217
+ # == Advanced Fixtures
202
218
  #
203
219
  # Fixtures that don't specify an ID get some extra features:
204
220
  #
@@ -212,7 +228,7 @@ module ActiveRecord
212
228
  # * Fixture label interpolation
213
229
  # * Support for YAML defaults
214
230
  #
215
- # == Stable, Autogenerated IDs
231
+ # === Stable, Autogenerated IDs
216
232
  #
217
233
  # Here, have a monkey fixture:
218
234
  #
@@ -241,13 +257,13 @@ module ActiveRecord
241
257
  # The generated ID for a given label is constant, so we can discover
242
258
  # any fixture's ID without loading anything, as long as we know the label.
243
259
  #
244
- # == Label references for associations (+belongs_to+, +has_one+, +has_many+)
260
+ # === Label references for associations (+belongs_to+, +has_one+, +has_many+)
245
261
  #
246
262
  # Specifying foreign keys in fixtures can be very fragile, not to
247
263
  # mention difficult to read. Since Active Record can figure out the ID of
248
264
  # any fixture from its label, you can specify FK's by label instead of ID.
249
265
  #
250
- # === +belongs_to+
266
+ # ==== +belongs_to+
251
267
  #
252
268
  # Let's break out some more monkeys and pirates.
253
269
  #
@@ -258,6 +274,8 @@ module ActiveRecord
258
274
  # name: Reginald the Pirate
259
275
  # monkey_id: 1
260
276
  #
277
+ # <code></code>
278
+ #
261
279
  # ### in monkeys.yml
262
280
  #
263
281
  # george:
@@ -275,6 +293,8 @@ module ActiveRecord
275
293
  # name: Reginald the Pirate
276
294
  # monkey: george
277
295
  #
296
+ # <code></code>
297
+ #
278
298
  # ### in monkeys.yml
279
299
  #
280
300
  # george:
@@ -296,6 +316,8 @@ module ActiveRecord
296
316
  #
297
317
  # belongs_to :eater, polymorphic: true
298
318
  #
319
+ # <code></code>
320
+ #
299
321
  # ### in fruits.yml
300
322
  #
301
323
  # apple:
@@ -311,9 +333,9 @@ module ActiveRecord
311
333
  #
312
334
  # Just provide the polymorphic target type and Active Record will take care of the rest.
313
335
  #
314
- # === +has_and_belongs_to_many+ or <tt>has_many :through</tt>
336
+ # ==== +has_and_belongs_to_many+ or <tt>has_many :through</tt>
315
337
  #
316
- # Time to give our monkey some fruit.
338
+ # \Time to give our monkey some fruit.
317
339
  #
318
340
  # ### in monkeys.yml
319
341
  #
@@ -321,6 +343,8 @@ module ActiveRecord
321
343
  # id: 1
322
344
  # name: George the Monkey
323
345
  #
346
+ # <code></code>
347
+ #
324
348
  # ### in fruits.yml
325
349
  #
326
350
  # apple:
@@ -335,6 +359,8 @@ module ActiveRecord
335
359
  # id: 3
336
360
  # name: grape
337
361
  #
362
+ # <code></code>
363
+ #
338
364
  # ### in fruits_monkeys.yml
339
365
  #
340
366
  # apple_george:
@@ -358,6 +384,8 @@ module ActiveRecord
358
384
  # name: George the Monkey
359
385
  # fruits: apple, orange, grape
360
386
  #
387
+ # <code></code>
388
+ #
361
389
  # ### in fruits.yml
362
390
  #
363
391
  # apple:
@@ -375,7 +403,7 @@ module ActiveRecord
375
403
  # the fixture's model class and discovers the +has_and_belongs_to_many+
376
404
  # associations.
377
405
  #
378
- # == Autofilled Timestamp Columns
406
+ # === Autofilled \Timestamp Columns
379
407
  #
380
408
  # If your table/model specifies any of Active Record's
381
409
  # standard timestamp columns (+created_at+, +created_on+, +updated_at+, +updated_on+),
@@ -383,7 +411,7 @@ module ActiveRecord
383
411
  #
384
412
  # If you've set specific values, they'll be left alone.
385
413
  #
386
- # == Fixture label interpolation
414
+ # === Fixture label interpolation
387
415
  #
388
416
  # The label of the current fixture is always available as a column value:
389
417
  #
@@ -400,7 +428,11 @@ module ActiveRecord
400
428
  # monkey_id: <%= ActiveRecord::FixtureSet.identify(:reginald) %>
401
429
  # pirate_id: <%= ActiveRecord::FixtureSet.identify(:george) %>
402
430
  #
403
- # == Support for YAML defaults
431
+ # If the model uses UUID values for identifiers, add the +:uuid+ argument:
432
+ #
433
+ # ActiveRecord::FixtureSet.identify(:boaty_mcboatface, :uuid)
434
+ #
435
+ # === Support for YAML defaults
404
436
  #
405
437
  # You can set and reuse defaults in your fixtures YAML file.
406
438
  # This is the same technique used in the +database.yml+ file to specify
@@ -442,6 +474,45 @@ module ActiveRecord
442
474
  # In the above example, 'base' will be ignored when creating fixtures.
443
475
  # This can be used for common attributes inheriting.
444
476
  #
477
+ # == Composite Primary Key Fixtures
478
+ #
479
+ # Fixtures for composite primary key tables are fairly similar to normal tables.
480
+ # When using an id column, the column may be omitted as usual:
481
+ #
482
+ # # app/models/book.rb
483
+ # class Book < ApplicationRecord
484
+ # self.primary_key = [:author_id, :id]
485
+ # belongs_to :author
486
+ # end
487
+ #
488
+ # <code></code>
489
+ #
490
+ # # books.yml
491
+ # alices_adventure_in_wonderland:
492
+ # author_id: <%= ActiveRecord::FixtureSet.identify(:lewis_carroll) %>
493
+ # title: "Alice's Adventures in Wonderland"
494
+ #
495
+ # However, in order to support composite primary key relationships,
496
+ # you must use the `composite_identify` method:
497
+ #
498
+ # # app/models/book_orders.rb
499
+ # class BookOrder < ApplicationRecord
500
+ # self.primary_key = [:shop_id, :id]
501
+ # belongs_to :order, foreign_key: [:shop_id, :order_id]
502
+ # belongs_to :book, foreign_key: [:author_id, :book_id]
503
+ # end
504
+ #
505
+ # <code></code>
506
+ #
507
+ # # book_orders.yml
508
+ # alices_adventure_in_wonderland_in_books:
509
+ # author: lewis_carroll
510
+ # book_id: <%= ActiveRecord::FixtureSet.composite_identify(
511
+ # :alices_adventure_in_wonderland, Book.primary_key)[:id] %>
512
+ # shop: book_store
513
+ # order_id: <%= ActiveRecord::FixtureSet.composite_identify(
514
+ # :books, Order.primary_key)[:id] %>
515
+ #
445
516
  # == Configure the fixture model class
446
517
  #
447
518
  # It's possible to set the fixture's model class directly in the YAML file.
@@ -456,6 +527,10 @@ module ActiveRecord
456
527
  #
457
528
  # Any fixtures labeled "_fixture" are safely ignored.
458
529
  class FixtureSet
530
+ require "active_record/fixture_set/file"
531
+ require "active_record/fixture_set/render_context"
532
+ require "active_record/fixture_set/table_rows"
533
+
459
534
  #--
460
535
  # An instance of FixtureSet is normally stored in a single YAML file and
461
536
  # possibly in a folder with the same name.
@@ -467,39 +542,6 @@ module ActiveRecord
467
542
 
468
543
  cattr_accessor :all_loaded_fixtures, default: {}
469
544
 
470
- class ClassCache
471
- def initialize(class_names, config)
472
- @class_names = class_names.stringify_keys
473
- @config = config
474
-
475
- # Remove string values that aren't constants or subclasses of AR
476
- @class_names.delete_if do |klass_name, klass|
477
- !insert_class(@class_names, klass_name, klass)
478
- end
479
- end
480
-
481
- def [](fs_name)
482
- @class_names.fetch(fs_name) do
483
- klass = default_fixture_model(fs_name, @config).safe_constantize
484
- insert_class(@class_names, fs_name, klass)
485
- end
486
- end
487
-
488
- private
489
- def insert_class(class_names, name, klass)
490
- # We only want to deal with AR objects.
491
- if klass && klass < ActiveRecord::Base
492
- class_names[name] = klass
493
- else
494
- class_names[name] = nil
495
- end
496
- end
497
-
498
- def default_fixture_model(fs_name, config)
499
- ActiveRecord::FixtureSet.default_fixture_model_name(fs_name, config)
500
- end
501
- end
502
-
503
545
  class << self
504
546
  def default_fixture_model_name(fixture_set_name, config = ActiveRecord::Base) # :nodoc:
505
547
  config.pluralize_table_names ?
@@ -517,24 +559,24 @@ module ActiveRecord
517
559
  @@all_cached_fixtures.clear
518
560
  end
519
561
 
520
- def cache_for_connection(connection)
521
- @@all_cached_fixtures[connection]
562
+ def cache_for_connection_pool(connection_pool)
563
+ @@all_cached_fixtures[connection_pool]
522
564
  end
523
565
 
524
- def fixture_is_cached?(connection, table_name)
525
- cache_for_connection(connection)[table_name]
566
+ def fixture_is_cached?(connection_pool, table_name)
567
+ cache_for_connection_pool(connection_pool)[table_name]
526
568
  end
527
569
 
528
- def cached_fixtures(connection, keys_to_fetch = nil)
570
+ def cached_fixtures(connection_pool, keys_to_fetch = nil)
529
571
  if keys_to_fetch
530
- cache_for_connection(connection).values_at(*keys_to_fetch)
572
+ cache_for_connection_pool(connection_pool).values_at(*keys_to_fetch)
531
573
  else
532
- cache_for_connection(connection).values
574
+ cache_for_connection_pool(connection_pool).values
533
575
  end
534
576
  end
535
577
 
536
- def cache_fixtures(connection, fixtures_map)
537
- cache_for_connection(connection).update(fixtures_map)
578
+ def cache_fixtures(connection_pool, fixtures_map)
579
+ cache_for_connection_pool(connection_pool).update(fixtures_map)
538
580
  end
539
581
 
540
582
  def instantiate_fixtures(object, fixture_set, load_instances = true)
@@ -552,31 +594,30 @@ module ActiveRecord
552
594
  end
553
595
  end
554
596
 
555
- def create_fixtures(fixtures_directory, fixture_set_names, class_names = {}, config = ActiveRecord::Base, &block)
597
+ def create_fixtures(fixtures_directories, fixture_set_names, class_names = {}, config = ActiveRecord::Base)
556
598
  fixture_set_names = Array(fixture_set_names).map(&:to_s)
557
- class_names = ClassCache.new class_names, config
558
-
559
- # FIXME: Apparently JK uses this.
560
- connection = block_given? ? block : lambda { ActiveRecord::Base.connection }
599
+ class_names.stringify_keys!
561
600
 
601
+ connection_pool = config.connection_pool
562
602
  fixture_files_to_read = fixture_set_names.reject do |fs_name|
563
- fixture_is_cached?(connection.call, fs_name)
603
+ fixture_is_cached?(connection_pool, fs_name)
564
604
  end
565
605
 
566
606
  if fixture_files_to_read.any?
567
607
  fixtures_map = read_and_insert(
568
- fixtures_directory,
608
+ Array(fixtures_directories),
569
609
  fixture_files_to_read,
570
610
  class_names,
571
- connection,
611
+ connection_pool,
572
612
  )
573
- cache_fixtures(connection.call, fixtures_map)
613
+ cache_fixtures(connection_pool, fixtures_map)
574
614
  end
575
- cached_fixtures(connection.call, fixture_set_names)
615
+ cached_fixtures(connection_pool, fixture_set_names)
576
616
  end
577
617
 
578
618
  # Returns a consistent, platform-independent identifier for +label+.
579
- # Integer identifiers are values less than 2^30. UUIDs are RFC 4122 version 5 SHA-1 hashes.
619
+ #
620
+ # \Integer identifiers are values less than 2^30. UUIDs are RFC 4122 version 5 SHA-1 hashes.
580
621
  def identify(label, column_type = :integer)
581
622
  if column_type == :uuid
582
623
  Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, label.to_s)
@@ -585,40 +626,54 @@ module ActiveRecord
585
626
  end
586
627
  end
587
628
 
588
- # Superclass for the evaluation contexts used by ERB fixtures.
629
+ # Returns a consistent, platform-independent hash representing a mapping
630
+ # between the label and the subcomponents of the provided composite key.
631
+ #
632
+ # Example:
633
+ #
634
+ # composite_identify("label", [:a, :b, :c]) # => { a: hash_1, b: hash_2, c: hash_3 }
635
+ def composite_identify(label, key)
636
+ key
637
+ .index_with
638
+ .with_index { |sub_key, index| (identify(label) << index) % MAX_ID }
639
+ .with_indifferent_access
640
+ end
641
+
642
+ # Superclass for the evaluation contexts used by \ERB fixtures.
589
643
  def context_class
590
644
  @context_class ||= Class.new
591
645
  end
592
646
 
593
647
  private
594
- def read_and_insert(fixtures_directory, fixture_files, class_names, connection) # :nodoc:
648
+ def read_and_insert(fixtures_directories, fixture_files, class_names, connection_pool) # :nodoc:
595
649
  fixtures_map = {}
650
+ directory_glob = "{#{fixtures_directories.join(",")}}"
596
651
  fixture_sets = fixture_files.map do |fixture_set_name|
597
652
  klass = class_names[fixture_set_name]
598
653
  fixtures_map[fixture_set_name] = new( # ActiveRecord::FixtureSet.new
599
654
  nil,
600
655
  fixture_set_name,
601
656
  klass,
602
- ::File.join(fixtures_directory, fixture_set_name)
657
+ ::File.join(directory_glob, fixture_set_name)
603
658
  )
604
659
  end
605
660
  update_all_loaded_fixtures(fixtures_map)
606
661
 
607
- insert(fixture_sets, connection)
662
+ insert(fixture_sets, connection_pool)
608
663
 
609
664
  fixtures_map
610
665
  end
611
666
 
612
- def insert(fixture_sets, connection) # :nodoc:
613
- fixture_sets_by_connection = fixture_sets.group_by do |fixture_set|
667
+ def insert(fixture_sets, connection_pool) # :nodoc:
668
+ fixture_sets_by_pool = fixture_sets.group_by do |fixture_set|
614
669
  if fixture_set.model_class
615
- fixture_set.model_class.connection
670
+ fixture_set.model_class.connection_pool
616
671
  else
617
- connection.call
672
+ connection_pool
618
673
  end
619
674
  end
620
675
 
621
- fixture_sets_by_connection.each do |conn, set|
676
+ fixture_sets_by_pool.each do |pool, set|
622
677
  table_rows_for_connection = Hash.new { |h, k| h[k] = [] }
623
678
 
624
679
  set.each do |fixture_set|
@@ -627,19 +682,29 @@ module ActiveRecord
627
682
  end
628
683
  end
629
684
 
630
- conn.insert_fixtures_set(table_rows_for_connection, table_rows_for_connection.keys)
685
+ pool.with_connection do |conn|
686
+ conn.insert_fixtures_set(table_rows_for_connection, table_rows_for_connection.keys)
631
687
 
632
- if ActiveRecord.verify_foreign_keys_for_fixtures && !conn.all_foreign_keys_valid?
633
- raise "Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations."
634
- end
688
+ check_all_foreign_keys_valid!(conn)
635
689
 
636
- # Cap primary key sequences to max(pk).
637
- if conn.respond_to?(:reset_pk_sequence!)
638
- set.each { |fs| conn.reset_pk_sequence!(fs.table_name) }
690
+ # Cap primary key sequences to max(pk).
691
+ if conn.respond_to?(:reset_pk_sequence!)
692
+ set.each { |fs| conn.reset_pk_sequence!(fs.table_name) }
693
+ end
639
694
  end
640
695
  end
641
696
  end
642
697
 
698
+ def check_all_foreign_keys_valid!(conn)
699
+ return unless ActiveRecord.verify_foreign_keys_for_fixtures
700
+
701
+ begin
702
+ conn.check_all_foreign_keys_valid!
703
+ rescue ActiveRecord::StatementInvalid => e
704
+ raise "Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations. Error from database:\n\n#{e.message}"
705
+ end
706
+ end
707
+
643
708
  def update_all_loaded_fixtures(fixtures_map) # :nodoc:
644
709
  all_loaded_fixtures.update(fixtures_map)
645
710
  end
@@ -653,7 +718,6 @@ module ActiveRecord
653
718
  @config = config
654
719
 
655
720
  self.model_class = class_name
656
-
657
721
  @fixtures = read_fixture_files(path)
658
722
 
659
723
  @table_name = model_class&.table_name || self.class.default_fixture_table_name(name, config)
@@ -715,14 +779,18 @@ module ActiveRecord
715
779
  # Loads the fixtures from the YAML file at +path+.
716
780
  # If the file sets the +model_class+ and current instance value is not set,
717
781
  # it uses the file value.
782
+
718
783
  def read_fixture_files(path)
719
- yaml_files = Dir["#{path}/{**,*}/*.yml"].select { |f|
784
+ yaml_files = Dir["#{path}{.yml,/{**,*}/*.yml}"].select { |f|
720
785
  ::File.file?(f)
721
- } + [yaml_file_path(path)]
786
+ }
787
+
788
+ raise ArgumentError, "No fixture files found for #{@name}" if yaml_files.empty?
722
789
 
723
790
  yaml_files.each_with_object({}) do |file, fixtures|
724
791
  FixtureSet::File.open(file) do |fh|
725
792
  self.model_class ||= fh.model_class if fh.model_class
793
+ self.model_class ||= default_fixture_model_class
726
794
  self.ignored_fixtures ||= fh.ignored_fixtures
727
795
  fh.each do |fixture_name, row|
728
796
  fixtures[fixture_name] = ActiveRecord::Fixture.new(row, model_class)
@@ -731,8 +799,9 @@ module ActiveRecord
731
799
  end
732
800
  end
733
801
 
734
- def yaml_file_path(path)
735
- "#{path}.yml"
802
+ def default_fixture_model_class
803
+ klass = ActiveRecord::FixtureSet.default_fixture_model_name(@name, @config).safe_constantize
804
+ klass if klass && klass < ActiveRecord::Base
736
805
  end
737
806
  end
738
807
 
@@ -769,7 +838,8 @@ module ActiveRecord
769
838
  def find
770
839
  raise FixtureClassNotFound, "No class attached to find." unless model_class
771
840
  object = model_class.unscoped do
772
- model_class.find(fixture[model_class.primary_key])
841
+ pk_clauses = fixture.slice(*Array(model_class.primary_key))
842
+ model_class.find_by!(pk_clauses)
773
843
  end
774
844
  # Fixtures can't be eagerly loaded
775
845
  object.instance_variable_set(:@strict_loading, false)
@@ -2,6 +2,27 @@
2
2
 
3
3
  module ActiveRecord
4
4
  class FutureResult # :nodoc:
5
+ class Complete
6
+ attr_reader :result
7
+ delegate :empty?, :to_a, to: :result
8
+
9
+ def initialize(result)
10
+ @result = result
11
+ end
12
+
13
+ def pending?
14
+ false
15
+ end
16
+
17
+ def canceled?
18
+ false
19
+ end
20
+
21
+ def then(&block)
22
+ Promise::Complete.new(@result.then(&block))
23
+ end
24
+ end
25
+
5
26
  class EventBuffer
6
27
  def initialize(future_result, instrumenter)
7
28
  @future_result = future_result
@@ -11,8 +32,11 @@ module ActiveRecord
11
32
 
12
33
  def instrument(name, payload = {}, &block)
13
34
  event = @instrumenter.new_event(name, payload)
14
- @events << event
15
- event.record(&block)
35
+ begin
36
+ event.record(&block)
37
+ ensure
38
+ @events << event
39
+ end
16
40
  end
17
41
 
18
42
  def flush
@@ -26,6 +50,15 @@ module ActiveRecord
26
50
 
27
51
  Canceled = Class.new(ActiveRecordError)
28
52
 
53
+ def self.wrap(result)
54
+ case result
55
+ when self, Complete
56
+ result
57
+ else
58
+ Complete.new(result)
59
+ end
60
+ end
61
+
29
62
  delegate :empty?, :to_a, to: :result
30
63
 
31
64
  attr_reader :lock_wait
@@ -45,6 +78,10 @@ module ActiveRecord
45
78
  @event_buffer = nil
46
79
  end
47
80
 
81
+ def then(&block)
82
+ Promise.new(self, block)
83
+ end
84
+
48
85
  def schedule!(session)
49
86
  @session = session
50
87
  @pool.schedule_query(self)
@@ -95,17 +132,19 @@ module ActiveRecord
95
132
  @pending && (!@session || @session.active?)
96
133
  end
97
134
 
98
- private
99
- def canceled?
100
- @session && !@session.active?
101
- end
135
+ def canceled?
136
+ @session && !@session.active?
137
+ end
102
138
 
139
+ private
103
140
  def execute_or_wait
104
141
  if pending?
105
142
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
106
143
  @mutex.synchronize do
107
144
  if pending?
108
- execute_query(@pool.connection)
145
+ @pool.with_connection do |connection|
146
+ execute_query(connection)
147
+ end
109
148
  else
110
149
  @lock_wait = (Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start)
111
150
  end
@@ -124,7 +163,7 @@ module ActiveRecord
124
163
  end
125
164
 
126
165
  def exec_query(connection, *args, **kwargs)
127
- connection.exec_query(*args, **kwargs)
166
+ connection.internal_exec_query(*args, **kwargs)
128
167
  end
129
168
 
130
169
  class SelectAll < FutureResult # :nodoc:
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecord
4
- # Returns the currently loaded version of Active Record as a <tt>Gem::Version</tt>.
4
+ # Returns the currently loaded version of Active Record as a +Gem::Version+.
5
5
  def self.gem_version
6
6
  Gem::Version.new VERSION::STRING
7
7
  end
8
8
 
9
9
  module VERSION
10
10
  MAJOR = 7
11
- MINOR = 0
12
- TINY = 8
11
+ MINOR = 2
12
+ TINY = 2
13
13
  PRE = "1"
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")