Skip to content

Rails 6.1: Coerce schema cache test to handle Time marshal in ruby 2.5 and 2.6 #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1474,8 +1474,36 @@ def test_statement_cache_values_differ_coerced
module ActiveRecord
module ConnectionAdapters
class SchemaCacheTest < ActiveRecord::TestCase
# Ruby 2.5 and 2.6 have issues to marshal Time before 1900. 2012.sql has one column with default value 1753
coerce_tests! :test_marshal_dump_and_load_with_gzip, :test_marshal_dump_and_load_via_disk
def test_marshal_dump_and_load_with_gzip_coerced
with_marshable_time_defaults { original_test_marshal_dump_and_load_with_gzip }
end
def test_marshal_dump_and_load_via_disk_coerced
with_marshable_time_defaults { original_test_marshal_dump_and_load_via_disk }
end

private

def with_marshable_time_defaults
# Detect problems
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7")
column = @connection.columns(:sst_datatypes).find { |c| c.name == "datetime" }
current_default = column.default if column.default.is_a?(Time) && column.default.year < 1900
end

# Correct problems
if current_default.present?
@connection.change_column_default(:sst_datatypes, :datetime, current_default.dup.change(year: 1900))
end

# Run original test
yield
ensure
# Revert changes
@connection.change_column_default(:sst_datatypes, :datetime, current_default) if current_default.present?
end

# We need to give the full path for this to work.
def schema_dump_path
File.join ARTest::SQLServer.root_activerecord, "test/assets/schema_dump_5_1.yml"
Expand Down Expand Up @@ -1876,5 +1904,3 @@ class BasePreventWritesLegacyTest < ActiveRecord::TestCase
end
end
end