Skip to content

Commit

Permalink
Extract #sync_timezone_changes method in AbstractMysqlAdapter
Browse files Browse the repository at this point in the history
This enables subclasses to sync database timezone changes without overriding `#raw_execute`,
which removes the need to redefine `#raw_execute` in the `Mysql2Adapter` and other
adapters subclassing `AbstractMysqlAdapter`.

Co-authored-by: Paarth Madan <paarth.madan@shopify.com>
  • Loading branch information
adrianna-chang-shopify and paarthmadan committed Nov 29, 2022
1 parent fa09e3c commit dc8c086
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
* Extract `#sync_timezone_changes` method in AbstractMysqlAdapter to enable subclasses
to sync database timezone changes without overriding `#raw_execute`.

*Adrianna Chang*, *Paarth Madan*

* Do not write additional new lines when dumping sql migration versions

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.
Expand Down
Expand Up @@ -731,11 +731,17 @@ def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: t

log(sql, name, async: async) do
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
sync_timezone_changes(conn)
conn.query(sql)
end
end
end

# Make sure we carry over any changes to ActiveRecord.default_timezone that have been
# made since we established the connection
def sync_timezone_changes(raw_connection)
end

def internal_execute(sql, name = "SCHEMA", allow_retry: true, uses_transaction: false)
raw_execute(sql, name, allow_retry: allow_retry, uses_transaction: uses_transaction)
end
Expand Down
Expand Up @@ -83,18 +83,8 @@ def high_precision_current_timestamp
end

private
def raw_execute(sql, name, async: false, allow_retry: false, uses_transaction: true)
mark_transaction_written_if_write(sql)

log(sql, name, async: async) do
with_raw_connection(allow_retry: allow_retry, uses_transaction: uses_transaction) do |conn|
# make sure we carry over any changes to ActiveRecord.default_timezone that have been
# made since we established the connection
conn.query_options[:database_timezone] = default_timezone

conn.query(sql)
end
end
def sync_timezone_changes(raw_connection)
raw_connection.query_options[:database_timezone] = default_timezone
end

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

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

if cache_stmt
stmt = @statements[sql] ||= conn.prepare(sql)
Expand Down
Expand Up @@ -314,6 +314,14 @@ def test_statement_timeout_error_codes
end
end

def test_database_timezone_changes_synced_to_connection
with_timezone_config default: :local do
assert_changes(-> { @conn.raw_connection.query_options[:database_timezone] }, from: :utc, to: :local) do
@conn.execute("SELECT 1")
end
end
end

private
def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block)
super(@conn, "ex", definition, &block)
Expand Down

0 comments on commit dc8c086

Please sign in to comment.