From 8fa52314a384d2ba1a22672aeb1e8017b5bc4e33 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Tue, 2 Jan 2024 12:25:05 -0800 Subject: [PATCH] Remove dependency on activerecord-import using vanilla ActiveRecord upsert_all --- .github/workflows/test.yml | 3 --- CHANGELOG.md | 2 +- active_snapshot.gemspec | 8 ++------ lib/active_snapshot.rb | 2 -- .../models/concerns/snapshots_concern.rb | 10 ++++++---- test/dummy_app/config/application.rb | 4 +--- test/test_helper.rb | 14 +++++--------- 7 files changed, 15 insertions(+), 28 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8860744..57045a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,9 +24,6 @@ jobs: - ruby: "3.2" - ruby: "3.3" ### TEST RAILS VERSIONS - - ruby: "2.6" - env: - RAILS_VERSION: "5.2" - ruby: "2.6" env: RAILS_VERSION: "6.0" diff --git a/CHANGELOG.md b/CHANGELOG.md index 52fdd72..7fdfd41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ CHANGELOG - **Unreleased** * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.2...master) - * Nothing yet + * [#xx](https://github.com/westonganger/active_snapshot/pull/xx) - Remove dependency on activerecord-import with vanilla ActiveRecord upsert_all - **v0.3.2** - Oct 17, 2023 * [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.3.1...v0.3.2) diff --git a/active_snapshot.gemspec b/active_snapshot.gemspec index 18fd7a2..cf56b9b 100644 --- a/active_snapshot.gemspec +++ b/active_snapshot.gemspec @@ -17,17 +17,13 @@ Gem::Specification.new do |s| s.files = Dir.glob("{lib/**/*}") + %w{ LICENSE README.md Rakefile CHANGELOG.md } s.require_path = 'lib' - s.add_runtime_dependency "activerecord" + s.add_runtime_dependency "activerecord", ">= 6.0" s.add_runtime_dependency "railties" - s.add_runtime_dependency "activerecord-import" s.add_development_dependency "rake" s.add_development_dependency "minitest" s.add_development_dependency "minitest-reporters" s.add_development_dependency "minitest-spec-rails" s.add_development_dependency "rspec-mocks" - - if RUBY_VERSION.to_f >= 2.4 - s.add_development_dependency "warning" - end + s.add_development_dependency "warning" end diff --git a/lib/active_snapshot.rb b/lib/active_snapshot.rb index 6da241c..f900bc5 100644 --- a/lib/active_snapshot.rb +++ b/lib/active_snapshot.rb @@ -4,8 +4,6 @@ require 'active_support/lazy_load_hooks' ActiveSupport.on_load(:active_record) do - require "activerecord-import" - require "active_snapshot/models/snapshot" require "active_snapshot/models/snapshot_item" diff --git a/lib/active_snapshot/models/concerns/snapshots_concern.rb b/lib/active_snapshot/models/concerns/snapshots_concern.rb index 49d7e35..c15cb36 100644 --- a/lib/active_snapshot/models/concerns/snapshots_concern.rb +++ b/lib/active_snapshot/models/concerns/snapshots_concern.rb @@ -22,21 +22,23 @@ def create_snapshot!(legacy_identifier=nil, identifier: nil, user: nil, metadata metadata: (metadata || {}), }) - snapshot_items = [] + new_entries = [] - snapshot_items << snapshot.build_snapshot_item(self) + current_time = Time.now + + new_entries << snapshot.build_snapshot_item(self).attributes.merge(created_at: current_time) snapshot_children = self.children_to_snapshot if snapshot_children snapshot_children.each do |child_group_name, h| h[:records].each do |child_item| - snapshot_items << snapshot.build_snapshot_item(child_item, child_group_name: child_group_name) + new_entries << snapshot.build_snapshot_item(child_item, child_group_name: child_group_name).attributes.merge(created_at: current_time) end end end - SnapshotItem.import(snapshot_items, validate: true) + SnapshotItem.upsert_all(new_entries.map{|x| x.delete("id"); x }, returning: false) snapshot end diff --git a/test/dummy_app/config/application.rb b/test/dummy_app/config/application.rb index c06d66b..f53f525 100644 --- a/test/dummy_app/config/application.rb +++ b/test/dummy_app/config/application.rb @@ -6,9 +6,7 @@ module Dummy class Application < Rails::Application - if Rails::VERSION::STRING.to_f >= 5.1 - config.load_defaults Rails::VERSION::STRING.to_f - end + config.load_defaults Rails::VERSION::STRING.to_f # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers diff --git a/test/test_helper.rb b/test/test_helper.rb index ff9a6e4..0bf6bff 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,15 +15,11 @@ ActiveSnapshot.config.storage_method = ENV["ACTIVE_SNAPSHOT_STORAGE_METHOD"] end -begin - require 'warning' - - Warning.ignore( - %r{mail/parsers/address_lists_parser}, ### Hide mail gem warnings - ) -rescue LoadError - # Do nothing -end +require 'warning' + +Warning.ignore( + %r{mail/parsers/address_lists_parser}, ### Hide mail gem warnings +) ### Delete the database completely before starting FileUtils.rm(