From 6a9301dcc57460b7d4fb1681e82529264cc2b9c4 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 30 Jun 2020 22:51:17 +0900 Subject: [PATCH] Backward compatibility to work `Marshal.load(legacy_record.dump)` for MySQL Context https://github.com/rails/rails/pull/39599#discussion_r445429365. Similar with #39611. Actually we never said we would keep Mashal compatibility between two versions of Rails, so we sometimes directly removed the dedicated types in the past (e.g. f1a0fa9, #29666), but that direct removing makes cache rotation harder. Keeping the unused constant in new version at least during one version will help cache rotation easier. --- .../abstract_mysql_adapter.rb | 4 ++++ activerecord/test/cases/base_test.rb | 15 +++++++++++++++ .../legacy_6_0_record_mysql.dump | Bin 0 -> 1901 bytes 3 files changed, 19 insertions(+) create mode 100644 activerecord/test/support/marshal_compatibility_fixtures/legacy_6_0_record_mysql.dump diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 9dbed2c1b7..39af3a02cf 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -844,6 +844,10 @@ def version_string(full_version_string) full_version_string.match(/^(?:5\.5\.5-)?(\d+\.\d+\.\d+)/)[1] end + # Alias MysqlString to work Mashal.load(File.read("legacy_record.dump")). + # TODO: Remove the constant alias once Rails 6.1 has released. + MysqlString = Type::String # :nodoc: + ActiveRecord::Type.register(:immutable_string, adapter: :mysql2) do |_, **args| Type::ImmutableString.new(true: "1", false: "0", **args) end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index f8f3e86acc..5e948224d7 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1226,6 +1226,21 @@ def test_marshalling_with_associations assert_equal 1, post.comments.length end + if current_adapter?(:Mysql2Adapter) + def test_marshal_load_legacy_6_0_record_mysql + path = File.expand_path( + "support/marshal_compatibility_fixtures/legacy_6_0_record_mysql.dump", + TEST_ROOT + ) + topic = Marshal.load(File.read(path)) + + assert_not_predicate topic, :new_record? + assert_equal 1, topic.id + assert_equal "The First Topic", topic.title + assert_equal "Have a nice day", topic.content + end + end + if Process.respond_to?(:fork) && !in_memory_db? def test_marshal_between_processes # Define a new model to ensure there are no caches diff --git a/activerecord/test/support/marshal_compatibility_fixtures/legacy_6_0_record_mysql.dump b/activerecord/test/support/marshal_compatibility_fixtures/legacy_6_0_record_mysql.dump new file mode 100644 index 0000000000000000000000000000000000000000..72ebbd88d0ea24dcfa68b94ea4cbd865cc1dd699 GIT binary patch literal 1901 zcmaJ?&2Aev5LSO6X;+r~(-e-;#8Ed*fg-S>_)s9+3thW{7l=_HmII_Z5Y+D2Voi}7 zE?16h6bR5q>I?V<`T~8E_TmnACB?GcNQdPNXE=Y~%zRnb#P8`;#4BWlDmZ5bVr`Nx zQruc22Dybylg-`OiZLAO1SBE5vzsHZ)v$I`ka0e+y!ipxI3|FUse!q6#I`QCrEPKQs zz^PDj%d3>X52Ow%k!dg&FtJ1-57ac6Kw9hT{#0cPokhDMyWQIm!UXLlc%ZCW0Wf?njgZi5N*AvAPx##*2EYca{4=c*??Nkxg8Ff94^_H%dLzoir_Jrk#uXL<)ASt;G0zKqa7;Xh`F=xG z#24r48Gi78vb3}F^5u&kUo3V&6g=MA+G-#0F|>JGi5S`mpZMF`+kWZsBFYdh3m_Af z`|#s3{J+3(DvQ9tNAKVQd2?9c`6DWjxCM$LBY&-LUj6awKX0cQxdoEDBA1oRD)lw_ zp=`_BYk`oi(IZSFAMgx3;!`&hsZSo!u>%$xz=<}%9CBHVQ>cR?j-UopN1%OH@bhBmcgf~QF*ca68^$_#$1=-}Ww;Jh#tRL{vACGl={?Mcl%L=ZVOHny L5S^705>fdN%5PJ4 literal 0 HcmV?d00001 -- GitLab