Skip to content

Commit 9b4fff2

Browse files
authored
Merge pull request rails#48068 from Shopify/ar-materialize-transactions
Rename `uses_transactions` to `materialize_transactions`
2 parents 073d90c + dfa6af6 commit 9b4fff2

File tree

13 files changed

+52
-52
lines changed

13 files changed

+52
-52
lines changed

activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,13 @@ def high_precision_current_timestamp
491491
end
492492

493493
private
494-
def internal_execute(sql, name = "SCHEMA", allow_retry: false, uses_transaction: true)
494+
def internal_execute(sql, name = "SCHEMA", allow_retry: false, materialize_transactions: true)
495495
sql = transform_query(sql)
496496
check_if_write_query(sql)
497497

498498
mark_transaction_written_if_write(sql)
499499

500-
raw_execute(sql, name, allow_retry: allow_retry, uses_transaction: uses_transaction)
500+
raw_execute(sql, name, allow_retry: allow_retry, materialize_transactions: materialize_transactions)
501501
end
502502

503503
def execute_batch(statements, name = nil)
@@ -506,7 +506,7 @@ def execute_batch(statements, name = nil)
506506
end
507507
end
508508

509-
def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: true)
509+
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
510510
raise NotImplementedError
511511
end
512512

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ def reconnect_can_restore_state?
963963
# the connection's configured +connection_retries+ setting
964964
# and the configured +retry_deadline+ limit.
965965
#
966-
# If +uses_transaction+ is false, the block will be run without
966+
# If +materialize_transactions+ is false, the block will be run without
967967
# ensuring virtual transactions have been materialized in the DB
968968
# server's state. The active transaction will also remain clean
969969
# (if it is not already dirty), meaning it's able to be restored
@@ -983,11 +983,11 @@ def reconnect_can_restore_state?
983983
# still-yielded connection in the outer block), but we currently
984984
# provide no special enforcement there.
985985
#
986-
def with_raw_connection(allow_retry: false, uses_transaction: true)
986+
def with_raw_connection(allow_retry: false, materialize_transactions: true)
987987
@lock.synchronize do
988988
connect! if @raw_connection.nil? && reconnect_can_restore_state?
989989

990-
materialize_transactions if uses_transaction
990+
self.materialize_transactions if materialize_transactions
991991

992992
retries_available = allow_retry ? connection_retries : 0
993993
deadline = retry_deadline && Process.clock_gettime(Process::CLOCK_MONOTONIC) + retry_deadline
@@ -1044,7 +1044,7 @@ def with_raw_connection(allow_retry: false, uses_transaction: true)
10441044

10451045
raise translated_exception
10461046
ensure
1047-
dirty_current_transaction if uses_transaction
1047+
dirty_current_transaction if materialize_transactions
10481048
end
10491049
end
10501050
end
@@ -1092,7 +1092,7 @@ def valid_raw_connection
10921092
# `allow_retry: false`, to force verification: the block won't
10931093
# raise, so a retry wouldn't help us get the valid connection we
10941094
# need.
1095-
with_raw_connection(allow_retry: false, uses_transaction: false) { |conn| conn }
1095+
with_raw_connection(allow_retry: false, materialize_transactions: false) { |conn| conn }
10961096
end
10971097

10981098
def extended_type_map_key

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,24 +232,24 @@ def execute_and_free(sql, name = nil, async: false) # :nodoc:
232232
end
233233

234234
def begin_db_transaction # :nodoc:
235-
internal_execute("BEGIN", "TRANSACTION", allow_retry: true, uses_transaction: false)
235+
internal_execute("BEGIN", "TRANSACTION", allow_retry: true, materialize_transactions: false)
236236
end
237237

238238
def begin_isolated_db_transaction(isolation) # :nodoc:
239-
internal_execute("SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}", "TRANSACTION", allow_retry: true, uses_transaction: false)
239+
internal_execute("SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}", "TRANSACTION", allow_retry: true, materialize_transactions: false)
240240
begin_db_transaction
241241
end
242242

243243
def commit_db_transaction # :nodoc:
244-
internal_execute("COMMIT", "TRANSACTION", allow_retry: false, uses_transaction: true)
244+
internal_execute("COMMIT", "TRANSACTION", allow_retry: false, materialize_transactions: true)
245245
end
246246

