careful-import 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 830a8aae59544ebd1a22cfde29b89da7b3c64a439e062a257c2902a6f5249d46
4
+ data.tar.gz: bb72d20146dad6f836f954e2b0619aececaa85e74291a8cce6e168d60288111e
5
+ SHA512:
6
+ metadata.gz: 98ceebebff4ec7aaff3e64dc394b7621561d930ec508c2d63e1b7dc8e902f8abfc2360977d73deb2dded09e00ce1d43e03d0ce03f65c3ac71b73dbb7f8dbc1e3
7
+ data.tar.gz: '02742850059c308dd9d94f48d8ab2354e4e704f841042ea3eab275a372838136b676f28514c4e726f02a21e17210a47ca807ede761296d73c64e6d23109ea825'
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in careful-import.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+
9
+ # Support
10
+ gem "factory_bot", "~> 4.11"
11
+ gem "pry-byebug", "~> 3.3"
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ careful-import (0.1.0)
5
+ activerecord-import (~> 0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.0.3.4)
11
+ activesupport (= 6.0.3.4)
12
+ activerecord (6.0.3.4)
13
+ activemodel (= 6.0.3.4)
14
+ activesupport (= 6.0.3.4)
15
+ activerecord-import (0.20.2)
16
+ activerecord (>= 3.2)
17
+ activesupport (6.0.3.4)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ zeitwerk (~> 2.2, >= 2.2.2)
23
+ byebug (11.1.3)
24
+ coderay (1.1.3)
25
+ concurrent-ruby (1.1.7)
26
+ diff-lcs (1.4.4)
27
+ factory_bot (4.11.1)
28
+ activesupport (>= 3.0.0)
29
+ i18n (1.8.5)
30
+ concurrent-ruby (~> 1.0)
31
+ method_source (1.0.0)
32
+ minitest (5.14.2)
33
+ pry (0.13.1)
34
+ coderay (~> 1.1)
35
+ method_source (~> 1.0)
36
+ pry-byebug (3.9.0)
37
+ byebug (~> 11.0)
38
+ pry (~> 0.13.0)
39
+ rake (12.3.3)
40
+ rspec (3.10.0)
41
+ rspec-core (~> 3.10.0)
42
+ rspec-expectations (~> 3.10.0)
43
+ rspec-mocks (~> 3.10.0)
44
+ rspec-core (3.10.0)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-expectations (3.10.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.10.0)
49
+ rspec-mocks (3.10.0)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.10.0)
52
+ rspec-support (3.10.0)
53
+ thread_safe (0.3.6)
54
+ tzinfo (1.2.7)
55
+ thread_safe (~> 0.1)
56
+ zeitwerk (2.4.1)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ careful-import!
63
+ factory_bot (~> 4.11)
64
+ pry-byebug (~> 3.3)
65
+ rake (~> 12.0)
66
+ rspec (~> 3.0)
67
+
68
+ BUNDLED WITH
69
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Clive Ivins
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Careful::Import
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/careful/import`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'careful-import'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install careful-import
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/careful-import.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "careful_import"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/careful_import/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "careful-import"
5
+ spec.version = CarefulImport::Version::VERSION
6
+ spec.authors = ["Clive Ivins"]
7
+ spec.email = ["civins@itglue.com"]
8
+
9
+ spec.summary = %q{Validates and updates args prior to ActiveRecord::Import import}
10
+ spec.description = %q{If a primary_key value is passed in when importing an error is raised}
11
+ spec.homepage = "https://bitbucket.org/itglue/careful-import/src/master/"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.2.8")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+ spec.add_dependency 'activerecord-import', '~> 0'
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'careful_import/railtie' if defined? ::Rails::Railtie
2
+ require 'careful_import/importer'
@@ -0,0 +1,77 @@
1
+ require "careful_import/version"
2
+
3
+ module CarefulImport
4
+ class Importer
5
+ class ImportArgumentError < ArgumentError; end
6
+
7
+ INVALID_ARGUMENT_MESSAGE = "Record cannot contain a primary key value as this will not use our Postgres 53-bit "\
8
+ "generated id upon insertion.Supply instead an array of hashes without primary keys or an array of ActiveRecord "\
9
+ "objects with nil primary keys."
10
+
11
+ def initialize(resource_class)
12
+ @resource_class = resource_class
13
+ @primary_key = resource_class.primary_key
14
+ end
15
+
16
+ # https://github.com/zdennis/activerecord-import/blob/v0.20.2/lib/activerecord-import/import.rb#L174
17
+ # Potential Args:
18
+ # Model.import array_of_models
19
+ # Model.import column_names, array_of_models
20
+ # Model.import array_of_hash_objects
21
+ # Model.import column_names, array_of_hash_objects
22
+ # Model.import column_names, array_of_values
23
+ # Model.import column_names, array_of_values, options
24
+
25
+ def import(*args)
26
+ options = args.extract_options!
27
+ validated_args = build_validated_args(*args)
28
+
29
+ @resource_class.__private_import_helper__(*validated_args, options)
30
+ end
31
+
32
+ private
33
+
34
+ def raise_usage_error(arg)
35
+ raise ImportArgumentError.new("#{INVALID_ARGUMENT_MESSAGE} Invalid argument: #{arg.inspect}")
36
+ end
37
+
38
+ def build_validated_args(*args)
39
+ return args if args.first.is_a?(Array) && args.first.empty?
40
+
41
+ if args.first.is_a?(Array) && (args.first.first.is_a?(ActiveRecord::Base) || args.first.first.is_a?(Hash))
42
+ records = validate_and_remove_primary_key(args.first)
43
+ [records]
44
+ elsif args.first.is_a?(Array) && args.second.is_a?(Array)
45
+ # the first arg will be a list of column names and the second arg will be the values. Check for column name 'id'
46
+ check_columns_for_primary_key(args.first)
47
+ args
48
+ else
49
+ # if the above conditions aren't met then the args are not correct so we will raise an error here.
50
+ raise ImportArgumentError.new("Invalid arguments!")
51
+ end
52
+ end
53
+
54
+ def validate_and_remove_primary_key(records)
55
+ return records if records.empty?
56
+
57
+ records.map do |record|
58
+ if record.is_a?(ActiveRecord::Base)
59
+ # if the record id is nil it is a new record and we will instead convert this record to a hash without id key
60
+ raise_usage_error(record) unless record.public_send(@primary_key).nil?
61
+
62
+ record.attributes.except(@primary_key)
63
+ elsif record.is_a?(Hash)
64
+ raise_usage_error(record) if record[@primary_key].present?
65
+
66
+ record.except(@primary_key)
67
+ else
68
+ raise ImportArgumentError.new("Invalid argument: #{record}")
69
+ end
70
+ end
71
+ end
72
+
73
+ def check_columns_for_primary_key(columns)
74
+ raise_usage_error(columns) if columns.include?(@primary_key.to_sym) || columns.include?(@primary_key)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,29 @@
1
+ module CarefulImport
2
+ module Initializer
3
+ module_function
4
+
5
+ # This wraps the activerecord-import gem's import_helper method so that we can validate the args passed to
6
+ # it to ensure no id keys are being used. This will ensure that we are using the generated 53-bit id
7
+ # instead of nextval.
8
+ def initialize
9
+ ActiveRecord::Base.class_eval do
10
+ class << self
11
+ unless ActiveRecord::Base.respond_to?(:__private_import_helper__)
12
+ # `activerecord-import` opens up ActiveRecord::Base and adds the import methods to it directly
13
+ # so we have to add the alias_method to it directly as well.
14
+ # We don't want the original `import_helper` to be called without having its args validated
15
+ # so we rename the original method which allows us to call it after validating.
16
+ alias_method :__private_import_helper__, :import_helper
17
+ end
18
+
19
+ # Under the hood import_helper is what is called by import and import! and it is a public method
20
+ # so you could directly call Configuration.import_helper(records, {}) and it would basically do what
21
+ # import does. So instead of overriding each of the methods we are overriding the one that they all use.
22
+ def import_helper(*args)
23
+ ::CarefulImport::Importer.new(self).import(*args)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+ require_relative 'careful_import/initializer'
3
+
4
+ module CarefulImport
5
+ class Railtie < Rails::Railtie
6
+ config.to_prepare do
7
+ # initialize our custom activerecord-import validator to ensure only 53-bit id's are used and not nextval
8
+ CarefulImport::Initializer.initialize
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module CarefulImport
2
+ module Version
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: careful-import
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Clive Ivins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord-import
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: If a primary_key value is passed in when importing an error is raised
28
+ email:
29
+ - civins@itglue.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - careful-import.gemspec
45
+ - lib/careful_import.rb
46
+ - lib/careful_import/importer.rb
47
+ - lib/careful_import/initializer.rb
48
+ - lib/careful_import/railtie.rb
49
+ - lib/careful_import/version.rb
50
+ homepage: https://bitbucket.org/itglue/careful-import/src/master/
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://bitbucket.org/itglue/careful-import/src/master/
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.2.8
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.8
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Validates and updates args prior to ActiveRecord::Import import
74
+ test_files: []