activerecord 7.0.5 → 7.1.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1624 -1338
- data/MIT-LICENSE +1 -1
- data/README.rdoc +18 -18
- data/lib/active_record/aggregations.rb +16 -13
- data/lib/active_record/association_relation.rb +1 -1
- data/lib/active_record/associations/association.rb +20 -4
- data/lib/active_record/associations/association_scope.rb +16 -9
- data/lib/active_record/associations/belongs_to_association.rb +14 -6
- data/lib/active_record/associations/builder/association.rb +3 -3
- data/lib/active_record/associations/builder/belongs_to.rb +21 -8
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +16 -10
- data/lib/active_record/associations/collection_proxy.rb +20 -10
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +20 -13
- data/lib/active_record/associations/has_many_through_association.rb +10 -6
- data/lib/active_record/associations/has_one_association.rb +10 -7
- data/lib/active_record/associations/join_dependency.rb +10 -8
- data/lib/active_record/associations/preloader/association.rb +31 -7
- data/lib/active_record/associations/preloader.rb +13 -10
- data/lib/active_record/associations/singular_association.rb +6 -8
- data/lib/active_record/associations/through_association.rb +22 -11
- data/lib/active_record/associations.rb +313 -217
- data/lib/active_record/attribute_assignment.rb +0 -2
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
- data/lib/active_record/attribute_methods/dirty.rb +52 -34
- data/lib/active_record/attribute_methods/primary_key.rb +76 -24
- data/lib/active_record/attribute_methods/query.rb +28 -16
- data/lib/active_record/attribute_methods/read.rb +18 -5
- data/lib/active_record/attribute_methods/serialization.rb +150 -31
- data/lib/active_record/attribute_methods/write.rb +3 -3
- data/lib/active_record/attribute_methods.rb +105 -21
- data/lib/active_record/attributes.rb +3 -3
- data/lib/active_record/autosave_association.rb +55 -9
- data/lib/active_record/base.rb +7 -2
- data/lib/active_record/callbacks.rb +10 -24
- 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 -42
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +163 -88
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +74 -51
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +129 -31
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +62 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +41 -6
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +290 -125
- data/lib/active_record/connection_adapters/abstract/transaction.rb +287 -58
- data/lib/active_record/connection_adapters/abstract_adapter.rb +505 -102
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +214 -113
- data/lib/active_record/connection_adapters/column.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +23 -144
- data/lib/active_record/connection_adapters/mysql/quoting.rb +21 -14
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +18 -13
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
- data/lib/active_record/connection_adapters/pool_config.rb +14 -5
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +75 -41
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +15 -8
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +361 -60
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +353 -192
- data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
- data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +52 -39
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +9 -5
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +28 -9
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +211 -83
- 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 +262 -0
- data/lib/active_record/connection_adapters.rb +3 -1
- data/lib/active_record/connection_handling.rb +72 -95
- data/lib/active_record/core.rb +175 -153
- data/lib/active_record/counter_cache.rb +46 -25
- data/lib/active_record/database_configurations/database_config.rb +9 -3
- data/lib/active_record/database_configurations/hash_config.rb +22 -12
- data/lib/active_record/database_configurations/url_config.rb +17 -11
- data/lib/active_record/database_configurations.rb +86 -33
- data/lib/active_record/delegated_type.rb +9 -4
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +2 -0
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
- data/lib/active_record/encryption/config.rb +25 -1
- data/lib/active_record/encryption/configurable.rb +12 -19
- data/lib/active_record/encryption/context.rb +10 -3
- data/lib/active_record/encryption/contexts.rb +5 -1
- data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
- data/lib/active_record/encryption/encryptable_record.rb +42 -18
- data/lib/active_record/encryption/encrypted_attribute_type.rb +21 -6
- data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
- data/lib/active_record/encryption/key_generator.rb +12 -1
- data/lib/active_record/encryption/message_serializer.rb +2 -0
- data/lib/active_record/encryption/properties.rb +3 -3
- data/lib/active_record/encryption/scheme.rb +19 -22
- data/lib/active_record/encryption.rb +1 -0
- data/lib/active_record/enum.rb +112 -28
- data/lib/active_record/errors.rb +112 -18
- data/lib/active_record/explain.rb +23 -3
- 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 +29 -8
- data/lib/active_record/fixtures.rb +135 -71
- data/lib/active_record/future_result.rb +31 -5
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +30 -16
- data/lib/active_record/insert_all.rb +57 -10
- data/lib/active_record/integration.rb +8 -8
- data/lib/active_record/internal_metadata.rb +120 -30
- data/lib/active_record/locking/optimistic.rb +32 -18
- data/lib/active_record/locking/pessimistic.rb +5 -2
- data/lib/active_record/log_subscriber.rb +29 -12
- data/lib/active_record/marshalling.rb +56 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
- data/lib/active_record/middleware/database_selector.rb +6 -8
- data/lib/active_record/middleware/shard_selector.rb +3 -1
- data/lib/active_record/migration/command_recorder.rb +104 -5
- data/lib/active_record/migration/compatibility.rb +150 -58
- 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/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +271 -114
- data/lib/active_record/model_schema.rb +64 -44
- data/lib/active_record/nested_attributes.rb +24 -6
- data/lib/active_record/normalization.rb +167 -0
- data/lib/active_record/persistence.rb +195 -42
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +3 -21
- data/lib/active_record/query_logs.rb +77 -52
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +15 -2
- data/lib/active_record/railtie.rb +109 -47
- data/lib/active_record/railties/controller_runtime.rb +14 -9
- data/lib/active_record/railties/databases.rake +142 -148
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +32 -5
- data/lib/active_record/reflection.rb +182 -44
- data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
- data/lib/active_record/relation/batches.rb +190 -61
- data/lib/active_record/relation/calculations.rb +187 -63
- data/lib/active_record/relation/delegation.rb +23 -9
- data/lib/active_record/relation/finder_methods.rb +77 -16
- data/lib/active_record/relation/merger.rb +2 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +27 -16
- data/lib/active_record/relation/query_attribute.rb +25 -1
- data/lib/active_record/relation/query_methods.rb +386 -70
- data/lib/active_record/relation/spawn_methods.rb +18 -1
- data/lib/active_record/relation.rb +91 -35
- data/lib/active_record/result.rb +25 -9
- data/lib/active_record/runtime_registry.rb +24 -1
- data/lib/active_record/sanitization.rb +51 -11
- data/lib/active_record/schema.rb +2 -3
- data/lib/active_record/schema_dumper.rb +46 -7
- data/lib/active_record/schema_migration.rb +68 -33
- data/lib/active_record/scoping/default.rb +15 -5
- data/lib/active_record/scoping/named.rb +2 -2
- data/lib/active_record/scoping.rb +2 -1
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/signed_id.rb +7 -5
- data/lib/active_record/store.rb +8 -8
- data/lib/active_record/suppressor.rb +3 -1
- data/lib/active_record/table_metadata.rb +16 -3
- data/lib/active_record/tasks/database_tasks.rb +127 -105
- data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
- data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
- data/lib/active_record/test_fixtures.rb +113 -96
- data/lib/active_record/timestamp.rb +27 -15
- data/lib/active_record/token_for.rb +113 -0
- data/lib/active_record/touch_later.rb +11 -6
- data/lib/active_record/transactions.rb +39 -13
- data/lib/active_record/type/adapter_specific_registry.rb +1 -8
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +4 -0
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/validations/absence.rb +1 -1
- 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 +47 -2
- data/lib/active_record/validations.rb +8 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +121 -16
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- 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/cte.rb +36 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/node.rb +111 -2
- data/lib/arel/nodes/sql_literal.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes.rb +4 -0
- data/lib/arel/predications.rb +2 -0
- data/lib/arel/table.rb +9 -5
- data/lib/arel/visitors/mysql.rb +8 -1
- data/lib/arel/visitors/to_sql.rb +81 -17
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +16 -2
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- 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
- metadata +51 -15
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -63
@@ -9,7 +9,14 @@ module ActiveRecord
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def queries
|
12
|
-
|
12
|
+
if associated_table.join_foreign_key.is_a?(Array)
|
13
|
+
id_list = ids
|
14
|
+
id_list = id_list.pluck(primary_key) if id_list.is_a?(Relation)
|
15
|
+
|
16
|
+
id_list.map { |ids_set| associated_table.join_foreign_key.zip(ids_set).to_h }
|
17
|
+
else
|
18
|
+
[ associated_table.join_foreign_key => ids ]
|
19
|
+
end
|
13
20
|
end
|
14
21
|
|
15
22
|
private
|
@@ -18,11 +25,14 @@ module ActiveRecord
|
|
18
25
|
def ids
|
19
26
|
case value
|
20
27
|
when Relation
|
21
|
-
|
28
|
+
relation = value
|
29
|
+
relation = relation.select(primary_key) if select_clause?
|
30
|
+
relation = relation.where(primary_type => polymorphic_name) if polymorphic_clause?
|
31
|
+
relation
|
22
32
|
when Array
|
23
33
|
value.map { |v| convert_to_id(v) }
|
24
34
|
else
|
25
|
-
convert_to_id(value)
|
35
|
+
[convert_to_id(value)]
|
26
36
|
end
|
27
37
|
end
|
28
38
|
|
@@ -30,7 +40,25 @@ module ActiveRecord
|
|
30
40
|
associated_table.join_primary_key
|
31
41
|
end
|
32
42
|
|
43
|
+
def primary_type
|
44
|
+
associated_table.join_primary_type
|
45
|
+
end
|
46
|
+
|
47
|
+
def polymorphic_name
|
48
|
+
associated_table.polymorphic_name_association
|
49
|
+
end
|
50
|
+
|
51
|
+
def select_clause?
|
52
|
+
value.select_values.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
def polymorphic_clause?
|
56
|
+
primary_type && !value.where_values_hash.has_key?(primary_type)
|
57
|
+
end
|
58
|
+
|
33
59
|
def convert_to_id(value)
|
60
|
+
return primary_key.map { |pk| value.public_send(pk) } if primary_key.is_a?(Array)
|
61
|
+
|
34
62
|
if value.respond_to?(primary_key)
|
35
63
|
value.public_send(primary_key)
|
36
64
|
else
|
@@ -34,19 +34,17 @@ module ActiveRecord
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def klass(value)
|
37
|
-
|
38
|
-
when Base
|
37
|
+
if value.is_a?(Base)
|
39
38
|
value.class
|
40
|
-
|
39
|
+
elsif value.is_a?(Relation)
|
41
40
|
value.klass
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
44
|
def convert_to_id(value)
|
46
|
-
|
47
|
-
when Base
|
45
|
+
if value.is_a?(Base)
|
48
46
|
value._read_attribute(primary_key(value))
|
49
|
-
|
47
|
+
elsif value.is_a?(Relation)
|
50
48
|
value.select(primary_key(value))
|
51
49
|
else
|
52
50
|
value
|
@@ -9,7 +9,11 @@ module ActiveRecord
|
|
9
9
|
end
|
10
10
|
|
11
11
|
if value.select_values.empty?
|
12
|
-
|
12
|
+
if value.klass.composite_primary_key?
|
13
|
+
raise ArgumentError, "Cannot map composite primary key #{value.klass.primary_key} to #{attribute.name}"
|
14
|
+
else
|
15
|
+
value = value.select(value.table[value.klass.primary_key])
|
16
|
+
end
|
13
17
|
end
|
14
18
|
|
15
19
|
attribute.in(value.arel)
|
@@ -29,8 +29,8 @@ module ActiveRecord
|
|
29
29
|
attributes.each_with_object([]) do |(key, value), result|
|
30
30
|
if value.is_a?(Hash)
|
31
31
|
result << Arel.sql(key)
|
32
|
-
elsif key.
|
33
|
-
result << Arel.sql(key
|
32
|
+
elsif (idx = key.rindex("."))
|
33
|
+
result << Arel.sql(key[0, idx])
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -65,8 +65,7 @@ module ActiveRecord
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def build_bind_attribute(column_name, value)
|
68
|
-
|
69
|
-
Relation::QueryAttribute.new(column_name, type.immutable_value(value), type)
|
68
|
+
Relation::QueryAttribute.new(column_name, value, table.type(column_name))
|
70
69
|
end
|
71
70
|
|
72
71
|
def resolve_arel_attribute(table_name, column_name, &block)
|
@@ -78,7 +77,13 @@ module ActiveRecord
|
|
78
77
|
return ["1=0"] if attributes.empty?
|
79
78
|
|
80
79
|
attributes.flat_map do |key, value|
|
81
|
-
if
|
80
|
+
if key.is_a?(Array)
|
81
|
+
queries = Array(value).map do |ids_set|
|
82
|
+
raise ArgumentError, "Expected corresponding value for #{key} to be an Array" unless ids_set.is_a?(Array)
|
83
|
+
expand_from_hash(key.zip(ids_set).to_h)
|
84
|
+
end
|
85
|
+
grouping_queries(queries)
|
86
|
+
elsif value.is_a?(Hash) && !table.has_column?(key)
|
82
87
|
table.associated_table(key, &block)
|
83
88
|
.predicate_builder.expand_from_hash(value.stringify_keys)
|
84
89
|
elsif table.associated_with?(key)
|
@@ -143,19 +148,25 @@ module ActiveRecord
|
|
143
148
|
end
|
144
149
|
|
145
150
|
def convert_dot_notation_to_hash(attributes)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
151
|
+
attributes.each_with_object({}) do |(key, value), converted|
|
152
|
+
if value.is_a?(Hash)
|
153
|
+
if (existing = converted[key])
|
154
|
+
existing.merge!(value)
|
155
|
+
else
|
156
|
+
converted[key] = value.dup
|
157
|
+
end
|
158
|
+
elsif (idx = key.rindex("."))
|
159
|
+
table_name, column_name = key[0, idx], key[idx + 1, key.length]
|
154
160
|
|
155
|
-
|
161
|
+
if (existing = converted[table_name])
|
162
|
+
existing[column_name] = value
|
163
|
+
else
|
164
|
+
converted[table_name] = { column_name => value }
|
165
|
+
end
|
166
|
+
else
|
167
|
+
converted[key] = value
|
168
|
+
end
|
156
169
|
end
|
157
|
-
|
158
|
-
attributes
|
159
170
|
end
|
160
171
|
|
161
172
|
def handler_for(object)
|
@@ -5,12 +5,27 @@ require "active_model/attribute"
|
|
5
5
|
module ActiveRecord
|
6
6
|
class Relation
|
7
7
|
class QueryAttribute < ActiveModel::Attribute # :nodoc:
|
8
|
+
def initialize(...)
|
9
|
+
super
|
10
|
+
|
11
|
+
# The query attribute value may be mutated before we actually "compile" the query.
|
12
|
+
# To avoid that if the type uses a serializer we eagerly compute the value for database
|
13
|
+
if value_before_type_cast.is_a?(StatementCache::Substitute)
|
14
|
+
# we don't need to serialize StatementCache::Substitute
|
15
|
+
elsif @type.serialized?
|
16
|
+
value_for_database
|
17
|
+
elsif @type.mutable? # If the type is simply mutable, we deep_dup it.
|
18
|
+
@value_before_type_cast = @value_before_type_cast.deep_dup
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
8
22
|
def type_cast(value)
|
9
23
|
value
|
10
24
|
end
|
11
25
|
|
12
26
|
def value_for_database
|
13
|
-
@value_for_database
|
27
|
+
@value_for_database = _value_for_database unless defined?(@value_for_database)
|
28
|
+
@value_for_database
|
14
29
|
end
|
15
30
|
|
16
31
|
def with_cast_value(value)
|
@@ -35,6 +50,15 @@ module ActiveRecord
|
|
35
50
|
@_unboundable
|
36
51
|
end
|
37
52
|
|
53
|
+
def ==(other)
|
54
|
+
super && value_for_database == other.value_for_database
|
55
|
+
end
|
56
|
+
alias eql? ==
|
57
|
+
|
58
|
+
def hash
|
59
|
+
[self.class, name, value_for_database, type].hash
|
60
|
+
end
|
61
|
+
|
38
62
|
private
|
39
63
|
def infinity?(value)
|
40
64
|
value.respond_to?(:infinite?) && value.infinite?
|