247247
def exec_rollback_db_transaction # :nodoc:
248-
internal_execute("ROLLBACK", "TRANSACTION", allow_retry: false, uses_transaction: true)
248+
internal_execute("ROLLBACK", "TRANSACTION", allow_retry: false, materialize_transactions: true)
249249
end
250250

251251
def exec_restart_db_transaction # :nodoc:
252-
internal_execute("ROLLBACK AND CHAIN", "TRANSACTION", allow_retry: false, uses_transaction: true)
252+
internal_execute("ROLLBACK AND CHAIN", "TRANSACTION", allow_retry: false, materialize_transactions: true)
253253
end
254254

255255
def empty_insert_statement_value(primary_key = nil) # :nodoc:

activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ def max_allowed_packet
165165
@max_allowed_packet ||= show_variable("max_allowed_packet")
166166
end
167167

168-
def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: true)
168+
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
169169
log(sql, name, async: async) do
170-
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
170+
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
171171
sync_timezone_changes(conn)
172172
result = conn.query(sql)
173173
handle_warnings(sql)

activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def error_number(exception)
104104

105105
# Quotes strings for use in SQL input.
106106
def quote_string(string)
107-
with_raw_connection(allow_retry: true, uses_transaction: false) do |connection|
107+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |connection|
108108
connection.escape(string)
109109
end
110110
end

activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ def execute(...) # :nodoc:
4747
@notice_receiver_sql_warnings = []
4848
end
4949

50-
def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: true)
50+
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
5151
log(sql, name, async: async) do
52-
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
52+
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
5353
result = conn.async_exec(sql)
5454
handle_warnings(result)
5555
result
5656
end
5757
end
5858
end
5959

60-
def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, uses_transaction: true) # :nodoc:
61-
execute_and_clear(sql, name, binds, prepare: prepare, async: async, allow_retry: allow_retry, uses_transaction: uses_transaction) do |result|
60+
def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true) # :nodoc:
61+
execute_and_clear(sql, name, binds, prepare: prepare, async: async, allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |result|
6262
types = {}
6363
fields = result.fields
6464
fields.each_with_index do |fname, i|
@@ -110,27 +110,27 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil) # :n
110110

111111
# Begins a transaction.
112112
def begin_db_transaction # :nodoc:
113-
internal_execute("BEGIN", "TRANSACTION", allow_retry: true, uses_transaction: false)
113+
internal_execute("BEGIN", "TRANSACTION", allow_retry: true, materialize_transactions: false)
114114
end
115115

116116
def begin_isolated_db_transaction(isolation) # :nodoc:
117-
internal_execute("BEGIN ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}", "TRANSACTION", allow_retry: true, uses_transaction: false)
117+
internal_execute("BEGIN ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}", "TRANSACTION", allow_retry: true, materialize_transactions: false)
118118
end
119119

120120
# Commits a transaction.
121121
def commit_db_transaction # :nodoc:
122-
internal_execute("COMMIT", "TRANSACTION", allow_retry: false, uses_transaction: true)
122+
internal_execute("COMMIT", "TRANSACTION", allow_retry: false, materialize_transactions: true)
123123
end
124124

125125
# Aborts a transaction.
126126
def exec_rollback_db_transaction # :nodoc:
127127
cancel_any_running_query
128-
internal_execute("ROLLBACK", "TRANSACTION", allow_retry: false, uses_transaction: true)
128+
internal_execute("ROLLBACK", "TRANSACTION", allow_retry: false, materialize_transactions: true)
129129
end
130130

131131
def exec_restart_db_transaction # :nodoc:
132132
cancel_any_running_query
133-
internal_execute("ROLLBACK AND CHAIN", "TRANSACTION", allow_retry: false, uses_transaction: true)
133+
internal_execute("ROLLBACK AND CHAIN", "TRANSACTION", allow_retry: false, materialize_transactions: true)
134134
end
135135

136136
# From https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def quote(value) # :nodoc:
6969

7070
# Quotes strings for use in SQL input.
7171
def quote_string(s) # :nodoc:
72-
with_raw_connection(allow_retry: true, uses_transaction: false) do |connection|
72+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |connection|
7373
connection.escape(s)
7474
end
7575
end

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def index_name(table_name, options) # :nodoc:
520520

