Skip to content

Commit

Permalink
Use native_json as default storage method for new installations
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Nov 12, 2024
1 parent d1b168a commit a1c4023
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Gemfile.lock
/test/**/tmp/

test/dummy_app/**/*.sqlite*
test/dummy_app/db/schema.rb
test/dummy_app/**/*.log
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 22 additions & 7 deletions lib/active_snapshot/config.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/active_snapshot/models/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions lib/active_snapshot/models/snapshot_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ 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

@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)
Expand Down
18 changes: 1 addition & 17 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 21 additions & 5 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit a1c4023

Please sign in to comment.