Skip to content

Commit ea2e3cc

Browse files
authored
Merge pull request rails#46604 from adrianna-chang-shopify/ac-extract-method-to-sync-db-timezones
Extract `#sync_timezone_changes` method in AbstractMysqlAdapter
2 parents ac36805 + dc8c086 commit ea2e3cc

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Extract `#sync_timezone_changes` method in AbstractMysqlAdapter to enable subclasses
2+
to sync database timezone changes without overriding `#raw_execute`.
3+
4+
*Adrianna Chang*, *Paarth Madan*
5+
16
* Do not write additional new lines when dumping sql migration versions
27

38
This change updates the `insert_versions_sql` function so that the database insert string containing the current database migration versions does not end with two additional new lines.

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,17 @@ def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: t
731731

732732
log(sql, name, async: async) do
733733
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
734+
sync_timezone_changes(conn)
734735
conn.query(sql)
735736
end
736737
end
737738
end
738739

740+
# Make sure we carry over any changes to ActiveRecord.default_timezone that have been
741+
# made since we established the connection
742+
def sync_timezone_changes(raw_connection)
743+
end
744+
739745
def internal_execute(sql, name = "SCHEMA", allow_retry: true, uses_transaction: false)
740746
raw_execute(sql, name, allow_retry: allow_retry, uses_transaction: uses_transaction)
741747
end

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,8 @@ def high_precision_current_timestamp
8383
end
8484

8585
private
86-
def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: true)
87-
mark_transaction_written_if_write(sql)
88-
89-
log(sql, name, async: async) do
90-
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
91-
# make sure we carry over any changes to ActiveRecord.default_timezone that have been
92-
# made since we established the connection
93-
conn.query_options[:database_timezone] = default_timezone
94-
95-
conn.query(sql)
96-
end
97-
end
86+
def sync_timezone_changes(raw_connection)
87+
raw_connection.query_options[:database_timezone] = default_timezone
9888
end
9989

10090
def execute_batch(statements, name = nil)
@@ -176,9 +166,7 @@ def exec_stmt_and_free(sql, name, binds, cache_stmt: false, async: false)
176166

177167
log(sql, name, binds, type_casted_binds, async: async) do
178168
with_raw_connection do |conn|
179-
# make sure we carry over any changes to ActiveRecord.default_timezone that have been
180-
# made since we established the connection
181-
conn.query_options[:database_timezone] = default_timezone
169+
sync_timezone_changes(conn)
182170

183171
if cache_stmt
184172
stmt = @statements[sql] ||= conn.prepare(sql)

activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ def test_statement_timeout_error_codes
314314
end
315315
end
316316

317+
def test_database_timezone_changes_synced_to_connection
318+
with_timezone_config default: :local do
319+
assert_changes(-> { @conn.raw_connection.query_options[:database_timezone] }, from: :utc, to: :local) do
320+
@conn.execute("SELECT 1")
321+
end
322+
end
323+
end
324+
317325
private
318326
def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block)
319327
super(@conn, "ex", definition, &block)

0 commit comments

Comments
 (0)