Skip to content

Commit

Permalink
Remove dependency on activerecord-import using vanilla ActiveRecord u…
Browse files Browse the repository at this point in the history
…psert_all
  • Loading branch information
westonganger committed Jan 2, 2024
1 parent 411cd3d commit e17799b
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 28 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
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.3.2...master)
* Nothing yet
* [#44](https://github.com/westonganger/active_snapshot/pull/44) - 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)
Expand Down
8 changes: 2 additions & 6 deletions active_snapshot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions lib/active_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 6 additions & 4 deletions lib/active_snapshot/models/concerns/snapshots_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e17799b

Please sign in to comment.