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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2030 -1020
- data/MIT-LICENSE +1 -1
- data/README.rdoc +18 -18
- data/lib/active_record/aggregations.rb +17 -14
- data/lib/active_record/association_relation.rb +1 -11
- data/lib/active_record/associations/association.rb +51 -19
- data/lib/active_record/associations/association_scope.rb +17 -12
- data/lib/active_record/associations/belongs_to_association.rb +28 -9
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
- data/lib/active_record/associations/builder/association.rb +11 -5
- data/lib/active_record/associations/builder/belongs_to.rb +40 -14
- data/lib/active_record/associations/builder/collection_association.rb +10 -3
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
- data/lib/active_record/associations/builder/has_many.rb +3 -2
- data/lib/active_record/associations/builder/has_one.rb +2 -1
- data/lib/active_record/associations/builder/singular_association.rb +6 -2
- data/lib/active_record/associations/collection_association.rb +39 -35
- data/lib/active_record/associations/collection_proxy.rb +30 -15
- data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +28 -18
- data/lib/active_record/associations/has_many_through_association.rb +12 -7
- data/lib/active_record/associations/has_one_association.rb +20 -10
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +3 -2
- data/lib/active_record/associations/join_dependency.rb +28 -20
- data/lib/active_record/associations/preloader/association.rb +210 -52
- data/lib/active_record/associations/preloader/batch.rb +48 -0
- data/lib/active_record/associations/preloader/branch.rb +147 -0
- data/lib/active_record/associations/preloader/through_association.rb +50 -14
- data/lib/active_record/associations/preloader.rb +50 -121
- data/lib/active_record/associations/singular_association.rb +9 -3
- data/lib/active_record/associations/through_association.rb +25 -14
- data/lib/active_record/associations.rb +446 -306
- data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
- data/lib/active_record/attribute_assignment.rb +1 -3
- data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
- data/lib/active_record/attribute_methods/dirty.rb +73 -22
- data/lib/active_record/attribute_methods/primary_key.rb +78 -26
- data/lib/active_record/attribute_methods/query.rb +31 -19
- data/lib/active_record/attribute_methods/read.rb +27 -12
- data/lib/active_record/attribute_methods/serialization.rb +194 -37
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +8 -3
- data/lib/active_record/attribute_methods/write.rb +12 -15
- data/lib/active_record/attribute_methods.rb +161 -40
- data/lib/active_record/attributes.rb +27 -38
- data/lib/active_record/autosave_association.rb +65 -31
- data/lib/active_record/base.rb +25 -2
- data/lib/active_record/callbacks.rb +18 -34
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -46
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +113 -597
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +367 -141
- data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
- data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -150
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +317 -164
- data/lib/active_record/connection_adapters/column.rb +13 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
- data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +39 -14
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +112 -55
- data/lib/active_record/connection_adapters/pool_config.rb +20 -11
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +397 -75
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +508 -246
- data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
- data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +296 -104
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +258 -0
- data/lib/active_record/connection_adapters.rb +9 -6
- data/lib/active_record/connection_handling.rb +108 -137
- data/lib/active_record/core.rb +242 -233
- data/lib/active_record/counter_cache.rb +52 -27
- data/lib/active_record/database_configurations/connection_url_resolver.rb +3 -2
- data/lib/active_record/database_configurations/database_config.rb +21 -12
- data/lib/active_record/database_configurations/hash_config.rb +88 -16
- data/lib/active_record/database_configurations/url_config.rb +18 -12
- data/lib/active_record/database_configurations.rb +95 -59
- data/lib/active_record/delegated_type.rb +66 -20
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +4 -2
- data/lib/active_record/disable_joins_association_relation.rb +39 -0
- data/lib/active_record/dynamic_matchers.rb +1 -1
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
- data/lib/active_record/encryption/cipher.rb +53 -0
- data/lib/active_record/encryption/config.rb +68 -0
- data/lib/active_record/encryption/configurable.rb +60 -0
- data/lib/active_record/encryption/context.rb +42 -0
- data/lib/active_record/encryption/contexts.rb +76 -0
- data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
- data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
- data/lib/active_record/encryption/encryptable_record.rb +230 -0
- data/lib/active_record/encryption/encrypted_attribute_type.rb +155 -0
- data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
- data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
- data/lib/active_record/encryption/encryptor.rb +155 -0
- data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
- data/lib/active_record/encryption/errors.rb +15 -0
- data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
- data/lib/active_record/encryption/key.rb +28 -0
- data/lib/active_record/encryption/key_generator.rb +53 -0
- data/lib/active_record/encryption/key_provider.rb +46 -0
- data/lib/active_record/encryption/message.rb +33 -0
- data/lib/active_record/encryption/message_serializer.rb +92 -0
- data/lib/active_record/encryption/null_encryptor.rb +21 -0
- data/lib/active_record/encryption/properties.rb +76 -0
- data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
- data/lib/active_record/encryption/scheme.rb +100 -0
- data/lib/active_record/encryption.rb +58 -0
- data/lib/active_record/enum.rb +154 -63
- data/lib/active_record/errors.rb +172 -15
- data/lib/active_record/explain.rb +23 -3
- data/lib/active_record/explain_registry.rb +11 -6
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +15 -1
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +70 -14
- data/lib/active_record/fixture_set/table_rows.rb +4 -4
- data/lib/active_record/fixtures.rb +147 -86
- data/lib/active_record/future_result.rb +174 -0
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +81 -29
- data/lib/active_record/insert_all.rb +135 -22
- data/lib/active_record/integration.rb +11 -10
- data/lib/active_record/internal_metadata.rb +119 -33
- data/lib/active_record/legacy_yaml_adapter.rb +2 -39
- data/lib/active_record/locking/optimistic.rb +37 -22
- data/lib/active_record/locking/pessimistic.rb +15 -6
- data/lib/active_record/log_subscriber.rb +52 -19
- data/lib/active_record/marshalling.rb +59 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
- data/lib/active_record/middleware/database_selector.rb +23 -13
- data/lib/active_record/middleware/shard_selector.rb +62 -0
- data/lib/active_record/migration/command_recorder.rb +112 -14
- data/lib/active_record/migration/compatibility.rb +233 -46
- data/lib/active_record/migration/default_strategy.rb +23 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/join_table.rb +1 -1
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +361 -173
- data/lib/active_record/model_schema.rb +125 -101
- data/lib/active_record/nested_attributes.rb +50 -20
- data/lib/active_record/no_touching.rb +3 -3
- data/lib/active_record/normalization.rb +167 -0
- data/lib/active_record/persistence.rb +409 -88
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +4 -22
- data/lib/active_record/query_logs.rb +174 -0
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +29 -6
- data/lib/active_record/railtie.rb +220 -44
- data/lib/active_record/railties/controller_runtime.rb +15 -10
- data/lib/active_record/railties/databases.rake +188 -252
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +41 -3
- data/lib/active_record/reflection.rb +248 -81
- data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
- data/lib/active_record/relation/batches.rb +192 -63
- data/lib/active_record/relation/calculations.rb +246 -90
- data/lib/active_record/relation/delegation.rb +28 -14
- data/lib/active_record/relation/finder_methods.rb +108 -51
- data/lib/active_record/relation/merger.rb +22 -13
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +27 -20
- data/lib/active_record/relation/query_attribute.rb +30 -12
- data/lib/active_record/relation/query_methods.rb +670 -129
- data/lib/active_record/relation/record_fetch_warning.rb +7 -9
- data/lib/active_record/relation/spawn_methods.rb +20 -3
- data/lib/active_record/relation/where_clause.rb +10 -19
- data/lib/active_record/relation.rb +287 -120
- data/lib/active_record/result.rb +37 -11
- data/lib/active_record/runtime_registry.rb +32 -13
- data/lib/active_record/sanitization.rb +65 -20
- data/lib/active_record/schema.rb +36 -22
- data/lib/active_record/schema_dumper.rb +73 -24
- data/lib/active_record/schema_migration.rb +68 -33
- data/lib/active_record/scoping/default.rb +72 -15
- data/lib/active_record/scoping/named.rb +5 -13
- data/lib/active_record/scoping.rb +65 -34
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/serialization.rb +6 -1
- data/lib/active_record/signed_id.rb +10 -8
- data/lib/active_record/store.rb +10 -10
- data/lib/active_record/suppressor.rb +13 -15
- data/lib/active_record/table_metadata.rb +16 -3
- data/lib/active_record/tasks/database_tasks.rb +251 -140
- data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
- data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
- data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
- data/lib/active_record/test_databases.rb +1 -1
- data/lib/active_record/test_fixtures.rb +117 -96
- data/lib/active_record/timestamp.rb +32 -19
- data/lib/active_record/token_for.rb +113 -0
- data/lib/active_record/touch_later.rb +11 -6
- data/lib/active_record/transactions.rb +48 -27
- data/lib/active_record/translation.rb +3 -3
- data/lib/active_record/type/adapter_specific_registry.rb +32 -14
- data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +9 -5
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/type/type_map.rb +17 -20
- data/lib/active_record/type.rb +1 -2
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +4 -4
- data/lib/active_record/validations/numericality.rb +5 -4
- data/lib/active_record/validations/presence.rb +5 -28
- data/lib/active_record/validations/uniqueness.rb +51 -6
- data/lib/active_record/validations.rb +8 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +335 -32
- data/lib/arel/attributes/attribute.rb +0 -8
- data/lib/arel/crud.rb +28 -22
- data/lib/arel/delete_manager.rb +18 -4
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/filter_predications.rb +9 -0
- data/lib/arel/insert_manager.rb +2 -3
- data/lib/arel/nodes/and.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -1
- data/lib/arel/nodes/bound_sql_literal.rb +61 -0
- data/lib/arel/nodes/casted.rb +1 -1
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/delete_statement.rb +12 -13
- data/lib/arel/nodes/filter.rb +10 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/function.rb +1 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/insert_statement.rb +2 -2
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/node.rb +111 -2
- data/lib/arel/nodes/select_core.rb +2 -2
- data/lib/arel/nodes/select_statement.rb +2 -2
- data/lib/arel/nodes/sql_literal.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes/update_statement.rb +8 -3
- data/lib/arel/nodes.rb +5 -0
- data/lib/arel/predications.rb +13 -3
- data/lib/arel/select_manager.rb +10 -4
- data/lib/arel/table.rb +9 -6
- data/lib/arel/tree_manager.rb +5 -13
- data/lib/arel/update_manager.rb +18 -4
- data/lib/arel/visitors/dot.rb +80 -90
- data/lib/arel/visitors/mysql.rb +16 -3
- data/lib/arel/visitors/postgresql.rb +0 -10
- data/lib/arel/visitors/to_sql.rb +141 -20
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +18 -3
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
- data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
- data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
- metadata +96 -16
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -67
@@ -3,11 +3,12 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Associations
|
5
5
|
# = Active Record Has Many Association
|
6
|
+
#
|
6
7
|
# This is the proxy that handles a has many association.
|
7
8
|
#
|
8
9
|
# If the association has a <tt>:through</tt> option further specialization
|
9
10
|
# is provided by its child HasManyThroughAssociation.
|
10
|
-
class HasManyAssociation < CollectionAssociation
|
11
|
+
class HasManyAssociation < CollectionAssociation # :nodoc:
|
11
12
|
include ForeignAssociation
|
12
13
|
|
13
14
|
def handle_dependency
|
@@ -33,20 +34,24 @@ module ActiveRecord
|
|
33
34
|
|
34
35
|
unless target.empty?
|
35
36
|
association_class = target.first.class
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
if association_class.query_constraints_list
|
38
|
+
primary_key_column = association_class.query_constraints_list
|
39
|
+
ids = target.collect { |assoc| primary_key_column.map { |col| assoc.public_send(col) } }
|
40
|
+
else
|
41
|
+
primary_key_column = association_class.primary_key
|
42
|
+
ids = target.collect { |assoc| assoc.public_send(primary_key_column) }
|
40
43
|
end
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
ids.each_slice(owner.class.destroy_association_async_batch_size || ids.size) do |ids_batch|
|
46
|
+
enqueue_destroy_association(
|
47
|
+
owner_model_name: owner.class.to_s,
|
48
|
+
owner_id: owner.id,
|
49
|
+
association_class: reflection.klass.to_s,
|
50
|
+
association_ids: ids_batch,
|
51
|
+
association_primary_key_column: primary_key_column,
|
52
|
+
ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
|
53
|
+
)
|
54
|
+
end
|
50
55
|
end
|
51
56
|
else
|
52
57
|
delete_all
|
@@ -79,10 +84,13 @@ module ActiveRecord
|
|
79
84
|
scope.count(:all)
|
80
85
|
end
|
81
86
|
|
82
|
-
# If there's nothing in the database
|
83
|
-
#
|
84
|
-
#
|
85
|
-
|
87
|
+
# If there's nothing in the database, @target should only contain new
|
88
|
+
# records or be an empty array. This is a documented side-effect of
|
89
|
+
# the method that may avoid an extra SELECT.
|
90
|
+
if count == 0
|
91
|
+
target.select!(&:new_record?)
|
92
|
+
loaded!
|
93
|
+
end
|
86
94
|
|
87
95
|
[association_scope.limit_value, count].compact.min
|
88
96
|
end
|
@@ -121,7 +129,9 @@ module ActiveRecord
|
|
121
129
|
records.each(&:destroy!)
|
122
130
|
update_counter(-records.length) unless reflection.inverse_updates_counter_cache?
|
123
131
|
else
|
124
|
-
|
132
|
+
query_constraints = reflection.klass.composite_query_constraints_list
|
133
|
+
values = records.map { |r| query_constraints.map { |col| r._read_attribute(col) } }
|
134
|
+
scope = self.scope.where(query_constraints => values)
|
125
135
|
update_counter(-delete_count(method, scope))
|
126
136
|
end
|
127
137
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Associations
|
5
5
|
# = Active Record Has Many Through Association
|
6
|
-
class HasManyThroughAssociation < HasManyAssociation
|
6
|
+
class HasManyThroughAssociation < HasManyAssociation # :nodoc:
|
7
7
|
include ThroughAssociation
|
8
8
|
|
9
9
|
def initialize(owner, reflection)
|
@@ -59,9 +59,10 @@ module ActiveRecord
|
|
59
59
|
|
60
60
|
attributes = through_scope_attributes
|
61
61
|
attributes[source_reflection.name] = record
|
62
|
-
attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
|
63
62
|
|
64
|
-
through_association.build(attributes)
|
63
|
+
through_association.build(attributes).tap do |new_record|
|
64
|
+
new_record.send("#{source_reflection.foreign_type}=", options[:source_type]) if options[:source_type]
|
65
|
+
end
|
65
66
|
end
|
66
67
|
end
|
67
68
|
|
@@ -69,9 +70,12 @@ module ActiveRecord
|
|
69
70
|
|
70
71
|
def through_scope_attributes
|
71
72
|
scope = through_scope || self.scope
|
72
|
-
scope.where_values_hash(through_association.reflection.
|
73
|
-
|
74
|
-
|
73
|
+
attributes = scope.where_values_hash(through_association.reflection.klass.table_name)
|
74
|
+
except_keys = [
|
75
|
+
*Array(through_association.reflection.foreign_key),
|
76
|
+
through_association.reflection.klass.inheritance_column
|
77
|
+
]
|
78
|
+
attributes.except!(*except_keys)
|
75
79
|
end
|
76
80
|
|
77
81
|
def save_through_record(record)
|
@@ -109,7 +113,7 @@ module ActiveRecord
|
|
109
113
|
end
|
110
114
|
|
111
115
|
def target_reflection_has_associated_record?
|
112
|
-
!(through_reflection.belongs_to? &&
|
116
|
+
!(through_reflection.belongs_to? && Array(through_reflection.foreign_key).all? { |foreign_key_column| owner[foreign_key_column].blank? })
|
113
117
|
end
|
114
118
|
|
115
119
|
def update_through_counter?(method)
|
@@ -214,6 +218,7 @@ module ActiveRecord
|
|
214
218
|
|
215
219
|
def find_target
|
216
220
|
return [] unless target_reflection_has_associated_record?
|
221
|
+
return scope.to_a if disable_joins
|
217
222
|
super
|
218
223
|
end
|
219
224
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Associations
|
5
5
|
# = Active Record Has One Association
|
6
|
-
class HasOneAssociation < SingularAssociation
|
6
|
+
class HasOneAssociation < SingularAssociation # :nodoc:
|
7
7
|
include ForeignAssociation
|
8
8
|
|
9
9
|
def handle_dependency
|
@@ -33,8 +33,13 @@ module ActiveRecord
|
|
33
33
|
target.destroy
|
34
34
|
throw(:abort) unless target.destroyed?
|
35
35
|
when :destroy_async
|
36
|
-
|
37
|
-
|
36
|
+
if target.class.query_constraints_list
|
37
|
+
primary_key_column = target.class.query_constraints_list
|
38
|
+
id = primary_key_column.map { |col| target.public_send(col) }
|
39
|
+
else
|
40
|
+
primary_key_column = target.class.primary_key
|
41
|
+
id = target.public_send(primary_key_column)
|
42
|
+
end
|
38
43
|
|
39
44
|
enqueue_destroy_association(
|
40
45
|
owner_model_name: owner.class.to_s,
|
@@ -70,7 +75,7 @@ module ActiveRecord
|
|
70
75
|
if save && !record.save
|
71
76
|
nullify_owner_attributes(record)
|
72
77
|
set_owner_attributes(target) if target
|
73
|
-
raise RecordNotSaved
|
78
|
+
raise RecordNotSaved.new("Failed to save the new associated #{reflection.name}.", record)
|
74
79
|
end
|
75
80
|
end
|
76
81
|
end
|
@@ -102,19 +107,24 @@ module ActiveRecord
|
|
102
107
|
|
103
108
|
if target.persisted? && owner.persisted? && !target.save
|
104
109
|
set_owner_attributes(target)
|
105
|
-
raise RecordNotSaved
|
106
|
-
|
110
|
+
raise RecordNotSaved.new(
|
111
|
+
"Failed to remove the existing associated #{reflection.name}. " \
|
112
|
+
"The record failed to save after its foreign key was set to nil.",
|
113
|
+
target
|
114
|
+
)
|
107
115
|
end
|
108
116
|
end
|
109
117
|
end
|
110
118
|
|
111
119
|
def nullify_owner_attributes(record)
|
112
|
-
|
120
|
+
Array(reflection.foreign_key).each do |foreign_key_column|
|
121
|
+
record[foreign_key_column] = nil unless foreign_key_column.in?(Array(record.class.primary_key))
|
122
|
+
end
|
113
123
|
end
|
114
124
|
|
115
|
-
def transaction_if(value)
|
125
|
+
def transaction_if(value, &block)
|
116
126
|
if value
|
117
|
-
reflection.klass.transaction
|
127
|
+
reflection.klass.transaction(&block)
|
118
128
|
else
|
119
129
|
yield
|
120
130
|
end
|
@@ -122,7 +132,7 @@ module ActiveRecord
|
|
122
132
|
|
123
133
|
def _create_record(attributes, raise_error = false, &block)
|
124
134
|
unless owner.persisted?
|
125
|
-
raise ActiveRecord::RecordNotSaved
|
135
|
+
raise ActiveRecord::RecordNotSaved.new("You cannot call create unless the parent is saved", owner)
|
126
136
|
end
|
127
137
|
|
128
138
|
super
|
@@ -25,8 +25,9 @@ module ActiveRecord
|
|
25
25
|
joins = []
|
26
26
|
chain = []
|
27
27
|
|
28
|
-
reflection.chain
|
29
|
-
|
28
|
+
reflection_chain = reflection.chain
|
29
|
+
reflection_chain.each_with_index do |reflection, index|
|
30
|
+
table, terminated = yield reflection, reflection_chain[index..]
|
30
31
|
@table ||= table
|
31
32
|
|
32
33
|
if terminated
|
@@ -3,8 +3,12 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Associations
|
5
5
|
class JoinDependency # :nodoc:
|
6
|
-
|
7
|
-
|
6
|
+
extend ActiveSupport::Autoload
|
7
|
+
|
8
|
+
eager_autoload do
|
9
|
+
autoload :JoinBase
|
10
|
+
autoload :JoinAssociation
|
11
|
+
end
|
8
12
|
|
9
13
|
class Aliases # :nodoc:
|
10
14
|
def initialize(tables)
|
@@ -57,7 +61,7 @@ module ActiveRecord
|
|
57
61
|
when Hash
|
58
62
|
associations.each do |k, v|
|
59
63
|
cache = hash[k] ||= {}
|
60
|
-
walk_tree v, cache
|
64
|
+
walk_tree v, cache if v
|
61
65
|
end
|
62
66
|
else
|
63
67
|
raise ConfigurationError, associations.inspect
|
@@ -186,12 +190,12 @@ module ActiveRecord
|
|
186
190
|
def make_constraints(parent, child, join_type)
|
187
191
|
foreign_table = parent.table
|
188
192
|
foreign_klass = parent.base_klass
|
189
|
-
child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker) do |reflection|
|
190
|
-
table, terminated = @joined_tables[
|
193
|
+
child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker) do |reflection, remaining_reflection_chain|
|
194
|
+
table, terminated = @joined_tables[remaining_reflection_chain]
|
191
195
|
root = reflection == child.reflection
|
192
196
|
|
193
197
|
if table && (!root || !terminated)
|
194
|
-
@joined_tables[
|
198
|
+
@joined_tables[remaining_reflection_chain] = [table, root] if root
|
195
199
|
next table, true
|
196
200
|
end
|
197
201
|
|
@@ -202,7 +206,7 @@ module ActiveRecord
|
|
202
206
|
root ? name : "#{name}_join"
|
203
207
|
end
|
204
208
|
|
205
|
-
@joined_tables[
|
209
|
+
@joined_tables[remaining_reflection_chain] ||= [table, root] if join_type == Arel::Nodes::OuterJoin
|
206
210
|
table
|
207
211
|
end.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
|
208
212
|
end
|
@@ -248,35 +252,39 @@ module ActiveRecord
|
|
248
252
|
next
|
249
253
|
end
|
250
254
|
|
251
|
-
|
252
|
-
|
253
|
-
|
255
|
+
if node.primary_key
|
256
|
+
keys = Array(node.primary_key).map { |column| aliases.column_alias(node, column) }
|
257
|
+
id = keys.map { |key| row[key] }
|
258
|
+
else
|
259
|
+
keys = Array(node.reflection.join_primary_key).map { |column| aliases.column_alias(node, column.to_s) }
|
260
|
+
id = keys.map { nil } # Avoid id-based model caching.
|
261
|
+
end
|
262
|
+
|
263
|
+
if keys.any? { |key| row[key].nil? }
|
254
264
|
nil_association = ar_parent.association(node.reflection.name)
|
255
265
|
nil_association.loaded!
|
256
266
|
next
|
257
267
|
end
|
258
268
|
|
259
|
-
model = seen[ar_parent][node][id]
|
260
|
-
|
261
|
-
if model
|
262
|
-
construct(model, node, row, seen, model_cache, strict_loading_value)
|
263
|
-
else
|
269
|
+
unless model = seen[ar_parent][node][id]
|
264
270
|
model = construct_model(ar_parent, node, row, model_cache, id, strict_loading_value)
|
265
|
-
|
266
|
-
seen[ar_parent][node][id] = model
|
267
|
-
construct(model, node, row, seen, model_cache, strict_loading_value)
|
271
|
+
seen[ar_parent][node][id] = model if id
|
268
272
|
end
|
273
|
+
|
274
|
+
construct(model, node, row, seen, model_cache, strict_loading_value)
|
269
275
|
end
|
270
276
|
end
|
271
277
|
|
272
278
|
def construct_model(record, node, row, model_cache, id, strict_loading_value)
|
273
279
|
other = record.association(node.reflection.name)
|
274
280
|
|
275
|
-
model = model_cache[node][id]
|
276
|
-
node.instantiate(row, aliases.column_aliases(node)) do |m|
|
281
|
+
unless model = model_cache[node][id]
|
282
|
+
model = node.instantiate(row, aliases.column_aliases(node)) do |m|
|
277
283
|
m.strict_loading! if strict_loading_value
|
278
284
|
other.set_inverse_instance(m)
|
279
285
|
end
|
286
|
+
model_cache[node][id] = model if id
|
287
|
+
end
|
280
288
|
|
281
289
|
if node.reflection.collection?
|
282
290
|
other.target.push(model)
|
@@ -1,19 +1,141 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# :enddoc:
|
4
|
+
|
3
5
|
module ActiveRecord
|
4
6
|
module Associations
|
5
7
|
class Preloader
|
6
|
-
class Association
|
7
|
-
|
8
|
+
class Association # :nodoc:
|
9
|
+
class LoaderQuery
|
10
|
+
attr_reader :scope, :association_key_name
|
11
|
+
|
12
|
+
def initialize(scope, association_key_name)
|
13
|
+
@scope = scope
|
14
|
+
@association_key_name = association_key_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def eql?(other)
|
18
|
+
association_key_name == other.association_key_name &&
|
19
|
+
scope.table_name == other.scope.table_name &&
|
20
|
+
scope.connection_specification_name == other.scope.connection_specification_name &&
|
21
|
+
scope.values_for_queries == other.scope.values_for_queries
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash
|
25
|
+
[association_key_name, scope.table_name, scope.connection_specification_name, scope.values_for_queries].hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def records_for(loaders)
|
29
|
+
LoaderRecords.new(loaders, self).records
|
30
|
+
end
|
31
|
+
|
32
|
+
def load_records_in_batch(loaders)
|
33
|
+
raw_records = records_for(loaders)
|
34
|
+
|
35
|
+
loaders.each do |loader|
|
36
|
+
loader.load_records(raw_records)
|
37
|
+
loader.run
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def load_records_for_keys(keys, &block)
|
42
|
+
return [] if keys.empty?
|
43
|
+
|
44
|
+
if association_key_name.is_a?(Array)
|
45
|
+
query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }
|
46
|
+
|
47
|
+
keys.each_with_object(query_constraints) do |values_set, constraints|
|
48
|
+
association_key_name.zip(values_set).each do |key_name, value|
|
49
|
+
constraints[key_name] << value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
scope.where(query_constraints)
|
54
|
+
else
|
55
|
+
scope.where(association_key_name => keys)
|
56
|
+
end.load(&block)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class LoaderRecords
|
61
|
+
def initialize(loaders, loader_query)
|
62
|
+
@loader_query = loader_query
|
63
|
+
@loaders = loaders
|
64
|
+
@keys_to_load = Set.new
|
65
|
+
@already_loaded_records_by_key = {}
|
66
|
+
|
67
|
+
populate_keys_to_load_and_already_loaded_records
|
68
|
+
end
|
69
|
+
|
70
|
+
def records
|
71
|
+
load_records + already_loaded_records
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
attr_reader :loader_query, :loaders, :keys_to_load, :already_loaded_records_by_key
|
76
|
+
|
77
|
+
def populate_keys_to_load_and_already_loaded_records
|
78
|
+
loaders.each do |loader|
|
79
|
+
loader.owners_by_key.each do |key, owners|
|
80
|
+
if loaded_owner = owners.find { |owner| loader.loaded?(owner) }
|
81
|
+
already_loaded_records_by_key[key] = loader.target_for(loaded_owner)
|
82
|
+
else
|
83
|
+
keys_to_load << key
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
@keys_to_load.subtract(already_loaded_records_by_key.keys)
|
89
|
+
end
|
90
|
+
|
91
|
+
def load_records
|
92
|
+
loader_query.load_records_for_keys(keys_to_load) do |record|
|
93
|
+
loaders.each { |l| l.set_inverse(record) }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def already_loaded_records
|
98
|
+
already_loaded_records_by_key.values.flatten
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
attr_reader :klass
|
103
|
+
|
104
|
+
def initialize(klass, owners, reflection, preload_scope, reflection_scope, associate_by_default)
|
8
105
|
@klass = klass
|
9
106
|
@owners = owners.uniq(&:__id__)
|
10
107
|
@reflection = reflection
|
11
108
|
@preload_scope = preload_scope
|
109
|
+
@reflection_scope = reflection_scope
|
12
110
|
@associate = associate_by_default || !preload_scope || preload_scope.empty_scope?
|
13
111
|
@model = owners.first && owners.first.class
|
112
|
+
@run = false
|
113
|
+
end
|
114
|
+
|
115
|
+
def table_name
|
116
|
+
@klass.table_name
|
117
|
+
end
|
118
|
+
|
119
|
+
def future_classes
|
120
|
+
if run?
|
121
|
+
[]
|
122
|
+
else
|
123
|
+
[@klass]
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def runnable_loaders
|
128
|
+
[self]
|
129
|
+
end
|
130
|
+
|
131
|
+
def run?
|
132
|
+
@run
|
14
133
|
end
|
15
134
|
|
16
135
|
def run
|
136
|
+
return self if run?
|
137
|
+
@run = true
|
138
|
+
|
17
139
|
records = records_by_owner
|
18
140
|
|
19
141
|
owners.each do |owner|
|
@@ -35,35 +157,85 @@ module ActiveRecord
|
|
35
157
|
@preloaded_records
|
36
158
|
end
|
37
159
|
|
38
|
-
|
39
|
-
|
160
|
+
# The name of the key on the associated records
|
161
|
+
def association_key_name
|
162
|
+
reflection.join_primary_key(klass)
|
163
|
+
end
|
40
164
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
@records_by_owner = {}.compare_by_identity
|
45
|
-
raw_records = owner_keys.empty? ? [] : records_for(owner_keys)
|
165
|
+
def loader_query
|
166
|
+
LoaderQuery.new(scope, association_key_name)
|
167
|
+
end
|
46
168
|
|
47
|
-
|
48
|
-
|
169
|
+
def owners_by_key
|
170
|
+
@owners_by_key ||= owners.each_with_object({}) do |owner, result|
|
171
|
+
key = derive_key(owner, owner_key_name)
|
172
|
+
(result[key] ||= []) << owner if key
|
173
|
+
end
|
174
|
+
end
|
49
175
|
|
50
|
-
|
51
|
-
|
176
|
+
def loaded?(owner)
|
177
|
+
owner.association(reflection.name).loaded?
|
178
|
+
end
|
52
179
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
180
|
+
def target_for(owner)
|
181
|
+
Array.wrap(owner.association(reflection.name).target)
|
182
|
+
end
|
58
183
|
|
59
|
-
|
184
|
+
def scope
|
185
|
+
@scope ||= build_scope
|
186
|
+
end
|
187
|
+
|
188
|
+
def set_inverse(record)
|
189
|
+
if owners = owners_by_key[derive_key(record, association_key_name)]
|
190
|
+
# Processing only the first owner
|
191
|
+
# because the record is modified but not an owner
|
192
|
+
association = owners.first.association(reflection.name)
|
193
|
+
association.set_inverse_instance(record)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def load_records(raw_records = nil)
|
198
|
+
# owners can be duplicated when a relation has a collection association join
|
199
|
+
# #compare_by_identity makes such owners different hash keys
|
200
|
+
@records_by_owner = {}.compare_by_identity
|
201
|
+
raw_records ||= loader_query.records_for([self])
|
202
|
+
@preloaded_records = raw_records.select do |record|
|
203
|
+
assignments = false
|
204
|
+
|
205
|
+
owners_by_key[derive_key(record, association_key_name)]&.each do |owner|
|
206
|
+
entries = (@records_by_owner[owner] ||= [])
|
207
|
+
|
208
|
+
if reflection.collection? || entries.empty?
|
209
|
+
entries << record
|
210
|
+
assignments = true
|
211
|
+
end
|
60
212
|
end
|
213
|
+
|
214
|
+
assignments
|
61
215
|
end
|
216
|
+
end
|
217
|
+
|
218
|
+
def associate_records_from_unscoped(unscoped_records)
|
219
|
+
return if unscoped_records.nil? || unscoped_records.empty?
|
220
|
+
return if !reflection_scope.empty_scope?
|
221
|
+
return if preload_scope && !preload_scope.empty_scope?
|
222
|
+
return if reflection.collection?
|
223
|
+
|
224
|
+
unscoped_records.select { |r| r[association_key_name].present? }.each do |record|
|
225
|
+
owners = owners_by_key[derive_key(record, association_key_name)]
|
226
|
+
owners&.each_with_index do |owner, i|
|
227
|
+
association = owner.association(reflection.name)
|
228
|
+
association.target = record
|
62
229
|
|
63
|
-
|
64
|
-
|
65
|
-
|
230
|
+
if i == 0 # Set inverse on first owner
|
231
|
+
association.set_inverse_instance(record)
|
232
|
+
end
|
233
|
+
end
|
66
234
|
end
|
235
|
+
end
|
236
|
+
|
237
|
+
private
|
238
|
+
attr_reader :owners, :reflection, :preload_scope, :model
|
67
239
|
|
68
240
|
# The name of the key on the model which declares the association
|
69
241
|
def owner_key_name
|
@@ -71,7 +243,10 @@ module ActiveRecord
|
|
71
243
|
end
|
72
244
|
|
73
245
|
def associate_records_to_owner(owner, records)
|
246
|
+
return if loaded?(owner)
|
247
|
+
|
74
248
|
association = owner.association(reflection.name)
|
249
|
+
|
75
250
|
if reflection.collection?
|
76
251
|
association.target = records
|
77
252
|
else
|
@@ -79,17 +254,6 @@ module ActiveRecord
|
|
79
254
|
end
|
80
255
|
end
|
81
256
|
|
82
|
-
def owner_keys
|
83
|
-
@owner_keys ||= owners_by_key.keys
|
84
|
-
end
|
85
|
-
|
86
|
-
def owners_by_key
|
87
|
-
@owners_by_key ||= owners.each_with_object({}) do |owner, result|
|
88
|
-
key = convert_key(owner[owner_key_name])
|
89
|
-
(result[key] ||= []) << owner if key
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
257
|
def key_conversion_required?
|
94
258
|
unless defined?(@key_conversion_required)
|
95
259
|
@key_conversion_required = (association_key_type != owner_key_type)
|
@@ -98,6 +262,14 @@ module ActiveRecord
|
|
98
262
|
@key_conversion_required
|
99
263
|
end
|
100
264
|
|
265
|
+
def derive_key(owner, key)
|
266
|
+
if key.is_a?(Array)
|
267
|
+
key.map { |k| convert_key(owner._read_attribute(k)) }
|
268
|
+
else
|
269
|
+
convert_key(owner._read_attribute(key))
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
101
273
|
def convert_key(key)
|
102
274
|
if key_conversion_required?
|
103
275
|
key.to_s
|
@@ -114,20 +286,6 @@ module ActiveRecord
|
|
114
286
|
@model.type_for_attribute(owner_key_name).type
|
115
287
|
end
|
116
288
|
|
117
|
-
def records_for(ids)
|
118
|
-
scope.where(association_key_name => ids).load do |record|
|
119
|
-
# Processing only the first owner
|
120
|
-
# because the record is modified but not an owner
|
121
|
-
owner = owners_by_key[convert_key(record[association_key_name])].first
|
122
|
-
association = owner.association(reflection.name)
|
123
|
-
association.set_inverse_instance(record)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def scope
|
128
|
-
@scope ||= build_scope
|
129
|
-
end
|
130
|
-
|
131
289
|
def reflection_scope
|
132
290
|
@reflection_scope ||= reflection.join_scopes(klass.arel_table, klass.predicate_builder, klass).inject(klass.unscoped, &:merge!)
|
133
291
|
end
|
@@ -145,11 +303,11 @@ module ActiveRecord
|
|
145
303
|
scope.merge!(preload_scope)
|
146
304
|
end
|
147
305
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
306
|
+
cascade_strict_loading(scope)
|
307
|
+
end
|
308
|
+
|
309
|
+
def cascade_strict_loading(scope)
|
310
|
+
preload_scope&.strict_loading_value ? scope.strict_loading : scope
|
153
311
|
end
|
154
312
|
end
|
155
313
|
end
|