activerecord 8.0.3 → 8.1.0
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 +538 -512
- data/README.rdoc +1 -1
- data/lib/active_record/association_relation.rb +1 -1
- data/lib/active_record/associations/association.rb +1 -1
- data/lib/active_record/associations/belongs_to_association.rb +2 -0
- data/lib/active_record/associations/builder/association.rb +16 -5
- data/lib/active_record/associations/builder/belongs_to.rb +17 -4
- data/lib/active_record/associations/builder/collection_association.rb +7 -3
- data/lib/active_record/associations/builder/has_one.rb +1 -1
- data/lib/active_record/associations/builder/singular_association.rb +33 -5
- data/lib/active_record/associations/collection_proxy.rb +22 -4
- data/lib/active_record/associations/deprecation.rb +88 -0
- data/lib/active_record/associations/errors.rb +3 -0
- data/lib/active_record/associations/join_dependency.rb +2 -0
- data/lib/active_record/associations/preloader/branch.rb +1 -0
- data/lib/active_record/associations.rb +159 -21
- data/lib/active_record/attribute_methods/serialization.rb +16 -3
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +10 -2
- data/lib/active_record/attributes.rb +3 -0
- data/lib/active_record/autosave_association.rb +1 -1
- data/lib/active_record/base.rb +0 -2
- data/lib/active_record/coders/json.rb +14 -5
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +1 -3
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +16 -3
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +51 -12
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +405 -72
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +55 -40
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +19 -3
- data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -24
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +7 -2
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +26 -34
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -1
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +85 -22
- data/lib/active_record/connection_adapters/abstract/transaction.rb +25 -3
- data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -20
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +43 -13
- data/lib/active_record/connection_adapters/column.rb +17 -4
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +4 -4
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +2 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +42 -5
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +26 -4
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +27 -22
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -2
- data/lib/active_record/connection_adapters/postgresql/column.rb +4 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +17 -15
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +21 -10
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +8 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +8 -21
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +67 -31
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +81 -48
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +23 -7
- data/lib/active_record/connection_adapters/schema_cache.rb +2 -2
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +37 -25
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +0 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +4 -13
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +56 -32
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +4 -3
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +1 -1
- data/lib/active_record/connection_adapters.rb +1 -0
- data/lib/active_record/connection_handling.rb +14 -9
- data/lib/active_record/core.rb +5 -4
- data/lib/active_record/counter_cache.rb +33 -8
- data/lib/active_record/database_configurations/database_config.rb +5 -1
- data/lib/active_record/database_configurations/hash_config.rb +53 -9
- data/lib/active_record/database_configurations/url_config.rb +13 -3
- data/lib/active_record/database_configurations.rb +7 -3
- data/lib/active_record/delegated_type.rb +1 -1
- data/lib/active_record/dynamic_matchers.rb +54 -69
- data/lib/active_record/encryption/encryptable_record.rb +4 -4
- data/lib/active_record/encryption/encrypted_attribute_type.rb +1 -1
- data/lib/active_record/encryption/encryptor.rb +12 -0
- data/lib/active_record/encryption/scheme.rb +1 -1
- data/lib/active_record/enum.rb +24 -8
- data/lib/active_record/errors.rb +20 -4
- data/lib/active_record/explain.rb +1 -1
- data/lib/active_record/explain_registry.rb +51 -2
- data/lib/active_record/filter_attribute_handler.rb +73 -0
- data/lib/active_record/fixtures.rb +2 -2
- data/lib/active_record/gem_version.rb +2 -2
- data/lib/active_record/inheritance.rb +1 -1
- data/lib/active_record/insert_all.rb +12 -7
- data/lib/active_record/locking/optimistic.rb +7 -0
- data/lib/active_record/locking/pessimistic.rb +5 -0
- data/lib/active_record/log_subscriber.rb +2 -6
- data/lib/active_record/middleware/shard_selector.rb +34 -17
- data/lib/active_record/migration/command_recorder.rb +14 -1
- data/lib/active_record/migration/compatibility.rb +34 -24
- data/lib/active_record/migration/default_schema_versions_formatter.rb +30 -0
- data/lib/active_record/migration.rb +26 -16
- data/lib/active_record/model_schema.rb +36 -10
- data/lib/active_record/nested_attributes.rb +2 -0
- data/lib/active_record/persistence.rb +34 -3
- data/lib/active_record/query_cache.rb +22 -15
- data/lib/active_record/query_logs.rb +3 -7
- data/lib/active_record/railtie.rb +32 -3
- data/lib/active_record/railties/controller_runtime.rb +11 -6
- data/lib/active_record/railties/databases.rake +15 -3
- data/lib/active_record/railties/job_checkpoints.rb +15 -0
- data/lib/active_record/railties/job_runtime.rb +10 -11
- data/lib/active_record/reflection.rb +35 -0
- data/lib/active_record/relation/batches.rb +25 -11
- data/lib/active_record/relation/calculations.rb +20 -9
- data/lib/active_record/relation/delegation.rb +0 -1
- data/lib/active_record/relation/finder_methods.rb +27 -11
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +9 -9
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +7 -7
- data/lib/active_record/relation/predicate_builder.rb +9 -7
- data/lib/active_record/relation/query_attribute.rb +3 -1
- data/lib/active_record/relation/query_methods.rb +40 -29
- data/lib/active_record/relation/where_clause.rb +1 -8
- data/lib/active_record/relation.rb +24 -12
- data/lib/active_record/result.rb +44 -21
- data/lib/active_record/runtime_registry.rb +41 -58
- data/lib/active_record/sanitization.rb +2 -0
- data/lib/active_record/schema_dumper.rb +12 -10
- data/lib/active_record/scoping.rb +0 -1
- data/lib/active_record/signed_id.rb +43 -15
- data/lib/active_record/statement_cache.rb +13 -9
- data/lib/active_record/store.rb +44 -19
- data/lib/active_record/structured_event_subscriber.rb +85 -0
- data/lib/active_record/table_metadata.rb +5 -20
- data/lib/active_record/tasks/abstract_tasks.rb +76 -0
- data/lib/active_record/tasks/database_tasks.rb +25 -34
- data/lib/active_record/tasks/mysql_database_tasks.rb +3 -40
- data/lib/active_record/tasks/postgresql_database_tasks.rb +5 -39
- data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -26
- data/lib/active_record/test_databases.rb +14 -4
- data/lib/active_record/test_fixtures.rb +27 -2
- data/lib/active_record/testing/query_assertions.rb +8 -2
- data/lib/active_record/timestamp.rb +4 -2
- data/lib/active_record/transaction.rb +2 -5
- data/lib/active_record/transactions.rb +32 -10
- data/lib/active_record/type/hash_lookup_type_map.rb +2 -1
- data/lib/active_record/type/internal/timezone.rb +7 -0
- data/lib/active_record/type/json.rb +15 -2
- data/lib/active_record/type/serialized.rb +11 -4
- data/lib/active_record/type/type_map.rb +1 -1
- data/lib/active_record/type_caster/connection.rb +2 -1
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record.rb +65 -3
- data/lib/arel/alias_predication.rb +2 -0
- data/lib/arel/crud.rb +6 -11
- data/lib/arel/nodes/count.rb +2 -2
- data/lib/arel/nodes/function.rb +4 -10
- data/lib/arel/nodes/named_function.rb +2 -2
- data/lib/arel/nodes/node.rb +1 -1
- data/lib/arel/nodes.rb +0 -2
- data/lib/arel/select_manager.rb +7 -2
- data/lib/arel/visitors/dot.rb +0 -3
- data/lib/arel/visitors/postgresql.rb +55 -0
- data/lib/arel/visitors/sqlite.rb +55 -8
- data/lib/arel/visitors/to_sql.rb +3 -21
- data/lib/arel.rb +3 -1
- data/lib/rails/generators/active_record/application_record/USAGE +1 -1
- metadata +14 -10
- data/lib/active_record/explain_subscriber.rb +0 -34
- data/lib/active_record/normalization.rb +0 -163
|
@@ -4,17 +4,27 @@ require "active_support/testing/parallelization"
|
|
|
4
4
|
|
|
5
5
|
module ActiveRecord
|
|
6
6
|
module TestDatabases # :nodoc:
|
|
7
|
+
ActiveSupport::Testing::Parallelization.before_fork_hook do
|
|
8
|
+
if ActiveSupport.parallelize_test_databases
|
|
9
|
+
ActiveRecord::Base.connection_handler.clear_all_connections!
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
7
13
|
ActiveSupport::Testing::Parallelization.after_fork_hook do |i|
|
|
8
|
-
|
|
14
|
+
if ActiveSupport.parallelize_test_databases
|
|
15
|
+
create_and_load_schema(i, env_name: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call)
|
|
16
|
+
end
|
|
9
17
|
end
|
|
10
18
|
|
|
11
19
|
def self.create_and_load_schema(i, env_name:)
|
|
12
20
|
old, ENV["VERBOSE"] = ENV["VERBOSE"], "false"
|
|
13
21
|
|
|
14
|
-
ActiveRecord::Base.configurations.configs_for(env_name: env_name).each do |db_config|
|
|
15
|
-
db_config._database = "#{db_config.database}
|
|
22
|
+
ActiveRecord::Base.configurations.configs_for(env_name: env_name, include_hidden: true).each do |db_config|
|
|
23
|
+
db_config._database = "#{db_config.database}_#{i}"
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
if db_config.database_tasks?
|
|
26
|
+
ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config, nil)
|
|
27
|
+
end
|
|
18
28
|
end
|
|
19
29
|
ensure
|
|
20
30
|
ActiveRecord::Base.establish_connection
|
|
@@ -36,11 +36,26 @@ module ActiveRecord
|
|
|
36
36
|
class_attribute :pre_loaded_fixtures, default: false
|
|
37
37
|
class_attribute :lock_threads, default: true
|
|
38
38
|
class_attribute :fixture_sets, default: {}
|
|
39
|
+
class_attribute :database_transactions_config, default: {}
|
|
39
40
|
|
|
40
41
|
ActiveSupport.run_load_hooks(:active_record_fixtures, self)
|
|
41
42
|
end
|
|
42
43
|
|
|
43
44
|
module ClassMethods
|
|
45
|
+
# Do not use transactional tests for the given database. This overrides
|
|
46
|
+
# the default setting as defined by `use_transactional_tests`, which
|
|
47
|
+
# applies to all database connection pools not explicitly configured here.
|
|
48
|
+
def skip_transactional_tests_for_database(database_name)
|
|
49
|
+
use_transactional_tests_for_database(database_name, false)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Enable or disable transactions per database. This overrides the default
|
|
53
|
+
# setting as defined by `use_transactional_tests`, which applies to all
|
|
54
|
+
# database connection pools not explicitly configured here.
|
|
55
|
+
def use_transactional_tests_for_database(database_name, enabled = true)
|
|
56
|
+
self.database_transactions_config = database_transactions_config.merge(database_name => enabled)
|
|
57
|
+
end
|
|
58
|
+
|
|
44
59
|
# Sets the model class for a fixture when the class name cannot be inferred from the fixture name.
|
|
45
60
|
#
|
|
46
61
|
# Examples:
|
|
@@ -106,7 +121,8 @@ module ActiveRecord
|
|
|
106
121
|
|
|
107
122
|
private
|
|
108
123
|
def run_in_transaction?
|
|
109
|
-
|
|
124
|
+
has_explicit_config = database_transactions_config.any? { |_, enabled| enabled }
|
|
125
|
+
(use_transactional_tests || has_explicit_config) &&
|
|
110
126
|
!self.class.uses_transaction?(name)
|
|
111
127
|
end
|
|
112
128
|
|
|
@@ -169,11 +185,19 @@ module ActiveRecord
|
|
|
169
185
|
@@already_loaded_fixtures.clear
|
|
170
186
|
end
|
|
171
187
|
|
|
188
|
+
def transactional_tests_for_pool?(pool)
|
|
189
|
+
database_transactions_config.fetch(pool.db_config.name.to_sym, use_transactional_tests)
|
|
190
|
+
end
|
|
191
|
+
|
|
172
192
|
def setup_transactional_fixtures
|
|
173
193
|
setup_shared_connection_pool
|
|
174
194
|
|
|
175
195
|
# Begin transactions for connections already established
|
|
176
196
|
@fixture_connection_pools = ActiveRecord::Base.connection_handler.connection_pool_list(:writing)
|
|
197
|
+
|
|
198
|
+
# Filter to pools that want to use transactions
|
|
199
|
+
@fixture_connection_pools.select! { |pool| transactional_tests_for_pool?(pool) }
|
|
200
|
+
|
|
177
201
|
@fixture_connection_pools.each do |pool|
|
|
178
202
|
pool.pin_connection!(lock_threads)
|
|
179
203
|
pool.lease_connection
|
|
@@ -189,7 +213,8 @@ module ActiveRecord
|
|
|
189
213
|
if pool
|
|
190
214
|
setup_shared_connection_pool
|
|
191
215
|
|
|
192
|
-
|
|
216
|
+
# Don't begin a transaction if we've already done so, or are not using them for this pool
|
|
217
|
+
if !@fixture_connection_pools.include?(pool) && transactional_tests_for_pool?(pool)
|
|
193
218
|
pool.pin_connection!(lock_threads)
|
|
194
219
|
pool.lease_connection
|
|
195
220
|
@fixture_connection_pools << pool
|
|
@@ -11,12 +11,18 @@ module ActiveRecord
|
|
|
11
11
|
# # Check for any number of queries
|
|
12
12
|
# assert_queries_count { Post.first }
|
|
13
13
|
#
|
|
14
|
-
#
|
|
14
|
+
# Any unmaterialized transactions will be materialized to ensure only
|
|
15
|
+
# queries attempted intside the block are counted.
|
|
16
|
+
#
|
|
17
|
+
# If the +:include_schema+ option is provided, any queries (including
|
|
18
|
+
# schema related) are counted. Setting this option also skips leasing a
|
|
19
|
+
# connection to materialize pending transactions since we want to count
|
|
20
|
+
# queries executed at connection open (e.g., type map).
|
|
15
21
|
#
|
|
16
22
|
# assert_queries_count(1, include_schema: true) { Post.columns }
|
|
17
23
|
#
|
|
18
24
|
def assert_queries_count(count = nil, include_schema: false, &block)
|
|
19
|
-
ActiveRecord::Base.lease_connection.materialize_transactions
|
|
25
|
+
ActiveRecord::Base.lease_connection.materialize_transactions unless include_schema
|
|
20
26
|
|
|
21
27
|
counter = SQLCounter.new
|
|
22
28
|
ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do
|
|
@@ -169,8 +169,10 @@ module ActiveRecord
|
|
|
169
169
|
# Clear attributes and changed_attributes
|
|
170
170
|
def clear_timestamp_attributes
|
|
171
171
|
all_timestamp_attributes_in_model.each do |attribute_name|
|
|
172
|
-
self[attribute_name]
|
|
173
|
-
|
|
172
|
+
if self[attribute_name]
|
|
173
|
+
self[attribute_name] = nil
|
|
174
|
+
clear_attribute_change(attribute_name)
|
|
175
|
+
end
|
|
174
176
|
end
|
|
175
177
|
end
|
|
176
178
|
end
|
|
@@ -26,7 +26,7 @@ module ActiveRecord
|
|
|
26
26
|
# # We are inside a real and not finalized transaction.
|
|
27
27
|
# end
|
|
28
28
|
#
|
|
29
|
-
# Closed transactions are
|
|
29
|
+
# Closed transactions are +blank?+ too.
|
|
30
30
|
#
|
|
31
31
|
# == Callbacks
|
|
32
32
|
#
|
|
@@ -101,9 +101,6 @@ module ActiveRecord
|
|
|
101
101
|
#
|
|
102
102
|
# If the entire chain of nested transactions are all successfully committed,
|
|
103
103
|
# the block is never called.
|
|
104
|
-
#
|
|
105
|
-
# If the transaction is already finalized, attempting to register a callback
|
|
106
|
-
# will raise ActiveRecord::ActiveRecordError.
|
|
107
104
|
def after_rollback(&block)
|
|
108
105
|
@internal_transaction&.after_rollback(&block)
|
|
109
106
|
end
|
|
@@ -115,7 +112,7 @@ module ActiveRecord
|
|
|
115
112
|
|
|
116
113
|
# Returns true if the transaction doesn't exist or is finalized.
|
|
117
114
|
def closed?
|
|
118
|
-
@internal_transaction.nil? || @internal_transaction.
|
|
115
|
+
@internal_transaction.nil? || @internal_transaction.closed?
|
|
119
116
|
end
|
|
120
117
|
|
|
121
118
|
alias_method :blank?, :closed?
|
|
@@ -230,8 +230,28 @@ module ActiveRecord
|
|
|
230
230
|
# See the ConnectionAdapters::DatabaseStatements#transaction API docs.
|
|
231
231
|
def transaction(**options, &block)
|
|
232
232
|
with_connection do |connection|
|
|
233
|
-
connection.
|
|
233
|
+
connection.pool.with_pool_transaction_isolation_level(ActiveRecord.default_transaction_isolation_level, connection.transaction_open?) do
|
|
234
|
+
connection.transaction(**options, &block)
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Makes all transactions the current pool use the isolation level initiated within the block.
|
|
240
|
+
def with_pool_transaction_isolation_level(isolation_level, &block)
|
|
241
|
+
if current_transaction.open?
|
|
242
|
+
raise ActiveRecord::TransactionIsolationError, "cannot set default isolation level while transaction is open"
|
|
234
243
|
end
|
|
244
|
+
|
|
245
|
+
old_level = connection_pool.pool_transaction_isolation_level
|
|
246
|
+
connection_pool.pool_transaction_isolation_level = isolation_level
|
|
247
|
+
yield
|
|
248
|
+
ensure
|
|
249
|
+
connection_pool.pool_transaction_isolation_level = old_level
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Returns the default isolation level for the connection pool, set earlier by #with_pool_transaction_isolation_level.
|
|
253
|
+
def pool_transaction_isolation_level
|
|
254
|
+
connection_pool.pool_transaction_isolation_level
|
|
235
255
|
end
|
|
236
256
|
|
|
237
257
|
# Returns a representation of the current transaction state,
|
|
@@ -407,18 +427,20 @@ module ActiveRecord
|
|
|
407
427
|
# instance.
|
|
408
428
|
def with_transaction_returning_status
|
|
409
429
|
self.class.with_connection do |connection|
|
|
410
|
-
|
|
411
|
-
|
|
430
|
+
connection.pool.with_pool_transaction_isolation_level(ActiveRecord.default_transaction_isolation_level, connection.transaction_open?) do
|
|
431
|
+
status = nil
|
|
432
|
+
ensure_finalize = !connection.transaction_open?
|
|
412
433
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
434
|
+
connection.transaction do
|
|
435
|
+
add_to_transaction(ensure_finalize || has_transactional_callbacks?)
|
|
436
|
+
remember_transaction_record_state
|
|
416
437
|
|
|
417
|
-
|
|
418
|
-
|
|
438
|
+
status = yield
|
|
439
|
+
raise ActiveRecord::Rollback unless status
|
|
440
|
+
end
|
|
441
|
+
@_last_transaction_return_status = status
|
|
442
|
+
status
|
|
419
443
|
end
|
|
420
|
-
@_last_transaction_return_status = status
|
|
421
|
-
status
|
|
422
444
|
end
|
|
423
445
|
end
|
|
424
446
|
|
|
@@ -26,6 +26,7 @@ module ActiveRecord
|
|
|
26
26
|
if block
|
|
27
27
|
@mapping[key] = block
|
|
28
28
|
else
|
|
29
|
+
value.freeze
|
|
29
30
|
@mapping[key] = proc { value }
|
|
30
31
|
end
|
|
31
32
|
@cache.clear
|
|
@@ -50,7 +51,7 @@ module ActiveRecord
|
|
|
50
51
|
|
|
51
52
|
private
|
|
52
53
|
def perform_fetch(type, *args, &block)
|
|
53
|
-
@mapping.fetch(type, block).call(type, *args)
|
|
54
|
+
@mapping.fetch(type, block).call(type, *args).freeze
|
|
54
55
|
end
|
|
55
56
|
end
|
|
56
57
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "active_support/json"
|
|
4
|
+
|
|
3
5
|
module ActiveRecord
|
|
4
6
|
module Type
|
|
5
7
|
class Json < ActiveModel::Type::Value
|
|
@@ -11,11 +13,22 @@ module ActiveRecord
|
|
|
11
13
|
|
|
12
14
|
def deserialize(value)
|
|
13
15
|
return value unless value.is_a?(::String)
|
|
14
|
-
|
|
16
|
+
begin
|
|
17
|
+
ActiveSupport::JSON.decode(value)
|
|
18
|
+
rescue JSON::ParserError => e
|
|
19
|
+
# NOTE: This may hide json with duplicate keys. We don't really want to just ignore it
|
|
20
|
+
# but it's the best we can do in order to still allow updating columns that somehow already
|
|
21
|
+
# contain invalid json from some other source.
|
|
22
|
+
# See https://github.com/rails/rails/pull/55536
|
|
23
|
+
ActiveSupport.error_reporter.report(e, source: "application.active_record")
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
15
26
|
end
|
|
16
27
|
|
|
28
|
+
JSON_ENCODER = ActiveSupport::JSON::Encoding.json_encoder.new(escape: false)
|
|
29
|
+
|
|
17
30
|
def serialize(value)
|
|
18
|
-
|
|
31
|
+
JSON_ENCODER.encode(value) unless value.nil?
|
|
19
32
|
end
|
|
20
33
|
|
|
21
34
|
def changed_in_place?(raw_old_value, new_value)
|
|
@@ -9,9 +9,10 @@ module ActiveRecord
|
|
|
9
9
|
|
|
10
10
|
attr_reader :subtype, :coder
|
|
11
11
|
|
|
12
|
-
def initialize(subtype, coder)
|
|
12
|
+
def initialize(subtype, coder, comparable: false)
|
|
13
13
|
@subtype = subtype
|
|
14
14
|
@coder = coder
|
|
15
|
+
@comparable = comparable
|
|
15
16
|
super(subtype)
|
|
16
17
|
end
|
|
17
18
|
|
|
@@ -34,9 +35,15 @@ module ActiveRecord
|
|
|
34
35
|
|
|
35
36
|
def changed_in_place?(raw_old_value, value)
|
|
36
37
|
return false if value.nil?
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
|
|
39
|
+
if @comparable
|
|
40
|
+
old_value = deserialize(raw_old_value)
|
|
41
|
+
old_value != value
|
|
42
|
+
else
|
|
43
|
+
raw_new_value = encoded(value)
|
|
44
|
+
raw_old_value.nil? != raw_new_value.nil? ||
|
|
45
|
+
subtype.changed_in_place?(raw_old_value, raw_new_value)
|
|
46
|
+
end
|
|
40
47
|
end
|
|
41
48
|
|
|
42
49
|
def accessor
|
|
@@ -19,7 +19,8 @@ module ActiveRecord
|
|
|
19
19
|
if schema_cache.data_source_exists?(table_name)
|
|
20
20
|
column = schema_cache.columns_hash(table_name)[attr_name.to_s]
|
|
21
21
|
if column
|
|
22
|
-
|
|
22
|
+
# TODO: Remove fetch_cast_type and the need for connection after we release 8.1.
|
|
23
|
+
type = column.fetch_cast_type(@klass.lease_connection)
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
|
|
@@ -7,7 +7,7 @@ module ActiveRecord
|
|
|
7
7
|
context = record_validation_context_for_association(record)
|
|
8
8
|
|
|
9
9
|
if Array(value).reject { |association| valid_object?(association, context) }.any?
|
|
10
|
-
record.errors.add(attribute, :invalid, **options
|
|
10
|
+
record.errors.add(attribute, :invalid, **options, value: value)
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
13
|
|
data/lib/active_record.rb
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
require "active_support"
|
|
27
27
|
require "active_support/rails"
|
|
28
28
|
require "active_support/ordered_options"
|
|
29
|
+
require "active_support/core_ext/array/conversions"
|
|
29
30
|
require "active_model"
|
|
30
31
|
require "arel"
|
|
31
32
|
require "yaml"
|
|
@@ -52,6 +53,7 @@ module ActiveRecord
|
|
|
52
53
|
autoload :Enum
|
|
53
54
|
autoload :Explain
|
|
54
55
|
autoload :FixtureSet, "active_record/fixtures"
|
|
56
|
+
autoload :FilterAttributeHandler
|
|
55
57
|
autoload :Inheritance
|
|
56
58
|
autoload :Integration
|
|
57
59
|
autoload :InternalMetadata
|
|
@@ -62,7 +64,6 @@ module ActiveRecord
|
|
|
62
64
|
autoload :ModelSchema
|
|
63
65
|
autoload :NestedAttributes
|
|
64
66
|
autoload :NoTouching
|
|
65
|
-
autoload :Normalization
|
|
66
67
|
autoload :Persistence
|
|
67
68
|
autoload :QueryCache
|
|
68
69
|
autoload :QueryLogs
|
|
@@ -174,7 +175,8 @@ module ActiveRecord
|
|
|
174
175
|
extend ActiveSupport::Autoload
|
|
175
176
|
|
|
176
177
|
autoload :DatabaseTasks
|
|
177
|
-
autoload :
|
|
178
|
+
autoload :AbstractTasks, "active_record/tasks/abstract_tasks"
|
|
179
|
+
autoload :MySQLDatabaseTasks, "active_record/tasks/mysql_database_tasks"
|
|
178
180
|
autoload :PostgreSQLDatabaseTasks, "active_record/tasks/postgresql_database_tasks"
|
|
179
181
|
autoload :SQLiteDatabaseTasks, "active_record/tasks/sqlite_database_tasks"
|
|
180
182
|
end
|
|
@@ -259,6 +261,9 @@ module ActiveRecord
|
|
|
259
261
|
##
|
|
260
262
|
# :singleton-method: db_warnings_ignore
|
|
261
263
|
# Specify allowlist of database warnings.
|
|
264
|
+
# Can be a string, regular expression, or an error code from the database.
|
|
265
|
+
#
|
|
266
|
+
# ActiveRecord::Base.db_warnings_ignore = [/`SHOW WARNINGS` did not return the warnings/, "01000"]
|
|
262
267
|
singleton_class.attr_accessor :db_warnings_ignore
|
|
263
268
|
self.db_warnings_ignore = []
|
|
264
269
|
|
|
@@ -286,6 +291,7 @@ module ActiveRecord
|
|
|
286
291
|
def self.global_thread_pool_async_query_executor # :nodoc:
|
|
287
292
|
concurrency = global_executor_concurrency || 4
|
|
288
293
|
@global_thread_pool_async_query_executor ||= Concurrent::ThreadPoolExecutor.new(
|
|
294
|
+
name: "ActiveRecord-global-async-query-executor",
|
|
289
295
|
min_threads: 0,
|
|
290
296
|
max_threads: concurrency,
|
|
291
297
|
max_queue: concurrency * 4,
|
|
@@ -351,6 +357,9 @@ module ActiveRecord
|
|
|
351
357
|
singleton_class.attr_accessor :run_after_transaction_callbacks_in_order_defined
|
|
352
358
|
self.run_after_transaction_callbacks_in_order_defined = false
|
|
353
359
|
|
|
360
|
+
singleton_class.attr_accessor :raise_on_missing_required_finder_order_columns
|
|
361
|
+
self.run_after_transaction_callbacks_in_order_defined = false
|
|
362
|
+
|
|
354
363
|
singleton_class.attr_accessor :application_record_class
|
|
355
364
|
self.application_record_class = nil
|
|
356
365
|
|
|
@@ -401,6 +410,12 @@ module ActiveRecord
|
|
|
401
410
|
singleton_class.attr_accessor :migration_strategy
|
|
402
411
|
self.migration_strategy = Migration::DefaultStrategy
|
|
403
412
|
|
|
413
|
+
##
|
|
414
|
+
# :singleton-method: schema_versions_formatter
|
|
415
|
+
# Specify the formatter used by schema dumper to format versions information.
|
|
416
|
+
singleton_class.attr_accessor :schema_versions_formatter
|
|
417
|
+
self.schema_versions_formatter = Migration::DefaultSchemaVersionsFormatter
|
|
418
|
+
|
|
404
419
|
##
|
|
405
420
|
# :singleton-method: dump_schema_after_migration
|
|
406
421
|
# Specify whether schema dump should happen at the end of the
|
|
@@ -461,6 +476,29 @@ module ActiveRecord
|
|
|
461
476
|
singleton_class.attr_accessor :generate_secure_token_on
|
|
462
477
|
self.generate_secure_token_on = :create
|
|
463
478
|
|
|
479
|
+
def self.deprecated_associations_options=(options)
|
|
480
|
+
raise ArgumentError, "deprecated_associations_options must be a hash" unless options.is_a?(Hash)
|
|
481
|
+
|
|
482
|
+
valid_keys = [:mode, :backtrace]
|
|
483
|
+
|
|
484
|
+
invalid_keys = options.keys - valid_keys
|
|
485
|
+
unless invalid_keys.empty?
|
|
486
|
+
inflected_key = invalid_keys.size == 1 ? "key" : "keys"
|
|
487
|
+
raise ArgumentError, "invalid deprecated_associations_options #{inflected_key} #{invalid_keys.map(&:inspect).to_sentence} (valid keys are #{valid_keys.map(&:inspect).to_sentence})"
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
options.each do |key, value|
|
|
491
|
+
ActiveRecord::Associations::Deprecation.send("#{key}=", value)
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def self.deprecated_associations_options
|
|
496
|
+
{
|
|
497
|
+
mode: ActiveRecord::Associations::Deprecation.mode,
|
|
498
|
+
backtrace: ActiveRecord::Associations::Deprecation.backtrace
|
|
499
|
+
}
|
|
500
|
+
end
|
|
501
|
+
|
|
464
502
|
def self.marshalling_format_version
|
|
465
503
|
Marshalling.format_version
|
|
466
504
|
end
|
|
@@ -497,6 +535,13 @@ module ActiveRecord
|
|
|
497
535
|
}
|
|
498
536
|
)
|
|
499
537
|
|
|
538
|
+
##
|
|
539
|
+
# :singleton-method: message_verifiers
|
|
540
|
+
#
|
|
541
|
+
# ActiveSupport::MessageVerifiers instance for Active Record. If you are using
|
|
542
|
+
# Rails, this will be set to +Rails.application.message_verifiers+.
|
|
543
|
+
singleton_class.attr_accessor :message_verifiers
|
|
544
|
+
|
|
500
545
|
def self.eager_load!
|
|
501
546
|
super
|
|
502
547
|
ActiveRecord::Locking.eager_load!
|
|
@@ -551,13 +596,30 @@ module ActiveRecord
|
|
|
551
596
|
if active_connection = pool.active_connection
|
|
552
597
|
current_transaction = active_connection.current_transaction
|
|
553
598
|
|
|
554
|
-
if current_transaction.open? && current_transaction.joinable?
|
|
599
|
+
if current_transaction.open? && current_transaction.joinable?
|
|
555
600
|
open_transactions << current_transaction
|
|
556
601
|
end
|
|
557
602
|
end
|
|
558
603
|
end
|
|
559
604
|
open_transactions
|
|
560
605
|
end
|
|
606
|
+
|
|
607
|
+
def self.default_transaction_isolation_level=(isolation_level) # :nodoc:
|
|
608
|
+
ActiveSupport::IsolatedExecutionState[:active_record_transaction_isolation] = isolation_level
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
def self.default_transaction_isolation_level # :nodoc:
|
|
612
|
+
ActiveSupport::IsolatedExecutionState[:active_record_transaction_isolation]
|
|
613
|
+
end
|
|
614
|
+
|
|
615
|
+
# Sets a transaction isolation level for all connection pools within the block.
|
|
616
|
+
def self.with_transaction_isolation_level(isolation_level, &block)
|
|
617
|
+
original_level = self.default_transaction_isolation_level
|
|
618
|
+
self.default_transaction_isolation_level = isolation_level
|
|
619
|
+
yield
|
|
620
|
+
ensure
|
|
621
|
+
self.default_transaction_isolation_level = original_level
|
|
622
|
+
end
|
|
561
623
|
end
|
|
562
624
|
|
|
563
625
|
ActiveSupport.on_load(:active_record) do
|
data/lib/arel/crud.rb
CHANGED
|
@@ -14,12 +14,7 @@ module Arel # :nodoc: all
|
|
|
14
14
|
InsertManager.new
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def compile_update(
|
|
18
|
-
values,
|
|
19
|
-
key = nil,
|
|
20
|
-
having_clause = nil,
|
|
21
|
-
group_values_columns = []
|
|
22
|
-
)
|
|
17
|
+
def compile_update(values, key = nil)
|
|
23
18
|
um = UpdateManager.new(source)
|
|
24
19
|
um.set(values)
|
|
25
20
|
um.take(limit)
|
|
@@ -29,12 +24,12 @@ module Arel # :nodoc: all
|
|
|
29
24
|
um.comment(comment)
|
|
30
25
|
um.key = key
|
|
31
26
|
|
|
32
|
-
um.
|
|
33
|
-
um.having(
|
|
27
|
+
um.ast.groups = @ctx.groups
|
|
28
|
+
@ctx.havings.each { |h| um.having(h) }
|
|
34
29
|
um
|
|
35
30
|
end
|
|
36
31
|
|
|
37
|
-
def compile_delete(key = nil
|
|
32
|
+
def compile_delete(key = nil)
|
|
38
33
|
dm = DeleteManager.new(source)
|
|
39
34
|
dm.take(limit)
|
|
40
35
|
dm.offset(offset)
|
|
@@ -42,8 +37,8 @@ module Arel # :nodoc: all
|
|
|
42
37
|
dm.wheres = constraints
|
|
43
38
|
dm.comment(comment)
|
|
44
39
|
dm.key = key
|
|
45
|
-
dm.
|
|
46
|
-
dm.having(
|
|
40
|
+
dm.ast.groups = @ctx.groups
|
|
41
|
+
@ctx.havings.each { |h| dm.having(h) }
|
|
47
42
|
dm
|
|
48
43
|
end
|
|
49
44
|
end
|
data/lib/arel/nodes/count.rb
CHANGED
data/lib/arel/nodes/function.rb
CHANGED
|
@@ -5,28 +5,22 @@ module Arel # :nodoc: all
|
|
|
5
5
|
class Function < Arel::Nodes::NodeExpression
|
|
6
6
|
include Arel::WindowPredications
|
|
7
7
|
include Arel::FilterPredications
|
|
8
|
-
attr_accessor :expressions, :alias, :distinct
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
attr_accessor :expressions, :distinct
|
|
10
|
+
|
|
11
|
+
def initialize(expr)
|
|
11
12
|
super()
|
|
12
13
|
@expressions = expr
|
|
13
|
-
@alias = aliaz && SqlLiteral.new(aliaz)
|
|
14
14
|
@distinct = false
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def as(aliaz)
|
|
18
|
-
self.alias = SqlLiteral.new(aliaz)
|
|
19
|
-
self
|
|
20
|
-
end
|
|
21
|
-
|
|
22
17
|
def hash
|
|
23
|
-
[@expressions, @
|
|
18
|
+
[@expressions, @distinct].hash
|
|
24
19
|
end
|
|
25
20
|
|
|
26
21
|
def eql?(other)
|
|
27
22
|
self.class == other.class &&
|
|
28
23
|
self.expressions == other.expressions &&
|
|
29
|
-
self.alias == other.alias &&
|
|
30
24
|
self.distinct == other.distinct
|
|
31
25
|
end
|
|
32
26
|
alias :== :eql?
|
data/lib/arel/nodes/node.rb
CHANGED
|
@@ -6,7 +6,7 @@ module Arel # :nodoc: all
|
|
|
6
6
|
#
|
|
7
7
|
# Active Record uses Arel to compose SQL statements. Instead of building SQL strings directly, it's building an
|
|
8
8
|
# abstract syntax tree (AST) of the statement using various types of Arel::Nodes::Node. Each node represents a
|
|
9
|
-
# fragment of
|
|
9
|
+
# fragment of an SQL statement.
|
|
10
10
|
#
|
|
11
11
|
# The intermediate representation allows Arel to compile the statement into the database's specific SQL dialect
|
|
12
12
|
# only before sending it without having to care about the nuances of each database when building the statement.
|
data/lib/arel/nodes.rb
CHANGED
|
@@ -45,8 +45,6 @@ require "arel/nodes/cte"
|
|
|
45
45
|
require "arel/nodes/nary"
|
|
46
46
|
|
|
47
47
|
# function
|
|
48
|
-
# FIXME: Function + Alias can be rewritten as a Function and Alias node.
|
|
49
|
-
# We should make Function a Unary node and deprecate the use of "aliaz"
|
|
50
48
|
require "arel/nodes/function"
|
|
51
49
|
require "arel/nodes/count"
|
|
52
50
|
require "arel/nodes/extract"
|
data/lib/arel/select_manager.rb
CHANGED
|
@@ -74,8 +74,13 @@ module Arel # :nodoc: all
|
|
|
74
74
|
def group(*columns)
|
|
75
75
|
columns.each do |column|
|
|
76
76
|
# FIXME: backwards compat
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
case column
|
|
78
|
+
when Nodes::SqlLiteral
|
|
79
|
+
when String
|
|
80
|
+
column = Nodes::SqlLiteral.new(column)
|
|
81
|
+
when Symbol
|
|
82
|
+
column = Nodes::SqlLiteral.new(column.name)
|
|
83
|
+
end
|
|
79
84
|
|
|
80
85
|
@ctx.groups.push Nodes::Group.new column
|
|
81
86
|
end
|
data/lib/arel/visitors/dot.rb
CHANGED
|
@@ -34,7 +34,6 @@ module Arel # :nodoc: all
|
|
|
34
34
|
def visit_Arel_Nodes_Function(o)
|
|
35
35
|
visit_edge o, "expressions"
|
|
36
36
|
visit_edge o, "distinct"
|
|
37
|
-
visit_edge o, "alias"
|
|
38
37
|
end
|
|
39
38
|
|
|
40
39
|
def visit_Arel_Nodes_Unary(o)
|
|
@@ -108,14 +107,12 @@ module Arel # :nodoc: all
|
|
|
108
107
|
|
|
109
108
|
def visit_Arel_Nodes_Extract(o)
|
|
110
109
|
visit_edge o, "expressions"
|
|
111
|
-
visit_edge o, "alias"
|
|
112
110
|
end
|
|
113
111
|
|
|
114
112
|
def visit_Arel_Nodes_NamedFunction(o)
|
|
115
113
|
visit_edge o, "name"
|
|
116
114
|
visit_edge o, "expressions"
|
|
117
115
|
visit_edge o, "distinct"
|
|
118
|
-
visit_edge o, "alias"
|
|
119
116
|
end
|
|
120
117
|
|
|
121
118
|
def visit_Arel_Nodes_InsertStatement(o)
|