From a1c40233a865b5aebbd150b64174b30f525bd64d Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Mon, 11 Nov 2024 23:29:10 -0800 Subject: [PATCH] Use native_json as default storage method for new installations --- .gitignore | 1 + CHANGELOG.md | 2 +- lib/active_snapshot/config.rb | 29 ++++++++++++++++----- lib/active_snapshot/models/snapshot.rb | 4 +-- lib/active_snapshot/models/snapshot_item.rb | 6 ++--- test/test_helper.rb | 18 +------------ test/unit/config_test.rb | 26 ++++++++++++++---- 7 files changed, 50 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 9fe3c00..ec40647 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ Gemfile.lock /test/**/tmp/ test/dummy_app/**/*.sqlite* +test/dummy_app/db/schema.rb test/dummy_app/**/*.log diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ec1836..1089094 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ CHANGELOG - **Unreleased** * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.1...master) - * Nothing yet + * [#67](https://github.com/westonganger/active_snapshot/pull/67) - Switch default storage mechanism to `ActiveSnapshot.config.storage_method = "native_json"` for new installations only - **v0.5.1** - Nov 11, 2024 * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.0...v0.5.1) diff --git a/lib/active_snapshot/config.rb b/lib/active_snapshot/config.rb index 4338486..f205eaf 100644 --- a/lib/active_snapshot/config.rb +++ b/lib/active_snapshot/config.rb @@ -1,9 +1,13 @@ module ActiveSnapshot class Config - attr_reader :storage_method - def initialize - @storage_method = 'serialized_json' + end + + def storage_method + if @storage_method.nil? + init_storage_method + end + @storage_method end def storage_method=(value) @@ -17,15 +21,26 @@ def storage_method=(value) end def storage_method_yaml? - @storage_method == 'serialized_yaml' + storage_method == 'serialized_yaml' end - def storage_method_json? - @storage_method == 'serialized_json' + def storage_method_serialized_json? + storage_method == 'serialized_json' end + alias_method :storage_method_json?, :storage_method_serialized_json? def storage_method_native_json? - @storage_method == 'native_json' + storage_method == 'native_json' + end + + private + + def init_storage_method + if ActiveSnapshot::SnapshotItem.table_exists? && ActiveSnapshot::SnapshotItem.type_for_attribute(:object).type.to_sym == :text + self.storage_method = 'serialized_json' + else + self.storage_method = 'native_json' + end end end end diff --git a/lib/active_snapshot/models/snapshot.rb b/lib/active_snapshot/models/snapshot.rb index fdfb5ab..0c703bc 100644 --- a/lib/active_snapshot/models/snapshot.rb +++ b/lib/active_snapshot/models/snapshot.rb @@ -18,7 +18,7 @@ class Snapshot < ActiveRecord::Base def metadata return @metadata if @metadata - if ActiveSnapshot.config.storage_method_json? + if ActiveSnapshot.config.storage_method_serialized_json? @metadata = JSON.parse(self[:metadata]) elsif ActiveSnapshot.config.storage_method_yaml? yaml_method = "unsafe_load" @@ -36,7 +36,7 @@ def metadata def metadata=(h) @metadata = nil - if ActiveSnapshot.config.storage_method_json? + if ActiveSnapshot.config.storage_method_serialized_json? self[:metadata] = h.to_json elsif ActiveSnapshot.config.storage_method_yaml? self[:metadata] = YAML.dump(h) diff --git a/lib/active_snapshot/models/snapshot_item.rb b/lib/active_snapshot/models/snapshot_item.rb index 96201b3..fe543be 100644 --- a/lib/active_snapshot/models/snapshot_item.rb +++ b/lib/active_snapshot/models/snapshot_item.rb @@ -17,7 +17,7 @@ class SnapshotItem < ActiveRecord::Base def object return @object if @object - if ActiveSnapshot.config.storage_method_json? + if ActiveSnapshot.config.storage_method_serialized_json? @object = self[:object] ? JSON.parse(self[:object]) : {} elsif ActiveSnapshot.config.storage_method_yaml? yaml_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load @@ -25,15 +25,13 @@ def object @object = self[:object] ? YAML.public_send(yaml_method, self[:object]) : {} elsif ActiveSnapshot.config.storage_method_native_json? @object = self[:object] - else - raise StandardError, "Unsupported storage_method: `#{ActiveSnapshot.config.storage_method}`" end end def object=(h) @object = nil - if ActiveSnapshot.config.storage_method_json? + if ActiveSnapshot.config.storage_method_serialized_json? self[:object] = h.to_json elsif ActiveSnapshot.config.storage_method_yaml? self[:object] = YAML.dump(h) diff --git a/test/test_helper.rb b/test/test_helper.rb index 934aba4..425e918 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -57,23 +57,7 @@ class ActiveSupport::TestCase ActiveRecord::MigrationContext.new(File.expand_path("dummy_app/db/migrate/", __dir__)).migrate end -require 'rspec/mocks' -module MinitestRSpecMocksIntegration - include RSpec::Mocks::ExampleMethods - - def before_setup - RSpec::Mocks.setup - super - end - - def after_teardown - super - RSpec::Mocks.verify - ensure - RSpec::Mocks.teardown - end -end -Minitest::Test.send(:include, MinitestRSpecMocksIntegration) +require "rspec/mocks/minitest_integration" DATA = {}.with_indifferent_access diff --git a/test/unit/config_test.rb b/test/unit/config_test.rb index 3812666..f7cdf73 100644 --- a/test/unit/config_test.rb +++ b/test/unit/config_test.rb @@ -11,25 +11,41 @@ def teardown ActiveSnapshot.config.storage_method = @orig_storage_method end - def test_defaults_to_serialized_json + def test_defaults_to_serialized_json_if_text_column_exists if ENV["ACTIVE_SNAPSHOT_STORAGE_METHOD"].present? skip end + ActiveSnapshot.config.instance_variable_set("@storage_method", nil) + + allow(ActiveSnapshot::SnapshotItem).to receive(:type_for_attribute).with(:object).and_return(ActiveRecord::Type::Text.new) + assert_equal 'serialized_json', ActiveSnapshot.config.storage_method assert_equal false, ActiveSnapshot.config.storage_method_yaml? - assert_equal true, ActiveSnapshot.config.storage_method_json? + assert_equal true, ActiveSnapshot.config.storage_method_serialized_json? assert_equal false, ActiveSnapshot.config.storage_method_native_json? end + def test_defaults_to_native_json + if ENV["ACTIVE_SNAPSHOT_STORAGE_METHOD"].present? + skip + end + + assert_equal 'native_json', ActiveSnapshot.config.storage_method + + assert_equal false, ActiveSnapshot.config.storage_method_yaml? + assert_equal false, ActiveSnapshot.config.storage_method_serialized_json? + assert_equal true, ActiveSnapshot.config.storage_method_native_json? + end + def test_accepts_to_serialized_json ActiveSnapshot.config.storage_method = 'serialized_json' assert_equal 'serialized_json', ActiveSnapshot.config.storage_method assert_equal false, ActiveSnapshot.config.storage_method_yaml? - assert_equal true, ActiveSnapshot.config.storage_method_json? + assert_equal true, ActiveSnapshot.config.storage_method_serialized_json? assert_equal false, ActiveSnapshot.config.storage_method_native_json? end @@ -40,7 +56,7 @@ def test_accepts_serialized_yaml assert_equal 'serialized_yaml', ActiveSnapshot.config.storage_method assert_equal true, ActiveSnapshot.config.storage_method_yaml? - assert_equal false, ActiveSnapshot.config.storage_method_json? + assert_equal false, ActiveSnapshot.config.storage_method_serialized_json? assert_equal false, ActiveSnapshot.config.storage_method_native_json? end @@ -50,7 +66,7 @@ def test_accepts_native_json assert_equal "native_json", ActiveSnapshot.config.storage_method, "native_json" assert_equal false, ActiveSnapshot.config.storage_method_yaml? - assert_equal false, ActiveSnapshot.config.storage_method_json? + assert_equal false, ActiveSnapshot.config.storage_method_serialized_json? assert_equal true, ActiveSnapshot.config.storage_method_native_json? end