521521
def foreign_keys(table_name)
522522
scope = quoted_scope(table_name)
523-
fk_info = exec_query(<<~SQL, "SCHEMA", allow_retry: true, uses_transaction: false)
523+
fk_info = exec_query(<<~SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
524524
SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, c.convalidated AS valid, c.condeferrable AS deferrable, c.condeferred AS deferred
525525
FROM pg_constraint c
526526
JOIN pg_class t1 ON c.conrelid = t1.oid
@@ -563,7 +563,7 @@ def foreign_table_exists?(table_name)
563563
def check_constraints(table_name) # :nodoc:
564564
scope = quoted_scope(table_name)
565565

566-
check_info = exec_query(<<-SQL, "SCHEMA", allow_retry: true, uses_transaction: false)
566+
check_info = exec_query(<<-SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
567567
SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid
568568
FROM pg_constraint c
569569
JOIN pg_class t ON c.conrelid = t.oid
@@ -623,7 +623,7 @@ def exclusion_constraints(table_name)
623623
def unique_keys(table_name)
624624
scope = quoted_scope(table_name)
625625

626-
unique_info = exec_query(<<~SQL, "SCHEMA", allow_retry: true, uses_transaction: false)
626+
unique_info = exec_query(<<~SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
627627
SELECT c.conname, c.conindid, c.condeferrable, c.condeferred
628628
FROM pg_constraint c
629629
JOIN pg_class t ON c.conrelid = t.oid

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def extension_enabled?(name)
482482
end
483483

484484
def extensions
485-
exec_query("SELECT extname FROM pg_extension", "SCHEMA", allow_retry: true, uses_transaction: false).cast_values
485+
exec_query("SELECT extname FROM pg_extension", "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values
486486
end
487487

488488
# Returns a list of defined enum types, and their values.
@@ -500,7 +500,7 @@ def enum_types
500500
GROUP BY type.OID, n.nspname, type.typname;
501501
SQL
502502

503-
exec_query(query, "SCHEMA", allow_retry: true, uses_transaction: false).cast_values.each_with_object({}) do |row, memo|
503+
exec_query(query, "SCHEMA", allow_retry: true, materialize_transactions: false).cast_values.each_with_object({}) do |row, memo|
504504
name, schema = row[0], row[2]
505505
schema = nil if schema == current_schema
506506
full_name = [schema, name].compact.join(".")
@@ -562,7 +562,7 @@ def table_name_length
562562
# Set the authorized user for this session
563563
def session_auth=(user)
564564
clear_cache!
565-
internal_execute("SET SESSION AUTHORIZATION #{user}", nil, uses_transaction: true)
565+
internal_execute("SET SESSION AUTHORIZATION #{user}", nil, materialize_transactions: true)
566566
end
567567

568568
def use_insert_returning?
@@ -801,7 +801,7 @@ def get_oid_type(oid, fmod, column_name, sql_type = "")
801801
def load_additional_types(oids = nil)
802802
initializer = OID::TypeMapInitializer.new(type_map)
803803
load_types_queries(initializer, oids) do |query|
804-
execute_and_clear(query, "SCHEMA", [], allow_retry: true, uses_transaction: false) do |records|
804+
execute_and_clear(query, "SCHEMA", [], allow_retry: true, materialize_transactions: false) do |records|
805805
initializer.run(records)
806806
end
807807
end
@@ -824,14 +824,14 @@ def load_types_queries(initializer, oids)
824824

825825
FEATURE_NOT_SUPPORTED = "0A000" # :nodoc:
826826

827-
def execute_and_clear(sql, name, binds, prepare: false, async: false, allow_retry: false, uses_transaction: true)
827+
def execute_and_clear(sql, name, binds, prepare: false, async: false, allow_retry: false, materialize_transactions: true)
828828
sql = transform_query(sql)
829829
check_if_write_query(sql)
830830

831831
if !prepare || without_prepared_statement?(binds)
832-
result = exec_no_cache(sql, name, binds, async: async, allow_retry: allow_retry, uses_transaction: uses_transaction)
832+
result = exec_no_cache(sql, name, binds, async: async, allow_retry: allow_retry, materialize_transactions: materialize_transactions)
833833
else
834-
result = exec_cache(sql, name, binds, async: async, allow_retry: allow_retry, uses_transaction: uses_transaction)
834+
result = exec_cache(sql, name, binds, async: async, allow_retry: allow_retry, materialize_transactions: materialize_transactions)
835835
end
836836
begin
837837
ret = yield result
@@ -841,7 +841,7 @@ def execute_and_clear(sql, name, binds, prepare: false, async: false, allow_retr
841841
ret
842842
end
843843

844-
def exec_no_cache(sql, name, binds, async:, allow_retry:, uses_transaction:)
844+
def exec_no_cache(sql, name, binds, async:, allow_retry:, materialize_transactions:)
845845
mark_transaction_written_if_write(sql)
846846

847847
# make sure we carry over any changes to ActiveRecord.default_timezone that have been
@@ -856,7 +856,7 @@ def exec_no_cache(sql, name, binds, async:, allow_retry:, uses_transaction:)
856856
end
857857
end
858858

859-
def exec_cache(sql, name, binds, async:, allow_retry:, uses_transaction:)
859+
def exec_cache(sql, name, binds, async:, allow_retry:, materialize_transactions:)
860860
mark_transaction_written_if_write(sql)
861861

862862
update_typemap_for_default_timezone
@@ -915,7 +915,7 @@ def sql_key(sql)
915915
# Prepare the statement if it hasn't been prepared, return
916916
# the statement key.
917917
def prepare_statement(sql, binds)
918-
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
918+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
919919
sql_key = sql_key(sql)
920920
unless @statements.key? sql_key
921921
nextkey = @statements.next_key
@@ -1075,7 +1075,7 @@ def can_perform_case_insensitive_comparison_for?(column)
10751075
AND castsource = #{quote column.sql_type}::regtype
10761076
)
10771077
SQL
1078-
execute_and_clear(sql, "SCHEMA", [], allow_retry: true, uses_transaction: false) do |result|
1078+
execute_and_clear(sql, "SCHEMA", [], allow_retry: true, materialize_transactions: false) do |result|
10791079
result.getvalue(0, 0)
10801080
end
10811081
end
@@ -1132,7 +1132,7 @@ def add_pg_decoders
11321132
FROM pg_type as t
11331133
WHERE t.typname IN (%s)
11341134
SQL
1135-
coders = execute_and_clear(query, "SCHEMA", [], allow_retry: true, uses_transaction: false) do |result|
1135+
coders = execute_and_clear(query, "SCHEMA", [], allow_retry: true, materialize_transactions: false) do |result|
11361136
result.filter_map { |row| construct_coder(row, coders_by_name[row["typname"]]) }
11371137
end
11381138

activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def begin_isolated_db_transaction(isolation) # :nodoc:
6666
raise TransactionIsolationError, "SQLite3 only supports the `read_uncommitted` transaction isolation level" if isolation != :read_uncommitted
6767
raise StandardError, "You need to enable the shared-cache mode in SQLite mode before attempting to change the transaction isolation level" unless shared_cache?
6868

69-
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
69+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
7070
ActiveSupport::IsolatedExecutionState[:active_record_read_uncommitted] = conn.get_first_value("PRAGMA read_uncommitted")
7171
conn.read_uncommitted = true
7272
begin_db_transaction
@@ -75,15 +75,15 @@ def begin_isolated_db_transaction(isolation) # :nodoc:
7575

7676
def begin_db_transaction # :nodoc:
7777
log("begin transaction", "TRANSACTION") do
78-
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
78+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
7979
conn.transaction
8080
end
8181
end
8282
end
8383

8484
def commit_db_transaction # :nodoc:
8585
log("commit transaction", "TRANSACTION") do
86-
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
86+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
8787
conn.commit
8888
end
8989
end
@@ -92,7 +92,7 @@ def commit_db_transaction # :nodoc:
9292

9393
def exec_rollback_db_transaction # :nodoc:
9494
log("rollback transaction", "TRANSACTION") do
95-
with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
95+
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
9696
conn.rollback
9797
end
9898
end
@@ -109,9 +109,9 @@ def high_precision_current_timestamp
109109
end
110110

111111
private
112-
def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: false)
112+
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: false)
113113
log(sql, name, async: async) do
114-
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
114+
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
115115
conn.execute(sql)
116116
end
117117
end

0 commit comments

Comments
 (0)