Honor database-config-specific default_timezone configuration value #1329
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ActiveRecord supports a database-configuration-level value for
default_timezone
, and this adapter is not honoring that setting, instead using the globalActiveRecord.default_timezone
setting. This change should honor the configuration-specific setting when present and still retain prior behavior.This PR is applied to the
8-0-stable
branch, as there is a conflict inmain
, which appears to be superficial and only related to some hash formatting changes. Not sure what the desired process is here.Some broader context
This all stems from the fact that we're using this SQLServer adapter as a means to connect to multiple legacy databases that are separate from the parent Rails application's main database. This is why setting the
ActiveRecord.default_timezone
value isn't a solution, I don't want it to affect either of the other two databases. The one database in question is unfortunately storing timestamps in a non-UTC time zone and we have no control over that, so we need to conform to that from an ActiveRecord perspective.An additional complication is that the other MSSQL database is separate, and does not have this issue, so it can't be solved on an adapter level either. The configuration option this PR solves the issue of reading the values from the database without mangling the datetime value.
I am still unable to fix writes - writing to a datetime column with a
Time
instance does a UTC conversion before building the query, causing data to be written in UTC which is not desired. I would assume that writing this data should also honor thedefault_timezone
value, but it only does so with the global setting, not the connection configuration level one.I looked at
ActiveRecord::ConnectionAdapters::SQLServer::Type::DateTime
, which does serialization, but that seems to slot in after whenever the time zone conversion would happen.So I eventually found
ActiveRecord::Type::DateTime
, which is just a wrapper around theActiveModel
equivalent withInternal::Timezone
mixed in. That Timezone module's initialize method accepts a time zone argument which very clearly is meant to override the value ofActiveRecord.default_timezone
, so I thought this was my silver bullet, but the only place I could find this could actually be used in the context of sqlserver-adapter was in the type map builder:And I'm at a road block here because all of this type map setup is done without the context of an active database connection and therefore has no way obvious way to fetch a configuration object that could inform the time zone selection.
Sorry if that wasn't concise enough, in general it seems like sqlserver-adapter doesn't really support a connection-level timezone config, even though ActiveRecord itself does.