activerecord 7.2.2 → 8.0.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.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +239 -878
- data/README.rdoc +1 -1
- data/lib/active_record/association_relation.rb +1 -0
- data/lib/active_record/associations/association.rb +34 -10
- data/lib/active_record/associations/builder/association.rb +7 -6
- data/lib/active_record/associations/collection_association.rb +10 -8
- data/lib/active_record/associations/disable_joins_association_scope.rb +1 -1
- data/lib/active_record/associations/has_many_through_association.rb +3 -2
- data/lib/active_record/associations/preloader/association.rb +2 -2
- data/lib/active_record/associations/singular_association.rb +8 -3
- data/lib/active_record/associations.rb +34 -4
- data/lib/active_record/asynchronous_queries_tracker.rb +28 -24
- data/lib/active_record/attribute_methods/primary_key.rb +2 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +2 -12
- data/lib/active_record/attribute_methods.rb +1 -1
- data/lib/active_record/autosave_association.rb +69 -27
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +16 -10
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +0 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +0 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +0 -9
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +90 -43
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +8 -2
- data/lib/active_record/connection_adapters/abstract/quoting.rb +1 -1
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +4 -5
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +7 -2
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +33 -6
- data/lib/active_record/connection_adapters/abstract/transaction.rb +15 -5
- data/lib/active_record/connection_adapters/abstract_adapter.rb +24 -26
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +21 -39
- data/lib/active_record/connection_adapters/mysql/quoting.rb +0 -8
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +2 -8
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +43 -45
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +42 -98
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +1 -8
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +64 -42
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +10 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +2 -4
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +1 -11
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +6 -12
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +2 -1
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +59 -16
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +45 -95
- data/lib/active_record/connection_adapters/schema_cache.rb +1 -3
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +76 -100
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +0 -6
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +13 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +8 -1
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +53 -12
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +37 -67
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +0 -17
- data/lib/active_record/connection_adapters.rb +0 -56
- data/lib/active_record/connection_handling.rb +22 -0
- data/lib/active_record/core.rb +18 -14
- data/lib/active_record/database_configurations/database_config.rb +4 -0
- data/lib/active_record/database_configurations/hash_config.rb +8 -0
- data/lib/active_record/encryption/config.rb +3 -1
- data/lib/active_record/encryption/encryptable_record.rb +4 -4
- data/lib/active_record/encryption/encrypted_attribute_type.rb +10 -1
- data/lib/active_record/encryption/encryptor.rb +15 -8
- data/lib/active_record/encryption/extended_deterministic_queries.rb +4 -2
- data/lib/active_record/encryption/scheme.rb +8 -1
- data/lib/active_record/enum.rb +9 -22
- data/lib/active_record/errors.rb +13 -5
- data/lib/active_record/fixtures.rb +0 -2
- data/lib/active_record/future_result.rb +14 -10
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/insert_all.rb +1 -1
- data/lib/active_record/locking/optimistic.rb +1 -1
- data/lib/active_record/log_subscriber.rb +5 -11
- data/lib/active_record/migration/command_recorder.rb +27 -10
- data/lib/active_record/migration/compatibility.rb +5 -2
- data/lib/active_record/migration.rb +35 -38
- data/lib/active_record/model_schema.rb +3 -4
- data/lib/active_record/nested_attributes.rb +4 -6
- data/lib/active_record/persistence.rb +128 -130
- data/lib/active_record/query_logs.rb +102 -50
- data/lib/active_record/query_logs_formatter.rb +17 -28
- data/lib/active_record/querying.rb +8 -8
- data/lib/active_record/railtie.rb +2 -26
- data/lib/active_record/railties/databases.rake +2 -17
- data/lib/active_record/reflection.rb +18 -21
- data/lib/active_record/relation/batches/batch_enumerator.rb +4 -3
- data/lib/active_record/relation/batches.rb +132 -72
- data/lib/active_record/relation/calculations.rb +40 -39
- data/lib/active_record/relation/delegation.rb +25 -14
- data/lib/active_record/relation/finder_methods.rb +18 -18
- data/lib/active_record/relation/merger.rb +8 -8
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +1 -1
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +4 -3
- data/lib/active_record/relation/predicate_builder.rb +13 -0
- data/lib/active_record/relation/query_methods.rb +115 -65
- data/lib/active_record/relation/spawn_methods.rb +1 -1
- data/lib/active_record/relation.rb +79 -61
- data/lib/active_record/result.rb +66 -4
- data/lib/active_record/sanitization.rb +7 -6
- data/lib/active_record/schema_dumper.rb +5 -0
- data/lib/active_record/schema_migration.rb +2 -1
- data/lib/active_record/scoping/named.rb +5 -2
- data/lib/active_record/statement_cache.rb +12 -12
- data/lib/active_record/store.rb +7 -3
- data/lib/active_record/table_metadata.rb +1 -3
- data/lib/active_record/tasks/database_tasks.rb +48 -47
- data/lib/active_record/tasks/mysql_database_tasks.rb +0 -2
- data/lib/active_record/tasks/sqlite_database_tasks.rb +2 -2
- data/lib/active_record/test_fixtures.rb +12 -0
- data/lib/active_record/token_for.rb +1 -1
- data/lib/active_record/validations/uniqueness.rb +8 -8
- data/lib/active_record.rb +15 -45
- data/lib/arel/collectors/bind.rb +1 -1
- data/lib/arel/table.rb +3 -7
- metadata +11 -12
- data/lib/active_record/relation/record_fetch_warning.rb +0 -52
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "thread"
|
|
4
3
|
require "concurrent/map"
|
|
5
4
|
|
|
6
5
|
module ActiveRecord
|
|
@@ -210,18 +209,25 @@ module ActiveRecord
|
|
|
210
209
|
# This makes retrieving the connection pool O(1) once the process is warm.
|
|
211
210
|
# When a connection is established or removed, we invalidate the cache.
|
|
212
211
|
def retrieve_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard, strict: false)
|
|
213
|
-
|
|
212
|
+
pool_manager = get_pool_manager(connection_name)
|
|
213
|
+
pool = pool_manager&.get_pool_config(role, shard)&.pool
|
|
214
214
|
|
|
215
215
|
if strict && !pool
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
216
|
+
selector = [
|
|
217
|
+
("'#{shard}' shard" unless shard == ActiveRecord::Base.default_shard),
|
|
218
|
+
("'#{role}' role" unless role == ActiveRecord::Base.default_role),
|
|
219
|
+
].compact.join(" and ")
|
|
220
|
+
|
|
221
|
+
selector = [
|
|
222
|
+
(connection_name unless connection_name == "ActiveRecord::Base"),
|
|
223
|
+
selector.presence,
|
|
224
|
+
].compact.join(" with ")
|
|
225
|
+
|
|
226
|
+
selector = " for #{selector}" if selector.present?
|
|
227
|
+
|
|
228
|
+
message = "No database connection defined#{selector}."
|
|
223
229
|
|
|
224
|
-
raise
|
|
230
|
+
raise ConnectionNotDefined.new(message, connection_name: connection_name, shard: shard, role: role)
|
|
225
231
|
end
|
|
226
232
|
|
|
227
233
|
pool
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "thread"
|
|
4
3
|
require "concurrent/map"
|
|
5
4
|
require "monitor"
|
|
6
5
|
|
|
@@ -317,14 +316,6 @@ module ActiveRecord
|
|
|
317
316
|
connection_lease.sticky.nil?
|
|
318
317
|
end
|
|
319
318
|
|
|
320
|
-
def connection
|
|
321
|
-
ActiveRecord.deprecator.warn(<<~MSG)
|
|
322
|
-
ActiveRecord::ConnectionAdapters::ConnectionPool#connection is deprecated
|
|
323
|
-
and will be removed in Rails 8.0. Use #lease_connection instead.
|
|
324
|
-
MSG
|
|
325
|
-
lease_connection
|
|
326
|
-
end
|
|
327
|
-
|
|
328
319
|
def pin_connection!(lock_thread) # :nodoc:
|
|
329
320
|
@pinned_connection ||= (connection_lease&.connection || checkout)
|
|
330
321
|
@pinned_connections_depth += 1
|
|
@@ -102,16 +102,16 @@ module ActiveRecord
|
|
|
102
102
|
select_all(arel, name, binds, async: async).then(&:rows)
|
|
103
103
|
end
|
|
104
104
|
|
|
105
|
-
def query_value(
|
|
106
|
-
single_value_from_rows(query(
|
|
105
|
+
def query_value(...) # :nodoc:
|
|
106
|
+
single_value_from_rows(query(...))
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
-
def query_values(
|
|
110
|
-
query(
|
|
109
|
+
def query_values(...) # :nodoc:
|
|
110
|
+
query(...).map(&:first)
|
|
111
111
|
end
|
|
112
112
|
|
|
113
|
-
def query(
|
|
114
|
-
internal_exec_query(
|
|
113
|
+
def query(...) # :nodoc:
|
|
114
|
+
internal_exec_query(...).rows
|
|
115
115
|
end
|
|
116
116
|
|
|
117
117
|
# Determines whether the SQL statement is a write query.
|
|
@@ -163,14 +163,14 @@ module ActiveRecord
|
|
|
163
163
|
# +binds+ as the bind substitutes. +name+ is logged along with
|
|
164
164
|
# the executed +sql+ statement.
|
|
165
165
|
def exec_delete(sql, name = nil, binds = [])
|
|
166
|
-
|
|
166
|
+
affected_rows(internal_execute(sql, name, binds))
|
|
167
167
|
end
|
|
168
168
|
|
|
169
169
|
# Executes update +sql+ statement in the context of this connection using
|
|
170
170
|
# +binds+ as the bind substitutes. +name+ is logged along with
|
|
171
171
|
# the executed +sql+ statement.
|
|
172
172
|
def exec_update(sql, name = nil, binds = [])
|
|
173
|
-
|
|
173
|
+
affected_rows(internal_execute(sql, name, binds))
|
|
174
174
|
end
|
|
175
175
|
|
|
176
176
|
def exec_insert_all(sql, name) # :nodoc:
|
|
@@ -224,11 +224,9 @@ module ActiveRecord
|
|
|
224
224
|
|
|
225
225
|
return if table_names.empty?
|
|
226
226
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
execute_batch(statements, "Truncate Tables")
|
|
231
|
-
end
|
|
227
|
+
disable_referential_integrity do
|
|
228
|
+
statements = build_truncate_statements(table_names)
|
|
229
|
+
execute_batch(statements, "Truncate Tables")
|
|
232
230
|
end
|
|
233
231
|
end
|
|
234
232
|
|
|
@@ -358,7 +356,7 @@ module ActiveRecord
|
|
|
358
356
|
end
|
|
359
357
|
yield current_transaction.user_transaction
|
|
360
358
|
else
|
|
361
|
-
|
|
359
|
+
within_new_transaction(isolation: isolation, joinable: joinable, &block)
|
|
362
360
|
end
|
|
363
361
|
rescue ActiveRecord::Rollback
|
|
364
362
|
# rollbacks are silently swallowed
|
|
@@ -411,6 +409,14 @@ module ActiveRecord
|
|
|
411
409
|
# Begins the transaction (and turns off auto-committing).
|
|
412
410
|
def begin_db_transaction() end
|
|
413
411
|
|
|
412
|
+
def begin_deferred_transaction(isolation_level = nil) # :nodoc:
|
|
413
|
+
if isolation_level
|
|
414
|
+
begin_isolated_db_transaction(isolation_level)
|
|
415
|
+
else
|
|
416
|
+
begin_db_transaction
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
|
|
414
420
|
def transaction_isolation_levels
|
|
415
421
|
{
|
|
416
422
|
read_uncommitted: "READ UNCOMMITTED",
|
|
@@ -427,6 +433,15 @@ module ActiveRecord
|
|
|
427
433
|
raise ActiveRecord::TransactionIsolationError, "adapter does not support setting transaction isolation"
|
|
428
434
|
end
|
|
429
435
|
|
|
436
|
+
# Hook point called after an isolated DB transaction is committed
|
|
437
|
+
# or rolled back.
|
|
438
|
+
# Most adapters don't need to implement anything because the isolation
|
|
439
|
+
# level is set on a per transaction basis.
|
|
440
|
+
# But some databases like SQLite set it on a per connection level
|
|
441
|
+
# and need to explicitly reset it after commit or rollback.
|
|
442
|
+
def reset_isolation_level
|
|
443
|
+
end
|
|
444
|
+
|
|
430
445
|
# Commits the transaction (and turns on auto-committing).
|
|
431
446
|
def commit_db_transaction() end
|
|
432
447
|
|
|
@@ -473,11 +488,9 @@ module ActiveRecord
|
|
|
473
488
|
table_deletes = tables_to_delete.map { |table| "DELETE FROM #{quote_table_name(table)}" }
|
|
474
489
|
statements = table_deletes + fixture_inserts
|
|
475
490
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
execute_batch(statements, "Fixtures Load")
|
|
480
|
-
end
|
|
491
|
+
transaction(requires_new: true) do
|
|
492
|
+
disable_referential_integrity do
|
|
493
|
+
execute_batch(statements, "Fixtures Load")
|
|
481
494
|
end
|
|
482
495
|
end
|
|
483
496
|
end
|
|
@@ -524,28 +537,64 @@ module ActiveRecord
|
|
|
524
537
|
HIGH_PRECISION_CURRENT_TIMESTAMP
|
|
525
538
|
end
|
|
526
539
|
|
|
527
|
-
|
|
528
|
-
|
|
540
|
+
# Same as raw_execute but returns an ActiveRecord::Result object.
|
|
541
|
+
def raw_exec_query(...) # :nodoc:
|
|
542
|
+
cast_result(raw_execute(...))
|
|
543
|
+
end
|
|
544
|
+
|
|
545
|
+
# Execute a query and returns an ActiveRecord::Result
|
|
546
|
+
def internal_exec_query(...) # :nodoc:
|
|
547
|
+
cast_result(internal_execute(...))
|
|
529
548
|
end
|
|
530
549
|
|
|
531
550
|
private
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
551
|
+
# Lowest level way to execute a query. Doesn't check for illegal writes, doesn't annotate queries, yields a native result object.
|
|
552
|
+
def raw_execute(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true, batch: false)
|
|
553
|
+
type_casted_binds = type_casted_binds(binds)
|
|
554
|
+
log(sql, name, binds, type_casted_binds, async: async) do |notification_payload|
|
|
555
|
+
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
|
|
556
|
+
perform_query(conn, sql, binds, type_casted_binds, prepare: prepare, notification_payload: notification_payload, batch: batch)
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
end
|
|
535
560
|
|
|
536
|
-
|
|
561
|
+
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
|
|
562
|
+
raise NotImplementedError
|
|
563
|
+
end
|
|
537
564
|
|
|
538
|
-
|
|
565
|
+
# Receive a native adapter result object and returns an ActiveRecord::Result object.
|
|
566
|
+
def cast_result(raw_result)
|
|
567
|
+
raise NotImplementedError
|
|
539
568
|
end
|
|
540
569
|
|
|
541
|
-
def
|
|
542
|
-
|
|
543
|
-
|
|
570
|
+
def affected_rows(raw_result)
|
|
571
|
+
raise NotImplementedError
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
def preprocess_query(sql)
|
|
575
|
+
check_if_write_query(sql)
|
|
576
|
+
mark_transaction_written_if_write(sql)
|
|
577
|
+
|
|
578
|
+
# We call tranformers after the write checks so we don't add extra parsing work.
|
|
579
|
+
# This means we assume no transformer whille change a read for a write
|
|
580
|
+
# but it would be insane to do such a thing.
|
|
581
|
+
ActiveRecord.query_transformers.each do |transformer|
|
|
582
|
+
sql = transformer.call(sql, self)
|
|
544
583
|
end
|
|
584
|
+
|
|
585
|
+
sql
|
|
545
586
|
end
|
|
546
587
|
|
|
547
|
-
|
|
548
|
-
|
|
588
|
+
# Same as #internal_exec_query, but yields a native adapter result
|
|
589
|
+
def internal_execute(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true, &block)
|
|
590
|
+
sql = preprocess_query(sql)
|
|
591
|
+
raw_execute(sql, name, binds, prepare: prepare, async: async, allow_retry: allow_retry, materialize_transactions: materialize_transactions, &block)
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
def execute_batch(statements, name = nil, **kwargs)
|
|
595
|
+
statements.each do |statement|
|
|
596
|
+
raw_execute(statement, name, **kwargs)
|
|
597
|
+
end
|
|
549
598
|
end
|
|
550
599
|
|
|
551
600
|
DEFAULT_INSERT_VALUE = Arel.sql("DEFAULT").freeze
|
|
@@ -614,10 +663,6 @@ module ActiveRecord
|
|
|
614
663
|
end
|
|
615
664
|
end
|
|
616
665
|
|
|
617
|
-
def with_multi_statements
|
|
618
|
-
yield
|
|
619
|
-
end
|
|
620
|
-
|
|
621
666
|
def combine_multi_statements(total_sql)
|
|
622
667
|
total_sql.join(";\n")
|
|
623
668
|
end
|
|
@@ -629,6 +674,8 @@ module ActiveRecord
|
|
|
629
674
|
raise AsynchronousQueryInsideTransactionError, "Asynchronous queries are not allowed inside transactions"
|
|
630
675
|
end
|
|
631
676
|
|
|
677
|
+
# We make sure to run query transformers on the original thread
|
|
678
|
+
sql = preprocess_query(sql)
|
|
632
679
|
future_result = async.new(
|
|
633
680
|
pool,
|
|
634
681
|
sql,
|
|
@@ -636,19 +683,19 @@ module ActiveRecord
|
|
|
636
683
|
binds,
|
|
637
684
|
prepare: prepare,
|
|
638
685
|
)
|
|
639
|
-
if supports_concurrent_connections? && current_transaction.
|
|
686
|
+
if supports_concurrent_connections? && !current_transaction.joinable?
|
|
640
687
|
future_result.schedule!(ActiveRecord::Base.asynchronous_queries_session)
|
|
641
688
|
else
|
|
642
689
|
future_result.execute!(self)
|
|
643
690
|
end
|
|
644
|
-
|
|
645
|
-
end
|
|
646
|
-
|
|
647
|
-
result = internal_exec_query(sql, name, binds, prepare: prepare, allow_retry: allow_retry)
|
|
648
|
-
if async
|
|
649
|
-
FutureResult.wrap(result)
|
|
691
|
+
future_result
|
|
650
692
|
else
|
|
651
|
-
result
|
|
693
|
+
result = internal_exec_query(sql, name, binds, prepare: prepare, allow_retry: allow_retry)
|
|
694
|
+
if async
|
|
695
|
+
FutureResult.wrap(result)
|
|
696
|
+
else
|
|
697
|
+
result
|
|
698
|
+
end
|
|
652
699
|
end
|
|
653
700
|
end
|
|
654
701
|
|
|
@@ -268,7 +268,7 @@ module ActiveRecord
|
|
|
268
268
|
if result
|
|
269
269
|
ActiveSupport::Notifications.instrument(
|
|
270
270
|
"sql.active_record",
|
|
271
|
-
|
|
271
|
+
cache_notification_info_result(sql, name, binds, result)
|
|
272
272
|
)
|
|
273
273
|
end
|
|
274
274
|
|
|
@@ -290,13 +290,19 @@ module ActiveRecord
|
|
|
290
290
|
if hit
|
|
291
291
|
ActiveSupport::Notifications.instrument(
|
|
292
292
|
"sql.active_record",
|
|
293
|
-
|
|
293
|
+
cache_notification_info_result(sql, name, binds, result)
|
|
294
294
|
)
|
|
295
295
|
end
|
|
296
296
|
|
|
297
297
|
result.dup
|
|
298
298
|
end
|
|
299
299
|
|
|
300
|
+
def cache_notification_info_result(sql, name, binds, result)
|
|
301
|
+
payload = cache_notification_info(sql, name, binds)
|
|
302
|
+
payload[:row_count] = result.length
|
|
303
|
+
payload
|
|
304
|
+
end
|
|
305
|
+
|
|
300
306
|
# Database adapters can override this method to
|
|
301
307
|
# provide custom cache information.
|
|
302
308
|
def cache_notification_info(sql, name, binds)
|
|
@@ -28,6 +28,7 @@ module ActiveRecord
|
|
|
28
28
|
sql << o.foreign_key_drops.map { |fk| visit_DropForeignKey fk }.join(" ")
|
|
29
29
|
sql << o.check_constraint_adds.map { |con| visit_AddCheckConstraint con }.join(" ")
|
|
30
30
|
sql << o.check_constraint_drops.map { |con| visit_DropCheckConstraint con }.join(" ")
|
|
31
|
+
sql << o.constraint_drops.map { |con| visit_DropConstraint con }.join(" ")
|
|
31
32
|
end
|
|
32
33
|
|
|
33
34
|
def visit_ColumnDefinition(o)
|
|
@@ -96,9 +97,11 @@ module ActiveRecord
|
|
|
96
97
|
"ADD #{accept(o)}"
|
|
97
98
|
end
|
|
98
99
|
|
|
99
|
-
def
|
|
100
|
+
def visit_DropConstraint(name)
|
|
100
101
|
"DROP CONSTRAINT #{quote_column_name(name)}"
|
|
101
102
|
end
|
|
103
|
+
alias :visit_DropForeignKey :visit_DropConstraint
|
|
104
|
+
alias :visit_DropCheckConstraint :visit_DropConstraint
|
|
102
105
|
|
|
103
106
|
def visit_CreateIndexDefinition(o)
|
|
104
107
|
index = o.index
|
|
@@ -127,10 +130,6 @@ module ActiveRecord
|
|
|
127
130
|
"ADD #{accept(o)}"
|
|
128
131
|
end
|
|
129
132
|
|
|
130
|
-
def visit_DropCheckConstraint(name)
|
|
131
|
-
"DROP CONSTRAINT #{quote_column_name(name)}"
|
|
132
|
-
end
|
|
133
|
-
|
|
134
133
|
def quoted_columns(o)
|
|
135
134
|
String === o.columns ? o.columns : quoted_columns_for_index(o.columns, o.column_options)
|
|
136
135
|
end
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
module ActiveRecord
|
|
5
4
|
module ConnectionAdapters # :nodoc:
|
|
6
5
|
# Abstract representation of an index definition on a table. Instances of
|
|
@@ -348,7 +347,7 @@ module ActiveRecord
|
|
|
348
347
|
# Inside migration files, the +t+ object in {create_table}[rdoc-ref:SchemaStatements#create_table]
|
|
349
348
|
# is actually of this type:
|
|
350
349
|
#
|
|
351
|
-
# class SomeMigration < ActiveRecord::Migration[
|
|
350
|
+
# class SomeMigration < ActiveRecord::Migration[8.0]
|
|
352
351
|
# def up
|
|
353
352
|
# create_table :foo do |t|
|
|
354
353
|
# puts t.class # => "ActiveRecord::ConnectionAdapters::TableDefinition"
|
|
@@ -622,6 +621,7 @@ module ActiveRecord
|
|
|
622
621
|
attr_reader :adds
|
|
623
622
|
attr_reader :foreign_key_adds, :foreign_key_drops
|
|
624
623
|
attr_reader :check_constraint_adds, :check_constraint_drops
|
|
624
|
+
attr_reader :constraint_drops
|
|
625
625
|
|
|
626
626
|
def initialize(td)
|
|
627
627
|
@td = td
|
|
@@ -630,6 +630,7 @@ module ActiveRecord
|
|
|
630
630
|
@foreign_key_drops = []
|
|
631
631
|
@check_constraint_adds = []
|
|
632
632
|
@check_constraint_drops = []
|
|
633
|
+
@constraint_drops = []
|
|
633
634
|
end
|
|
634
635
|
|
|
635
636
|
def name; @td.name; end
|
|
@@ -650,6 +651,10 @@ module ActiveRecord
|
|
|
650
651
|
@check_constraint_drops << constraint_name
|
|
651
652
|
end
|
|
652
653
|
|
|
654
|
+
def drop_constraint(constraint_name)
|
|
655
|
+
@constraint_drops << constraint_name
|
|
656
|
+
end
|
|
657
|
+
|
|
653
658
|
def add_column(name, type, **options)
|
|
654
659
|
name = name.to_s
|
|
655
660
|
type = type.to_sym
|
|
@@ -345,6 +345,15 @@ module ActiveRecord
|
|
|
345
345
|
# # Creates a table called 'assemblies_parts' with no id.
|
|
346
346
|
# create_join_table(:assemblies, :parts)
|
|
347
347
|
#
|
|
348
|
+
# # Creates a table called 'paper_boxes_papers' with no id.
|
|
349
|
+
# create_join_table('papers', 'paper_boxes')
|
|
350
|
+
#
|
|
351
|
+
# A duplicate prefix is combined into a single prefix. This is useful for
|
|
352
|
+
# namespaced models like Music::Artist and Music::Record:
|
|
353
|
+
#
|
|
354
|
+
# # Creates a table called 'music_artists_records' with no id.
|
|
355
|
+
# create_join_table('music_artists', 'music_records')
|
|
356
|
+
#
|
|
348
357
|
# You can pass an +options+ hash which can include the following keys:
|
|
349
358
|
# [<tt>:table_name</tt>]
|
|
350
359
|
# Sets the table name, overriding the default.
|
|
@@ -516,7 +525,7 @@ module ActiveRecord
|
|
|
516
525
|
raise NotImplementedError, "rename_table is not implemented"
|
|
517
526
|
end
|
|
518
527
|
|
|
519
|
-
# Drops a table from the database.
|
|
528
|
+
# Drops a table or tables from the database.
|
|
520
529
|
#
|
|
521
530
|
# [<tt>:force</tt>]
|
|
522
531
|
# Set to +:cascade+ to drop dependent objects as well.
|
|
@@ -527,10 +536,12 @@ module ActiveRecord
|
|
|
527
536
|
#
|
|
528
537
|
# Although this command ignores most +options+ and the block if one is given,
|
|
529
538
|
# it can be helpful to provide these in a migration's +change+ method so it can be reverted.
|
|
530
|
-
# In that case, +options+ and the block will be used by #create_table.
|
|
531
|
-
def drop_table(
|
|
532
|
-
|
|
533
|
-
|
|
539
|
+
# In that case, +options+ and the block will be used by #create_table except if you provide more than one table which is not supported.
|
|
540
|
+
def drop_table(*table_names, **options)
|
|
541
|
+
table_names.each do |table_name|
|
|
542
|
+
schema_cache.clear_data_source_cache!(table_name.to_s)
|
|
543
|
+
execute "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}"
|
|
544
|
+
end
|
|
534
545
|
end
|
|
535
546
|
|
|
536
547
|
# Add a new +type+ column named +column_name+ to +table_name+.
|
|
@@ -844,6 +855,16 @@ module ActiveRecord
|
|
|
844
855
|
#
|
|
845
856
|
# Note: only supported by PostgreSQL.
|
|
846
857
|
#
|
|
858
|
+
# ====== Creating an index where NULLs are treated equally
|
|
859
|
+
#
|
|
860
|
+
# add_index(:people, :last_name, nulls_not_distinct: true)
|
|
861
|
+
#
|
|
862
|
+
# generates:
|
|
863
|
+
#
|
|
864
|
+
# CREATE INDEX index_people_on_last_name ON people (last_name) NULLS NOT DISTINCT
|
|
865
|
+
#
|
|
866
|
+
# Note: only supported by PostgreSQL version 15.0.0 and greater.
|
|
867
|
+
#
|
|
847
868
|
# ====== Creating an index with a specific method
|
|
848
869
|
#
|
|
849
870
|
# add_index(:developers, :name, using: 'btree')
|
|
@@ -1313,7 +1334,6 @@ module ActiveRecord
|
|
|
1313
1334
|
execute schema_creation.accept(at)
|
|
1314
1335
|
end
|
|
1315
1336
|
|
|
1316
|
-
|
|
1317
1337
|
# Checks to see if a check constraint exists on a table for a given check constraint definition.
|
|
1318
1338
|
#
|
|
1319
1339
|
# check_constraint_exists?(:products, name: "price_check")
|
|
@@ -1325,6 +1345,13 @@ module ActiveRecord
|
|
|
1325
1345
|
check_constraint_for(table_name, **options).present?
|
|
1326
1346
|
end
|
|
1327
1347
|
|
|
1348
|
+
def remove_constraint(table_name, constraint_name) # :nodoc:
|
|
1349
|
+
at = create_alter_table(table_name)
|
|
1350
|
+
at.drop_constraint(constraint_name)
|
|
1351
|
+
|
|
1352
|
+
execute schema_creation.accept(at)
|
|
1353
|
+
end
|
|
1354
|
+
|
|
1328
1355
|
def dump_schema_information # :nodoc:
|
|
1329
1356
|
versions = pool.schema_migration.versions
|
|
1330
1357
|
insert_versions_sql(versions) if versions.any?
|
|
@@ -448,10 +448,14 @@ module ActiveRecord
|
|
|
448
448
|
# = Active Record Real \Transaction
|
|
449
449
|
class RealTransaction < Transaction
|
|
450
450
|
def materialize!
|
|
451
|
-
if
|
|
452
|
-
|
|
451
|
+
if joinable?
|
|
452
|
+
if isolation_level
|
|
453
|
+
connection.begin_isolated_db_transaction(isolation_level)
|
|
454
|
+
else
|
|
455
|
+
connection.begin_db_transaction
|
|
456
|
+
end
|
|
453
457
|
else
|
|
454
|
-
connection.
|
|
458
|
+
connection.begin_deferred_transaction(isolation_level)
|
|
455
459
|
end
|
|
456
460
|
|
|
457
461
|
super
|
|
@@ -472,13 +476,19 @@ module ActiveRecord
|
|
|
472
476
|
end
|
|
473
477
|
|
|
474
478
|
def rollback
|
|
475
|
-
|
|
479
|
+
if materialized?
|
|
480
|
+
connection.rollback_db_transaction
|
|
481
|
+
connection.reset_isolation_level if isolation_level
|
|
482
|
+
end
|
|
476
483
|
@state.full_rollback!
|
|
477
484
|
@instrumenter.finish(:rollback) if materialized?
|
|
478
485
|
end
|
|
479
486
|
|
|
480
487
|
def commit
|
|
481
|
-
|
|
488
|
+
if materialized?
|
|
489
|
+
connection.commit_db_transaction
|
|
490
|
+
connection.reset_isolation_level if isolation_level
|
|
491
|
+
end
|
|
482
492
|
@state.full_commit!
|
|
483
493
|
@instrumenter.finish(:commit) if materialized?
|
|
484
494
|
end
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "set"
|
|
4
3
|
require "active_record/connection_adapters/sql_type_metadata"
|
|
5
4
|
require "active_record/connection_adapters/abstract/schema_dumper"
|
|
6
5
|
require "active_record/connection_adapters/abstract/schema_creation"
|
|
@@ -576,23 +575,31 @@ module ActiveRecord
|
|
|
576
575
|
end
|
|
577
576
|
|
|
578
577
|
# This is meant to be implemented by the adapters that support custom enum types
|
|
579
|
-
def create_enum(
|
|
578
|
+
def create_enum(...) # :nodoc:
|
|
580
579
|
end
|
|
581
580
|
|
|
582
581
|
# This is meant to be implemented by the adapters that support custom enum types
|
|
583
|
-
def drop_enum(
|
|
582
|
+
def drop_enum(...) # :nodoc:
|
|
584
583
|
end
|
|
585
584
|
|
|
586
585
|
# This is meant to be implemented by the adapters that support custom enum types
|
|
587
|
-
def rename_enum(
|
|
586
|
+
def rename_enum(...) # :nodoc:
|
|
588
587
|
end
|
|
589
588
|
|
|
590
589
|
# This is meant to be implemented by the adapters that support custom enum types
|
|
591
|
-
def add_enum_value(
|
|
590
|
+
def add_enum_value(...) # :nodoc:
|
|
592
591
|
end
|
|
593
592
|
|
|
594
593
|
# This is meant to be implemented by the adapters that support custom enum types
|
|
595
|
-
def rename_enum_value(
|
|
594
|
+
def rename_enum_value(...) # :nodoc:
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
# This is meant to be implemented by the adapters that support virtual tables
|
|
598
|
+
def create_virtual_table(*) # :nodoc:
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
# This is meant to be implemented by the adapters that support virtual tables
|
|
602
|
+
def drop_virtual_table(*) # :nodoc:
|
|
596
603
|
end
|
|
597
604
|
|
|
598
605
|
def advisory_locks_enabled? # :nodoc:
|
|
@@ -1044,7 +1051,8 @@ module ActiveRecord
|
|
|
1044
1051
|
end
|
|
1045
1052
|
|
|
1046
1053
|
def retryable_connection_error?(exception)
|
|
1047
|
-
exception.is_a?(ConnectionNotEstablished)
|
|
1054
|
+
(exception.is_a?(ConnectionNotEstablished) && !exception.is_a?(ConnectionNotDefined)) ||
|
|
1055
|
+
exception.is_a?(ConnectionFailed)
|
|
1048
1056
|
end
|
|
1049
1057
|
|
|
1050
1058
|
def invalidate_transaction(exception)
|
|
@@ -1105,24 +1113,25 @@ module ActiveRecord
|
|
|
1105
1113
|
end
|
|
1106
1114
|
end
|
|
1107
1115
|
|
|
1108
|
-
def translate_exception_class(
|
|
1109
|
-
|
|
1116
|
+
def translate_exception_class(native_error, sql, binds)
|
|
1117
|
+
return native_error if native_error.is_a?(ActiveRecordError)
|
|
1118
|
+
|
|
1119
|
+
message = "#{native_error.class.name}: #{native_error.message}"
|
|
1110
1120
|
|
|
1111
|
-
|
|
1112
|
-
|
|
1121
|
+
active_record_error = translate_exception(
|
|
1122
|
+
native_error, message: message, sql: sql, binds: binds
|
|
1113
1123
|
)
|
|
1114
|
-
|
|
1115
|
-
|
|
1124
|
+
active_record_error.set_backtrace(native_error.backtrace)
|
|
1125
|
+
active_record_error
|
|
1116
1126
|
end
|
|
1117
1127
|
|
|
1118
|
-
def log(sql, name = "SQL", binds = [], type_casted_binds = [],
|
|
1128
|
+
def log(sql, name = "SQL", binds = [], type_casted_binds = [], async: false, &block) # :doc:
|
|
1119
1129
|
@instrumenter.instrument(
|
|
1120
1130
|
"sql.active_record",
|
|
1121
1131
|
sql: sql,
|
|
1122
1132
|
name: name,
|
|
1123
1133
|
binds: binds,
|
|
1124
1134
|
type_casted_binds: type_casted_binds,
|
|
1125
|
-
statement_name: statement_name,
|
|
1126
1135
|
async: async,
|
|
1127
1136
|
connection: self,
|
|
1128
1137
|
transaction: current_transaction.user_transaction.presence,
|
|
@@ -1133,13 +1142,6 @@ module ActiveRecord
|
|
|
1133
1142
|
raise ex.set_query(sql, binds)
|
|
1134
1143
|
end
|
|
1135
1144
|
|
|
1136
|
-
def transform_query(sql)
|
|
1137
|
-
ActiveRecord.query_transformers.each do |transformer|
|
|
1138
|
-
sql = transformer.call(sql, self)
|
|
1139
|
-
end
|
|
1140
|
-
sql
|
|
1141
|
-
end
|
|
1142
|
-
|
|
1143
1145
|
def translate_exception(exception, message:, sql:, binds:)
|
|
1144
1146
|
# override in derived class
|
|
1145
1147
|
case exception
|
|
@@ -1150,10 +1152,6 @@ module ActiveRecord
|
|
|
1150
1152
|
end
|
|
1151
1153
|
end
|
|
1152
1154
|
|
|
1153
|
-
def without_prepared_statement?(binds)
|
|
1154
|
-
!prepared_statements || binds.empty?
|
|
1155
|
-
end
|
|
1156
|
-
|
|
1157
1155
|
def column_for(table_name, column_name)
|
|
1158
1156
|
column_name = column_name.to_s
|
|
1159
1157
|
columns(table_name).detect { |c| c.name == column_name } ||
|